结构体stat

来源:互联网 发布:如何加入淘宝商城 编辑:程序博客网 时间:2024/05/19 22:57

 Linux系统调用函数:stat、fstat、lstat

     在Linux中,获得文件属性的系统调用函数有三个为stat、fstat、lstat,原型如下:

 

    int stat(const char* file_name,struct stat* buf);

    参数:

    [in]file_name:文件(文件夹)的路径,相对路径和绝对路径都可以;

    [out]buf:描述文件(文件夹)属性的结构体;

    [return] 成功 0, 失败 -1。错误代码存在全局的errno中;

 

    int fstat(int fd,struct stat* buf); 

    [in]fd:文件(文件夹)的标识符;

    [out]buf:描述文件(文件夹)属性的结构体;

    [return] 成功 0, 失败 -1。错误代码存在全局的errno中;

 

    int lstat(const char* file_name,struct stat* buf);

 

    [in]file_name:文件(文件夹)的路径,相对路径和绝对路径都可以;

    [out]buf:描述文件(文件夹)属性的结构体;

    [return] 成功 0, 失败 -1。错误代码存在全局的errno中;

    lstat和stat的作用完全一样,区别在于如果传入lstat的路径为符号连接(“软连接”)时,获得的是符号连接本身的属性,而stat获得是符号连接目标文件(文件夹)的属性。

    想要在程序中调用这三种方法,必须include的头文件有:

    #include <sys/unistd.h>

    #include <sys/stat.h>

 

   结构体 stat

  

    用man 2 stat,看下结构体stat的定义:

   

view plaincopy to clipboardprint?
  1. struct stat  
  2. {  
  3.     dev_t     st_dev;     /* ID of device containing file */  
  4.     ino_t     st_ino;     /* inode number */  
  5.     mode_t    st_mode;    /* protection */  
  6.     nlink_t   st_nlink;   /* number of hard links */  
  7.     uid_t     st_uid;     /* user ID of owner */  
  8.     gid_t     st_gid;     /* group ID of owner */  
  9.     dev_t     st_rdev;    /* device ID (if special file) */  
  10.     off_t     st_size;    /* total size, in bytes */  
  11.     blksize_t st_blksize; /* blocksize for filesystem I/O */  
  12.     blkcnt_t  st_blocks;  /* number of blocks allocated */  
  13.     time_t    st_atime;   /* time of last access */  
  14.     time_t    st_mtime;   /* time of last modification */  
  15.     time_t    st_ctime;   /* time of last status change */  
  16. };  

 

  

     官方的注释已经很清楚了。关于文件(文件夹)属性的信息都存在这里面的,下面就利用这个结构体,举例来说能做的一些应用。

 

  •     应用一:判断文件(文件夹)是否可读、可写、可执行:

    判断文件(文件夹)是否可读的函数:

    

view plaincopy to clipboardprint?
  1. #include <sys/unistd.h>  
  2. #include <sys/stat.h>  
  3. #include <sys/types.h>  
  4.   
  5. /** /brief 判断文件(文件夹)在当前上下文环境下是否可读 
  6.  * 
  7.  * /param const char* _path: 文件或文件夹的路径,可以为绝对路径或相对路径 
  8.  * /return signed char 
  9.  *  1:可读; 
  10.  *  0:不可读; 
  11.  * -1:错误,错误号可以从全局的errno获取; 
  12.  */  
  13. signed char canRead(const char* _path)  
  14. {  
  15.     struct stat buff;  
  16.     if(stat(_path,&buff) == 0)  
  17.     {  
  18.         /**当前用户为root,当然拥有读的权限*/  
  19.         if(0 == geteuid())  
  20.         {  
  21.             return 1;  
  22.         }  
  23.         /**当前用户为该文件(文件夹)的所有者,判断是否有所有者可读权限*/  
  24.         else if(buff.st_uid == geteuid())  
  25.         {  
  26.             return ((buff.st_mode & S_IRUSR != 0)?1 : 0);  
  27.         }  
  28.         /**当前用户组为该文件(文件夹)的用户组,判断是否有用户组可读权限*/  
  29.         else if(buff.st_gid == getegid())  
  30.         {  
  31.             return ((buff.st_mode & S_IRGRP != 0)?1 : 0);  
  32.         }  
  33.         /**判断其他人是否有可读权限*/  
  34.         else  
  35.         {  
  36.             return ((buff.st_mode & S_IROTH != 0)?1 : 0);  
  37.         }  
  38.     }  
  39.     else  
  40.     {  
  41.         return -1;  
  42.     }  
  43. }  

 

     函数的过程很简单,判断逻辑在注释中也写的很清楚了,需要包含的头文件:

    #include <sys/unistd.h>
    #include <sys/stat.h>
    #include <sys/types.h>

    利用这个思路,判断可写,判断可运行的函数就很容易写出了。

    下面是判断文件(文件夹)是否可写的函数:

   

view plaincopy to clipboardprint?
  1. #include <sys/unistd.h>  
  2. #include <sys/stat.h>  
  3. #include <sys/types.h>  
  4.   
  5. /** /brief 判断文件(文件夹)在当前上下文环境下是否可写 
  6.  * 
  7.  * /param const char* _path: 文件或文件夹的路径,可以为绝对路径或相对路径 
  8.  * /return signed char 
  9.  *  1:可读; 
  10.  *  0:不可读; 
  11.  * -1:错误,错误号可以从全局的errno获取; 
  12.  */  
  13. signed char canWrite(const char* _path)  
  14. {  
  15.     struct stat buff;  
  16.     if(stat(_path,&buff) == 0)  
  17.     {  
  18.         /**当前用户为root,当然拥有写的权限*/  
  19.         if(0 == geteuid())  
  20.         {  
  21.             return 1;  
  22.         }  
  23.         /**当前用户为该文件(文件夹)的所有者,判断是否有所有者可写权限*/  
  24.         else if(buff.st_uid == geteuid())  
  25.         {  
  26.             return ((buff.st_mode & S_IWUSR != 0)?1 : 0);  
  27.         }  
  28.         /**当前用户组为该文件(文件夹)的用户组,判断是否有用户组可写权限*/  
  29.         else if(buff.st_gid == getegid())  
  30.         {  
  31.             return ((buff.st_mode & S_IWGRP != 0)?1 : 0);  
  32.         }  
  33.         /**判断其他人是否有可读权限*/  
  34.         else  
  35.         {  
  36.             return ((buff.st_mode & S_IWOTH != 0)?1 : 0);  
  37.         }  
  38.     }  
  39.     else  
  40.     {  
  41.         return -1;  
  42.     }  
  43. }  

 

    下面是判断文件(文件夹)是否可运行的函数:

 

   

view plaincopy to clipboardprint?
  1. #include <sys/unistd.h>  
  2. #include <sys/stat.h>  
  3. #include <sys/types.h>  
  4.   
  5. /** /brief 判断文件(文件夹)在当前上下文环境下是否可执行 
  6.  * 
  7.  * /param const char* _path: 文件或文件夹的路径,可以为绝对路径或相对路径 
  8.  * /return signed char 
  9.  *  1:可读; 
  10.  *  0:不可读; 
  11.  * -1:错误,错误号可以从全局的errno获取; 
  12.  */  
  13. signed char canExecute(const char* _path)  
  14. {  
  15.     struct stat buff;  
  16.     if(stat(_path,&buff) == 0)  
  17.     {  
  18.         /**当前用户为root,当然拥有读的权限*/  
  19.         if(0 == geteuid())  
  20.         {  
  21.             return 1;  
  22.         }  
  23.         /**当前用户为该文件(文件夹)的所有者,判断是否有所有者可执行权限*/  
  24.         else if(buff.st_uid == geteuid())  
  25.         {  
  26.             return ((buff.st_mode & S_IXUSR != 0)?1 : 0);  
  27.         }  
  28.         /**当前用户组为该文件(文件夹)的用户组,判断是否有用户组可执行权限*/  
  29.         else if(buff.st_gid == getegid())  
  30.         {  
  31.             return ((buff.st_mode & S_IXGRP != 0)?1 : 0);  
  32.         }  
  33.         /**判断其他人是否有可执行权限*/  
  34.         else  
  35.         {  
  36.             return ((buff.st_mode & S_IXOTH != 0)?1 : 0);  
  37.         }  
  38.     }  
  39.     else  
  40.     {  
  41.         return -1;  
  42.     }  
  43. }  
 

 

  •     应用二:获得文件(文件夹)的大小

    对于普通文件来说,获取文件占用的大小很简单,只需要返回结构体stat的st_sizee即可。但是对于文件夹来说,结构体stat的st_size表明的是文件夹本身占用的空间大小(在Linux文件体系中,对于文件夹来说是需要空间来存储自身文件夹下的文件或文件夹的inode号的),与我们普遍意义上理解的文件夹应该返回的是其包含文件或文件夹的总容量不同,因此需要设计一个函数来获得文件夹下所有文件(文件夹)的总容量:

   

view plaincopy to clipboardprint?
  1. #include <sys/unistd.h>  
  2. #include <sys/stat.h>  
  3. #include <sys/types.h>  
  4. #include <dirent.h>  
  5. #include <string>  
  6.   
  7. /** /brief 获得文件夹的总大小 
  8.  * 
  9.  * /param const char* _path: 文件夹的路径,可以为绝对路径或相对路径 
  10.  * /return off_t 
  11.  * 返回路径指向文件夹的总容量; 
  12.  */  
  13. off_t getDirTotalSize(const char* _path)  
  14. {  
  15.     struct dirent* ent(0);  
  16.     DIR* pDir(opendir(_path));  
  17.     off_t result(0);  
  18.     char buff[512] = {0};  
  19.     while ((ent = readdir(pDir)) != 0)  
  20.     {  
  21.         /**在Linux文件系统中 .和..也是特殊的子目录,明显这里不应该计算*/  
  22.         if(strcmp(ent->d_name,".") == 0 || strcmp(ent->d_name,"..") == 0)  
  23.         {  
  24.             continue;  
  25.         }  
  26.         sprintf(buff, "%s/%s", _path, ent->d_name);  
  27.         /**如果当前是目录 则递归计算子目录的大小*/  
  28.         if (ent->d_type == DT_DIR)  
  29.         {  
  30.             result += getDirTotalSize(buff);  
  31.         }  
  32.         else  
  33.         {  
  34.             result += getFileSize(buff);  
  35.         }  
  36.     }  
  37.     return result;  
  38. }  
  39.   
  40. /** /brief 获得文件的大小 
  41.  * 
  42.  * /param const char* _path: 文件的路径,可以为绝对路径或相对路径 
  43.  * /return off_t 
  44.  * 成功则返回路径指向文件的大小; 
  45.  * -1:错误,错误号可以从全局的errno获取; 
  46.  */  
  47. off_t getFileSize(const char* _path)  
  48. {  
  49.     struct stat buff;  
  50.     if (stat(_path, &buff) == 0)  
  51.     {  
  52.         return buff.st_size;  
  53.     }  
  54.     else  
  55.     {  
  56.         return -1;  
  57.     }  
  58. }  

 

 

   其实更加通用的遍历目录函数可以这样设计:用注册回调函数的方法来实现,这个回调函数的参数就是每个遍历项的路径(最好是绝对路径),那么以后遍历目录就不需要改变了 只需要在应用中注册不同的回调函数就可以了。实现如下:

  

view plaincopy to clipboardprint?
  1. #include <sys/unistd.h>  
  2. #include <sys/stat.h>  
  3. #include <sys/types.h>  
  4. #include <dirent.h>  
  5. #include <string>  
  6. #include <stdio.h>  
  7.   
  8. off_t getFileSize(const char* _path);  
  9. void traverseDir(const char* _path,off_t(*_callPtr)(const char*),void(*_callbackResPtr)(off_t) = 0);  
  10. void sumSize(off_t _size);  
  11.   
  12. /**< 计算的文件夹大小结果 */  
  13. off_t result(0);  
  14. int main(int argc, char** argv)  
  15. {  
  16.     traverseDir(*(++argv),getFileSize,sumSize);  
  17.     printf("%ld", result);  
  18.     return 0;  
  19. }  
  20.   
  21. /** /brief 递归遍历目录,并在遇到非文件夹时 
  22.  *  调用回调函数off_t(*_callPtr)(const char*) 参数为当前的绝对路径 
  23.  * 
  24.  * /param const char* _path: 需要遍历的文件夹的路径,可以为绝对路径或相对路径 
  25.  * /param off_t(*_callPtr)(const char*): 
  26.  * 需要遍历的文件夹的路径,可以为绝对路径或相对路径 
  27.  * /param void(*_callbackResPtr)(off_t): 
  28.  * 以每次调用完_callPtr后的返回值为参数的回调函数,默认值为0, 
  29.  *  表示不对每次调用_callPtr的结果感兴趣 
  30.  * /return void 
  31.  */  
  32. void traverseDir(const char* _path,off_t(*_callPtr)(const char*),void(*_callbackResPtr)(off_t))  
  33. {  
  34.     struct dirent* ent(0);  
  35.     DIR* pDir(opendir(_path));  
  36.     char buff[512] = {0};  
  37.     while ((ent = readdir(pDir)) != 0)  
  38.     {  
  39.         /**在Linux文件系统中 .和..也是特殊的子目录,明显这里不应该递归*/  
  40.         if(strcmp(ent->d_name,".") == 0 || strcmp(ent->d_name,"..") == 0)  
  41.         {  
  42.             continue;  
  43.         }  
  44.         sprintf(buff, "%s/%s", _path, ent->d_name);  
  45.         /**如果当前是目录 则递归子目录*/  
  46.         if (ent->d_type == DT_DIR)  
  47.         {  
  48.             traverseDir(buff,_callPtr,_callbackResPtr);  
  49.         }  
  50.         else  
  51.         {  
  52.             if(_callbackResPtr)  
  53.             {  
  54.                 (*_callbackResPtr)( (*_callPtr)(buff) );  
  55.             }  
  56.             else  
  57.             {  
  58.                 (*_callPtr)(buff);  
  59.             }  
  60.         }  
  61.     }  
  62.     return;  
  63. }  
  64.   
  65.   
  66. /** /brief 获得文件的大小 
  67.  * 
  68.  * /param const char* _path: 文件的路径,可以为绝对路径或相对路径 
  69.  * /return off_t 
  70.  * 成功则返回路径指向文件的大小; 
  71.  * -1:错误,错误号可以从全局的errno获取; 
  72.  */  
  73. off_t getFileSize(const char* _path)  
  74. {  
  75.     struct stat buff;  
  76.     if (stat(_path, &buff) == 0)  
  77.     {  
  78.         return buff.st_size;  
  79.     }  
  80.     else  
  81.     {  
  82.         return -1;  
  83.     }  
  84. }  
  85.   
  86. /** /brief 一个简单的统计,把每次传入的数值累加起来 赋值到result上 
  87.  * 
  88.  * /param off_t _size: 文件的大小 
  89.  * /return void 
  90.  */  
  91. void sumSize(off_t _size)  
  92. {  
  93.     result += _size;  
  94.     return;  
  95. }  

 

   这种实现方式的优势是利用回调函数,遍历文件夹的操作可以复用,缺点是如果需要统计每次回调函数的结果就需要额外的一个全局参数(当然可以用命名空间的方式局部化。。。)。利用这种方式,还能方便的实现出统计文件夹下各种文件类型的数量,属于某个用户ID文件的数量等等(改改两个回调函数就行了)。

  •     应用三:获得文件(文件夹)的三个时间:最后访问(读)时间、最后修改(写)时间、创建时间或最后更改(属性更改)时间

    在项目中,我们经常会需要获得文件(文件夹)的最后访问(读)时间、最后修改(写)时间、创建时间或最后更改(属性更改)时间这三种时间,在Linux中,触发这三种时间改变的条件分别是:

   最后访问(读)时间:文件(文件夹)最后一次被存取或执行的时间;

   最后修改(写)时间:文件(文件夹)最后一次被修改的时间,这里指的修改是内容上的;

   创建时间或最后更改(属性更改)时间:文件(文件夹)最后一次被更改的时间,这里指的修改是属性上的,如所有者、权限等;

   对应到结构体stat上就是:

   time_t    st_atime;   /* time of last access */
   time_t    st_mtime;   /* time of last modification */
   time_t    st_ctime;   /* time of last status change */

   值得一提的是,以上三种时间在Linux中是用UTC表示的,单位是秒,举个例子:1285328411表示的是从1970年1月1日开始所经过的秒数,值得注意的是这里的时间是UTC时间。

 

  这里仅用最后访问(读)时间为例:

 

view plaincopy to clipboardprint?
  1. #include <sys/unistd.h>  
  2. #include <sys/stat.h>  
  3.   
  4. /** /brief 判断文件(文件夹)的最后访问时间 
  5.  * 
  6.  * /param const char* _path: 文件或文件夹的路径,可以为绝对路径或相对路径 
  7.  * /return time_t 
  8.  *  >0:成功; 
  9.  *  0:错误; 
  10.  */  
  11. time_t getReadTime(const char* _path)  
  12. {  
  13.     struct stat buff;  
  14.     if(stat(_path,&buff) == 0)  
  15.     {  
  16.         return buff.st_atime;  
  17.     }  
  18.     return 0;  
  19. }  

 

   另外两种时间的获取方式,就当作小练习吧。

  •   应用四:获得文件类型

   最后来谈谈如何根据st_mode来判断文件(文件夹)的类型,这里可以利用库本身就定义好的一些宏:

   #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) /**文件夹的判断*/
   #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) /**管道文件的判断*/
   #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) /**字符设备的判断*/
   #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)  /**块设备的判断*/
   #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) /**普通文件的判断*/

   实例如下:

  

view plaincopy to clipboardprint?
  1. #include <sys/unistd.h>  
  2. #include <sys/stat.h>  
  3. #include <sys/types.h>  
  4.   
  5. /** /brief 判断文件(文件夹)的类型 
  6.  * 
  7.  * /param const char* _path: 文件或文件夹的路径,可以为绝对路径或相对路径 
  8.  * /return signed char 
  9.  *  0:普通文件 
  10.  *  1:文件夹 
  11.  *  2:管道文件 
  12.  *  3:字符设备文件 
  13.  *  4:块设备文件 
  14.  * -1:错误,错误号可以从全局的errno获取; 
  15.  */  
  16. signed char getFileType(const char* _path)  
  17. {  
  18.     struct stat buff;  
  19.     if(stat(_path,&buff) == 0)  
  20.     {  
  21.         if(S_ISREG(buff.st_mode))  
  22.         {  
  23.             return 0;  
  24.         }  
  25.         else if(S_ISDIR(buff.st_mode))  
  26.         {  
  27.             return 1;  
  28.         }  
  29.         else if(S_ISFIFO(buff.st_mode))  
  30.         {  
  31.             return 2;  
  32.         }  
  33.         else if(S_ISCHR(buff.st_mode))  
  34.         {  
  35.             return 3;  
  36.         }  
  37.         else if(S_ISBLK(buff.st_mode))  
  38.         {  
  39.             return 4;  
  40.         }  
  41.         else  
  42.         {  
  43.             return -1;  
  44.         }  
  45.     }  
  46.     else  
  47.     {  
  48.         return -1;  
  49.     }  
  50. }  

 

   当然在项目中一般是不用硬编码的,可以定义相关的enum。

 

原创粉丝点击