设为首页 收藏本站
查看: 806|回复: 2

[经验分享] linux系统编程之文件与IO:stat()系统调用获取文件信息

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2013-7-12 09:09:31 | 显示全部楼层 |阅读模式

一、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

示例程序:

#include <unistd.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#define ERR_EXIT(m) \    do \    { \        perror(m); \        exit(EXIT_FAILURE); \    } while(0)#define MAJOR(a) (int)((unsigned short)a >> 8) //高8位:主设备号#define MINOR(a) (int)((unsigned short)a & 0xFF)//低8位:次设备号int filetype(struct stat *buf);void fileperm(struct stat *buf, char *perm);int main(int argc, char *argv[]){    if (argc != 2)    {        fprintf(stderr, "Usage %s file\n", argv[0]);        exit(EXIT_FAILURE);    }    struct stat sbuf;    printf("Filename:%s\n", argv[1]);    if (lstat(argv[1], &sbuf) == -1)        ERR_EXIT("stat error");    printf("File number:major %d,minor %d inode %d\n", MAJOR(sbuf.st_dev), MINOR(sbuf.st_dev), (int)sbuf.st_ino);    if (filetype(&sbuf))    {        printf("Device number:major %d,minor %d\n", MAJOR(sbuf.st_rdev), MINOR(sbuf.st_rdev));    }        char perm[11] = {0};    fileperm(&sbuf, perm);    printf("File permission bits=%o %s\n", sbuf.st_mode & 07777, perm);        return 0;}int filetype(struct stat *buf){    int flag = 0;    printf("Filetype:");    mode_t mode;    mode = buf->st_mode;    switch (mode & S_IFMT)    {    case S_IFSOCK:        printf("socket\n");        break;    case S_IFLNK:        printf("symbolic link\n");        break;    case S_IFREG:        printf("regular file\n");        break;    case S_IFBLK:        printf("block device\n");        flag = 1; //该文件为设备文件        break;    case S_IFDIR:        printf("directory\n");        break;    case S_IFCHR:        printf("character device\n");        flag = 1;        break;    case S_IFIFO:        printf("FIFO\n");        break;    default:        printf("unknown file type\n");        break;    }    return flag;}void fileperm(struct stat *buf, char *perm){    strcpy(perm, "----------");    perm[0] = '?';    mode_t mode;        mode = buf->st_mode;        switch (mode & S_IFMT)        {        case S_IFSOCK:        perm[0] = 's';                break;        case S_IFLNK:        perm[0] = 'l';                break;        case S_IFREG:        perm[0] = '-';                break;        case S_IFBLK:        perm[0] = 'b';                break;        case S_IFDIR:        perm[0] = 'd';                break;        case S_IFCHR:        perm[0] = 'c';                break;        case S_IFIFO:        perm[0] = 'p';                break;    }    if (mode & S_IRUSR)        perm[1] = 'r';    if (mode & S_IWUSR)        perm[2] = 'w';    if (mode & S_IXUSR)        perm[3] = 'x';    if (mode & S_IRGRP)        perm[4] = 'r';    if (mode & S_IWGRP)        perm[5] = 'w';    if (mode & S_IXGRP)        perm[6] = 'x';    if (mode & S_IROTH)        perm[7] = 'r';    if (mode & S_IWOTH)        perm[8] = 'w';    if (mode & S_IXOTH)        perm[9] = 'x';    perm[10] = '\0';}

运行结果:

7547481_1373552894d1E7.jpg

以下是man手册上的一个示例:

#include <sys/types.h>#include <sys/stat.h>#include <time.h>#include <stdio.h>#include <stdlib.h>intmain(int argc, char *argv[]){    struct stat sb;    if (argc != 2) {    fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);    exit(EXIT_FAILURE);    }    if (stat(argv[1], &sb) == -1) {    perror("stat");    exit(EXIT_SUCCESS);    }    printf("File type:                ");    switch (sb.st_mode & S_IFMT) {       case S_IFBLK:  printf("block device\n");            break;       case S_IFCHR:  printf("character device\n");        break;       case S_IFDIR:  printf("directory\n");               break;       case S_IFIFO:  printf("FIFO/pipe\n");               break;       case S_IFLNK:  printf("symlink\n");                 break;       case S_IFREG:  printf("regular file\n");            break;       case S_IFSOCK: printf("socket\n");                  break;       default:       printf("unknown?\n");                break;       }       printf("I-node number:            %ld\n", (long) sb.st_ino);       printf("Mode:                     %lo (octal)\n",               (unsigned long) sb.st_mode);       printf("Link count:               %ld\n", (long) sb.st_nlink);       printf("Ownership:                UID=%ld   GID=%ld\n",               (long) sb.st_uid, (long) sb.st_gid);       printf("Preferred I/O block size: %ld bytes\n",               (long) sb.st_blksize);       printf("File size:                %lld bytes\n",               (long long) sb.st_size);       printf("Blocks allocated:         %lld\n",               (long long) sb.st_blocks);       printf("Last status change:       %s", ctime(&sb.st_ctime));       printf("Last file access:         %s", ctime(&sb.st_atime));       printf("Last file modification:   %s", ctime(&sb.st_mtime));       exit(EXIT_SUCCESS);}


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-7416-1-1.html 上篇帖子: linux系统编程之文件与IO:利用lseek()创建空洞文件 下篇帖子: linux系统编程之文件与IO:实现ls -l功能 linux 信息

尚未签到

发表于 2013-9-23 02:42:50 | 显示全部楼层
有道理。。。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

尚未签到

发表于 2014-1-5 10:55:38 | 显示全部楼层
地址:银河系中的太阳系中的第三个行星的大公鸡的鸡胗处、

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表