大佬教程收集整理的这篇文章主要介绍了Linux stat函数和stat命令,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
linux文件里的【inode = index node】解释:要理解inode必须了解磁盘和【目录项】,inode实际是连接【目录项】和磁盘的中间物质。@H_404_9@
如何查看文件的【inode】呢?使用【-i】选项@H_404_9@
ls -li 文件名
执行结果:@H_404_9@
[email protected]:~/lianxi1$ ls -li hello hello.hard 3801352 -rw-rw-r-- 2 ys ys 0 4月 24 11:01 hello 3801352 -rw-rw-r-- 2 ys ys 0 4月 24 11:01 hello.hard
发现hello和hello.hard的inode(3801352)是相同的,也就说明了,只在磁盘上存了一份。@H_404_9@
如何查看目录项呢?用emacs或者vim打开目录(lianxi1),截图如下。但是看不到文件的【inode】。@H_404_9@
1,stat函数:取得指定文件的文件属性,文件属性存储在结构体stat里。@H_404_9@
#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);
struct stat 结构体:@H_404_9@
struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* Inode number */ 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) */ off_t st_size; /* Total size,in bytes */ blksize_t st_blksize; /* Block size for filesystem I/O */ blkcnt_t st_blocks; /* Number of 512B blocks allocated */ /* 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 };
st_dev:设备ID,不太常用@H_404_9@
st_ino:【inode】,【inode】是啥?不知道就看上面关于【inode】的解释@H_404_9@
st_mode:文件的类型和权限,共16位,如下图。@H_404_9@
9-11比特位:特殊权限@H_404_9@
另一种文件类型的宏: S_ISREG(m) is it a regular file? S_ISDIR(m) directory? S_ISCHR(m) character device? 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_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,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,and execute permission S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission
st_nlink:硬连接计数@H_404_9@
st_rdev:特殊设备的ID,不太常用@H_404_9@
st_blksize:不明是干啥的@H_404_9@
st_blocks:不明是干啥的@H_404_9@
struct timespec st_atim:最后访问的时间@H_404_9@
struct timespec st_ctim:最后状态改变的时间@H_404_9@
struct timespec { __kernel_time_t tv_sec; /* seconds */当前时间到1970.1.1 00:00:00的秒数 long tv_nsec; /* nanoseconds *//纳秒数(不知道从哪到哪的) }; 1s 秒 = 1000ms 毫秒 1ms 毫秒 = 1000us 微秒 1us 微秒 = 1000ns 纳秒
返回值:0代表成功;-1代表失败,并设置error@H_404_9@
例子:statbuf是结构体stat,可以看出来st_mode是个10进制的数字。@H_404_9@
@H_404_9@@H_404_9@st_mode@H_404_9@
用gdb显示st_mode,发现返回的st_mode是个10进制的数字,用gdb的【p/o】(o代表用8进制表示)命令把10进制的33204转换成了8进制的【0100664】,第一个0代笔是8进制,后三位的【100】代表文件类型,从上面的说明可以看出来【100】代表普通文件,最后三位的【664】代表这个文件的权限(本用户:rw-,组用户:rw-,其他用户:r--)。所以从st_mode里就可以得知文件的类型和权限设置(只使用了16个比特位,真的好节省空间,牛逼!)@H_404_9@
st_uid@H_404_9@
st_gid@H_404_9@
发现st_uid和st_gid是1000,但这个1000怎么和用户对应上呢,查看/etc/passwd文件,发现用于ys的uid和gid都是1000,所以就对应上了。@H_404_9@
stat命令,是stat函数对应,执行结果如下:@H_404_9@
[email protected]:~/lianxi1$ stat hello File: hello Size: 11 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 3801352 Links: 2 Access: (0764/-rwxrw-r--) Uid: ( 1000/ ys) Gid: ( 1000/ ys) Access: 2019-04-24 17:02:39.199461489 +0800 Modify: 2019-04-24 16:54:16.407461489 +0800 Change: 2019-04-24 17:03:44.927461489 +0800
以上是大佬教程为你收集整理的Linux stat函数和stat命令全部内容,希望文章能够帮你解决Linux stat函数和stat命令所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。