linux 文件操作 文件属性 - stat,lstat,fstat

news/2024/7/3 20:17:28

stat()/lstat()/fstat函数

功能

获取文件属性

定义函数

  • 函数原型:

      int stat(const char *pathname, struct stat *buf);
      int lstat(const char *pathname, struct stat *buf);
      int fstat(int filedes, struct stat *buf);
    
  • 函数返回值:
    成功为:0
    失败为:-1

 struct stat {
               dev_t     st_dev;     /* ID of device containing file */ 文件的设备编号
               ino_t     st_ino;     /* inode number */ 节点
               mode_t    st_mode;    /* protection */ 文件的类型和存取的权限
               nlink_t   st_nlink;   /* number of hard links */ 连到该文件的硬连接数目,刚建立的文件值为1
               uid_t     st_uid;     /* user ID of owner */ 用户ID
               gid_t     st_gid;     /* group ID of owner */ 组ID
               dev_t     st_rdev;    /* device ID (if special file) */ (设备类型)若此文件为设备文件,则为其设备编号
               off_t     st_size;    /* total size, in bytes */ 文件字节数(文件大小)
               blksize_t st_blksize; /* blocksize for filesystem I/O */ 块大小(文件系统的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 & S_IFMT 0170000 文件类型的位遮罩文件类型
S_IFSOCK 0140000套接字文件(s)
S_IFLNK 0120000链接文件(l)
S_IFREG 0100000一般文件(-)
S_IFBLK 0060000块设备文件(b)
S_IFDIR 0040000目录文件(d)
S_IFCHR 0020000字符驱动文件©
S_IFIFO 0010000管道文件§

文件访问权限:

st_mode && (1<<n)说明
S_ISUID 04000 11(bit位)文件的(set user-id on execution)位
S_ISGID 02000 10(bit位)文件的(set group-id on execution)位
S_ISVTX 01000 9(bit位)文件的sticky位
S_IRUSR(S_IREAD) 00400 8(bit位)文件所有者具可读取权限
S_IWUSR(S_IWRITE)00200 7(bit位)文件所有者具可写入权限
S_IXUSR(S_IEXEC) 00100 6(bit位)文件所有者具可执行权限
S_IRGRP 00040 5(bit位)用户组具可读取权限
S_IWGRP 00020 4(bit位)用户组具可写入权限
S_IXGRP 00010 3(bit位)用户组具可执行权限
S_IROTH 00004 2(bit位)其他用户具可读取权限
S_IWOTH 00002 1(bit位)其他用户具可写入权限
S_IXOTH 00001 0(bit位)其他用户具可执行权限

stat 和 lstat 差别

当遇到符号链接文件时:

  • stat : 只能对符号链接文件睁一只眼闭一只眼,直接去处理链接所指文件
  • lstat: stat返回的是该符号链接本身的信息

http://www.niftyadmin.cn/n/3293236.html

相关文章

Android异常:android.view.InflateException: Binary XML file line #14: Error inflating class

总体原因如下: 1. 引用类名问题&#xff1a;自定义了一个View&#xff0c;将他用于布局文件中&#xff0c;假设他的包名叫MyPackage&#xff0c;类名叫MyTestView&#xff0c;这个时候你在XML作为布局元素来布局的话&#xff0c;必须使用完整路径名&#xff0c;也就是包名加类名…

讲故事vs讲知识,区别太大

讲故事vs讲知识&#xff0c;区别很大 以故事化的方式讲知识&#xff0c;一般受众爱听 讲故事&#xff0c;通常受众爱听 趣讲大白话&#xff1a;但不是所有的故事都爱听 【趣讲信息科技219期】 #非著名IT人安志强的趣味笔记# **************************** 有一小部分故事&#…

根据输入的中文的搜索其拼音相似的内容

根据输入的中文的搜索其拼音相似的内容 根据你输入的汉字&#xff0c;先解析出他的拼音&#xff0c;然后你可以利用这些拼音去查找一些相似的汉字&#xff0c;感觉就像百度的搜索一样&#xff0c;呵呵&#xff0c;看来PHP函数功能确实很强大很强大&#xff0c;有待继续学习啊。…

给Protobuf中的repeated类型变量添加子项

Protobuf为repeated类型变量生成的自动代码&#xff0c;不提供通常的类似add_item(item)的添加子项的成员函数&#xff0c;Protobuf的做法是&#xff1a; UserDocChangesResp changes; DocChangeInfo change; DocChangeInfo* pChange changes.add_docchangeinfos(); *pChange …

vc6.0++插件

Visual C 插件系列介绍 说到Visual C的插件&#xff0c;大家可能只有想到Visual Assist吧。天真&#xff01;行内开发的插件可只有这个&#xff01;&#xff1f;下面介绍一下vc6.0的其他插件。 Visual Assist&#xff08;强烈推荐&#xff09; 网址&#xff1a;http://www.whol…

ProtoBuf 常用序列化/反序列化API

转自&#xff1a;http://blog.csdn.net/sealyao/article/details/6940245 1、C数组的序列化和反序列化API [cpp] view plaincopy //C数组的序列化和序列化API bool ParseFromArray(const void* data, int size); bool SerializeToArray(void* data, int size) const; //使…

多列索引生效规则

btree索引的常见误区 在where条件常用的列上都加上索引 比如&#xff1a;where cat_id3 and price>100 #查询第3个栏目&#xff0c;100以上的商品 只能用上cat_id或price索引&#xff0c;因为独立的索引同时只能用上1个。 多列索引生效规则 多列索引发挥作用&#xff0c;需要…

C++ 左值引用、右值引用和指针在汇编层面的差别

右值引用 // 右值引用int&& rret 1 2;00D01FDF mov dword ptr [ebp-18h],3 00D01FE6 lea eax,[ebp-18h] 00D01FE9 mov dword ptr [rret],eax rret 10;00D01FEC mov eax,dword ptr [rret] 00D01FEF mov dword ptr […