Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1855668
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2388
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-21 22:26
个人简介

90后空巢老码农

文章分类

全部博文(184)

文章存档

2021年(26)

2020年(56)

2019年(54)

2018年(47)

2017年(1)

我的朋友

分类: LINUX

2020-03-01 21:06:35

1. 获取文件信息:


点击(此处)折叠或打开

  1. #include <sys/stat.h>
  2. int stat(const char *pathname, struct stat *statbuf);
  3. int lstat(const char *pathname, struct stat *statbuf);//若为软链接,不follow
  4. int fstat(int fd, struct stat *statbuf);
  5. /*all return 0 on success, or -1 on error*/

  6. struct stat{
  7.     dev_t st_dev;// 文件所驻留的设备
  8.     ino_t st_ino;// 文件的i节点号
  9.     mode_t st_mode;// 标识文件类型和指定文件权限的双重作用
  10.     nlink_t st_nlink;// 硬链接数
  11.     uid_t st_uid;
  12.     gid_t st_gid;
  13.     dev_t st_rdev;// 如果是针对设备的i节点,该字段包含设备的主、辅ID
  14.     off_t st_size;// 文件的字节数
  15.     blksize_t st_blksize;// 针对文件系统上进行I/O操作时的最优块大小(以字节为单位)
  16.     blkcnt_t st_blocks;// 分配给文件的总块数, 块大小为512字节
  17.     time_t st_atime;
  18.     time_t st_mtime;// 上次修改时间
  19.     time_t st_ctime;// 文件状态发生改变的上次时间
  20. };
其中st_mode字段所包含各位的布局情况如下:


2. 文件时间戳

点击(此处)折叠或打开

  1. #include <utime.h>
  2. int utime(const char *pathname, const struct utimbuf *buf);
  3. /*returns 0 on success, or -1 on error*/
  4. struct utimbuf{
  5.     time_t actime;
  6.     time_t modtime;
  7. };
  8. #include <sys/time.h>
  9. int utimes(const char *pathname, const struct timeval tv[2]);
  10. int futimes(int fd, const struct timeval tv[2]);
  11. int lutimes(const char *pathname, const struct timeval tv[2]);
  12. /*both return 0 on success, or -1 on error*/
3. 文件属主
文件创建时,其用户id取自进程的有效用户id,而新建文件的组id则取自进程的有效组id(systemV),或父目录的组id(bsd)

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. int chown(const char *pathname, uid_t owner, gid_t group);
  3. #define _XOPEN_SOURCE 500
  4. #include <unistd.h>
  5. int lchown(const char *pathname, uid_t owner, gid_t group);
  6. int fchown(int fd, uid_t owner, gid_t group);
  7. /*all return 0 on success, or -1 on error0*/
如果文件的属主或属组发生了改变,那么set-user-ID和set-group-ID权限位也会随之关闭

4. 文件权限

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. int access(const char *pathname, int mode);
  3. /*returns 0 if all permissions are granted, otherwise -1*/
sticky权限位起到限制删除位的作用。为目录设置该位,则表明仅当非特权进程具有对目录的写权限,且为文件或目录的属主时,才能对目录下的文件进行删除和重命名操作

umask是一种进程属性,当进程新建文件或目录时,该属性用于指明哪些权限位。进程的umask通常继承自其父shell,用户可以使用shell的内置命令umask来改变shell进程的umask,从而控制在shell下运行程序的umask

点击(此处)折叠或打开

  1. #include <sys/stat.h>
  2. mode_t umask(mode_t mask);
  3. /*always successfully returns the previous process umask*/

  4. #include <sys/stat.h>
  5. int chmod(const char *pathname, mode_t mode);
  6. #define _XOPEN_SOURCE 500 /*or: #define _BSD_SOURCE*/
  7. #include <sys/stat.h>
  8. int fchmod(int fd, mode_t mode);
  9. /*both return 0 on success, or -1 on error*/













阅读(1695) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~