随笔小屋 Logo
首页
瞬间
反馈
随笔小屋 Logo
首页 瞬间 反馈
  1. 首页
  2. linux
  3. 元数据

元数据

  • linux
  • 发布于 2025-10-01
  • 86 次阅读
flor
flor
#include <sys/stat.h>
int stat(char const* path, struct stat* buf);
int fstat(int fd, struct stat* buf);
int lstat(char const* path, struct stat* buf); //与前两个函数区别,不跟踪符号链接
功能:从i节点中获取元数据
参数:path  文件路径
buf   文件元数据结构
fd    文件描述符
返回值:成功返回0,失败-1

struct stat{
  dev_t  st_dev;            设备ID
  ino_t  st_ino;            i节点
  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;      存储块数
  time_t  st_atime;         最后访问时间
  time_t  st_mtime;         最后修改时间
  time_t  st_ctime;         最后状态改变时间
};

stat.st_mode其中低16位有意义(B15-B0)
B15-B12  文件类型:  1000(S_IFREG 普通文件) 0100(S_IFDIR 目录) 1100(S_IFSOCK 本地套接字) 0010(S_IFCHR 字符设备) 0110(S_IFBLK 块设备) 1010(S_IFLNK 符号链接) 0001(S_IFIFO 有名管道) 1111(S_IFMT 掩码)
B11-B9   设置用户ID(进程中有实际用户ID(当前登录用户)和有效用户ID(一般等于实际用户,用于权限验证,当B11置1时有效用户ID改成可执行文件拥有者(如root用户))),设置组ID,粘滞
B8-B6    拥有者用户,读,写,执行权限
B5-B3    拥有者组,读,写,执行权限
B2-B0    其他用户,读,写,执行权限
辅助分析文件类型的实用宏:
S_ISREG()  普通文件
S_ISDIR()  目录
S_ISSOCK() 本地套节字
S_ISCHR()  字符设备
S_ISBLK()  块设备
S_ISLNK()  符号链接
S_ISFIFO() 有名管道

man 2 stat

湘ICP备2025147565号-1
gongan beian 湘公网安备43102602000213号
CPU --% | 内存 0.00G/0.00G (0%) | 网络 无活动网卡
服务器资源占用 更新时间 --:--:--