版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39956356/article/details/86585450
目录
- 基本知识
- 结构体stat
- stat lstat fstat应用实例–有两个实例,一个man文档的,一个itop4412实验
以下的内容主要参照man文档及一部分博客,在文章末尾附上链接。
(一)基本知识
stat, fstat, lstat – get file status(获取文件状态属性),包括一些特殊文件如:管道,socket,字符,块等。
//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
//三个相关函数
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
(二)获取文件属性–stat结构体
struct stat {
dev_t st_dev; /* ID of device containing file */--文件所在设备ID
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 */--所有者ID
gid_t st_gid; /* group ID of owner */--组ID
dev_t st_rdev; /* device ID (if special file) */--特殊文件ID
blksize_t st_blksize; /* blocksize for file system I/O */--系统文件i/o的块大小
off_t st_size; /* total size, in bytes */--大小,单位字节
blkcnt_t st_blocks; /* number of 512B blocks allocated */--分配的512b块数
time_t st_atime; /* time of last access */--上次访问
time_t st_mtime; /* time of last modification */--最近一次修改
time_t st_ctime; /* time of last status change */--最近一次的状态变化
};
- st_dev:描述该文件所在的设备,由两部分组成(主设备号major(3),从设备号minor(3))
主设备号: 标识设备类;
从设备号:标识特定实例
int major(dev_t dev);
int minor(dev_t dev);
- st_ino:节点号,是唯一标识文件的,如果你发现有两个文件节点号一样,那是硬连接哦!
- st_mode:为了更好地确定文件的类型,定义了以下的宏(macros)
The following POSIX macros are defined to check the file type using the st_mode field:
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.) //符号链接?(不在 POSIX.1-1996。)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.) //套接口?(不在 POSIX.1-1996。)
下面标志为 st_mode 域定义:
- st_nlink: 硬链接个数
- st_rdev: 特殊文件ID
- st_size: size of the file
- st_blocks: 分配给文件的块数, 512 字节单位
- st_blksize: gives the “preferred” blocksize for efficient file system I/O,对系统而言最优的块大小。
- 对于三种时间自己列了一张表,方便对比学习
- stat lstat fstat应用实例
这里提一点,man文档上不是每个都有实例的。只有那些重要,不容易弄懂的,才会写上实例供所有人参考。有很重要的参考价值!!!
实例二—man手册
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h> //时间的头文件
#include <stdio.h>
#include <stdlib.h> //没有的话,exit要报警告
#include <sys/sysmacros.h>
/****************************************************************************************
** get file status(获取文件状态)
** stat():文件路径
** int stat(const char *path, struct stat *buf);
** fstat():文件句柄
** int fstat(int fd, struct stat *buf);
** lstat():文件路径
** int lstat(const char *path, struct stat *buf);
** RETURN VALUE
** On success, zero is returned. On error, -1 is returned
****************************************************************************************/
int main(int argc, char *argv[])
{
struct stat sb; //定义一个结构体变量
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]); //stderr--标准错误输出设备, stdout输出到磁盘文件,stderr在屏幕哦
exit(EXIT_FAILURE);
}
if (lstat(argv[1], &sb) == -1) { //第一个参数:path
perror("lstat");
exit(EXIT_FAILURE);
}
printf("ID of containing device: [%lx,%lx]\n", //主设备,从设备
(long) major(sb.st_dev), (long) minor(sb.st_dev));
printf("File type: "); //文件类型
switch (sb.st_mode & S_IFMT) { //S_IFMT:文件类型位域掩码,与
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("I-node number: %ld\n", (long) sb.st_ino); //节点号
printf("Mode: %lo (octal)\n", //八进制(向左靠齐)
(unsigned long) sb.st_mode);
printf("Link count: %ld\n", (long) sb.st_nlink); //链接个数
printf("Ownership: UID=%ld GID=%ld\n", //UID,GID
(long) sb.st_uid, (long) sb.st_gid);
printf("Preferred I/O block size: %ld bytes\n", //gives the "preferred" blocksize for efficient file system I/O
(long) sb.st_blksize);
printf("File size: %lld bytes\n", //文件大小,单位字节
(long long) sb.st_size);
printf("Blocks allocated: %lld\n", //占用块数
(long long) sb.st_blocks);
printf("Last status change: %s", ctime(&sb.st_ctime)); //三个时间,文件访问时更改
printf("Last file access: %s", ctime(&sb.st_atime)); //文件更改时更新
printf("Last file modification: %s", ctime(&sb.st_mtime));
exit(EXIT_SUCCESS);
}
输出:很清晰了解所有参数的含义,这里提一点:文件大小与所占块数不是线性的。
实例二
#include <stdio.h>
//stat头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h> //open头文件
//stat结构体
//struct stat {
// dev_t st_dev; /* ID of device containing file */--设备id
// 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 */--用户ID
// gid_t st_gid; /* group ID of owner */--组ID
// dev_t st_rdev; /* device ID (if special file) */--特殊文件ID
// blksize_t st_blksize; /* blocksize for file system I/O */--系统文件i/o的块大小
// off_t st_size; /* total size, in bytes */--大小,单位字节
// blkcnt_t st_blocks; /* number of 512B blocks allocated */--分配的512b块数
// time_t st_atime; /* time of last access */--上次访问
// time_t st_mtime; /* time of last modification */--最近一次修改
// time_t st_ctime; /* time of last status change */--最近一次的状态变化
//};
/****************************************************************************************
** get file status(获取文件状态)
** stat():文件路径
** int stat(const char *path, struct stat *buf);
** fstat():文件句柄
** int fstat(int fd, struct stat *buf);
** lstat():文件路径
** int lstat(const char *path, struct stat *buf);
** RETURN VALUE
** On success, zero is returned. On error, -1 is returned
****************************************************************************************/
int main(int argc, char *argv[])
{
int fd, ret;
struct stat groupstat;
if(argc<2) //至少需要传入两个参数,第一个程序名字,第二个要查看文件状态的文件路径
{
printf("Please enter two arguments!!!\n");
return 1;
}
//stat
ret = stat(argv[1], &groupstat); //返回indo,ji
if(ret)
{
printf("List-stat groupstat error!!!\n");
return 1;
}
printf("The st_ino of %s id %d.\n",argv[1], groupstat.st_ino);
//fstat
if((fd = open(argv[1],O_RDWR|O_NDELAY|O_NOCTTY))<0)
{
printf("Open %s failed!!!\n",argv[1]);
return 1;
}
ret = fstat(fd, &groupstat);
if(ret)
{
printf("List--fstat groupstat error!!!\n");
return 1;
}
printf("The st_ino of %s id %d.\n",argv[1], groupstat.st_ino);
//lstat
ret = lstat(argv[1], &groupstat);
if(ret)
{
printf("List--lstat groupstat error!!!\n");
return 1;
}
printf("The st_ino of %s id %d.\n",argv[1], groupstat.st_ino);
return 0;
}
输出:三个函数输出一样,ok.
参考博客:
https://blog.csdn.net/wh_19910525/article/details/13503221