Linux下获取文件或目录的状态信息(属性、大小、创建时间等

这是我参与11月更文挑战的第22天,活动详情查看:2021最后一次更文挑战

一、前言

在Linux下进行文件、目录编程时经常需要获取指定文件的属性信息,比如: 文件类型、大小、创建日期、修改日期等属性信息。

获取这些属性信息非常有用,比如: 打开一个设备文件时,为了防止未知错误,可以提前判断打开的设备文件是否是正确的类型。 拷贝文件时、读取文件时,mmap映射文件时,可以根据文件本身的大小,判断文件是否读写完毕。

Linux下有3种函数可以快速得到文件、目录的属性信息:

1
cpp复制代码stat 、fstat、lstat

详细的介绍如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cpp复制代码#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);
函数功能: 获取文件或者目录的状态
函数参数:
const char *path 文件或者目录的路径.
struct stat *buf 保存获取的状态信息

int fstat(int fd, struct stat *buf);
函数功能: 获取文件的状态信息.
函数形参:
int fd 文件描述符
struct stat *buf 保存获取的状态信息

int lstat(const char *path, struct stat *buf);
函数功能: 获取文件的状态信息. 不区分链接文件.

保存状态信息的结构体:
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 file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
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 */
};

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.) socket文件

在使用stat函数获取目录的信息时,st_size属性是没有的,目录的大小需要自己计算,里面可能包含了很多子目录等。

二、案例代码

下面使用state函数编写示例: 程序运行时,从命令行传入路径,调用opendir 函数打开目录,循环遍历目录,读取目录下的所有文件,并判断每个文件的类型,空间大小等信息打印到终端显示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
cpp复制代码#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

int main(int argc,char **argv)
{
if(argc!=2)
{
printf("参数: ./a.out <目录的路径>\n");
return 0;
}
/*1. 打开目录*/
DIR *dir=opendir(argv[1]);
if(dir==NULL)
{
printf("%s 目录打开失败.\n",argv[1]);
return -1;
}
/*2. 遍历目录*/
struct dirent *dir_info;
struct stat s_buf; //存放状态信息的
char *abs_path=NULL;
while(dir_info=readdir(dir))
{
//1. 申请空间
abs_path=malloc(strlen(argv[1])+strlen(dir_info->d_name)+1);
//2. 拼接路径
sprintf(abs_path,"%s%s",argv[1],dir_info->d_name);
//3. 获取文件的状态信息
stat(abs_path,&s_buf);
//4. 打印文件的状态
if(S_ISREG(s_buf.st_mode))
{
printf("%s 是普通文件.文件大小:%d Byte\n",abs_path,s_buf.st_size);
}
else if(S_ISDIR(s_buf.st_mode))
{
printf("%s 是目录.\n",abs_path);
}
else if(S_ISCHR(s_buf.st_mode))
{
printf("%s 字符设备文件.\n",abs_path);
}
else if(S_ISBLK(s_buf.st_mode))
{
printf("%s 块设备文件.\n",abs_path);
}
else if(S_ISFIFO(s_buf.st_mode))
{
printf("%s 是管道文件.\n",abs_path);
}
else if(S_ISLNK(s_buf.st_mode))
{
printf("%s 是链接文件.\n",abs_path);
}

//5. 释放空间
free(abs_path);
}
/*3. 关闭目录*/
closedir(dir);
return 0;
}

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%