IT技术互动交流平台

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

作者:佚名  来源:IT165收集  发布日期:2016-07-18 20:35:07

一、文件属性概述
        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;
}
四、程序实例
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
#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 ");
        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 ");
        exit(-1);
    }
    printf("The device no is: %d ", st->st_dev);
    printf("The file's node number is: %d ", st->st_ino);
    printf("The file's access mode is: %d ", st->st_mode);
    printf("The file's hard link number is: %d ", st->st_nlink);
    printf("The file's user id is: %d ", st->st_uid);
    printf("The file's group id is: %d ", st->st_gid);
    printf("The file's size is: %d ", st->st_size);
    printf("The block size is: %d ", st->st_blksize);
    printf("The number of allocated blocks is: %d ", 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 ", pAccess_time);
    strftime(mBuffer, 64, "The last modify time is: %Y-%m-%d %H:%M:%S ", pModify_time);
    strftime(cBuffer, 64, "The last change time is: %Y-%m-%d %H:%M:%S ", pChange_time);
 
    printf(aBuffer);
    printf(mBuffer);
printf(cBuffer);
return 0;
}
 

 

 

Tag标签: 嵌入式   属性   文件  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规