]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/stat-util.h
Merge pull request #30513 from rpigott/resolved-ede
[thirdparty/systemd.git] / src / basic / stat-util.h
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 #include "siphash24.h"
15
16 int is_symlink(const char *path);
17 int is_dir_full(int atfd, const char *fname, bool follow);
18 static inline int is_dir(const char *path, bool follow) {
19 return is_dir_full(AT_FDCWD, path, follow);
20 }
21 static inline int is_dir_fd(int fd) {
22 return is_dir_full(fd, NULL, false);
23 }
24 int is_device_node(const char *path);
25
26 int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup);
27 static inline int dir_is_empty(const char *path, bool ignore_hidden_or_backup) {
28 return dir_is_empty_at(AT_FDCWD, path, ignore_hidden_or_backup);
29 }
30
31 bool null_or_empty(struct stat *st) _pure_;
32 int null_or_empty_path_with_root(const char *fn, const char *root);
33
34 static inline int null_or_empty_path(const char *fn) {
35 return null_or_empty_path_with_root(fn, NULL);
36 }
37
38 int path_is_read_only_fs(const char *path);
39
40 int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags);
41
42 static inline int inode_same(const char *filea, const char *fileb, int flags) {
43 return inode_same_at(AT_FDCWD, filea, AT_FDCWD, fileb, flags);
44 }
45
46 /* The .f_type field of struct statfs is really weird defined on
47 * different archs. Let's give its type a name. */
48 typedef typeof(((struct statfs*)NULL)->f_type) statfs_f_type_t;
49
50 bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_;
51 int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value);
52 static inline int fd_is_fs_type(int fd, statfs_f_type_t magic_value) {
53 return is_fs_type_at(fd, NULL, magic_value);
54 }
55 static inline int path_is_fs_type(const char *path, statfs_f_type_t magic_value) {
56 return is_fs_type_at(AT_FDCWD, path, magic_value);
57 }
58
59 bool is_temporary_fs(const struct statfs *s) _pure_;
60 bool is_network_fs(const struct statfs *s) _pure_;
61
62 int fd_is_temporary_fs(int fd);
63 int fd_is_network_fs(int fd);
64
65 int path_is_temporary_fs(const char *path);
66 int path_is_network_fs(const char *path);
67
68 /* Because statfs.t_type can be int on some architectures, we have to cast
69 * the const magic to the type, otherwise the compiler warns about
70 * signed/unsigned comparison, because the magic can be 32 bit unsigned.
71 */
72 #define F_TYPE_EQUAL(a, b) (a == (typeof(a)) b)
73
74 int stat_verify_regular(const struct stat *st);
75 int fd_verify_regular(int fd);
76 int verify_regular_at(int dir_fd, const char *path, bool follow);
77
78 int stat_verify_directory(const struct stat *st);
79 int fd_verify_directory(int fd);
80
81 int proc_mounted(void);
82
83 bool stat_inode_same(const struct stat *a, const struct stat *b);
84 bool stat_inode_unmodified(const struct stat *a, const struct stat *b);
85
86 bool statx_inode_same(const struct statx *a, const struct statx *b);
87 bool statx_mount_same(const struct new_statx *a, const struct new_statx *b);
88
89 int statx_fallback(int dfd, const char *path, int flags, unsigned mask, struct statx *sx);
90
91 int xstatfsat(int dir_fd, const char *path, struct statfs *ret);
92
93 #if HAS_FEATURE_MEMORY_SANITIZER
94 # warning "Explicitly initializing struct statx, to work around msan limitation. Please remove as soon as msan has been updated to not require this."
95 # define STRUCT_STATX_DEFINE(var) \
96 struct statx var = {}
97 # define STRUCT_NEW_STATX_DEFINE(var) \
98 union { \
99 struct statx sx; \
100 struct new_statx nsx; \
101 } var = {}
102 #else
103 # define STRUCT_STATX_DEFINE(var) \
104 struct statx var
105 # define STRUCT_NEW_STATX_DEFINE(var) \
106 union { \
107 struct statx sx; \
108 struct new_statx nsx; \
109 } var
110 #endif
111
112 void inode_hash_func(const struct stat *q, struct siphash *state);
113 int inode_compare_func(const struct stat *a, const struct stat *b);
114 extern const struct hash_ops inode_hash_ops;
115
116 const char* inode_type_to_string(mode_t m);
117 mode_t inode_type_from_string(const char *s);