unix-4

stat、fstat、lstat函数

#include

#include
int stat(const char pathname,struct state buf);
int fstat(int filedes,struct stat
buf);
int lstat(const char pathname ,struct stat *buf);
给定一个pathname,
stat函数返回一个与此命令文件有关的信息结构;
fstat函数获得已在描述符filedes上打开的文件的有关信息。
lstat类似于stat,但是当命令的文件是一个符号连接时,lstat返回符号连接的有关信息,而不是由符号连接引用的文件信息。

文件类型

* 普遍文件(regular file)
* 目录文件(deirctory file)
* 字符特殊文件(character special file)
* 块特殊文件(block special file)
* FIFO
* 套接口(socket)
* 符号连接(symbolic link)

文件类型信息包含在stat结构的st_mode成员中。

设置-用户-ID和设置-组-ID

文件存取许可

内核测试文件的时候过程

access函数

在open打开一个文件时,内核以进程的有效用户ID和有效组ID为基础执行其存取许可测试。
access函数按实际用户ID和实际组ID进行存取许可测试。

umask函数

umask函数为进程设置文件方式方式创建屏蔽字,并返回以前的值。

chmod和fchmod函数

chmod函数在指定文件上进行操作。
fchmod函数则对已打开的文件进行操作。
chmod函数更新的只是i节点最近一次被更改的时间,ls -l列出的是最后修改文件内容的时间
chmod函数在下列条件下将自动清除两个许可权位:
1.如果我们试图设置普通文件的粘住位(S_ISVTX),而且有没有超级用户的优先权,那么mode中的粘住位将自动被关闭;
2.新创建文件的组ID可能不是调用进程所属的组。

粘住位

chown fchown 和fchown函数

可用于更改文件的用户ID和组ID
lchown更改符号连接本身的所有者,而不是该符号连接所指向的文件。
wc -c 文件名
返回文件的长度

文件截短

truncate(const char pathname,off_t length);
ftruncate(int filedes,offt_t length)
link : 创建一个向现存文件连接的方法;
int link(const char pathname,const char new pathname);
unlink:删除一个现存的目录项
int unlink(const char pathname);
remove:解除对一个文件或目录的连接。
rename:文件或目录用人那么更名。
int rename(const char pathname,const char newname);

符号连接

文件的时间

utime函数:文件的存取和修改时间
int utime(const char pathname ,const struct utimebuf);
struct utimbuf{
time_t acime;
time_t modtime;
}

mkdir函数 创建目录, . 和..目录是自动创建的。所指定的文件存取许可权mode由进程的文件方式创建屏蔽字修改。
rmdir函数 删除目录;
int mkdir(const char pathname,mode_t mode);

读目录

对某个目录具有存取许可权的任一用户都可以读该目录 ,但是只有内核才能写目录。

DIR opendir(const char pathname);
struct dirent
readdir(DIR);
void rewinddir(DIR dp);
int closedir(DIR dp
);

struct dirent{
ino_t d_ino;
char d_name[NAME_MAX+1];
}

###chdir,fchdir和getcwd函数
每个进程都有一个当前工作目录,此目录是搜索所有相对路径名的起点。

st_dev st_rdev

每个文件系统都由其主、次设备号;

文章目录
  1. 1. stat、fstat、lstat函数
  2. 2. 文件类型
  3. 3. 设置-用户-ID和设置-组-ID
  4. 4. 文件存取许可
  5. 5. 内核测试文件的时候过程
  6. 6. access函数
  7. 7. umask函数
  8. 8. chmod和fchmod函数
  9. 9. 粘住位
  10. 10. chown fchown 和fchown函数
  11. 11. 文件截短
  12. 12. 符号连接
  13. 13. 文件的时间
  14. 14. 读目录
  15. 15. st_dev st_rdev
,