3.1.1 linux下的VFS虚拟文件系统 Linux采用VFS来管理系统,VFS全称是Virtual File System(虚拟文件系统)。VFS是不同类型文件之上的软件粘合层,因为VFS可以无缝使用多个不同类型的文件系统。 VFS的作用就是采用标准的UNIX系统调用来读写位于不同物理介质上的不同文件类型。 Linux内核采用inode结构体来保存与文件相关的信息,比如访问权限,文件大小和创建时间,这些信息被称为文件的元数据。inode数据结构和内容本身是分开存放的。VFS采用的是面向对象的编程方法,利用回调函数实现。 3.1.2 ext2文件系统结构 对于一个磁盘分区来说,在其被指定为相应的文件系统后,整个区被分为1024、2048、4096字节大小的块,可以分为: 超级快:包含整个文件系统的基本信息 inode块:文件系统索引 数据块:具体存放数据的位置区域
inode结构体
拥有者/拥有者组文件类型文件大小文件权限时间戳 创建时间 修改内容时间 修改属性时间硬链接个数额外标识指向数据块的指针3.1.3 目录文件及常规文件存储方法 目录文件和普通文件都是文件,只是目录文件的内容是其子文件或子文件夹的名称及inode值的对应,查找一个文件,首先根据路径找到文件所在目录,获取目录的内容,从中获取相应文件的inode信息。
3.2 linux系统下文件类型及属性 inode结构体 struct inode{ 。。。 unsigned long i_ino; //索引节点值 umode_t i_mode; //文件类型及访问权限 。。。 } 3.2.1 linux文件类型及权限 16位变量功能划分 0-8位为权限位,分别对应拥有者,同组其他用户,其他用户的R,W,X。 9-11对应权限修饰位,包括设置有效用户ID(setuid),设置有效用户组ID(setgid)和粘贴位(sticky)。 12-15文件类型位 3.2.2 linux文件类型 常规文件 - 目录文件 d 字符设备文件 c 块设备文件 b 符号链接文件 l 套接字文件 s 管道文件 p
测试某个打开的文件是何种类型 int isfdtype(fd, fdtype); 3.2.3 文件权限修饰位 9-11位为权限修饰位,文件权限包括setuid,setgid,sticky。setuid,setgid在守护进程管理中应用较多。 3.2.4 文件访问权限位 chmod修改文件权限 chmod 777 /etc/passwd
3.3 linux文件属性管理 3.3.1 读取文件属性 stat(_const char *_restrict _file, struct stat *_resetrict _buf); 若读取已打开的文件状态 extern int fstat(fd, struct stat *_buf); 若stat函数第一个参数为符号连接文件,其读取属性为源文件的属性,调用lstat lstat(_const char *_restrict _file, struct stat *_resetrict _buf);
3.3.2 修改文件权限操作 int chmod(_const char *_file, _mode_t _mode); 若为符号连接文件 int lchmod(_const char *_file, _mode_t _mode); 对于已经打开的文件 int fchmod(_const char *_file, _mode_t _mode);
3.3.3 修改系统的umask值 https://www.cnblogs.com/xingxiudong/p/3986888.html umask简介
umask查看值 umask 000 //修改当前屏蔽码
创建普通文件权限为0666-umask, 创建目录为0777-umask
_mode_t getmask(void); //获取系统umask值
3.3.4 修改文件的拥有者组 chown修改拥有者 chgrp修改拥有者组 root@lyw:/home/vicli/2_linux_program/test# touch test.txt root@lyw:/home/vicli/2_linux_program/test# ls -l total 0 -rw-r–r-- 1 root root 0 7月 3 10:17 test.txt root@lyw:/home/vicli/2_linux_program/test# chown guess test.txt chown: invalid user: ‘guess’
int chown(file, _owner, _group); int fchown(file, _owner, _group);//已打开 int lchown(file, _owner, _group);//连接文件
3.3.5 用户名/组名与UID/GID转换
getpwuid(_uid_t _uid); getpwnam(_const char *_name);
示例程序 root@lyw:/home/vicli/2_linux_program# cat userid_exp.c #include “stdio.h” #include “stdlib.h” #include “stdlib.h” #include <sys/types.h> #include <pwd.h>
int main(int argc, char *argv[]) { struct passwd *ptr; uid_t uid; uid = atoi(argv[1]); ptr = getpwuid(uid); printf(“name:%s\n”, ptr->pw_name); printf(“passwd:%s\n”, ptr->pw_passwd); printf(“home_dir:%s\n”, ptr->pw_dir); return 0; } root@lyw:/home/vicli/2_linux_program# ./userid_exp vicli name:root passwd:x home_dir:/root root@lyw:/home/vicli/2_linux_program#
3.3.6 创建/删除硬连接 int link(_const char *_form, _const char *_to);
int unlink(_const char *_name);
3.3.8 文件时间属性修改与时间处理