文件属性和目录属性

文件属性和目录属性

文件属性

文件属性:使用 stat() 函数可以获取

int stat(const char *pathname, struct stat *statbuf);

里面有一个重要的结构体 struct stat ,在 man 2 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 */

    /* 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
};

结构体中的 st_mode 用来判断文件类型,该文件类型如何使用,在 man 7 inode 中可以查看

目录属性

打开目录 opendir() 函数

读目录 readdir() 函数

遍历目录 scandir() 函数

其中有目录项结构体为 struct dirent 在 man 2 readdir 中可以查看

 struct old_linux_dirent {
     long  d_ino;              /* inode number */
     off_t d_off;              /* offset to this old_linux_dirent */
     unsigned short d_reclen;  /* length of this d_name */
     char  d_name[NAME_MAX+1]; /* filename (null-terminated) */
 };

猜你喜欢

转载自blog.csdn.net/qq_41775886/article/details/107943443