1.Inode
首先我们再复习下关于Inode的概念,
在文件存储中,Inode的本质为结构体,存储着文件的属性信息,如权限,类型,大小,事件,用户,
盘块位置...也被称作文件属性管理结构,大多数的inode都存储在磁盘上,少量常用的近期使用的
inode会被缓存到内存中.
inode是index node(索引节点)的缩写.
2.文件的类型
文件的分类
编码 文件类型
0 Unknown(不识别的)
1 Regular file(普通)
2 Directory(目录)
3 Character device(字符设备)
4 Block Device(块设备)
5 Named Pipe(管道)
6 Socket(套接字)
7 Symbolic link(符号链接)
3.stat
既有命令也有函数:
man 1 stat
man 2 stat
本函数的主要用途是获取文件的属性.
函数:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *buf);//通过文件名来获取文件的属性,跟踪符号链接
int fstat(int fd, struct stat *buf);// 通过文件描述符来获取文件的属性
int lstat(const char *pathname, struct stat *buf);//通过文件名来获取文件的属性,不跟踪符号链接
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
int fstatat(int dirfd, const char *pathname, struct stat *buf,
int flags);
The stat structure
All of these system calls return a stat structure, which contains the following fields:
struct stat {
dev_t st_dev; /* ID of device containing file */文件所在磁盘驱动器号,包含当前文件的设备编号
ino_t st_ino; /* inode number */Inode编号
mode_t st_mode; /* file type and mode */权限
nlink_t st_nlink; /* number of hard links */硬链接数
uid_t st_uid; /* user ID of owner */所有者
gid_t st_gid; /* group ID of owner */所有组
dev_t st_rdev; /* device ID (if special file) */是否特殊文件,如果是的话,这里的st_rdev就是设备号
off_t st_size; /* total size, in bytes */总长度
blksize_t st_blksize; /* blocksize for filesystem I/O */文件IO的大小,一般可设置成4096bytes
blkcnt_t st_blocks; /* number of 512B blocks allocated */当文件IO大小被设置成4096bytes的时候,是8
/* 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
};
The following mask values are defined for the file type of the st_mode field:
S_IFMT 0170000 bit mask for the file type bit field
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
Thus, to test for a regular file (for example), one could write:
stat(pathname, &sb);
if ((sb.st_mode & S_IFMT) == S_IFREG) {
/* Handle regular file */
}
The following mask values are defined for the file mode component of the st_mode field:
S_ISUID 04000 set-user-ID bit
S_ISGID 02000 set-group-ID bit (see below)
S_ISVTX 01000 sticky bit (see below)
S_IRWXU 00700 owner has read, write, and execute permission
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 group has read, write, and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others (not in group) have read, write, and execute
permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
符号链接,比如本来有一个文件hello,
现在
ln -s hello abc
则abc是文件hello的符号链接,相当于abc是hello的快捷方式,abc的长度是hello这个文件的文件名长度.
stat跟踪符号链接
lstat不跟踪符号链接
测试小案例:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
int main(char argc,char *argv[])
{
struct stat s_buf;
if(argc < 2)
{
printf("输入文件名\n");
exit(1);
}
if(stat(argv[1],&s_buf)<0)
{
perror("stat");
exit(1);
};
printf("stat跟踪符号连接%s \t %ld \n",argv[1],s_buf.st_size);
if(lstat(argv[1],&s_buf)<0)
{
perror("stat");
exit(1);
};
printf("lstat不跟踪符号连接%s \t %ld \n",argv[1],s_buf.st_size);
return 0;
}
4.ls命令与stat
尝试
ls -i filename
5. access
man 1 access
man 2 access
#include <unistd.h>
int access(const char *pathname, int mode);
RETURN VALUE
On success (all requested permissions granted, or mode is F_OK and the file exists), zero is returned. On error (at least one bit in mode asked for a permission that is denied, or mode is F_OK and
the file does not exist, or some other error occurred), -1 is returned, and errno is set appropriately.
按照实际用户ID和实际组ID测试,跟踪符号链接.
R_OK 是否有读权限
W_OK 是否有写权限
X_OK 是否有执行权限
F_OK 测试一个文件是否存在
open也可以有相似的功能,但是open的开销较大.
实际用户ID和有效用户ID,比如用户muten在/root目录下执行
sudo touch xxx
则这里实际用户用户ID是muten,有效用户ID是root.