1 | /* Generic implementation of statx based on fstatat64. |
2 | Copyright (C) 2018-2019 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <fcntl.h> |
21 | #include <sys/stat.h> |
22 | #include <sys/sysmacros.h> |
23 | |
24 | static inline struct statx_timestamp |
25 | statx_convert_timestamp (struct timespec tv) |
26 | { |
27 | return (struct statx_timestamp) { tv.tv_sec, tv.tv_nsec }; |
28 | } |
29 | |
30 | /* Approximate emulation of statx. This will always fill in |
31 | POSIX-mandated attributes even if the underlying file system does |
32 | not actually support it (for example, GID and UID on file systems |
33 | without UNIX-style permissions). */ |
34 | static __attribute__ ((unused)) int |
35 | statx_generic (int fd, const char *path, int flags, |
36 | unsigned int mask, struct statx *buf) |
37 | { |
38 | /* Flags which need to be cleared before passing them to |
39 | fstatat64. */ |
40 | static const int clear_flags = AT_STATX_SYNC_AS_STAT; |
41 | |
42 | /* Flags supported by our emulation. */ |
43 | static const int supported_flags |
44 | = AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW | clear_flags; |
45 | |
46 | if (__glibc_unlikely ((flags & ~supported_flags) != 0)) |
47 | { |
48 | __set_errno (EINVAL); |
49 | return -1; |
50 | } |
51 | |
52 | struct stat64 st; |
53 | int ret = __fstatat64 (fd, path, &st, flags & ~clear_flags); |
54 | if (ret != 0) |
55 | return ret; |
56 | |
57 | /* The interface is defined in such a way that unused (padding) |
58 | fields have to be cleared. STATX_BASIC_STATS corresponds to the |
59 | data which is available via fstatat64. */ |
60 | *buf = (struct statx) |
61 | { |
62 | .stx_mask = STATX_BASIC_STATS, |
63 | .stx_blksize = st.st_blksize, |
64 | .stx_nlink = st.st_nlink, |
65 | .stx_uid = st.st_uid, |
66 | .stx_gid = st.st_gid, |
67 | .stx_mode = st.st_mode, |
68 | .stx_ino = st.st_ino, |
69 | .stx_size = st.st_size, |
70 | .stx_blocks = st.st_blocks, |
71 | .stx_atime = statx_convert_timestamp (st.st_atim), |
72 | .stx_ctime = statx_convert_timestamp (st.st_ctim), |
73 | .stx_mtime = statx_convert_timestamp (st.st_mtim), |
74 | .stx_rdev_major = major (st.st_rdev), |
75 | .stx_rdev_minor = minor (st.st_rdev), |
76 | .stx_dev_major = major (st.st_dev), |
77 | .stx_dev_minor = minor (st.st_dev), |
78 | }; |
79 | |
80 | return 0; |
81 | } |
82 | |