日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發布時間:2024/8/5 linux 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 嵌入式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: 2016-05-30 04:06:10.151098056 -0400

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

Change: 2016-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?

int?print_stat(struct?stat?*st);

int?main(int?argc,?char?**argv)

{

if(argc?

{

fprintf(stderr,?"argc?is?less?than?2\n");

fprintf(stdout,?"please?enter:?appname?filename");

return?-1;

}

struct?stat?s;

bzero(&s,?sizeof(struct?stat));

stat(argv[1],?&s);

print_stat(&s);

return?0;

}

int?print_stat(struct?stat?*st)

{

if(NULL?==?st)

{

fprintf(stderr,?"struct?stat?*st?is?NULL\n");

exit(-1);

}

printf("The?device?no?is:?%d\n",?st->st_dev);

printf("The?file's?node?number?is:?%d\n",?st->st_ino);

printf("The?file's?access?mode?is:?%d\n",?st->st_mode);

printf("The?file's?hard?link?number?is:?%d\n",?st->st_nlink);

printf("The?file's?user?id?is:?%d\n",?st->st_uid);

printf("The?file's?group?id?is:?%d\n",?st->st_gid);

printf("The?file's?size?is:?%d\n",?st->st_size);

printf("The?block?size?is:?%d\n",?st->st_blksize);

printf("The?number?of?allocated?blocks?is:?%d\n",?st->st_blocks);

struct?tm*?pAccess_time=localtime(&(st->st_atime));

struct?tm*?pModify_time=localtime(&(st->st_mtime));

struct?tm*?pChange_time=localtime(&(st->st_ctime));

char?aBuffer[64]?=?{0};

char?mBuffer[64]?=?{0};

char?cBuffer[64]?=?{0};

strftime(aBuffer,?64,?"The?last?access?time?is:?%Y-%m-%d?%H:%M:%S?\n",?pAccess_time);

strftime(mBuffer,?64,?"The?last?modify?time?is:?%Y-%m-%d?%H:%M:%S?\n",?pModify_time);

strftime(cBuffer,?64,?"The?last?change?time?is:?%Y-%m-%d?%H:%M:%S?\n",?pChange_time);

printf(aBuffer);

printf(mBuffer);

printf(cBuffer);

return?0;

}

總結

以上是生活随笔為你收集整理的嵌入式Linux文件提取,嵌入式 Linux系统编程(四)——文件属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。