linux⽂件系统中i节点和stat函数命令
⼀、概述
unix系统中,每打开⼀个⽂件,内核会使⽤三种数据结构来表⽰这个⽂件:进程表、v节点和i节点。
linux只保留了unix中的i节点来维护打开的⽂件;
⼆、i节点和struct stat结构体
linux的i节点信息存储在struct stat结构体中,可以通过man 2 stat来查看这个结构体的内容,如下:
struct stat {
dev_t    st_dev;    /* ID of device containing file */
ino_t    st_ino;    /* inode number */
mode_t    st_mode;    /* protection */
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) */
off_t    st_size;    /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
time_t    st_atime;  /* time of last access */
json格式怎么打开图片time_t    st_mtime;  /* time of last modification */
time_t    st_ctime;  /* time of last status change */
};
st_dev 表⽰该⽂件所存在设备的id;
st_ino i节点编号,每⼀个⽂件都有唯⼀的i节点编号;
st_mode ⽂件的权限,如0777表⽰所有的⽤户具有可读可写和可执⾏权限,这个位还指明了⽂件的类型,可以和以下宏组合使⽤:
S_ISREG(m)  is it a regular file?
详细的dw网页制作教程S_ISDIR(m)  directory?
S_ISCHR(m)  character device?mysql企业版价格
S_ISBLK(m)  block device?
微服务架构教程S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m)  symbolic link?  (Not in POSIX.1-1996.)
S_ISSOCK(m) socket?  (Not in POSIX.1-1996.)
S_IFMT    0170000  bit mask for the file type bit fields
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
S_ISUID    0004000  set-user-ID bit
S_ISGID    0002000  set-group-ID bit (see below)
S_ISVTX    0001000  sticky bit (see below)
S_IRWXU    00700    mask for file owner permissions
S_IRUSR    00400    owner has read permission
S_IWUSR    00200    owner has write permission
S_IXUSR    00100    owner has execute permission
S_IRWXG    00070    mask for group permissions
S_IRGRP    00040    group has read permission
S_IWGRP    00020    group has write permission
S_IXGRP    00010    group has execute permission
S_IRWXO    00007    mask for permissions for others (not in group)
S_IROTH    00004    others have read permission
S_IWOTH    00002    others have write permission
S_IXOTH    00001    others have execute permission
st_nlink 硬链接的个数;
st_uid 拥有者组id;
st_gid 拥有者组id;
st_rdev 设备id;
st_size 设备的⼤⼩,以字节为单位,如果⽂件时⼀个符号链接,那么这个值的⼤⼩是它路径名的长度;
st_blksize
st_blocks
st_atime 最后访问时间
st_mtime 最后修改时间
st_ctime 上⼀次访问的时间
最后的三个时间可以使⽤localtime函数转换成标准的北京时间:
#include <time.h>
struct tm *localtime(const time_t *timep);
struct tm {
int tm_sec;    /* Seconds (0-60) */
int tm_min;    /* Minutes (0-59) */
int tm_hour;  /* Hours (0-23) */
int tm_mday;  /* Day of the month (1-31) */
int tm_mon;    /* Month (0-11) */
int tm_year;  /* Year - 1900 */
int tm_wday;  /* Day of the week (0-6, Sunday = 0) */
int tm_yday;  /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst;  /* Daylight saving time */
};
三、stat函数和stat命令
3.1、stat函数
使⽤stat函数可以得到对应⽂件的struct stat结构体,函数原型如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
这三个函数的区别在于,stat⽤来获取指定路径的⽂件信息,lstat函数和stat的作⽤完全相同,但是当l
stat函数的pathname是⼀个符号链接时,lstat得到的信息是这个符号链接本⾝的信息,⽽不是这个符号链接指向的⽂件信息。fstat可以根据已经打开⽂件的描述符获取⽂件信息。
1. ⾸先我们在当前⽬录下新建⼀个text⽂件,并在⽂件中随便写⼊⼀些内容::
touch text
vim text
2.使⽤stat函数读取text⽂件的内容
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
void check_file_type(mode_t mode)
{
printf("File Type:");
switch(mode & S_IFMT)
{
case S_IFSOCK:
printf("socket\r\n");
break;
case S_IFLNK:
printf("symbolic link\r\n");
break;
case S_IFREG:
printf("regular file\r\n");
break;
case S_IFBLK:
printf("block file\r\n");
break;
case S_IFDIR:
printf("directory\r\n");
break;
break;
case S_IFCHR:
printf("character device\r\n");
break;
case S_IFIFO:
printf("FIFO\r\n");
break;
default    :
printf("unknown file type\r\n");
break;
}
}
void check_file_permission(mode_t mode,char * perm) {
perm[0]='?';//先获取类型
switch(mode & S_IFMT){
case S_IFSOCK:
perm[0]='s';
break;
case S_IFLNK:
perm[0]='l';
break;linux建立文件系统的命令
case S_IFREG:
perm[0]='-';
break;
case S_IFBLK:
perm[0]='b';
break;
case S_IFDIR:
perm[0]='d';
break;
case S_IFCHR:
perm[0]='c';
break;
case S_IFIFO:
perm[0]='p';
break;
}
if(mode & S_IRUSR)
perm[1]='r';
if(mode & S_IWUSR)
perm[2]='w';
if(mode & S_IXUSR)
perm[3]='x';
if(mode & S_IRGRP)
perm[4]='r';
if(mode & S_IWGRP)
perm[5]='w';
if(mode & S_IXGRP)
perm[6]='x';
if(mode & S_IROTH)
perm[7]='r';
if(mode & S_IWOTH)
perm[8]='w';
if(mode & S_IXOTH)
perm[9]='x';
spring下载api
perm[10]='\0';
}
int main()
{
int ret = -1;
struct tm *tim;
char pem[] = "----------";
struct stat stabuf;
struct stat stabuf;
char pathname[] = "text";
stat(pathname,&stabuf);
printf("Inode Number:%d\r\n",stabuf.st_ino);
printf("Hard Links:%d\r\n",stabuf.st_nlink);
check_file_type(stabuf.st_mode);
check_file_permission(stabuf.st_mode,pem);
printf("File Permission:%s\r\n",pem);
printf("User ID:%u\r\n",stabuf.st_uid);
printf("Group ID:%u\r\n",stabuf.st_gid);
printf("Device ID:%u\r\n",stabuf.st_rdev);
printf("File Size:%d\r\n",stabuf.st_size);
printf("Block Size:%d\r\n",stabuf.st_blksize);
printf("Block Nums:%d\r\n",stabuf.st_blocks);
tim = localtime(&stabuf.st_atime);
printf("Acess Time:%04d-%02d-%02d %02d:%02d:%02d %02d\r\n",tim->tm_year+1900,tim->tm_mon+1,tim->tm_mday,tim->tm_hour,tim->tm_min,tim->tm _sec,tim->tm_isdst);
tim = localtime(&stabuf.st_mtime);
printf("Modify Time:%04d-%02d-%02d %02d:%02d:%02d %02d\r\n",tim->tm_year+1900,tim->tm_mon+1,tim->tm_mday,tim->tm_hour,tim->tm_min,tim->t m_sec,tim->tm_isdst);
tim = localtime(&stabuf.st_atime);
printf("Change Time:%04d-%02d-%02d %02d:%02d:%02d %02d\r\n",tim->tm_year+1900,tim->tm_mon+1,tim->tm_mday,tim->tm_hour,tim->tm_min,tim->t m_sec,tim->tm_isdst);
}
3.编译上⾯的代码并执⾏后,紧接着使⽤stat命令查看⽂件的信息,得到如下结果,可以看出使⽤stat函数和stat命令得到的⽂件信息是⼀致的。
4.给text⽂件建⽴⼀个硬链接,在shell中执⾏ln text tex,然后查看⽂件信息,可以看到⽂件的硬链接变为了2。
ln text tex
图中⽂件的i节点编号和前⾯的不⼀致是因为我把text⽂件从共享⽂件夹中拷贝到了/home⽬录,使⽤vmtool共享⽂件夹是⽆法创建硬链接的,会提⽰如下错误:
ln: failed to create hard link ‘tex’ => ‘text’: Operation not permitted
3.2、stat命令
stat命令在shell中使⽤,它具有和stat函数⼤致相同的功能,但⽐stat函数的使⽤更加灵活和多样化,在shll终端中使⽤man stat可以查看⽐较详细的介绍。
stat命令我们通常不需要添加任何参数就可以查看⽂件的详细信息,在shell中执⾏如下指令:
stat text
其它选项
-f 查看⽂件系统信息⽽⾮⽂件信息;
-c 查看指定格式的⽂件信息,如stat -c %i text表⽰只查看⽂件的i节点信息;
-t 将⽂件信息以简略形式输出到shell终端

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。