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