1 /* Test the statx() system call.
3 * Note that the output of this program is intended to look like the output of
4 * /bin/stat where possible.
6 * Copyright (C) 2015 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
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.
16 #define _ATFILE_SOURCE
24 #include <sys/syscall.h>
25 #include <sys/types.h>
26 #include <linux/stat.h>
27 #include <linux/fcntl.h>
29 #define statx_timestamp foo_timestamp
32 #undef statx_timestamp
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
43 static __attribute__((unused))
44 ssize_t statx(int dfd, const char *filename, unsigned flags,
45 unsigned int mask, struct statx *buffer)
47 return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
50 static void print_time(const char *field, struct statx_timestamp *ts)
58 if (!localtime_r(&tim, &tm)) {
59 perror("localtime_r");
62 len = strftime(buffer, 100, "%F %T", &tm);
68 fwrite(buffer, 1, len, stdout);
69 printf(".%09u", ts->tv_nsec);
70 len = strftime(buffer, 100, "%z", &tm);
75 fwrite(buffer, 1, len, stdout);
79 static void dump_statx(struct statx *stx)
81 char buffer[256], ft = '?';
83 printf("results=%x\n", stx->stx_mask);
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;
101 printf(" unknown type (%o)\n", stx->stx_mode & S_IFMT);
105 printf(" no type\n");
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) {
118 printf(" Device type: %u,%u",
119 stx->stx_rdev_major, stx->stx_rdev_minor);
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,
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);
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);
152 if (stx->stx_attributes_mask) {
153 unsigned char bits, mbits;
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 */
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;
177 putchar('.'); /* Not supported */
178 else if (bits & 0x80)
179 putchar(attr_representation[63 - bit]);
181 putchar('-'); /* Not set */
192 static void dump_hex(unsigned long long *data, int from, int to)
194 unsigned offset, print_offset = 1, col = 0;
199 for (offset = from; offset < to; offset++) {
201 printf("%04x: ", offset * 8);
204 printf("%016llx", data[offset]);
206 if ((col & 3) == 0) {
218 int main(int argc, char **argv)
221 int ret, raw = 0, atflag = AT_SYMLINK_NOFOLLOW;
223 unsigned int mask = STATX_ALL;
225 for (argv++; *argv; argv++) {
226 if (strcmp(*argv, "-F") == 0) {
227 atflag &= ~AT_STATX_SYNC_TYPE;
228 atflag |= AT_STATX_FORCE_SYNC;
231 if (strcmp(*argv, "-D") == 0) {
232 atflag &= ~AT_STATX_SYNC_TYPE;
233 atflag |= AT_STATX_DONT_SYNC;
236 if (strcmp(*argv, "-L") == 0) {
237 atflag &= ~AT_SYMLINK_NOFOLLOW;
240 if (strcmp(*argv, "-O") == 0) {
241 mask &= ~STATX_BASIC_STATS;
244 if (strcmp(*argv, "-A") == 0) {
245 atflag |= AT_NO_AUTOMOUNT;
248 if (strcmp(*argv, "-R") == 0) {
253 memset(&stx, 0xbf, sizeof(stx));
254 ret = statx(AT_FDCWD, *argv, atflag, mask, &stx);
255 printf("statx(%s) = %d\n", *argv, ret);
262 dump_hex((unsigned long long *)&stx, 0, sizeof(stx));