• Linux之系统文件


    stat 主要函数

    stat, fstat, lstat, fstatat —— get file status 获取文件属性。

    #include 
    #include 
    #include 
    
    int stat(const char *pathname, struct stat *statbuf);
    int fstat(int fd, struct stat *statbuf);
    int lstat(const char *pathname, struct stat *statbuf);
    
    #include            /* Definition of AT_* constants */
    #include 
    
    int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    stat 描述

    • 这些函数在 statbuf 指向的缓冲区中返回有关文件的信息。文件本身不需要任何权限,但是ーー在 stat ()、 fstatat ()和 lstat ()的情况下ーー需要对导向文件的路径名中的所有目录执行(search)权限。
    • Stat ()和 fstatat ()检索由路径名指向的文件的信息; fstatat ()的区别如下所述。
    • Lstat ()与 stat ()完全相同,除了如果路径名是一个符号链接,那么它返回关于链接本身的信息,而不是它引用的文件。
    • Fstat ()与 stat ()完全相同,只是要检索的信息的文件由文件描述符 fd 指定。

    stat 结构体

    All of these system calls return a stat structure, which contains the following fields:

    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
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    判断文件各类操作

    st_mode是用特征位来表示文件类型的,特征位的定义如下:

  • 相关阅读:
    【08】FISCOBCOS一键部署【07+08即可完成一键部署,默认生成两个节点的链】
    Java学习 习题 1.
    vcontact2:病毒聚类(失败)
    深入理解互联网的原理——服务器、客户端和http协议
    设计模式—结构型模式之外观模式(门面模式)
    安卓USB模块分析(二)- API使用
    RabbitMQ 3.9( 续 )
    基于视觉重定位的室内AR导航APP的大创项目思路(1):最初的项目思路(SLAM)
    狂神说Go语言学习笔记(四)
    Windows下Docker搭建Flink集群
  • 原文地址:https://blog.csdn.net/weixin_45678463/article/details/132646131