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