udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / stat-util.c
blobc54374b2c99adeb8b3d05cecdde683ee5803cd08
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <sched.h>
6 #include <sys/statvfs.h>
7 #include <sys/types.h>
8 #include <unistd.h>
10 #include "alloc-util.h"
11 #include "chase.h"
12 #include "dirent-util.h"
13 #include "errno-util.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "filesystems.h"
17 #include "fs-util.h"
18 #include "hash-funcs.h"
19 #include "macro.h"
20 #include "missing_fs.h"
21 #include "missing_magic.h"
22 #include "missing_syscall.h"
23 #include "nulstr-util.h"
24 #include "parse-util.h"
25 #include "stat-util.h"
26 #include "string-util.h"
28 int is_symlink(const char *path) {
29 struct stat info;
31 assert(path);
33 if (lstat(path, &info) < 0)
34 return -errno;
36 return !!S_ISLNK(info.st_mode);
39 int is_dir_full(int atfd, const char* path, bool follow) {
40 struct stat st;
41 int r;
43 assert(atfd >= 0 || atfd == AT_FDCWD);
44 assert(atfd >= 0 || path);
46 if (path)
47 r = fstatat(atfd, path, &st, follow ? 0 : AT_SYMLINK_NOFOLLOW);
48 else
49 r = fstat(atfd, &st);
50 if (r < 0)
51 return -errno;
53 return !!S_ISDIR(st.st_mode);
56 int is_device_node(const char *path) {
57 struct stat info;
59 assert(path);
61 if (lstat(path, &info) < 0)
62 return -errno;
64 return !!(S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode));
67 int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) {
68 _cleanup_close_ int fd = -EBADF;
69 struct dirent *buf;
70 size_t m;
72 if (path) {
73 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
75 fd = openat(dir_fd, path, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
76 if (fd < 0)
77 return -errno;
78 } else if (dir_fd == AT_FDCWD) {
79 fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
80 if (fd < 0)
81 return -errno;
82 } else {
83 /* Note that DUPing is not enough, as the internal pointer would still be shared and moved
84 * getedents64(). */
85 assert(dir_fd >= 0);
87 fd = fd_reopen(dir_fd, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
88 if (fd < 0)
89 return fd;
92 /* Allocate space for at least 3 full dirents, since every dir has at least two entries ("." +
93 * ".."), and only once we have seen if there's a third we know whether the dir is empty or not. If
94 * 'ignore_hidden_or_backup' is true we'll allocate a bit more, since we might skip over a bunch of
95 * entries that we end up ignoring. */
96 m = (ignore_hidden_or_backup ? 16 : 3) * DIRENT_SIZE_MAX;
97 buf = alloca(m);
99 for (;;) {
100 struct dirent *de;
101 ssize_t n;
103 n = getdents64(fd, buf, m);
104 if (n < 0)
105 return -errno;
106 if (n == 0)
107 break;
109 assert((size_t) n <= m);
110 msan_unpoison(buf, n);
112 FOREACH_DIRENT_IN_BUFFER(de, buf, n)
113 if (!(ignore_hidden_or_backup ? hidden_or_backup_file(de->d_name) : dot_or_dot_dot(de->d_name)))
114 return 0;
117 return 1;
120 bool null_or_empty(struct stat *st) {
121 assert(st);
123 if (S_ISREG(st->st_mode) && st->st_size <= 0)
124 return true;
126 /* We don't want to hardcode the major/minor of /dev/null, hence we do a simpler "is this a character
127 * device node?" check. */
129 if (S_ISCHR(st->st_mode))
130 return true;
132 return false;
135 int null_or_empty_path_with_root(const char *fn, const char *root) {
136 struct stat st;
137 int r;
139 assert(fn);
141 /* A symlink to /dev/null or an empty file?
142 * When looking under root_dir, we can't expect /dev/ to be mounted,
143 * so let's see if the path is a (possibly dangling) symlink to /dev/null. */
145 if (path_equal_ptr(path_startswith(fn, root ?: "/"), "dev/null"))
146 return true;
148 r = chase_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st);
149 if (r < 0)
150 return r;
152 return null_or_empty(&st);
155 static int fd_is_read_only_fs(int fd) {
156 struct statvfs st;
158 assert(fd >= 0);
160 if (fstatvfs(fd, &st) < 0)
161 return -errno;
163 if (st.f_flag & ST_RDONLY)
164 return true;
166 /* On NFS, fstatvfs() might not reflect whether we can actually write to the remote share. Let's try
167 * again with access(W_OK) which is more reliable, at least sometimes. */
168 if (access_fd(fd, W_OK) == -EROFS)
169 return true;
171 return false;
174 int path_is_read_only_fs(const char *path) {
175 _cleanup_close_ int fd = -EBADF;
177 assert(path);
179 fd = open(path, O_CLOEXEC | O_PATH);
180 if (fd < 0)
181 return -errno;
183 return fd_is_read_only_fs(fd);
186 int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags) {
187 struct stat a, b;
189 assert(fda >= 0 || fda == AT_FDCWD);
190 assert(filea);
191 assert(fdb >= 0 || fdb == AT_FDCWD);
192 assert(fileb);
194 if (fstatat(fda, filea, &a, flags) < 0)
195 return log_debug_errno(errno, "Cannot stat %s: %m", filea);
197 if (fstatat(fdb, fileb, &b, flags) < 0)
198 return log_debug_errno(errno, "Cannot stat %s: %m", fileb);
200 return stat_inode_same(&a, &b);
203 bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
204 assert(s);
205 assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
207 return F_TYPE_EQUAL(s->f_type, magic_value);
210 int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value) {
211 struct statfs s;
212 int r;
214 r = xstatfsat(dir_fd, path, &s);
215 if (r < 0)
216 return r;
218 return is_fs_type(&s, magic_value);
221 bool is_temporary_fs(const struct statfs *s) {
222 return fs_in_group(s, FILESYSTEM_SET_TEMPORARY);
225 bool is_network_fs(const struct statfs *s) {
226 return fs_in_group(s, FILESYSTEM_SET_NETWORK);
229 int fd_is_temporary_fs(int fd) {
230 struct statfs s;
232 if (fstatfs(fd, &s) < 0)
233 return -errno;
235 return is_temporary_fs(&s);
238 int fd_is_network_fs(int fd) {
239 struct statfs s;
241 if (fstatfs(fd, &s) < 0)
242 return -errno;
244 return is_network_fs(&s);
247 int path_is_temporary_fs(const char *path) {
248 struct statfs s;
250 if (statfs(path, &s) < 0)
251 return -errno;
253 return is_temporary_fs(&s);
256 int path_is_network_fs(const char *path) {
257 struct statfs s;
259 if (statfs(path, &s) < 0)
260 return -errno;
262 return is_network_fs(&s);
265 int stat_verify_regular(const struct stat *st) {
266 assert(st);
268 /* Checks whether the specified stat() structure refers to a regular file. If not returns an appropriate error
269 * code. */
271 if (S_ISDIR(st->st_mode))
272 return -EISDIR;
274 if (S_ISLNK(st->st_mode))
275 return -ELOOP;
277 if (!S_ISREG(st->st_mode))
278 return -EBADFD;
280 return 0;
283 int fd_verify_regular(int fd) {
284 struct stat st;
286 assert(fd >= 0);
288 if (fstat(fd, &st) < 0)
289 return -errno;
291 return stat_verify_regular(&st);
294 int verify_regular_at(int dir_fd, const char *path, bool follow) {
295 struct stat st;
297 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
298 assert(path);
300 if (fstatat(dir_fd, path, &st, (isempty(path) ? AT_EMPTY_PATH : 0) | (follow ? 0 : AT_SYMLINK_NOFOLLOW)) < 0)
301 return -errno;
303 return stat_verify_regular(&st);
306 int stat_verify_directory(const struct stat *st) {
307 assert(st);
309 if (S_ISLNK(st->st_mode))
310 return -ELOOP;
312 if (!S_ISDIR(st->st_mode))
313 return -ENOTDIR;
315 return 0;
318 int fd_verify_directory(int fd) {
319 struct stat st;
321 assert(fd >= 0);
323 if (fstat(fd, &st) < 0)
324 return -errno;
326 return stat_verify_directory(&st);
329 int proc_mounted(void) {
330 int r;
332 /* A quick check of procfs is properly mounted */
334 r = path_is_fs_type("/proc/", PROC_SUPER_MAGIC);
335 if (r == -ENOENT) /* not mounted at all */
336 return false;
338 return r;
341 bool stat_inode_same(const struct stat *a, const struct stat *b) {
343 /* Returns if the specified stat structure references the same (though possibly modified) inode. Does
344 * a thorough check, comparing inode nr, backing device and if the inode is still of the same type. */
346 return a && b &&
347 (a->st_mode & S_IFMT) != 0 && /* We use the check for .st_mode if the structure was ever initialized */
348 ((a->st_mode ^ b->st_mode) & S_IFMT) == 0 && /* same inode type */
349 a->st_dev == b->st_dev &&
350 a->st_ino == b->st_ino;
353 bool stat_inode_unmodified(const struct stat *a, const struct stat *b) {
355 /* Returns if the specified stat structures reference the same, unmodified inode. This check tries to
356 * be reasonably careful when detecting changes: we check both inode and mtime, to cater for file
357 * systems where mtimes are fixed to 0 (think: ostree/nixos type installations). We also check file
358 * size, backing device, inode type and if this refers to a device not the major/minor.
360 * Note that we don't care if file attributes such as ownership or access mode change, this here is
361 * about contents of the file. The purpose here is to detect file contents changes, and nothing
362 * else. */
364 return stat_inode_same(a, b) &&
365 a->st_mtim.tv_sec == b->st_mtim.tv_sec &&
366 a->st_mtim.tv_nsec == b->st_mtim.tv_nsec &&
367 (!S_ISREG(a->st_mode) || a->st_size == b->st_size) && /* if regular file, compare file size */
368 (!(S_ISCHR(a->st_mode) || S_ISBLK(a->st_mode)) || a->st_rdev == b->st_rdev); /* if device node, also compare major/minor, because we can */
371 bool statx_inode_same(const struct statx *a, const struct statx *b) {
373 /* Same as stat_inode_same() but for struct statx */
375 return a && b &&
376 FLAGS_SET(a->stx_mask, STATX_TYPE|STATX_INO) && FLAGS_SET(b->stx_mask, STATX_TYPE|STATX_INO) &&
377 (a->stx_mode & S_IFMT) != 0 &&
378 ((a->stx_mode ^ b->stx_mode) & S_IFMT) == 0 &&
379 a->stx_dev_major == b->stx_dev_major &&
380 a->stx_dev_minor == b->stx_dev_minor &&
381 a->stx_ino == b->stx_ino;
384 bool statx_mount_same(const struct new_statx *a, const struct new_statx *b) {
385 if (!a || !b)
386 return false;
388 /* if we have the mount ID, that's all we need */
389 if (FLAGS_SET(a->stx_mask, STATX_MNT_ID) && FLAGS_SET(b->stx_mask, STATX_MNT_ID))
390 return a->stx_mnt_id == b->stx_mnt_id;
392 /* Otherwise, major/minor of backing device must match */
393 return a->stx_dev_major == b->stx_dev_major &&
394 a->stx_dev_minor == b->stx_dev_minor;
397 static bool is_statx_fatal_error(int err, int flags) {
398 assert(err < 0);
400 /* If statx() is not supported or if we see EPERM (which might indicate seccomp filtering or so),
401 * let's do a fallback. Note that on EACCES we'll not fall back, since that is likely an indication of
402 * fs access issues, which we should propagate. */
403 if (ERRNO_IS_NOT_SUPPORTED(err) || err == -EPERM)
404 return false;
406 /* When unsupported flags are specified, glibc's fallback function returns -EINVAL.
407 * See statx_generic() in glibc. */
408 if (err != -EINVAL)
409 return true;
411 if ((flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW | AT_STATX_SYNC_AS_STAT)) != 0)
412 return false; /* Unsupported flags are specified. Let's try to use our implementation. */
414 return true;
417 int statx_fallback(int dfd, const char *path, int flags, unsigned mask, struct statx *sx) {
418 static bool avoid_statx = false;
419 struct stat st;
420 int r;
422 if (!avoid_statx) {
423 r = RET_NERRNO(statx(dfd, path, flags, mask, sx));
424 if (r >= 0 || is_statx_fatal_error(r, flags))
425 return r;
427 avoid_statx = true;
430 /* Only do fallback if fstatat() supports the flag too, or if it's one of the sync flags, which are
431 * OK to ignore */
432 if ((flags & ~(AT_EMPTY_PATH|AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW|
433 AT_STATX_SYNC_AS_STAT|AT_STATX_FORCE_SYNC|AT_STATX_DONT_SYNC)) != 0)
434 return -EOPNOTSUPP;
436 if (fstatat(dfd, path, &st, flags & (AT_EMPTY_PATH|AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW)) < 0)
437 return -errno;
439 *sx = (struct statx) {
440 .stx_mask = STATX_TYPE|STATX_MODE|
441 STATX_NLINK|STATX_UID|STATX_GID|
442 STATX_ATIME|STATX_MTIME|STATX_CTIME|
443 STATX_INO|STATX_SIZE|STATX_BLOCKS,
444 .stx_blksize = st.st_blksize,
445 .stx_nlink = st.st_nlink,
446 .stx_uid = st.st_uid,
447 .stx_gid = st.st_gid,
448 .stx_mode = st.st_mode,
449 .stx_ino = st.st_ino,
450 .stx_size = st.st_size,
451 .stx_blocks = st.st_blocks,
452 .stx_rdev_major = major(st.st_rdev),
453 .stx_rdev_minor = minor(st.st_rdev),
454 .stx_dev_major = major(st.st_dev),
455 .stx_dev_minor = minor(st.st_dev),
456 .stx_atime.tv_sec = st.st_atim.tv_sec,
457 .stx_atime.tv_nsec = st.st_atim.tv_nsec,
458 .stx_mtime.tv_sec = st.st_mtim.tv_sec,
459 .stx_mtime.tv_nsec = st.st_mtim.tv_nsec,
460 .stx_ctime.tv_sec = st.st_ctim.tv_sec,
461 .stx_ctime.tv_nsec = st.st_ctim.tv_nsec,
464 return 0;
467 int xstatfsat(int dir_fd, const char *path, struct statfs *ret) {
468 _cleanup_close_ int fd = -EBADF;
470 assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
471 assert(ret);
473 fd = xopenat(dir_fd, path, O_PATH|O_CLOEXEC|O_NOCTTY, /* xopen_flags = */ 0, /* mode = */ 0);
474 if (fd < 0)
475 return fd;
477 return RET_NERRNO(fstatfs(fd, ret));
480 void inode_hash_func(const struct stat *q, struct siphash *state) {
481 siphash24_compress(&q->st_dev, sizeof(q->st_dev), state);
482 siphash24_compress(&q->st_ino, sizeof(q->st_ino), state);
485 int inode_compare_func(const struct stat *a, const struct stat *b) {
486 int r;
488 r = CMP(a->st_dev, b->st_dev);
489 if (r != 0)
490 return r;
492 return CMP(a->st_ino, b->st_ino);
495 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(inode_hash_ops, struct stat, inode_hash_func, inode_compare_func, free);
497 const char* inode_type_to_string(mode_t m) {
499 /* Returns a short string for the inode type. We use the same name as the underlying macros for each
500 * inode type. */
502 switch (m & S_IFMT) {
503 case S_IFREG:
504 return "reg";
505 case S_IFDIR:
506 return "dir";
507 case S_IFLNK:
508 return "lnk";
509 case S_IFCHR:
510 return "chr";
511 case S_IFBLK:
512 return "blk";
513 case S_IFIFO:
514 return "fifo";
515 case S_IFSOCK:
516 return "sock";
519 return NULL;