linux中stat函式和使用方法
stat 函式講解
表頭檔案: #include <sys/stat.h>
#include <unistd.h>
定義函式: int stat(const char *file_name, struct stat *buf);
函式說明: 通過檔名filename獲取檔案資訊,並儲存在buf所指的結構體stat中
返回值: 執行成功則返回0,失敗返回-1,錯誤程式碼存於errno
錯誤程式碼: 複製程式碼
ENOENT 引數file_name指定的檔案不存在 ENOTDIR 路徑中的目錄存在但卻非真正的目錄 ELOOP 欲開啟的檔案有過多符號連線問題,上限為16符號連線 EFAULT 引數buf為無效指標,指向無法存在的記憶體空間 EACCESS 存取檔案時被拒絕 ENOMEM 核心記憶體不足 ENAMETOOLONG 引數file_name的路徑名稱太長
複製程式碼 複製程式碼
#include <sys/stat.h> #include <unistd.h> #include <stdio.h>
int main() { struct stat buf; stat("/etc/hosts", &buf); printf("/etc/hosts file size = %d\n", buf.st_size); }
/*************************************************************************/ struct stat { dev_t st_dev; //檔案的裝置編號 ino_t st_ino; //節點 mode_t st_mode; //檔案的型別和存取的許可權 nlink_t st_nlink; //連到該檔案的硬連線數目,剛建立的檔案值為1 uid_t st_uid; //使用者ID gid_t st_gid; //組ID dev_t st_rdev; //(裝置型別)若此檔案為裝置檔案,則為其裝置編號 off_t st_size; //檔案位元組數(檔案大小) unsigned long st_blksize; //塊大小(檔案系統的I/O 緩衝區大小) unsigned long st_blocks; //塊數 time_t st_atime; //最後一次訪問時間 time_t st_mtime; //最後一次修改時間 time_t st_ctime; //最後一次改變時間(指屬性) };
複製程式碼
先前所描述的st_mode 則定義了下列數種情況: 複製程式碼
S_IFMT 0170000 檔案型別的位遮罩 S_IFSOCK 0140000 scoket S_IFLNK 0120000 符號連線 S_IFREG 0100000 一般檔案 S_IFBLK 0060000 區塊裝置 S_IFDIR 0040000 目錄 S_IFCHR 0020000 字元裝置 S_IFIFO 0010000 先進先出 S_ISUID 04000 檔案的(set user-id on execution)位 S_ISGID 02000 檔案的(set group-id on execution)位 S_ISVTX 01000 檔案的sticky位 S_IRUSR(S_IREAD) 00400 檔案所有者具可讀取許可權 S_IWUSR(S_IWRITE)00200 檔案所有者具可寫入許可權 S_IXUSR(S_IEXEC) 00100 檔案所有者具可執行許可權 S_IRGRP 00040 使用者組具可讀取許可權 S_IWGRP 00020 使用者組具可寫入許可權 S_IXGRP 00010 使用者組具可執行許可權 S_IROTH 00004 其他使用者具可讀取許可權 S_IWOTH 00002 其他使用者具可寫入許可權 S_IXOTH 00001 其他使用者具可執行許可權
複製程式碼
上述的檔案型別在POSIX中定義了檢查這些型別的巨集定義:
複製程式碼
S_ISLNK (st_mode) 判斷是否為符號連線
S_ISREG (st_mode) 是否為一般檔案
S_ISDIR (st_mode) 是否為目錄
S_ISCHR (st_mode) 是否為字元裝置檔案
S_ISBLK (s3e) 是否為先進先出
S_ISSOCK (st_mode) 是否為socket
複製程式碼
若一目錄具有sticky位(S_ISVTX),則表示在此目錄下的檔案只能被該檔案所有者、此目錄所有者或root來刪除或改名。
複製程式碼
struct statfs { long f_type; //檔案系統型別 long f_bsize; //塊大小 long f_blocks; //塊多少 long f_bfree; //空閒的塊 long f_bavail; //可用塊 long f_files; //總檔案節點 long f_ffree; //空閒檔案節點 fsid_t f_fsid; //檔案系統id long f_namelen; //檔名的最大長度 long f_spare[6]; //spare for later };
複製程式碼
stat、fstat 和 lstat 函式(UNIX)
#include<sys/types.h> #include<sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf);
stat 提供檔名字,獲取檔案對應屬性。感覺一般是檔案沒有開啟的時候這樣操作。
int fstat(int filedes, struct stat *buf);
fstat 通過檔案描述符獲取檔案對應的屬性。檔案開啟後這樣操作
int lstat(const char *restrict pathname, struct stat *restrict buf);
lstat 連線檔案
三個函式的返回:若成功則為0,若出錯則為-1。
給定一個pathname,stat函式返回一個與此命名檔案有關的資訊結構,fstat函式獲得已在描述符filedes上開啟的檔案的有關資訊。lstat函式類似於stat,但是當命名的檔案是一個符號連線時,lstat返回該符號連線的有關資訊,而不是由該符號連線引用的檔案的資訊。
第二個引數是個指標,它指向一個我們應提供的結構。這些函式填寫由buf指向的結構。該結構的實際定義可能隨實現而有所不同,但其基本形式是: 複製程式碼
struct stat{ mode_t st_mode; /file tpye &mode (permissions)/ ino_t st_ino; /i=node number (serial number)/ dev_t st_rdev; /device number for special files/ nlink_t st_nlink; /number of links/ uid_t st_uid; /user id of owner/ gid_t st_gid; /group ID of owner/ off_t st_size; /size in bytes for regular files/ time_t st_atime; /time of last access/ time_t st_mtime; /time of last modification/ time_t st_ctime; /time of last file status change/ long st_blksize; /*best I/O block size */ long st_blocks; /number of 512-byte blocks allocated/ };
複製程式碼
注意,除最後兩個以外,其他各成員都為基本系統資料型別。我們將說明此結構的每個成員以瞭解檔案屬性。
使用stat函式最多的可能是ls-l命令,用其可以獲得有關一個檔案的所有資訊。
- 函式都是獲取檔案(普通檔案,目錄,管道,socket,字元,塊)的屬性。
函式原型:
#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf);
提供檔名字,獲取檔案對應屬性。
int fstat(int filedes, struct stat *buf);
通過檔案描述符獲取檔案對應的屬性。
int lstat(const char *restrict pathname, struct stat *restrict buf);
連線檔案描述命,獲取檔案屬性。
- 檔案對應的屬性 複製程式碼
struct stat { mode_t st_mode; //檔案對應的模式,檔案,目錄等 ino_t st_ino; //inode節點號 dev_t st_dev; //裝置號碼 dev_t st_rdev; //特殊裝置號碼 nlink_t st_nlink; //檔案的連線數 uid_t st_uid; //檔案所有者 gid_t st_gid; //檔案所有者對應的組 off_t st_size; //普通檔案,對應的檔案位元組數 time_t st_atime; //檔案最後被訪問的時間 time_t st_mtime; //檔案內容最後被修改的時間 time_t st_ctime; //檔案狀態改變時間 blksize_t st_blksize; //檔案內容對應的塊大小 blkcnt_t st_blocks; //檔案內容對應的塊數量 };