1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <fcntl.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <sys/stat.h>
8 #include <sys/statfs.h>
9 #include <sys/types.h>
10 #include <sys/vfs.h>
11
12 #include "macro.h"
13 #include "missing_stat.h"
14
15 int is_symlink(const char *path);
16 int is_dir(const char *path, bool follow);
17 int is_dir_fd(int fd);
18 int is_device_node(const char *path);
19
20 int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup);
dir_is_empty(const char * path,bool ignore_hidden_or_backup)21 static inline int dir_is_empty(const char *path, bool ignore_hidden_or_backup) {
22 return dir_is_empty_at(AT_FDCWD, path, ignore_hidden_or_backup);
23 }
24
25 bool null_or_empty(struct stat *st) _pure_;
26 int null_or_empty_path_with_root(const char *fn, const char *root);
27 int null_or_empty_fd(int fd);
28
null_or_empty_path(const char * fn)29 static inline int null_or_empty_path(const char *fn) {
30 return null_or_empty_path_with_root(fn, NULL);
31 }
32
33 int path_is_read_only_fs(const char *path);
34
35 int files_same(const char *filea, const char *fileb, int flags);
36
37 /* The .f_type field of struct statfs is really weird defined on
38 * different archs. Let's give its type a name. */
39 typedef typeof(((struct statfs*)NULL)->f_type) statfs_f_type_t;
40
41 bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_;
42 int fd_is_fs_type(int fd, statfs_f_type_t magic_value);
43 int path_is_fs_type(const char *path, statfs_f_type_t magic_value);
44
45 bool is_temporary_fs(const struct statfs *s) _pure_;
46 bool is_network_fs(const struct statfs *s) _pure_;
47
48 int fd_is_temporary_fs(int fd);
49 int fd_is_network_fs(int fd);
50
51 int path_is_temporary_fs(const char *path);
52 int path_is_network_fs(const char *path);
53
54 /* Because statfs.t_type can be int on some architectures, we have to cast
55 * the const magic to the type, otherwise the compiler warns about
56 * signed/unsigned comparison, because the magic can be 32 bit unsigned.
57 */
58 #define F_TYPE_EQUAL(a, b) (a == (typeof(a)) b)
59
60 int stat_verify_regular(const struct stat *st);
61 int fd_verify_regular(int fd);
62
63 int stat_verify_directory(const struct stat *st);
64 int fd_verify_directory(int fd);
65
66 int proc_mounted(void);
67
68 bool stat_inode_same(const struct stat *a, const struct stat *b);
69 bool stat_inode_unmodified(const struct stat *a, const struct stat *b);
70
71 int statx_fallback(int dfd, const char *path, int flags, unsigned mask, struct statx *sx);
72
73 #if HAS_FEATURE_MEMORY_SANITIZER
74 # warning "Explicitly initializing struct statx, to work around msan limitation. Please remove as soon as msan has been updated to not require this."
75 # define STRUCT_STATX_DEFINE(var) \
76 struct statx var = {}
77 # define STRUCT_NEW_STATX_DEFINE(var) \
78 union { \
79 struct statx sx; \
80 struct new_statx nsx; \
81 } var = {}
82 #else
83 # define STRUCT_STATX_DEFINE(var) \
84 struct statx var
85 # define STRUCT_NEW_STATX_DEFINE(var) \
86 union { \
87 struct statx sx; \
88 struct new_statx nsx; \
89 } var
90 #endif
91