Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/samples/vfs/test-statx.c
49231 views
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/* Test the statx() system call.
3
*
4
* Note that the output of this program is intended to look like the output of
5
* /bin/stat where possible.
6
*
7
* Copyright (C) 2015 Red Hat, Inc. All Rights Reserved.
8
* Written by David Howells ([email protected])
9
*/
10
11
#define _GNU_SOURCE
12
#define _ATFILE_SOURCE
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <string.h>
16
#include <unistd.h>
17
#include <ctype.h>
18
#include <errno.h>
19
#include <time.h>
20
#include <sys/syscall.h>
21
#include <sys/types.h>
22
23
// Work around glibc header silliness
24
#undef AT_RENAME_NOREPLACE
25
#undef AT_RENAME_EXCHANGE
26
#undef AT_RENAME_WHITEOUT
27
28
#include <linux/stat.h>
29
#include <linux/fcntl.h>
30
#define statx foo
31
#define statx_timestamp foo_timestamp
32
struct statx;
33
struct statx_timestamp;
34
#include <sys/stat.h>
35
#undef statx
36
#undef statx_timestamp
37
38
#define AT_STATX_SYNC_TYPE 0x6000
39
#define AT_STATX_SYNC_AS_STAT 0x0000
40
#define AT_STATX_FORCE_SYNC 0x2000
41
#define AT_STATX_DONT_SYNC 0x4000
42
43
#ifndef __NR_statx
44
#define __NR_statx -1
45
#endif
46
47
static __attribute__((unused))
48
ssize_t statx(int dfd, const char *filename, unsigned flags,
49
unsigned int mask, struct statx *buffer)
50
{
51
return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
52
}
53
54
static void print_time(const char *field, struct statx_timestamp *ts)
55
{
56
struct tm tm;
57
time_t tim;
58
char buffer[100];
59
int len;
60
61
tim = ts->tv_sec;
62
if (!localtime_r(&tim, &tm)) {
63
perror("localtime_r");
64
exit(1);
65
}
66
len = strftime(buffer, 100, "%F %T", &tm);
67
if (len == 0) {
68
perror("strftime");
69
exit(1);
70
}
71
printf("%s", field);
72
fwrite(buffer, 1, len, stdout);
73
printf(".%09u", ts->tv_nsec);
74
len = strftime(buffer, 100, "%z", &tm);
75
if (len == 0) {
76
perror("strftime2");
77
exit(1);
78
}
79
fwrite(buffer, 1, len, stdout);
80
printf("\n");
81
}
82
83
static void dump_statx(struct statx *stx)
84
{
85
char buffer[256], ft = '?';
86
87
printf("results=%x\n", stx->stx_mask);
88
89
printf(" ");
90
if (stx->stx_mask & STATX_SIZE)
91
printf(" Size: %-15llu", (unsigned long long)stx->stx_size);
92
if (stx->stx_mask & STATX_BLOCKS)
93
printf(" Blocks: %-10llu", (unsigned long long)stx->stx_blocks);
94
printf(" IO Block: %-6llu", (unsigned long long)stx->stx_blksize);
95
if (stx->stx_mask & STATX_TYPE) {
96
switch (stx->stx_mode & S_IFMT) {
97
case S_IFIFO: printf(" FIFO\n"); ft = 'p'; break;
98
case S_IFCHR: printf(" character special file\n"); ft = 'c'; break;
99
case S_IFDIR: printf(" directory\n"); ft = 'd'; break;
100
case S_IFBLK: printf(" block special file\n"); ft = 'b'; break;
101
case S_IFREG: printf(" regular file\n"); ft = '-'; break;
102
case S_IFLNK: printf(" symbolic link\n"); ft = 'l'; break;
103
case S_IFSOCK: printf(" socket\n"); ft = 's'; break;
104
default:
105
printf(" unknown type (%o)\n", stx->stx_mode & S_IFMT);
106
break;
107
}
108
} else {
109
printf(" no type\n");
110
}
111
112
sprintf(buffer, "%02x:%02x", stx->stx_dev_major, stx->stx_dev_minor);
113
printf("Device: %-15s", buffer);
114
if (stx->stx_mask & STATX_INO)
115
printf(" Inode: %-11llu", (unsigned long long) stx->stx_ino);
116
if (stx->stx_mask & STATX_NLINK)
117
printf(" Links: %-5u", stx->stx_nlink);
118
if (stx->stx_mask & STATX_TYPE) {
119
switch (stx->stx_mode & S_IFMT) {
120
case S_IFBLK:
121
case S_IFCHR:
122
printf(" Device type: %u,%u",
123
stx->stx_rdev_major, stx->stx_rdev_minor);
124
break;
125
}
126
}
127
printf("\n");
128
129
if (stx->stx_mask & STATX_MODE)
130
printf("Access: (%04o/%c%c%c%c%c%c%c%c%c%c) ",
131
stx->stx_mode & 07777,
132
ft,
133
stx->stx_mode & S_IRUSR ? 'r' : '-',
134
stx->stx_mode & S_IWUSR ? 'w' : '-',
135
stx->stx_mode & S_IXUSR ? 'x' : '-',
136
stx->stx_mode & S_IRGRP ? 'r' : '-',
137
stx->stx_mode & S_IWGRP ? 'w' : '-',
138
stx->stx_mode & S_IXGRP ? 'x' : '-',
139
stx->stx_mode & S_IROTH ? 'r' : '-',
140
stx->stx_mode & S_IWOTH ? 'w' : '-',
141
stx->stx_mode & S_IXOTH ? 'x' : '-');
142
if (stx->stx_mask & STATX_UID)
143
printf("Uid: %5d ", stx->stx_uid);
144
if (stx->stx_mask & STATX_GID)
145
printf("Gid: %5d\n", stx->stx_gid);
146
147
if (stx->stx_mask & STATX_ATIME)
148
print_time("Access: ", &stx->stx_atime);
149
if (stx->stx_mask & STATX_MTIME)
150
print_time("Modify: ", &stx->stx_mtime);
151
if (stx->stx_mask & STATX_CTIME)
152
print_time("Change: ", &stx->stx_ctime);
153
if (stx->stx_mask & STATX_BTIME)
154
print_time(" Birth: ", &stx->stx_btime);
155
156
if (stx->stx_attributes_mask) {
157
unsigned char bits, mbits;
158
int loop, byte;
159
160
static char attr_representation[64 + 1] =
161
/* STATX_ATTR_ flags: */
162
"????????" /* 63-56 */
163
"????????" /* 55-48 */
164
"????????" /* 47-40 */
165
"????????" /* 39-32 */
166
"????????" /* 31-24 0x00000000-ff000000 */
167
"????????" /* 23-16 0x00000000-00ff0000 */
168
"???me???" /* 15- 8 0x00000000-0000ff00 */
169
"?dai?c??" /* 7- 0 0x00000000-000000ff */
170
;
171
172
printf("Attributes: %016llx (",
173
(unsigned long long)stx->stx_attributes);
174
for (byte = 64 - 8; byte >= 0; byte -= 8) {
175
bits = stx->stx_attributes >> byte;
176
mbits = stx->stx_attributes_mask >> byte;
177
for (loop = 7; loop >= 0; loop--) {
178
int bit = byte + loop;
179
180
if (!(mbits & 0x80))
181
putchar('.'); /* Not supported */
182
else if (bits & 0x80)
183
putchar(attr_representation[63 - bit]);
184
else
185
putchar('-'); /* Not set */
186
bits <<= 1;
187
mbits <<= 1;
188
}
189
if (byte)
190
putchar(' ');
191
}
192
printf(")\n");
193
}
194
}
195
196
static void dump_hex(unsigned long long *data, int from, int to)
197
{
198
unsigned offset, print_offset = 1, col = 0;
199
200
from /= 8;
201
to = (to + 7) / 8;
202
203
for (offset = from; offset < to; offset++) {
204
if (print_offset) {
205
printf("%04x: ", offset * 8);
206
print_offset = 0;
207
}
208
printf("%016llx", data[offset]);
209
col++;
210
if ((col & 3) == 0) {
211
printf("\n");
212
print_offset = 1;
213
} else {
214
printf(" ");
215
}
216
}
217
218
if (!print_offset)
219
printf("\n");
220
}
221
222
int main(int argc, char **argv)
223
{
224
struct statx stx;
225
int ret, raw = 0, atflag = AT_SYMLINK_NOFOLLOW;
226
227
unsigned int mask = STATX_BASIC_STATS | STATX_BTIME;
228
229
for (argv++; *argv; argv++) {
230
if (strcmp(*argv, "-F") == 0) {
231
atflag &= ~AT_STATX_SYNC_TYPE;
232
atflag |= AT_STATX_FORCE_SYNC;
233
continue;
234
}
235
if (strcmp(*argv, "-D") == 0) {
236
atflag &= ~AT_STATX_SYNC_TYPE;
237
atflag |= AT_STATX_DONT_SYNC;
238
continue;
239
}
240
if (strcmp(*argv, "-L") == 0) {
241
atflag &= ~AT_SYMLINK_NOFOLLOW;
242
continue;
243
}
244
if (strcmp(*argv, "-O") == 0) {
245
mask &= ~STATX_BASIC_STATS;
246
continue;
247
}
248
if (strcmp(*argv, "-A") == 0) {
249
atflag |= AT_NO_AUTOMOUNT;
250
continue;
251
}
252
if (strcmp(*argv, "-R") == 0) {
253
raw = 1;
254
continue;
255
}
256
257
memset(&stx, 0xbf, sizeof(stx));
258
ret = statx(AT_FDCWD, *argv, atflag, mask, &stx);
259
printf("statx(%s) = %d\n", *argv, ret);
260
if (ret < 0) {
261
perror(*argv);
262
exit(1);
263
}
264
265
if (raw)
266
dump_hex((unsigned long long *)&stx, 0, sizeof(stx));
267
268
dump_statx(&stx);
269
}
270
return 0;
271
}
272
273