一、目录和文件
1.1 文件属性(stat)
stat() 可以通过文件名获取文件的属性。
fstat() 可以通过打开的文件描述符获取文件的属性。
lstat() 和 stat() 功能相同,有一点区别就是当 pathname 是一个符号链接文件的时候,lstat() 返回的是符号链接文件本身的属性,而不是链接文件指向的文件的属性。而 stat() 则是返回符号链接所指向文件的属性。
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
下面是 stat 结构体中的部分成员:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
nlink_t st_nlink; /* Number of hard links */
uid_t st_uid; /* User ID of owner */
gid_t st_gid; /* Group ID of owner */
dev_t st_rdev; /* Device ID (if special file) */
off_t st_size; /* Total size, in bytes */
blksize_t st_blksize; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
}
我们可以通过 stat 结构体中的 st_size 成员获取文件长度,程序 flen.c 如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
off_t flen(char *path){
struct stat fstat;
if (stat(path, &fstat) < 0) {
perror("stat()");
exit(1);
}
return fstat.st_size;
}
int main(int argc, char * argv[])
{
if (argc < 2) {
perror("flen <filepath>");
exit(1);
}
off_t fsize = flen(argv[1]);
printf("file size: %lld\n", (long long)fsize);
exit(0);
}
但 st_size 仅仅是文件的属性,实际占用磁盘的块大小和个数是 st_blksize 和 st_blocks。如下图,文件 flen.c 的 Size 为 429 B,但是占用磁盘块数为 8个,8 * 512B = 4096B:
下面是一段创建空洞文件的代码 hole_file.c,该程序会创建一个大小为 5g 的空洞文件(注意 5LL * 1024LL * 1024LL * 1024LL 一定要加单位 LL,否则计算结果会溢出):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
if (argc < 2) {
perror("hole_file <filepath>");
exit(1);
}
int fd = open(argv[1], O_RDWR | O_CREAT);
if(fd < 0){
perror("open");
exit(1);
}
/* make a 5g hole file */
lseek(fd, 5LL * 1024LL * 1024LL * 1024LL, SEEK_SET);
write(fd, "", 1);
exit(0);
}