Debian Codegraph @

src/basic/stat-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <sys/stat.h> /* IWYU pragma: export */
5 #include <sys/statfs.h> /* IWYU pragma: export */
6
7 #include "basic-forward.h"
8
9 int stat_verify_regular(const struct stat *st);
10 int statx_verify_regular(const struct statx *stx);
11 int verify_regular_at(int fd, const char *path, bool follow);
12 int fd_verify_regular(int fd);
13
14 int stat_verify_directory(const struct stat *st);
15 int statx_verify_directory(const struct statx *stx);
16 int fd_verify_directory(int fd);
17 int is_dir_at(int fd, const char *path, bool follow);
18 int is_dir(const char *path, bool follow);
19
20 int stat_verify_symlink(const struct stat *st);
21 int fd_verify_symlink(int fd);
22 int is_symlink(const char *path);
23
24 int stat_verify_socket(const struct stat *st);
25 int statx_verify_socket(const struct statx *stx);
26 int fd_verify_socket(int fd);
27 int is_socket(const char *path);
28
29 int stat_verify_linked(const struct stat *st);
30 int fd_verify_linked(int fd);
31
32 int stat_verify_block(const struct stat *st);
33 int fd_verify_block(int fd);
34
35 int stat_verify_char(const struct stat *st);
36
37 int stat_verify_device_node(const struct stat *st);
38 int is_device_node(const char *path);
39
40 int stat_verify_regular_or_block(const struct stat *st);
41 int fd_verify_regular_or_block(int fd);
42
43 int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup);
44 static inline int dir_is_empty(const char *path, bool ignore_hidden_or_backup) {
45 return dir_is_empty_at(AT_FDCWD, path, ignore_hidden_or_backup);
46 }
47
48 bool stat_may_be_dev_null(struct stat *st) _pure_;
49 bool stat_is_empty(struct stat *st) _pure_;
50 static inline bool null_or_empty(struct stat *st) {
51 return stat_may_be_dev_null(st) || stat_is_empty(st);
52 }
53 int null_or_empty_path_with_root(const char *fn, const char *root);
54
55 static inline int null_or_empty_path(const char *fn) {
56 return null_or_empty_path_with_root(fn, NULL);
57 }
58
59 typedef enum XStatXFlags {
60 XSTATX_MNT_ID_BEST = 1 << 0, /* Like STATX_MNT_ID_UNIQUE if available, STATX_MNT_ID otherwise */
61 } XStatXFlags;
62
63 int xstatx_full(int fd,
64 const char *path,
65 int statx_flags,
66 XStatXFlags xstatx_flags,
67 unsigned mandatory_mask,
68 unsigned optional_mask,
69 uint64_t mandatory_attributes,
70 struct statx *ret);
71
72 static inline int xstatx(
73 int fd,
74 const char *path,
75 int statx_flags,
76 unsigned mandatory_mask,
77 struct statx *ret) {
78
79 return xstatx_full(fd, path, statx_flags, 0, mandatory_mask, 0, 0, ret);
80 }
81
82 int fd_is_read_only_fs(int fd);
83 int path_is_read_only_fs(const char *path);
84
85 int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags);
86 static inline int inode_same(const char *filea, const char *fileb, int flags) {
87 return inode_same_at(AT_FDCWD, filea, AT_FDCWD, fileb, flags);
88 }
89 static inline int fd_inode_same(int fda, int fdb) {
90 return inode_same_at(fda, NULL, fdb, NULL, AT_EMPTY_PATH);
91 }
92
93 /* The .f_type field of struct statfs is really weird defined on
94 * different archs. Let's give its type a name. */
95 typedef typeof_field(struct statfs, f_type) statfs_f_type_t;
96
97 bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_;
98 int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value);
99 static inline int fd_is_fs_type(int fd, statfs_f_type_t magic_value) {
100 return is_fs_type_at(fd, NULL, magic_value);
101 }
102 static inline int path_is_fs_type(const char *path, statfs_f_type_t magic_value) {
103 return is_fs_type_at(AT_FDCWD, path, magic_value);
104 }
105
106 bool is_temporary_fs(const struct statfs *s) _pure_;
107 bool is_network_fs(const struct statfs *s) _pure_;
108
109 int fd_is_temporary_fs(int fd);
110 int fd_is_network_fs(int fd);
111
112 int path_is_temporary_fs(const char *path);
113 int path_is_network_fs(const char *path);
114
115 /* Because statfs.t_type can be int on some architectures, we have to cast
116 * the const magic to the type, otherwise the compiler warns about
117 * signed/unsigned comparison, because the magic can be 32 bit unsigned.
118 */
119 #define F_TYPE_EQUAL(a, b) (a == (typeof(a)) b)
120
121 int proc_mounted(void);
122
123 bool stat_inode_same(const struct stat *a, const struct stat *b);
124 bool stat_inode_unmodified(const struct stat *a, const struct stat *b);
125
126 bool statx_inode_same(const struct statx *a, const struct statx *b);
127 int statx_mount_same(const struct statx *a, const struct statx *b);
128
129 int xstatfsat(int dir_fd, const char *path, struct statfs *ret);
130
131 usec_t statx_timestamp_load(const struct statx_timestamp *ts) _pure_;
132 nsec_t statx_timestamp_load_nsec(const struct statx_timestamp *ts) _pure_;
133
134 /* This compares inode number, backing device and inode type, but not modification info */
135 void inode_hash_func(const struct stat *q, struct siphash *state);
136 int inode_compare_func(const struct stat *a, const struct stat *b);
137 extern const struct hash_ops inode_hash_ops;
138
139 /* This is a more thorough version of the above, and also checks the mtimes, the size, and the rdev. It does
140 * not check "external" attributes such as access mode or ownership. */
141 void inode_unmodified_hash_func(const struct stat *q, struct siphash *state);
142 int inode_unmodified_compare_func(const struct stat *a, const struct stat *b);
143 extern const struct hash_ops inode_unmodified_hash_ops;
144
145 DECLARE_STRING_TABLE_LOOKUP(inode_type, mode_t);
146
147 /* Macros that check whether the stat/statx structures have been initialized already. For "struct stat" we
148 * use a check for .st_dev being non-zero, since the kernel unconditionally fills that in, mapping the file
149 * to its originating superblock, regardless if the fs is block based or virtual (we also check for .st_mode
150 * being MODE_INVALID, since we use that as an invalid marker for separate mode_t fields). For "struct statx"
151 * we use the .stx_mask field, which must be non-zero if any of the fields have already been initialized. */
152 static inline bool stat_is_set(const struct stat *st) {
153 return st && st->st_dev != 0 && st->st_mode != MODE_INVALID;
154 }
155 static inline bool statx_is_set(const struct statx *sx) {
156 return sx && sx->stx_mask != 0;
157 }
158
159 static inline bool inode_type_can_hardlink(mode_t m) {
160 /* returns true for all inode types that support hardlinks on linux. Note this is effectively all
161 * inode types except for directories (and those weird misc fds such as eventfds() that have no inode
162 * type). */
163 return IN_SET(m & S_IFMT, S_IFSOCK, S_IFLNK, S_IFREG, S_IFBLK, S_IFCHR, S_IFIFO);
164 }
165
166 int vfs_free_bytes(int fd, uint64_t *ret);