1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Test the statx() system call.
4 * Note that the output of this program is intended to look like the output of
5 * /bin/stat where possible.
7 * Copyright (C) 2015 Red Hat, Inc. All Rights Reserved.
8 * Written by David Howells (dhowells@redhat.com)
12 #define _ATFILE_SOURCE
20 #include <sys/syscall.h>
21 #include <sys/types.h>
22 #include <linux/stat.h>
23 #include <linux/fcntl.h>
25 #define statx_timestamp foo_timestamp
27 struct statx_timestamp;
30 #undef statx_timestamp
32 #define AT_STATX_SYNC_TYPE 0x6000
33 #define AT_STATX_SYNC_AS_STAT 0x0000
34 #define AT_STATX_FORCE_SYNC 0x2000
35 #define AT_STATX_DONT_SYNC 0x4000
41 static __attribute__((unused))
42 ssize_t statx(int dfd, const char *filename, unsigned flags,
43 unsigned int mask, struct statx *buffer)
45 return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
48 static void print_time(const char *field, struct statx_timestamp *ts)
56 if (!localtime_r(&tim, &tm)) {
57 perror("localtime_r");
60 len = strftime(buffer, 100, "%F %T", &tm);
66 fwrite(buffer, 1, len, stdout);
67 printf(".%09u", ts->tv_nsec);
68 len = strftime(buffer, 100, "%z", &tm);
73 fwrite(buffer, 1, len, stdout);
77 static void dump_statx(struct statx *stx)
79 char buffer[256], ft = '?';
81 printf("results=%x\n", stx->stx_mask);
84 if (stx->stx_mask & STATX_SIZE)
85 printf(" Size: %-15llu", (unsigned long long)stx->stx_size);
86 if (stx->stx_mask & STATX_BLOCKS)
87 printf(" Blocks: %-10llu", (unsigned long long)stx->stx_blocks);
88 printf(" IO Block: %-6llu", (unsigned long long)stx->stx_blksize);
89 if (stx->stx_mask & STATX_TYPE) {
90 switch (stx->stx_mode & S_IFMT) {
91 case S_IFIFO: printf(" FIFO\n"); ft = 'p'; break;
92 case S_IFCHR: printf(" character special file\n"); ft = 'c'; break;
93 case S_IFDIR: printf(" directory\n"); ft = 'd'; break;
94 case S_IFBLK: printf(" block special file\n"); ft = 'b'; break;
95 case S_IFREG: printf(" regular file\n"); ft = '-'; break;
96 case S_IFLNK: printf(" symbolic link\n"); ft = 'l'; break;
97 case S_IFSOCK: printf(" socket\n"); ft = 's'; break;
99 printf(" unknown type (%o)\n", stx->stx_mode & S_IFMT);
103 printf(" no type\n");
106 sprintf(buffer, "%02x:%02x", stx->stx_dev_major, stx->stx_dev_minor);
107 printf("Device: %-15s", buffer);
108 if (stx->stx_mask & STATX_INO)
109 printf(" Inode: %-11llu", (unsigned long long) stx->stx_ino);
110 if (stx->stx_mask & STATX_NLINK)
111 printf(" Links: %-5u", stx->stx_nlink);
112 if (stx->stx_mask & STATX_TYPE) {
113 switch (stx->stx_mode & S_IFMT) {
116 printf(" Device type: %u,%u",
117 stx->stx_rdev_major, stx->stx_rdev_minor);
123 if (stx->stx_mask & STATX_MODE)
124 printf("Access: (%04o/%c%c%c%c%c%c%c%c%c%c) ",
125 stx->stx_mode & 07777,
127 stx->stx_mode & S_IRUSR ? 'r' : '-',
128 stx->stx_mode & S_IWUSR ? 'w' : '-',
129 stx->stx_mode & S_IXUSR ? 'x' : '-',
130 stx->stx_mode & S_IRGRP ? 'r' : '-',
131 stx->stx_mode & S_IWGRP ? 'w' : '-',
132 stx->stx_mode & S_IXGRP ? 'x' : '-',
133 stx->stx_mode & S_IROTH ? 'r' : '-',
134 stx->stx_mode & S_IWOTH ? 'w' : '-',
135 stx->stx_mode & S_IXOTH ? 'x' : '-');
136 if (stx->stx_mask & STATX_UID)
137 printf("Uid: %5d ", stx->stx_uid);
138 if (stx->stx_mask & STATX_GID)
139 printf("Gid: %5d\n", stx->stx_gid);
141 if (stx->stx_mask & STATX_ATIME)
142 print_time("Access: ", &stx->stx_atime);
143 if (stx->stx_mask & STATX_MTIME)
144 print_time("Modify: ", &stx->stx_mtime);
145 if (stx->stx_mask & STATX_CTIME)
146 print_time("Change: ", &stx->stx_ctime);
147 if (stx->stx_mask & STATX_BTIME)
148 print_time(" Birth: ", &stx->stx_btime);
150 if (stx->stx_attributes_mask) {
151 unsigned char bits, mbits;
154 static char attr_representation[64 + 1] =
155 /* STATX_ATTR_ flags: */
156 "????????" /* 63-56 */
157 "????????" /* 55-48 */
158 "????????" /* 47-40 */
159 "????????" /* 39-32 */
160 "????????" /* 31-24 0x00000000-ff000000 */
161 "????????" /* 23-16 0x00000000-00ff0000 */
162 "???me???" /* 15- 8 0x00000000-0000ff00 */
163 "?dai?c??" /* 7- 0 0x00000000-000000ff */
166 printf("Attributes: %016llx (",
167 (unsigned long long)stx->stx_attributes);
168 for (byte = 64 - 8; byte >= 0; byte -= 8) {
169 bits = stx->stx_attributes >> byte;
170 mbits = stx->stx_attributes_mask >> byte;
171 for (loop = 7; loop >= 0; loop--) {
172 int bit = byte + loop;
175 putchar('.'); /* Not supported */
176 else if (bits & 0x80)
177 putchar(attr_representation[63 - bit]);
179 putchar('-'); /* Not set */
190 static void dump_hex(unsigned long long *data, int from, int to)
192 unsigned offset, print_offset = 1, col = 0;
197 for (offset = from; offset < to; offset++) {
199 printf("%04x: ", offset * 8);
202 printf("%016llx", data[offset]);
204 if ((col & 3) == 0) {
216 int main(int argc, char **argv)
219 int ret, raw = 0, atflag = AT_SYMLINK_NOFOLLOW;
221 unsigned int mask = STATX_BASIC_STATS | STATX_BTIME;
223 for (argv++; *argv; argv++) {
224 if (strcmp(*argv, "-F") == 0) {
225 atflag &= ~AT_STATX_SYNC_TYPE;
226 atflag |= AT_STATX_FORCE_SYNC;
229 if (strcmp(*argv, "-D") == 0) {
230 atflag &= ~AT_STATX_SYNC_TYPE;
231 atflag |= AT_STATX_DONT_SYNC;
234 if (strcmp(*argv, "-L") == 0) {
235 atflag &= ~AT_SYMLINK_NOFOLLOW;
238 if (strcmp(*argv, "-O") == 0) {
239 mask &= ~STATX_BASIC_STATS;
242 if (strcmp(*argv, "-A") == 0) {
243 atflag |= AT_NO_AUTOMOUNT;
246 if (strcmp(*argv, "-R") == 0) {
251 memset(&stx, 0xbf, sizeof(stx));
252 ret = statx(AT_FDCWD, *argv, atflag, mask, &stx);
253 printf("statx(%s) = %d\n", *argv, ret);
260 dump_hex((unsigned long long *)&stx, 0, sizeof(stx));