使用stat()函数判断文件类型

来源:互联网 发布:mac的excel数据有效性 编辑:程序博客网 时间:2024/04/27 21:56

一、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_IFMT     0170000   bit mask for the file type bit fields 
    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_IFMT     0170000   bit mask for the file type bit fields 

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

0 0
原创粉丝点击