本文共 2739 字,大约阅读时间需要 9 分钟。
1、索引号–一个文件对应一个索引号inode,inode是文件系统提供的唯一数值编址,
这个数值叫inode编号(索引号)
–使用命令"ls -i""ls -al""ls -ail"可以查看索引号等元数据– inode中存储了与文件相关的元数据。
2、通过ls命令查看到的文件信息,都可以使用stat函数组提取出来
3、stat函数组 –使用命令man stat查看相关文档
4、函数int stat(const char *path, struct stat *buf) –参数*path:文件路径 –参数*buf:文件信息 –返回值:成功为0,否则为-1
5、函数int fstat(int fd, struct stat *buf); –参数fd:文件描述符 –参数*buf:文件信息 –返回值:成功为0,否则为-1
6、函数int lstat(const char *path, struct stat *buf); –参数*path:文件路径 –参数*buf:返回文件的信息,针对符号链接,lstat 返回链接本身,而不是而非目标文件 –返回值:成功为0,否则为-1
7、struct stat 结构体:
struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ 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; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ /* Since Linux 2.6, the kernel supports nanosecond precision for the following timestamp fields. For the details before Linux 2.6, see NOTES. */ 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 */ #define st_atime st_atim.tv_sec /* Backward compatibility */ #define st_mtime st_mtim.tv_sec #define st_ctime st_ctim.tv_sec};例:
#include//通过man文档可以查看到stat函数组头文件#include #include #include //open函数的参数头文件#include int main(int argc,char *argv[]){ struct stat groupstat; int fd,ret; if(argc <2){ printf("\nPlease input file path\n"); return 1; }//stat函数测试 ret = stat(argv[1],&groupstat); if(ret){ printf("Please make sure file path\n"); return 1; } printf("stat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino); //fstat函数测试 fd = open(argv[1],O_RDWR|O_NOCTTY|O_NDELAY); if(fd<0) { printf("Please make sure file path\n"); return 1; } ret = fstat(fd,&groupstat); if(ret){ printf("Please make sure file path\n"); return 1; } printf("fstat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino); //lstat函数测试 ret = lstat(argv[1],&groupstat); if(ret){ printf("Please make sure file path\n"); return 1; } printf("lstat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino); return 0;}
转载地址:http://uynws.baihongyu.com/