linux系統編程之檔案與IO(五):stat()系統調用擷取檔案資訊

來源:互聯網
上載者:User

一、stat()擷取檔案中繼資料

stat系統調用原型:
#include <sys/stat.h>

int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

協助資訊可通過:man 2 stat 查看

DESCRIPTION
       These  functions  return  information about a file.  No permissions are
       required on the file itself, but — in the case of stat() and lstat()  —
       execute  (search)  permission  is required on all of the directories in
       path that lead to the file.

       stat() stats the file pointed to by path and fills in buf.

       lstat() is identical to stat(), except that if path is a symbolic link,
       then the link itself is stat-ed, not the file that it refers to.

       fstat()  is  identical to stat(), except that the file to be stat-ed is
       specified by the file descriptor fd.

       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 :該檔案所屬裝置的裝置號,裝置號包括主裝置和和次裝置號,dev_t是16位整數,高8位表示主裝置號,低8位表示次裝置號*/
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection :包含檔案存取權限資訊及檔案類型*/
    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; /* blocksize for file system I/O */
    blkcnt_t  st_blocks;  /* number of 512B blocks allocated :分配的塊數量*/
    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:如修改檔案的許可權 */
};

檔案類型有兩種方式獲得:

1.通過以下的一些宏進行驗證:m為struct stat中得st_mode欄位

    S_ISREG(m)  is it a regular file?

    S_ISDIR(m)  directory?

    S_ISCHR(m)  character device?

    S_ISBLK(m)  block device?

    S_ISFIFO(m) FIFO (named pipe)?

    S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.)

    S_ISSOCK(m) socket? (Not in POSIX.1-1996.)

2.利用struct stat中得st_mode欄位與S_IFMT進行與運算:mode&S_IFMT,然後將得到的結果與下列的常量比較,相等就是

The following flags are defined for the st_mode field:

  
    S_IFSOCK   0140000   socket
    S_IFLNK    0120000   symbolic link
    S_IFREG    0100000   regular file
    S_IFBLK    0060000   block device
    S_IFDIR    0040000   directory
    S_IFCHR    0020000   character device
    S_IFIFO    0010000   FIFO

檔案存取權限獲得:利用struct stat中得st_mode欄位與S_IFMT進行與運算:mode&S_IFMT,然後將得到的結果與下列的常量比較,相等就是



S_ISUID    0004000   set UID bit
S_ISGID    0002000   set-group-ID bit (see below)
S_ISVTX    0001000   sticky bit (see below)
S_IRWXU    00700     mask for file owner permissions
S_IRUSR    00400     owner has read permission
S_IWUSR    00200     owner has write permission
S_IXUSR    00100     owner has execute permission
S_IRWXG    00070     mask for group permissions
S_IRGRP    00040     group has read permission
S_IWGRP    00020     group has write permission
S_IXGRP    00010     group has execute permission
S_IRWXO    00007     mask for permissions for others (not in group)
S_IROTH    00004     others have read permission
S_IWOTH    00002     others have write permission
S_IXOTH    00001     others have execute permission

樣本程式:

#include <unistd.h><sys/stat.h><sys/types.h><sys/stat.h><fcntl.h><stdlib.h><stdio.h><errno.h><.h> ERR_EXIT(m) \    ( MAJOR(a) (int)((unsigned short)a >> 8)  MINOR(a) (int)((unsigned short)a & 0xFF) filetype( stat * fileperm( stat *buf,  * main( argc,  * (argc != , argv[, argv[ (lstat(argv[], &sbuf) == -, MAJOR(sbuf.st_dev), MINOR(sbuf.st_dev), ( (filetype(& perm[] = {&, sbuf.st_mode &   filetype( stat * flag = = buf-> (mode &= ;         =  fileperm( stat *buf,  *] = = buf-> (mode &] = ] = ] = ] = ] = ] = ] =  (mode &] =  (mode &] =  (mode &] =  (mode &] =  (mode &] =  (mode &] =  (mode &] =  (mode &] =  (mode &] = ] = 

運行結果:

<sys/stat.h><time.h><stdio.h><stdlib.h> argc, * (argc != , argv[ (stat(argv[], &sb) == - (sb.st_mode & S_IFBLK: printf(); S_IFCHR: printf(); S_IFDIR: printf(); S_IFIFO: printf(); S_IFLNK: printf(); S_IFREG: printf(); S_IFSOCK: printf(); : printf(); , (, () sb.st_uid, ( , ctime(&, ctime(&, ctime(&

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.