Linux programming file and I/O (4): file attributes

Source: Internet
Author: User
Tags lstat

I. Reading file metadata

Int Stat (const char * path, struct stat * BUF );
Int fstat (int fd, struct stat * BUF );
Int lstat (const char * path, struct stat * BUF );


Stat () stats the file pointed to by path and fills in Buf.

Lstat () is identical to stat (), doesn t that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers.

Fstat () is identical to stat (), doesn't that the file to be stat-ed is specified by the file descriptor FD.


Ii. Stat struct

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


Example program:

C ++ code
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*************************************** **********************************
> File name: file_stat.c
> Author: Simba
> Mail: dameng34@163.com
> Created time: sat 23 Feb 2013 02:34:02 pm CST
**************************************** ********************************/
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <unistd. h>
# Include <fcntl. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>

# Define err_exit (m )\
Do {\
Perror (m );\
Exit (exit_failure );\
} While (0)

# Define major (A) (INT) (unsigned short) A> 8) // high 8-bit, master device number
# Define minor (a) (INT) (unsigned short) A & 0xff)

Int filetype (struct stat * BUF)
{
Int flag = 0;
Printf ("filetype :");
Mode_t mode;
Mode = Buf-> st_mode;
Switch (Mode & s_ifmt)
{

Case s_ifsock:
Printf ("socket \ n ");
Break;
Case s_iflnk:
Printf ("Symbolic Link \ n ");
Break;
Case s_ifreg:
Printf ("regular file \ n ");
Break;
Case s_ifblk:
Printf ("Block device \ n ");
Flag = 1;
Break;
Case s_ifdir:
Printf ("directory \ n ");
Break;
Case s_ifchr:
Printf ("character device \ n ");
Flag = 1;
Break;
Case s_ififo:
Printf ("FIFO \ n ");
Break;
Default:
Printf ("unknown file type \ n ");
Break;
}

Return flag;
}

Void fileperm (struct stat * Buf, char perm [])
{
Strcpy (perm ,"----------");
Perm [0] = '? ';
Mode_t mode;
Mode = Buf-> st_mode;
Switch (Mode & s_ifmt)
{

Case s_ifsock:
Perm [0] ='s ';
Break;
Case s_iflnk:
Perm [0] = 'l ';
Break;
Case s_ifreg:
Perm [0] = '-';
Break;
Case s_ifblk:
Perm [0] = 'B ';
Break;
Case s_ifdir:
Perm [0] = 'D ';
Break;
Case s_ifchr:
Perm [0] = 'C ';
Break;
Case s_ififo:
Perm [0] = 'P ';
Break;
}

If (Mode & s_irusr)
Perm [1] = 'R ';
If (Mode & s_iwusr)
Perm [2] = 'W ';
If (Mode & s_ixusr)
Perm [3] = 'X ';
If (Mode & s_irgrp)
Perm [4] = 'R ';
If (Mode & s_iwgrp)
Perm [5] = 'W ';
If (Mode & s_ixgrp)
Perm [6] = 'X ';
If (Mode & s_iroth)
Perm [7] = 'R ';
If (Mode & s_iwoth)
Perm [8] = 'W ';
If (Mode & s_ixoth)
Perm [9] = 'X ';
Perm [10] = '\ 0 ';
}

Int main (INT argc, char * argv [])
{
If (argc! = 2)
{
Fprintf (stderr, "usage % s file \ n", argv [0]);
Exit (exit_failure );
}

Printf ("filename: % s \ n", argv [1]);
Struct stat sbuf;
If (lstat (argv [1], & sbuf) =-1)
Err_exit ("stat error ");

Printf ("file in Dev number: Major % d, minor % d \ n ",
Major (sbuf. st_dev), minor (sbuf. st_dev ));
Printf ("file inode: % d \ n", (INT) sbuf. st_ino );

If (filetype (& sbuf ))
{
Printf ("device number: Major % d, minor % d \ n ",
Major (sbuf. st_rdev), minor (sbuf. st_rdev ));
}

Char perm [11] = {0 };
Fileperm (& sbuf, Perm );
Printf ("File Permission bits = % o % s \ n", sbuf. st_mode & 07777, Perm );

Return 0;
}

The test is as follows:

Simba @ Ubuntu :~ /Documents/code/linux_programming/apue/file_io $./file_stat makefile
Filename: makefile
File in Dev number: Major 8, minor 1
File inode: 660022
Filetype: regular file
File Permission bits = 664-RW-r --


Reference: apue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.