stat,fstate,lstat函數


  1. #include <sys/stat.h>  
  2. int stat (const char *restrict pathname,struct stat* restrict buf)  
  3.   
  4. int fstat(int filedes,struct stat *buf);  
  5.   
  6. int lstat(const char *restrict pathname,struct stat *restrict buf);  

stat, fstat and lstat是用來檢查文件屬性的。他們將文件屬性信息通過a struct stat object 返回。

 

int stat (const char *filename, struct stat *buf) [Function]
The stat function returns information about the attributes of the file named by
filename in the structure pointed to by buf.
If filename is the name of a symbolic link, the attributes you get describe the file
that the link points to. If the link points to a nonexistent file name, then stat fails
reporting a nonexistent file.
The return value is 0 if the operation is successful, or -1 on failure. In addition to the
usual file name errors (see Section 11.2.3 [File Name Errors], page 224, the following
errno error conditions are defined for this function:
ENOENT The file named by filename doesn’t exist.
When the sources are compiled with _FILE_OFFSET_BITS == 64 this function is in fact
stat64 since the LFS interface transparently replaces the normal implementation.

 

int fstat (int filedes, struct stat *buf) [Function]
The fstat function is like stat, except that it takes an open file descriptor as an
argument instead of a file name. See Chapter 13 [Low-Level Input/Output], page 296.
Like stat, fstat returns 0 on success and -1 on failure. The following errno error
conditions are defined for fstat:
EBADF The filedes argument is not a valid file descriptor.
When the sources are compiled with _FILE_OFFSET_BITS == 64 this function is in fact
fstat64 since the LFS interface transparently replaces the normal implementation.

 

int lstat (const char *filename, struct stat *buf) [Function]
The lstat function is like stat, except that it does not follow symbolic links. If
filename is the name of a symbolic link, lstat returns information about the link
itself; otherwise lstat works like stat. See Section 14.5 [Symbolic Links], page 357.
When the sources are compiled with _FILE_OFFSET_BITS == 64 this function is in fact
lstat64 since the LFS interface transparently replaces the normal implementation.

 

一旦給出pathname,stat函數就返回與此命名有關的信息結構,fstat函數獲取已在描述符filedes上打開文件的有關信息。lstat函數類似於stat,但是當命名文件時一個符號鏈接,lstat返回該符號鏈接的有關信息。而不是有該符號引用文的信息。

第二個參數是buf,指針。指向我們必須提供的結構。

 

 

  •    應用一:判斷文件(文件夾)是否可讀、可寫、可執行:

 

    判斷文件(文件夾)是否可讀的函數:

   

  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(constchar* _path) 
  14.     struct stat buff; 
  15.     if(stat(_path,&buff) == 0) 
  16.     { 
  17.         /**當前用戶為root,當然擁有讀的權限*/ 
  18.         if(0 == geteuid()) 
  19.         { 
  20.             return 1; 
  21.         } 
  22.         /**當前用戶為該文件(文件夾)的所有者,判斷是否有所有者可讀權限*/ 
  23.         else if(buff.st_uid == geteuid()) 
  24.         { 
  25.             return ((buff.st_mode & S_IRUSR != 0)?1 : 0); 
  26.         } 
  27.         /**當前用戶組為該文件(文件夾)的用戶組,判斷是否有用戶組可讀權限*/ 
  28.         else if(buff.st_gid == getegid()) 
  29.         { 
  30.             return ((buff.st_mode & S_IRGRP != 0)?1 : 0); 
  31.         } 
  32.         /**判斷其他人是否有可讀權限*/ 
  33.         else 
  34.         { 
  35.             return ((buff.st_mode & S_IROTH != 0)?1 : 0); 
  36.         } 
  37.     } 
  38.     else 
  39.     { 
  40.         return -1; 
  41.     } 

 

 

     函數的過程很簡單,判斷邏輯在注釋中也寫的很清楚了,需要包含的頭文件:

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

    利用這個思路,判斷可寫,判斷可運行的函數就很容易寫出了。

    下面是判斷文件(文件夾)是否可寫的函數:

  

  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(constchar* _path) 
  14.     struct stat buff; 
  15.     if(stat(_path,&buff) == 0) 
  16.     { 
  17.         /**當前用戶為root,當然擁有寫的權限*/ 
  18.         if(0 == geteuid()) 
  19.         { 
  20.             return 1; 
  21.         } 
  22.         /**當前用戶為該文件(文件夾)的所有者,判斷是否有所有者可寫權限*/ 
  23.         else if(buff.st_uid == geteuid()) 
  24.         { 
  25.             return ((buff.st_mode & S_IWUSR != 0)?1 : 0); 
  26.         } 
  27.         /**當前用戶組為該文件(文件夾)的用戶組,判斷是否有用戶組可寫權限*/ 
  28.         else if(buff.st_gid == getegid()) 
  29.         { 
  30.             return ((buff.st_mode & S_IWGRP != 0)?1 : 0); 
  31.         } 
  32.         /**判斷其他人是否有可讀權限*/ 
  33.         else 
  34.         { 
  35.             return ((buff.st_mode & S_IWOTH != 0)?1 : 0); 
  36.         } 
  37.     } 
  38.     else 
  39.     { 
  40.         return -1; 
  41.     } 

 

 

    下面是判斷文件(文件夾)是否可運行的函數:

 

  

  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(constchar* _path) 
  14.     struct stat buff; 
  15.     if(stat(_path,&buff) == 0) 
  16.     { 
  17.         /**當前用戶為root,當然擁有讀的權限*/ 
  18.         if(0 == geteuid()) 
  19.         { 
  20.             return 1; 
  21.         } 
  22.         /**當前用戶為該文件(文件夾)的所有者,判斷是否有所有者可執行權限*/ 
  23.         else if(buff.st_uid == geteuid()) 
  24.         { 
  25.             return ((buff.st_mode & S_IXUSR != 0)?1 : 0); 
  26.         } 
  27.         /**當前用戶組為該文件(文件夾)的用戶組,判斷是否有用戶組可執行權限*/ 
  28.         else if(buff.st_gid == getegid()) 
  29.         { 
  30.             return ((buff.st_mode & S_IXGRP != 0)?1 : 0); 
  31.         } 
  32.         /**判斷其他人是否有可執行權限*/ 
  33.         else 
  34.         { 
  35.             return ((buff.st_mode & S_IXOTH != 0)?1 : 0); 
  36.         } 
  37.     } 
  38.     else 
  39.     { 
  40.         return -1; 
  41.     } 

 

 

  •    應用二:獲得文件(文件夾)的大小

    對於普通文件來說,獲取文件占用的大小很簡單,只需要返回結構體stat的st_sizee即可。但是對於文件夾來說,結構體stat的st_size表明的是文件夾本身占用的空間大小(在Linux文件體系中,對於文件夾來說是需要空間來存儲自身文件夾下的文件或文件夾的inode號的),與我們普遍意義上理解的文件夾應該返回的是其包含文件或文件夾的總容量不同,因此需要設計一個函數來獲得文件夾下所有文件(文件夾)的總容量:

  

  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(constchar* _path) 
  14.     struct dirent* ent(0); 
  15.     DIR* pDir(opendir(_path)); 
  16.     off_t result(0); 
  17.     char buff[512] = {0}; 
  18.     while ((ent = readdir(pDir)) != 0) 
  19.     { 
  20.         /**在Linux文件系統中 .和..也是特殊的子目錄,明顯這里不應該計算*/ 
  21.         if(strcmp(ent->d_name,".") == 0 || strcmp(ent->d_name,"..") == 0) 
  22.         { 
  23.             continue; 
  24.         } 
  25.         sprintf(buff, "%s/%s", _path, ent->d_name); 
  26.         /**如果當前是目錄 則遞歸計算子目錄的大小*/ 
  27.         if (ent->d_type == DT_DIR) 
  28.         { 
  29.             result += getDirTotalSize(buff); 
  30.         } 
  31.         else 
  32.         { 
  33.             result += getFileSize(buff); 
  34.         } 
  35.     } 
  36.     return result; 
  37.  
  38. /** /brief 獲得文件的大小
  39. *
  40. * /param const char* _path: 文件的路徑,可以為絕對路徑或相對路徑
  41. * /return off_t
  42. * 成功則返回路徑指向文件的大小;
  43. * -1:錯誤,錯誤號可以從全局的errno獲取;
  44. */ 
  45. off_t getFileSize(const char* _path) 
  46.     struct stat buff; 
  47.     if (stat(_path, &buff) == 0) 
  48.     { 
  49.         return buff.st_size; 
  50.     } 
  51.     else 
  52.     { 
  53.         return -1; 
  54.     } 

 

 

  其實更加通用的遍歷目錄函數可以這樣設計:用注冊回調函數的方法來實現,這個回調函數的參數就是每個遍歷項的路徑(最好是絕對路徑),那么以后遍歷目錄就不需要改變了 只需要在應用中注冊不同的回調函數就可以了。實現如下:

 

  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(constchar* _path,off_t(*_callPtr)(constchar*),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.     traverseDir(*(++argv),getFileSize,sumSize); 
  16.     printf("%ld", result); 
  17.     return 0; 
  18.  
  19. /** /brief 遞歸遍歷目錄,並在遇到非文件夾時
  20. *  調用回調函數off_t(*_callPtr)(const char*) 參數為當前的絕對路徑
  21. *
  22. * /param const char* _path: 需要遍歷的文件夾的路徑,可以為絕對路徑或相對路徑
  23. * /param off_t(*_callPtr)(const char*):
  24. * 需要遍歷的文件夾的路徑,可以為絕對路徑或相對路徑
  25. * /param void(*_callbackResPtr)(off_t):
  26. * 以每次調用完_callPtr后的返回值為參數的回調函數,默認值為0,
  27. *  表示不對每次調用_callPtr的結果感興趣
  28. * /return void
  29. */ 
  30. void traverseDir(constchar* _path,off_t(*_callPtr)(constchar*),void(*_callbackResPtr)(off_t)) 
  31.     struct dirent* ent(0); 
  32.     DIR* pDir(opendir(_path)); 
  33.     char buff[512] = {0}; 
  34.     while ((ent = readdir(pDir)) != 0) 
  35.     { 
  36.         /**在Linux文件系統中 .和..也是特殊的子目錄,明顯這里不應該遞歸*/ 
  37.         if(strcmp(ent->d_name,".") == 0 || strcmp(ent->d_name,"..") == 0) 
  38.         { 
  39.             continue; 
  40.         } 
  41.         sprintf(buff, "%s/%s", _path, ent->d_name); 
  42.         /**如果當前是目錄 則遞歸子目錄*/ 
  43.         if (ent->d_type == DT_DIR) 
  44.         { 
  45.             traverseDir(buff,_callPtr,_callbackResPtr); 
  46.         } 
  47.         else 
  48.         { 
  49.             if(_callbackResPtr) 
  50.             { 
  51.                 (*_callbackResPtr)( (*_callPtr)(buff) ); 
  52.             } 
  53.             else 
  54.             { 
  55.                 (*_callPtr)(buff); 
  56.             } 
  57.         } 
  58.     } 
  59.     return; 
  60.  
  61.  
  62. /** /brief 獲得文件的大小
  63. *
  64. * /param const char* _path: 文件的路徑,可以為絕對路徑或相對路徑
  65. * /return off_t
  66. * 成功則返回路徑指向文件的大小;
  67. * -1:錯誤,錯誤號可以從全局的errno獲取;
  68. */ 
  69. off_t getFileSize(const char* _path) 
  70.     struct stat buff; 
  71.     if (stat(_path, &buff) == 0) 
  72.     { 
  73.         return buff.st_size; 
  74.     } 
  75.     else 
  76.     { 
  77.         return -1; 
  78.     } 
  79.  
  80. /** /brief 一個簡單的統計,把每次傳入的數值累加起來 賦值到result上
  81. *
  82. * /param off_t _size: 文件的大小
  83. * /return void
  84. */ 
  85. void sumSize(off_t _size) 
  86.     result += _size; 
  87.     return; 

 

   這種實現方式的優勢是利用回調函數,遍歷文件夾的操作可以復用,缺點是如果需要統計每次回調函數的結果就需要額外的一個全局參數(當然可以用命名空間的方式局部化。。。)。利用這種方式,還能方便的實現出統計文件夾下各種文件類型的數量,屬於某個用戶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時間。

 

  這里僅用最后訪問(讀)時間為例:

 

  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(constchar* _path) 
  12.     struct stat buff; 
  13.     if(stat(_path,&buff) == 0) 
  14.     { 
  15.         return buff.st_atime; 
  16.     } 
  17.     return 0; 

 

  另外兩種時間的獲取方式,就當作小練習吧。

  • 應用四:獲得文件類型

   最后來談談如何根據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) /**普通文件的判斷*/

   實例如下:

 

  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(constchar* _path) 
  17.     struct stat buff; 
  18.     if(stat(_path,&buff) == 0) 
  19.     { 
  20.         if(S_ISREG(buff.st_mode)) 
  21.         { 
  22.             return 0; 
  23.         } 
  24.         else if(S_ISDIR(buff.st_mode)) 
  25.         { 
  26.             return 1; 
  27.         } 
  28.         else if(S_ISFIFO(buff.st_mode)) 
  29.         { 
  30.             return 2; 
  31.         } 
  32.         else if(S_ISCHR(buff.st_mode)) 
  33.         { 
  34.             return 3; 
  35.         } 
  36.         else if(S_ISBLK(buff.st_mode)) 
  37.         { 
  38.             return 4; 
  39.         } 
  40.         else 
  41.         { 
  42.             return -1; 
  43.         } 
  44.     } 
  45.     else 
  46.     { 
  47.         return -1; 
  48.     } 

   當然在項目中一般是不用硬編碼的,可以定義相關的enum。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM