zoukankan      html  css  js  c++  java
  • struct stat 操作 小结


    stat,lstat,fstat1 函数都是 获取文件(普通文件,目录,管道,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);连接文件描述命,获取文件属性。2 文件对应的属性;


    描述: fstat()用来将参数filedes 所指向的文件状态复制到参数buf 所指向的结构中(structstat)。fstat()与stat()作用完全相同,不同之处在于传入的参数为已打开的文件描述符。


    返回值:执行成功返回0,失败返回-1,错误代码保存在errno。


    ============


    下面举一个小例子:
    ------------------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <fcntl.h>

    main()
    {
               struct stat buf;
               intfd;
               fd = open( "/etc/passwd", O_RDONLY);
               fstat (fd,&buf);
              printf("/etc/passwd file size = %d ",(int)(buf.st_size));
    }

    ------------------------------
    执行结果:
    /etc/passwd file size = 1656
       
    也许,你看到这里会产生和我一样的疑惑:这个struct stat 结构到底是什么样的呢?
    下面我就来详细的介绍一下它了哦~~~      


    二、struct stat结构体

    定义如下:

    struct stat { 
             dev_t     st_dev; // 文件所在设备ID 
             ino_t     st_ino;     // 结点(inode)编号  
             mode_t    st_mode;    // 保护模式 
             nlink_t   st_nlink;   // 硬链接个数  
             uid_t     st_uid;     // 所有者用户ID  
             gid_t     st_gid;     // 所有者组ID  
             dev_t     st_rdev;    // 设备ID(如果是特殊文件) 
             off_t     st_size;    // 总体尺寸,以字节为单位 
             blksize_t st_blksize; // 文件系统 I/O 块大小
    
             blkcnt_t  st_blocks;  // 已分配 512B 块个数
    
             time_t    st_atime;   // 上次访问时间 
             time_t    st_mtime;   // 上次更新时间 
             time_t    st_ctime;   // 上次状态更改时间 
    };
    

    st_dev字段描述该文件所在的设备。(major() 和minor()宏可能在分析这个设备ID域时有用。)

    st_rdev 字段描述这个文件(inode)本身代表的设备。

    st_size字段 给出文件字节尺寸(如果它是一个普通文件或符号链接)。对于符号链接而言是它所有包含路径名长度,不包括结尾的空字符。

    st_blocks 字段指明文件已经分配数据块的个数,数据块以 512 字节为单位。(这可能小于st_size/512,当文件有空洞时。)

    st_blksize 给出对于高效文件系统 I/O 操作的优先块尺寸。(数据以小尺寸块写入文件时可能导致低效的读-更改-覆盖 操作。)

    不是所有的 Linux 文件系统实现了所有的时间域。一些文件系统允许挂载时不去访问文件或目录且不会导致st_atime 字段更新。(参考 mount() 的noatimenodiratimerelatime,以及mount() 中相关的信息。)此外,如果文件以O_NOATIME 标志打开,st_atime 不会被更新。

    st_atime字段在文件访问时更改,比如,execve()、mknod()、pipe()、utime() 和read()(多于零个字节)。

    st_mtime在文件更改时更新,比如,mknod()、truncate()、utime() 和write()(多于零个字节)。再者,一个目录的st_mtime在这个目录里创建或删除文件时更新。st_mtime 在更改所有者、组、硬链接个数或权限模式时 不会更新。

    st_ctime 字段在写入或设置结点(inode)信息(如所有者、组、链接数、权限等等)时会更改。

    下面 POSIX 宏用于使用 st_mode 字段测试文件类型:

    S_ISREG(m)
    它是普通文件吗?
    S_ISDIR(m)
    目录吗?
    S_ISCHR(m)
    字符设备?
    S_ISBLK(m)
    块设备?
    S_ISFIFO(m)
    FIFO(命名管道)?
    S_ISLNK(m)
    符号链接?(不在 POSIX.1-1996。)
    S_ISSOCK(m)
    套接口?(不在 POSIX.1-1996。)

    下面标志为 st_mode 域定义:

    S_IFMT 0170000 文件类型位域掩码
    S_IFSOCK 0140000 套接口
    S_IFLNK 0120000 符号链接
    S_IFREG 0100000 普通文件
    S_IFBLK 0060000 块设备
    S_IFDIR 0040000 目录
    S_IFCHR 0020000 字符设备
    S_IFIFO 0010000 FIFO
    S_ISUID 0004000 设置 UID 位
    S_ISGID 0002000 设置 组ID 位 (看下面)
    S_ISVTX 0001000 粘滞位(看下面)
    S_IRWXU 00700 文件所有者权限掩码
    S_IRUSR 00400 所有者有读权限
    S_IWUSR 00200 所有者有写权限
    S_IXUSR 00100 所有者有执行权限
    S_IRWXG 00070 组权限掩码
    S_IRGRP 00040 组有读权限
    S_IWGRP 00020 组有写权限
    S_IXGRP 00010 组有执行权限
    S_IRWXO 00007 其他用户权限掩码(不在组内)
    S_IROTH 00004 其他有读权限
    S_IWOTH 00002 其他有写权限
    S_IXOTH 00001 其他有执行权限

    设置组ID位S_ISGID)有多个特殊用处。对于一个目录而言,它指明 BSD语义应用到这个目录;创建的文件继承其父目录的组ID,而不是来自创建进程的有效组ID,并且创建的目录同时也会获得S_SIGID位设置。对于一个没有组执行权限位(S_IXGRP)文件而言,设置组ID位用于指明托管文件/记录锁。

    在目录上的粘滞位(S_ISVTX)表示目录里的文件只有其所有者,目录所有者,和特权进程,可以对其进行重命名或删除。





    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;     //伟建内容对应的块数量

          };

    ----------------------------------------------------------------------------------------

    #include <unsitd.h>

    #inlcude <sys/stat.h>

    #include <sys/types.h>

    int fstat(int filedes,struct stat *buf);

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

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

    这三个系统调用都可以返回指定文件的状态信息,这些信息被写到结构struct stat的缓冲区中。通过分析这个结构可以获得指定文件的信息。

    void report(struct stat *ptr)

    {

         printf("The major device no is:%d ",major(ptr->st_dev));//主设备号

         printf("The minor device no is:%d ",minor(ptr->st_dev));//从设备号

         printf("The file's node number is:%d ",ptr->st_ino);//文件节点号

         printf("The file's access mode is:%d ",ptr->st_mode);//文件的访问模式

         printf("The file's hard link number is:%d ",ptr->st_nlink);//文件的硬链接数目

         printf("The file's user id is:%d ",ptr->uid);//文件拥有者的ID

         printf("The file's group id is:%d ",ptr->gid);//文件的组ID

         printf("The file's size is:%d ",ptr->st_size);//文件的大小

         printf("The block size is:%d ",ptr->blksize);//文件占用的块数量

         printf("The number of allocated blocks is:%d ",ptr->st_blocks);//文件分配块数量

         struct tm*accesstime,*lmodifytime,*lchangetime;//访问时间,修改时间,最后一个改变时间(属性)

         accesstime=localtime(&(ptr->st_atime));

         accesstime=localtime(&(ptr->st_mtime));

         accesstime=localtime(&(ptr->st_ctime));

         printf("The last access time is: %d::%d::%d ",accesstime->hour,accesstime->min,accesstime->sec);

         printf("The last modify time is:%d::%d::%d ",lmodifytime->hour,lmodifytime->min,lmodifytime->sec);

         printf("The last change time is:%d::%d::%d ",lchangetime->hour,lchangetime->min,lchangetime->sec);

    }

    结构time_t可用用localtime转换成tm结构,获得本地时间。
















  • 相关阅读:
    MapReduce的自定义结果文件名OutputFormat
    MapReduce的Mapper端JOIN
    服务器配置 隐藏apache和php的版本
    mysqldump参数详细说明
    PHP漏洞全解(PHP安全性/命令注入/脚本植入/xss跨站/SQL注入/伪跨站请求/Session劫持/HTTP响应拆分/文件上传漏洞)
    apache nginx 通过 rewrite 设置 禁止执行PHP程序
    javascript 数组的知识整理
    is_uploaded_file函数引发的问题
    php 读取文件头部两个字节 判断文件的实际类型
    discuz 数据字典大全
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744595.html
Copyright © 2011-2022 走看看