Blame view

samples/vfs/test-statx.c 6.52 KB
b4d0d230c   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
a528d35e8   David Howells   statx: Add a syst...
2
3
4
5
6
7
8
  /* Test the statx() system call.
   *
   * Note that the output of this program is intended to look like the output of
   * /bin/stat where possible.
   *
   * Copyright (C) 2015 Red Hat, Inc. All Rights Reserved.
   * Written by David Howells (dhowells@redhat.com)
a528d35e8   David Howells   statx: Add a syst...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   */
  
  #define _GNU_SOURCE
  #define _ATFILE_SOURCE
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
  #include <ctype.h>
  #include <errno.h>
  #include <time.h>
  #include <sys/syscall.h>
  #include <sys/types.h>
  #include <linux/stat.h>
  #include <linux/fcntl.h>
f1b5618e0   David Howells   vfs: Add a sample...
24
25
  #define statx foo
  #define statx_timestamp foo_timestamp
c3eeaae9f   Kees Cook   samples/vfs: avoi...
26
27
  struct statx;
  struct statx_timestamp;
a528d35e8   David Howells   statx: Add a syst...
28
  #include <sys/stat.h>
f1b5618e0   David Howells   vfs: Add a sample...
29
30
  #undef statx
  #undef statx_timestamp
a528d35e8   David Howells   statx: Add a syst...
31
32
33
34
35
  
  #define AT_STATX_SYNC_TYPE	0x6000
  #define AT_STATX_SYNC_AS_STAT	0x0000
  #define AT_STATX_FORCE_SYNC	0x2000
  #define AT_STATX_DONT_SYNC	0x4000
f1b5618e0   David Howells   vfs: Add a sample...
36
37
38
  #ifndef __NR_statx
  #define __NR_statx -1
  #endif
a528d35e8   David Howells   statx: Add a syst...
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
148
149
150
151
152
153
154
155
156
157
158
159
  static __attribute__((unused))
  ssize_t statx(int dfd, const char *filename, unsigned flags,
  	      unsigned int mask, struct statx *buffer)
  {
  	return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
  }
  
  static void print_time(const char *field, struct statx_timestamp *ts)
  {
  	struct tm tm;
  	time_t tim;
  	char buffer[100];
  	int len;
  
  	tim = ts->tv_sec;
  	if (!localtime_r(&tim, &tm)) {
  		perror("localtime_r");
  		exit(1);
  	}
  	len = strftime(buffer, 100, "%F %T", &tm);
  	if (len == 0) {
  		perror("strftime");
  		exit(1);
  	}
  	printf("%s", field);
  	fwrite(buffer, 1, len, stdout);
  	printf(".%09u", ts->tv_nsec);
  	len = strftime(buffer, 100, "%z", &tm);
  	if (len == 0) {
  		perror("strftime2");
  		exit(1);
  	}
  	fwrite(buffer, 1, len, stdout);
  	printf("
  ");
  }
  
  static void dump_statx(struct statx *stx)
  {
  	char buffer[256], ft = '?';
  
  	printf("results=%x
  ", stx->stx_mask);
  
  	printf(" ");
  	if (stx->stx_mask & STATX_SIZE)
  		printf(" Size: %-15llu", (unsigned long long)stx->stx_size);
  	if (stx->stx_mask & STATX_BLOCKS)
  		printf(" Blocks: %-10llu", (unsigned long long)stx->stx_blocks);
  	printf(" IO Block: %-6llu", (unsigned long long)stx->stx_blksize);
  	if (stx->stx_mask & STATX_TYPE) {
  		switch (stx->stx_mode & S_IFMT) {
  		case S_IFIFO:	printf("  FIFO
  ");			ft = 'p'; break;
  		case S_IFCHR:	printf("  character special file
  ");	ft = 'c'; break;
  		case S_IFDIR:	printf("  directory
  ");		ft = 'd'; break;
  		case S_IFBLK:	printf("  block special file
  ");	ft = 'b'; break;
  		case S_IFREG:	printf("  regular file
  ");		ft = '-'; break;
  		case S_IFLNK:	printf("  symbolic link
  ");		ft = 'l'; break;
  		case S_IFSOCK:	printf("  socket
  ");			ft = 's'; break;
  		default:
  			printf(" unknown type (%o)
  ", stx->stx_mode & S_IFMT);
  			break;
  		}
  	} else {
  		printf(" no type
  ");
  	}
  
  	sprintf(buffer, "%02x:%02x", stx->stx_dev_major, stx->stx_dev_minor);
  	printf("Device: %-15s", buffer);
  	if (stx->stx_mask & STATX_INO)
  		printf(" Inode: %-11llu", (unsigned long long) stx->stx_ino);
  	if (stx->stx_mask & STATX_NLINK)
  		printf(" Links: %-5u", stx->stx_nlink);
  	if (stx->stx_mask & STATX_TYPE) {
  		switch (stx->stx_mode & S_IFMT) {
  		case S_IFBLK:
  		case S_IFCHR:
  			printf(" Device type: %u,%u",
  			       stx->stx_rdev_major, stx->stx_rdev_minor);
  			break;
  		}
  	}
  	printf("
  ");
  
  	if (stx->stx_mask & STATX_MODE)
  		printf("Access: (%04o/%c%c%c%c%c%c%c%c%c%c)  ",
  		       stx->stx_mode & 07777,
  		       ft,
  		       stx->stx_mode & S_IRUSR ? 'r' : '-',
  		       stx->stx_mode & S_IWUSR ? 'w' : '-',
  		       stx->stx_mode & S_IXUSR ? 'x' : '-',
  		       stx->stx_mode & S_IRGRP ? 'r' : '-',
  		       stx->stx_mode & S_IWGRP ? 'w' : '-',
  		       stx->stx_mode & S_IXGRP ? 'x' : '-',
  		       stx->stx_mode & S_IROTH ? 'r' : '-',
  		       stx->stx_mode & S_IWOTH ? 'w' : '-',
  		       stx->stx_mode & S_IXOTH ? 'x' : '-');
  	if (stx->stx_mask & STATX_UID)
  		printf("Uid: %5d   ", stx->stx_uid);
  	if (stx->stx_mask & STATX_GID)
  		printf("Gid: %5d
  ", stx->stx_gid);
  
  	if (stx->stx_mask & STATX_ATIME)
  		print_time("Access: ", &stx->stx_atime);
  	if (stx->stx_mask & STATX_MTIME)
  		print_time("Modify: ", &stx->stx_mtime);
  	if (stx->stx_mask & STATX_CTIME)
  		print_time("Change: ", &stx->stx_ctime);
  	if (stx->stx_mask & STATX_BTIME)
  		print_time(" Birth: ", &stx->stx_btime);
3209f68b3   David Howells   statx: Include a ...
160
161
  	if (stx->stx_attributes_mask) {
  		unsigned char bits, mbits;
a528d35e8   David Howells   statx: Add a syst...
162
163
164
165
166
167
168
169
170
171
172
173
174
  		int loop, byte;
  
  		static char attr_representation[64 + 1] =
  			/* STATX_ATTR_ flags: */
  			"????????"	/* 63-56 */
  			"????????"	/* 55-48 */
  			"????????"	/* 47-40 */
  			"????????"	/* 39-32 */
  			"????????"	/* 31-24	0x00000000-ff000000 */
  			"????????"	/* 23-16	0x00000000-00ff0000 */
  			"???me???"	/* 15- 8	0x00000000-0000ff00 */
  			"?dai?c??"	/*  7- 0	0x00000000-000000ff */
  			;
f1b5618e0   David Howells   vfs: Add a sample...
175
176
  		printf("Attributes: %016llx (",
  		       (unsigned long long)stx->stx_attributes);
a528d35e8   David Howells   statx: Add a syst...
177
178
  		for (byte = 64 - 8; byte >= 0; byte -= 8) {
  			bits = stx->stx_attributes >> byte;
3209f68b3   David Howells   statx: Include a ...
179
  			mbits = stx->stx_attributes_mask >> byte;
a528d35e8   David Howells   statx: Add a syst...
180
181
  			for (loop = 7; loop >= 0; loop--) {
  				int bit = byte + loop;
3209f68b3   David Howells   statx: Include a ...
182
183
184
  				if (!(mbits & 0x80))
  					putchar('.');	/* Not supported */
  				else if (bits & 0x80)
a528d35e8   David Howells   statx: Add a syst...
185
186
  					putchar(attr_representation[63 - bit]);
  				else
3209f68b3   David Howells   statx: Include a ...
187
  					putchar('-');	/* Not set */
a528d35e8   David Howells   statx: Add a syst...
188
  				bits <<= 1;
3209f68b3   David Howells   statx: Include a ...
189
  				mbits <<= 1;
a528d35e8   David Howells   statx: Add a syst...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  			}
  			if (byte)
  				putchar(' ');
  		}
  		printf(")
  ");
  	}
  }
  
  static void dump_hex(unsigned long long *data, int from, int to)
  {
  	unsigned offset, print_offset = 1, col = 0;
  
  	from /= 8;
  	to = (to + 7) / 8;
  
  	for (offset = from; offset < to; offset++) {
  		if (print_offset) {
  			printf("%04x: ", offset * 8);
  			print_offset = 0;
  		}
  		printf("%016llx", data[offset]);
  		col++;
  		if ((col & 3) == 0) {
  			printf("
  ");
  			print_offset = 1;
  		} else {
  			printf(" ");
  		}
  	}
  
  	if (!print_offset)
  		printf("
  ");
  }
  
  int main(int argc, char **argv)
  {
  	struct statx stx;
  	int ret, raw = 0, atflag = AT_SYMLINK_NOFOLLOW;
581701b7e   Miklos Szeredi   uapi: deprecate S...
231
  	unsigned int mask = STATX_BASIC_STATS | STATX_BTIME;
a528d35e8   David Howells   statx: Add a syst...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  
  	for (argv++; *argv; argv++) {
  		if (strcmp(*argv, "-F") == 0) {
  			atflag &= ~AT_STATX_SYNC_TYPE;
  			atflag |= AT_STATX_FORCE_SYNC;
  			continue;
  		}
  		if (strcmp(*argv, "-D") == 0) {
  			atflag &= ~AT_STATX_SYNC_TYPE;
  			atflag |= AT_STATX_DONT_SYNC;
  			continue;
  		}
  		if (strcmp(*argv, "-L") == 0) {
  			atflag &= ~AT_SYMLINK_NOFOLLOW;
  			continue;
  		}
  		if (strcmp(*argv, "-O") == 0) {
  			mask &= ~STATX_BASIC_STATS;
  			continue;
  		}
  		if (strcmp(*argv, "-A") == 0) {
  			atflag |= AT_NO_AUTOMOUNT;
  			continue;
  		}
  		if (strcmp(*argv, "-R") == 0) {
  			raw = 1;
  			continue;
  		}
  
  		memset(&stx, 0xbf, sizeof(stx));
  		ret = statx(AT_FDCWD, *argv, atflag, mask, &stx);
  		printf("statx(%s) = %d
  ", *argv, ret);
  		if (ret < 0) {
  			perror(*argv);
  			exit(1);
  		}
  
  		if (raw)
  			dump_hex((unsigned long long *)&stx, 0, sizeof(stx));
  
  		dump_statx(&stx);
  	}
  	return 0;
  }