File: statx.c

package info (click to toggle)
zfs-linux 2.3.4~git20250812.3b64a96-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 70,688 kB
  • sloc: ansic: 393,668; sh: 68,068; asm: 47,734; python: 8,160; makefile: 5,125; perl: 859; sed: 41
file content (307 lines) | stat: -rw-r--r-- 7,021 bytes parent folder | download | duplicates (2)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
231
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright (c) 2025, Rob Norris <robn@despairlabs.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <unistd.h>

/*
 * statx() may be available in the kernel, but not in the libc, so we build
 * our own wrapper if we can't link one.
 */

#ifndef __NR_statx
#if defined(__x86_64__)
#define	__NR_statx (332)
#elif defined(__i386__)
#define	__NR_statx (383)
#elif defined(__s390__)
#define	__NR_statx (379)
#elif defined(__arm__)
#define	__NR_statx (397)
#elif defined(__aarch64__)
#define	__NR_statx (291)
#elif defined(__powerpc__)
#define	__NR_statx (383)
#else
#error "no definition of __NR_statx for this platform"
#endif
#endif /* __NR_statx */


int
statx(int, const char *, int, unsigned int, void *)
    __attribute__((weak));

static inline int
_statx(int fd, const char *path, int flags, unsigned int mask, void *stx)
{
	if (statx)
		return (statx(fd, path, flags, mask, stx));
	else
		return (syscall(__NR_statx, fd, path, flags, mask, stx));
}

#ifndef STATX_TYPE
#define	STATX_TYPE	(1<<0)
#endif
#ifndef STATX_MODE
#define	STATX_MODE	(1<<1)
#endif
#ifndef STATX_NLINK
#define	STATX_NLINK	(1<<2)
#endif
#ifndef STATX_UID
#define	STATX_UID	(1<<3)
#endif
#ifndef STATX_GID
#define	STATX_GID	(1<<4)
#endif
#ifndef STATX_ATIME
#define	STATX_ATIME	(1<<5)
#endif
#ifndef STATX_MTIME
#define	STATX_MTIME	(1<<6)
#endif
#ifndef STATX_CTIME
#define	STATX_CTIME	(1<<7)
#endif
#ifndef STATX_INO
#define	STATX_INO	(1<<8)
#endif
#ifndef STATX_SIZE
#define	STATX_SIZE	(1<<9)
#endif
#ifndef STATX_BLOCKS
#define	STATX_BLOCKS	(1<<10)
#endif
#ifndef STATX_BTIME
#define	STATX_BTIME	(1<<11)
#endif
#ifndef STATX_MNT_ID
#define	STATX_MNT_ID	(1<<12)
#endif
#ifndef STATX_DIOALIGN
#define	STATX_DIOALIGN	(1<<13)
#endif
#ifndef S_IFMT
#define	S_IFMT 0170000
#endif

typedef struct {
	int64_t		tv_sec;
	uint32_t	tv_nsec;
	int32_t		_pad;
} stx_timestamp_t;
_Static_assert(sizeof (stx_timestamp_t) == 0x10,
	"stx_timestamp_t not 16 bytes");

typedef struct {
	uint32_t	stx_mask;
	uint32_t	stx_blksize;
	uint64_t	stx_attributes;
	uint32_t	stx_nlink;
	uint32_t	stx_uid;
	uint32_t	stx_gid;
	uint16_t	stx_mode;
	uint16_t	_pad1;
	uint64_t	stx_ino;
	uint64_t	stx_size;
	uint64_t	stx_blocks;
	uint64_t	stx_attributes_mask;
	stx_timestamp_t	stx_atime;
	stx_timestamp_t	stx_btime;
	stx_timestamp_t	stx_ctime;
	stx_timestamp_t	stx_mtime;
	uint32_t	stx_rdev_major;
	uint32_t	stx_rdev_minor;
	uint32_t	stx_dev_major;
	uint32_t	stx_dev_minor;
	uint64_t	stx_mnt_id;
	uint32_t	stx_dio_mem_align;
	uint32_t	stx_dio_offset_align;
	uint64_t	_pad2[12];
} stx_t;
_Static_assert(sizeof (stx_t) == 0x100, "stx_t not 256 bytes");

typedef struct {
	const char *name;
	unsigned int mask;
} stx_field_t;

stx_field_t fields[] = {
	{ "type",	STATX_TYPE },
	{ "mode",	STATX_MODE },
	{ "nlink",	STATX_NLINK },
	{ "uid",	STATX_UID },
	{ "gid",	STATX_GID },
	{ "atime",	STATX_ATIME },
	{ "mtime",	STATX_MTIME },
	{ "ctime",	STATX_CTIME },
	{ "ino",	STATX_INO },
	{ "size",	STATX_SIZE },
	{ "blocks",	STATX_BLOCKS },
	{ "btime",	STATX_BTIME },
	{ "mnt_id",	STATX_MNT_ID },
	{ "dioalign",	STATX_DIOALIGN },
	{ NULL },
};

static int
usage(void)
{
	printf(
	    "usage: statx <field[,field,field]> <file>\n"
	    "available fields:\n");

	int w = 0;
	for (stx_field_t *f = fields; f->name != NULL; f++) {
		if (w > 0 && (w + strlen(f->name) + 1) > 60) {
			fputc('\n', stdout);
			w = 0;
		}
		if (w == 0)
			fputc(' ', stdout);
		w += printf(" %s", f->name);
	}
	if (w > 0)
		fputc('\n', stdout);
	return (1);
}

int
main(int argc, char **argv)
{
	if (argc < 3)
		return (usage());

	unsigned int mask = 0;

	char *name;
	while ((name = strsep(&argv[1], ",")) != NULL) {
		stx_field_t *f;
		for (f = fields; f->name != NULL; f++) {
			if (strcmp(name, f->name) == 0) {
				mask |= f->mask;
				break;
			}
		}
		if (f->name == NULL) {
			fprintf(stderr, "unknown field name: %s\n", name);
			return (usage());
		}
	}

	int fd = open(argv[2], O_PATH);
	if (fd < 0) {
		fprintf(stderr, "open: %s: %s\n", argv[2], strerror(errno));
		return (1);
	}

	stx_t stx = {};

	if (_statx(fd, "",
	    AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW, mask, &stx) < 0) {
		fprintf(stderr, "statx: %s: %s\n", argv[2], strerror(errno));
		close(fd);
		return (1);
	}

	int rc = 0;

	for (stx_field_t *f = fields; f->name != NULL; f++) {
		if (!(mask & f->mask))
			continue;
		if (!(stx.stx_mask & f->mask)) {
			printf("statx: kernel did not return field: %s\n",
			    f->name);
			rc = 2;
			continue;
		}
	}

	if (rc > 0)
		return (rc);

	for (stx_field_t *f = fields; f->name != NULL; f++) {
		if (!(mask & f->mask))
			continue;

		switch (f->mask) {
		case STATX_TYPE:
			printf("type: %u\n", stx.stx_mode & S_IFMT);
			break;
		case STATX_MODE:
			printf("mode: %u\n", stx.stx_mode & ~S_IFMT);
			break;
		case STATX_NLINK:
			printf("nlink: %u\n", stx.stx_nlink);
			break;
		case STATX_UID:
			printf("uid: %u\n", stx.stx_uid);
			break;
		case STATX_GID:
			printf("gid: %u\n", stx.stx_gid);
			break;
		case STATX_ATIME:
			printf("atime: %ld.%u\n",
			    stx.stx_atime.tv_sec, stx.stx_atime.tv_nsec);
			break;
		case STATX_MTIME:
			printf("mtime: %ld.%u\n",
			    stx.stx_mtime.tv_sec, stx.stx_mtime.tv_nsec);
			break;
		case STATX_CTIME:
			printf("ctime: %ld.%u\n",
			    stx.stx_ctime.tv_sec, stx.stx_ctime.tv_nsec);
			break;
		case STATX_INO:
			printf("ino: %lu\n", stx.stx_ino);
			break;
		case STATX_SIZE:
			printf("size: %lu\n", stx.stx_size);
			break;
		case STATX_BLOCKS:
			printf("blocks: %lu\n", stx.stx_blocks);
			break;
		case STATX_BTIME:
			printf("btime: %ld.%u\n",
			    stx.stx_btime.tv_sec, stx.stx_btime.tv_nsec);
			break;
		case STATX_MNT_ID:
			printf("mnt_id: %lu\n", stx.stx_mnt_id);
			break;
		case STATX_DIOALIGN:
			printf("dioalign: %u %u\n",
			    stx.stx_dio_mem_align, stx.stx_dio_offset_align);
			break;
		}
	}

	return (rc);
}