linux用stat函数获取文件各项信息

用stat命令获取文件的各项信息

vijay@vijay-virtual-machine:~/project/io$ stat test.txt 
  File: test.txt
  Size: 9         	Blocks: 8          IO Block: 4096   regular file
Device: 804h/2052d	Inode: 786481      Links: 1
Access: (0600/-rw-------)  Uid: ( 1000/   vijay)   Gid: ( 1000/   vijay)
Access: 2020-02-08 10:46:04.513785805 +0800//文件最近访问时间
Modify: 2020-02-08 10:45:56.905572936 +0800//文件类容最近改变时间
Change: 2020-02-08 10:45:56.905572936 +0800//文件属性最近改变时间
 Birth: -//文件创建时间


stat函数讲解:


int stat(const char *pathname, struct stat *statbuf);

struct stat结构体:

           struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               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;     /* Block size for filesystem I/O */
               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */

               struct timespec st_atim;  /* Time of last access */
               struct timespec st_mtim;  /* Time of last modification */
               struct timespec st_ctim;  /* Time of last status change */

           #define st_atime st_atim.tv_sec      /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };

结构体中st_mode参数中可以获取文件的类型,特殊权限位,所属者、所属组、其他人的权限信息

st_mode是一个16位数,高四位包含的是文件类型信息

       The following mask values are defined for the file type:

           S_IFMT     0170000   bit mask for the file type bit 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

       Thus, to test for a regular file (for example), one could write:

           stat(pathname, &sb);
           if ((sb.st_mode & S_IFMT) == S_IFREG) {
               /* Handle regular file */
           }

要知道文件时什么类型:st_mode&S_IFMT==S_IFREG为真就是普通文件
另外还有一种方法

           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.)

       The preceding code snippet could thus be rewritten as:

           stat(pathname, &sb);
           if (S_ISREG(sb.st_mode)) {
               /* Handle regular file */
           }


使用上面这些函数可以判断出文件类型;

低八位是所属者、所属组、其他人的权限位

       
           S_IRWXU     00700   owner has read, write, and execute permission
           S_IRUSR     00400   owner has read permission
           S_IWUSR     00200   owner has write permission
           S_IXUSR     00100   owner has execute permission

           S_IRWXG     00070   group has read, write, and execute permission
           S_IRGRP     00040   group has read permission
           S_IWGRP     00020   group has write permission
           S_IXGRP     00010   group has execute permission

           S_IRWXO     00007   others  (not  in group) have read, write, and
                               execute permission

           S_IROTH     00004   others have read permission
           S_IWOTH     00002   others have write permission
           S_IXOTH     00001   others have execute permission


比如,要判断所属者是否有读权限 st.st_mode& S_IRUSR ,为真就具有读权限;

另外三位是特殊权限位
分别为SUID、SGID、Sticky;
https://wenku.baidu.com/view/cefb6506b52acfc789ebc946.html

获取文件类型和权限的例子

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>

int main(int argc,char * argv[])
{
        struct stat st;
        stat(argv[1],&st);
        if(S_ISREG(st.st_mode))
        {
                printf("-");
        }
        if((st.st_mode&S_IFMT)==S_IFDIR)
        {
                printf("d");
        }

        //user权限位的判断
        if(st.st_mode&S_IRUSR)
        {
                printf("r");
        }else{
                printf("-");
        }

        if(st.st_mode&S_IWUSR)
        {
                printf("w");
        }else{
                printf("-");
        }

        if(st.st_mode&S_IXUSR)
        {
                printf("x");
        }else{
                printf("-");
        }



        //group权限位的判断
        if(st.st_mode&S_IRGRP)
        {
                printf("r");
        }else{
                printf("-");
        }


        if(st.st_mode&S_IWGRP)
        {
                printf("W");
        }else{
                printf("-");
        }


        if(st.st_mode&S_IXGRP)
        {
                printf("x");
        }else{
                printf("-");
        }


        //other权限位的判断
        if(st.st_mode&S_IROTH)
        {
                printf("r");
        }else{
                printf("-");
        }


        if(st.st_mode&S_IWOTH)
        {
                printf("W");
        }else{
                printf("-");
        }



        if(st.st_mode&S_IXOTH)
        {
                printf("x");
        }else{
                printf("-");
        }



        return 0;
}
                                                                                                             

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值