第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 嵌入式Linux文件提取 嵌入式 Linux系统编程(四)——文件属性

嵌入式Linux文件提取 嵌入式 Linux系统编程(四)——文件属性

时间:2020-05-19 10:58:47

相关推荐

嵌入式Linux文件提取 嵌入式 Linux系统编程(四)——文件属性

嵌入式 Linux系统编程(四)——文件属性

一、文件属性概述

Linux 文件的属性主要包括:文件的节点、种类、权限模式、链接数量、所归属的用户和用户组、最近访问或修改的时间等内容。文件属性示例如下:

多个文件属性查看:

ls -lih

1341714 -rw-r--r-- 1 root root 2.5K May 28 10:24 bit_marco.c

1341718 -rw-r--r-- 1 root root 2.1K May 28 09:08 bit_marco.c~

1341706 -rw-r--r-- 1 root root 2.6K May 28 08:54 bit_operaion.c

1335906 -rwxr-xr-x 1 root root 7.2K May 30 04:06 file

1341722 -rw-r--r-- 1 root root 2.7K May 30 04:06 file.c

1321584 -rw-r--r-- 1 root root 2.7K May 30 04:04 file.c~

1341708 -rwxr-xr-x 1 root root 6.4K May 28 09:08 main

1335846 -rw-r--r-- 1 root root 2.6K May 28 08:55 main.c

第一字段:inode

第二字段:文件类型和权限

第三字段:硬链接数

第四字段:所属用户

第五字段:所属用户组

第六字段:文件大小

第七字段:最后访问或修改时间

第八字段:文件名

单个文件属性详细查看:

root#stat file

File: `file'

Size: 7308 Blocks: 16 IO Block: 4096 regular file

Device: fd00h/64768d Inode: 1335906 Links: 1

Access: (0755/-rwxr-xr-x) Uid: (0/root) Gid: (0/root)

Access: -05-30 04:06:10.151098056 -0400

Modify: -05-30 04:06:07.227089617 -0400

Change: -05-30 04:06:07.227089617 -0400

二、文件属性函数

#include

#include

#include

int stat(const char *path, struct stat *buf);

int fstat(int fd, struct stat *buf);

int lstat(const char *path, struct stat *buf);

成功返回0,错误返回-1,并设置errno全局变量

文件属性数据结构:

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 */

};

1、stat

int stat(const char *path, struct stat *buf);

path:文件的路径,buf:指向存储文件信息的数据结构的指针

2、fstat

int fstat(int fd, struct stat *buf);

fd:文件描述符,buf:指向存储文件信息的数据结构的指针

3、lstat

int lstat(const char *path, struct stat *buf);

path:文件的路径,buf:指向存储文件信息的数据结构的指针

用于查阅链接文件的文件属性

三、文件权限信息提取

文件的文件类型和文件权限存在于文件属性数据结构中的st_mode成员。为了使用st_mode检查文件类型,POSIX标准定义了如下的宏:

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_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 UID 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 //掩码

S_IROTH 00004 others have read permission

S_IWOTH 00002 others have write permission

S_IXOTH 00001 others have execute permission

文件类型的获取:

int get_stat_type(const struct stat *st, char *buf)

{

if(S_ISREG(st->st_mode))

{

sprintf(buf, "regular file");

}

if(S_ISDIR(st->st_mode))

{

sprintf(buf, "directory");

}

if(S_ISCHR(st->st_mode))

{

sprintf(buf, "charactor device");

}

if(S_ISBLK(st->st_mode))

{

sprintf(buf, "block device");

}

if(S_ISFIFO(st->st_mode))

{

sprintf(buf, "fifo file");

}

if(S_ISLNK(st->st_mode))

{

sprintf(buf, "symbloc link");

}

if(S_ISSOCK(st->st_mode))

{

sprintf(buf, "socket");

}

return 0;

}

四、程序实例#include

#include

#include

#include

#include

#include

#include

intprint_stat(structstat*st);

intmain(intargc,char**argv)

{

if(argc

{

fprintf(stderr,"argcislessthan2\n");

fprintf(stdout,"pleaseenter:appnamefilename");

return-1;

}

structstats;

bzero(&s,sizeof(structstat));

stat(argv[1],&s);

print_stat(&s);

return0;

}

intprint_stat(structstat*st)

{

if(NULL==st)

{

fprintf(stderr,"structstat*stisNULL\n");

exit(-1);

}

printf("Thedevicenois:%d\n",st->st_dev);

printf("Thefile'snodenumberis:%d\n",st->st_ino);

printf("Thefile'saccessmodeis:%d\n",st->st_mode);

printf("Thefile'shardlinknumberis:%d\n",st->st_nlink);

printf("Thefile'suseridis:%d\n",st->st_uid);

printf("Thefile'sgroupidis:%d\n",st->st_gid);

printf("Thefile'ssizeis:%d\n",st->st_size);

printf("Theblocksizeis:%d\n",st->st_blksize);

printf("Thenumberofallocatedblocksis:%d\n",st->st_blocks);

structtm*pAccess_time=localtime(&(st->st_atime));

structtm*pModify_time=localtime(&(st->st_mtime));

structtm*pChange_time=localtime(&(st->st_ctime));

charaBuffer[64]={0};

charmBuffer[64]={0};

charcBuffer[64]={0};

strftime(aBuffer,64,"Thelastaccesstimeis:%Y-%m-%d%H:%M:%S\n",pAccess_time);

strftime(mBuffer,64,"Thelastmodifytimeis:%Y-%m-%d%H:%M:%S\n",pModify_time);

strftime(cBuffer,64,"Thelastchangetimeis:%Y-%m-%d%H:%M:%S\n",pChange_time);

printf(aBuffer);

printf(mBuffer);

printf(cBuffer);

return0;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。