]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8fcde012 LP |
2 | |
3 | #include <fcntl.h> | |
3fc2a440 | 4 | #include <linux/magic.h> |
8fcde012 LP |
5 | #include <sys/statvfs.h> |
6 | #include <unistd.h> | |
7 | ||
846b3bd6 | 8 | #include "alloc-util.h" |
f461a28d | 9 | #include "chase.h" |
8fcde012 | 10 | #include "dirent-util.h" |
f4351959 | 11 | #include "errno-util.h" |
8fcde012 | 12 | #include "fd-util.h" |
659d1924 ILG |
13 | #include "filesystems.h" |
14 | #include "fs-util.h" | |
ddfdf86f | 15 | #include "hash-funcs.h" |
93a1f792 | 16 | #include "log.h" |
f663a15a | 17 | #include "mountpoint-util.h" |
0c15577a DDM |
18 | #include "path-util.h" |
19 | #include "siphash24.h" | |
8fcde012 LP |
20 | #include "stat-util.h" |
21 | #include "string-util.h" | |
0c15577a | 22 | #include "time-util.h" |
8fcde012 | 23 | |
2560dcbf MY |
24 | static int verify_stat_at( |
25 | int fd, | |
26 | const char *path, | |
27 | bool follow, | |
28 | int (*verify_func)(const struct stat *st), | |
29 | bool verify) { | |
8fcde012 | 30 | |
2560dcbf MY |
31 | struct stat st; |
32 | int r; | |
33 | ||
34 | assert(fd >= 0 || fd == AT_FDCWD); | |
35 | assert(!isempty(path) || !follow); | |
36 | assert(verify_func); | |
8fcde012 | 37 | |
2560dcbf MY |
38 | if (fstatat(fd, strempty(path), &st, |
39 | (isempty(path) ? AT_EMPTY_PATH : 0) | (follow ? 0 : AT_SYMLINK_NOFOLLOW)) < 0) | |
8fcde012 LP |
40 | return -errno; |
41 | ||
2560dcbf MY |
42 | r = verify_func(&st); |
43 | return verify ? r : r >= 0; | |
8fcde012 LP |
44 | } |
45 | ||
2560dcbf MY |
46 | int stat_verify_regular(const struct stat *st) { |
47 | assert(st); | |
48 | ||
49 | /* Checks whether the specified stat() structure refers to a regular file. If not returns an | |
50 | * appropriate error code. */ | |
8fcde012 | 51 | |
2560dcbf MY |
52 | if (S_ISDIR(st->st_mode)) |
53 | return -EISDIR; | |
8fcde012 | 54 | |
2560dcbf MY |
55 | if (S_ISLNK(st->st_mode)) |
56 | return -ELOOP; | |
57 | ||
58 | if (!S_ISREG(st->st_mode)) | |
59 | return -EBADFD; | |
8fcde012 | 60 | |
2560dcbf | 61 | return 0; |
a12e4ade FB |
62 | } |
63 | ||
2560dcbf MY |
64 | int verify_regular_at(int fd, const char *path, bool follow) { |
65 | return verify_stat_at(fd, path, follow, stat_verify_regular, true); | |
66 | } | |
8fcde012 | 67 | |
2560dcbf MY |
68 | int fd_verify_regular(int fd) { |
69 | assert(fd >= 0); | |
70 | return verify_regular_at(fd, NULL, false); | |
71 | } | |
8fcde012 | 72 | |
2560dcbf MY |
73 | int stat_verify_directory(const struct stat *st) { |
74 | assert(st); | |
75 | ||
76 | if (S_ISLNK(st->st_mode)) | |
77 | return -ELOOP; | |
78 | ||
79 | if (!S_ISDIR(st->st_mode)) | |
80 | return -ENOTDIR; | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
85 | int fd_verify_directory(int fd) { | |
86 | assert(fd >= 0); | |
87 | return verify_stat_at(fd, NULL, false, stat_verify_directory, true); | |
88 | } | |
89 | ||
90 | int is_dir_at(int fd, const char *path, bool follow) { | |
91 | return verify_stat_at(fd, path, follow, stat_verify_directory, false); | |
92 | } | |
93 | ||
94 | int is_dir(const char *path, bool follow) { | |
95 | assert(!isempty(path)); | |
96 | return is_dir_at(AT_FDCWD, path, follow); | |
97 | } | |
98 | ||
99 | int stat_verify_symlink(const struct stat *st) { | |
100 | assert(st); | |
101 | ||
102 | if (S_ISDIR(st->st_mode)) | |
103 | return -EISDIR; | |
104 | ||
105 | if (!S_ISLNK(st->st_mode)) | |
106 | return -ENOLINK; | |
107 | ||
108 | return 0; | |
109 | } | |
110 | ||
111 | int is_symlink(const char *path) { | |
112 | assert(!isempty(path)); | |
113 | return verify_stat_at(AT_FDCWD, path, false, stat_verify_symlink, false); | |
114 | } | |
115 | ||
116 | int stat_verify_linked(const struct stat *st) { | |
117 | assert(st); | |
118 | ||
119 | if (st->st_nlink <= 0) | |
120 | return -EIDRM; /* recognizable error. */ | |
121 | ||
122 | return 0; | |
123 | } | |
124 | ||
125 | int fd_verify_linked(int fd) { | |
126 | assert(fd >= 0); | |
127 | return verify_stat_at(fd, NULL, false, stat_verify_linked, true); | |
128 | } | |
129 | ||
130 | int stat_verify_device_node(const struct stat *st) { | |
131 | assert(st); | |
132 | ||
133 | if (S_ISLNK(st->st_mode)) | |
134 | return -ELOOP; | |
135 | ||
136 | if (S_ISDIR(st->st_mode)) | |
137 | return -EISDIR; | |
138 | ||
139 | if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode)) | |
140 | return -ENOTTY; | |
141 | ||
142 | return 0; | |
143 | } | |
8fcde012 | 144 | |
2560dcbf MY |
145 | int is_device_node(const char *path) { |
146 | assert(!isempty(path)); | |
147 | return verify_stat_at(AT_FDCWD, path, false, stat_verify_device_node, false); | |
8fcde012 LP |
148 | } |
149 | ||
db55bbf2 | 150 | int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) { |
254d1313 | 151 | _cleanup_close_ int fd = -EBADF; |
db55bbf2 LP |
152 | struct dirent *buf; |
153 | size_t m; | |
8fcde012 | 154 | |
e4722385 MY |
155 | fd = xopenat(dir_fd, path, O_DIRECTORY|O_CLOEXEC); |
156 | if (fd < 0) | |
157 | return fd; | |
d7c1a15e | 158 | |
db55bbf2 LP |
159 | /* Allocate space for at least 3 full dirents, since every dir has at least two entries ("." + |
160 | * ".."), and only once we have seen if there's a third we know whether the dir is empty or not. If | |
161 | * 'ignore_hidden_or_backup' is true we'll allocate a bit more, since we might skip over a bunch of | |
162 | * entries that we end up ignoring. */ | |
163 | m = (ignore_hidden_or_backup ? 16 : 3) * DIRENT_SIZE_MAX; | |
164 | buf = alloca(m); | |
165 | ||
166 | for (;;) { | |
167 | struct dirent *de; | |
168 | ssize_t n; | |
169 | ||
43aacae8 | 170 | n = getdents64(fd, buf, m); |
db55bbf2 LP |
171 | if (n < 0) |
172 | return -errno; | |
173 | if (n == 0) | |
174 | break; | |
8fcde012 | 175 | |
db55bbf2 LP |
176 | assert((size_t) n <= m); |
177 | msan_unpoison(buf, n); | |
0dbce03c | 178 | |
db55bbf2 LP |
179 | FOREACH_DIRENT_IN_BUFFER(de, buf, n) |
180 | if (!(ignore_hidden_or_backup ? hidden_or_backup_file(de->d_name) : dot_or_dot_dot(de->d_name))) | |
181 | return 0; | |
182 | } | |
8fcde012 LP |
183 | |
184 | return 1; | |
185 | } | |
186 | ||
9d0fed7b | 187 | bool stat_may_be_dev_null(struct stat *st) { |
8fcde012 LP |
188 | assert(st); |
189 | ||
29da4193 LP |
190 | /* We don't want to hardcode the major/minor of /dev/null, hence we do a simpler "is this a character |
191 | * device node?" check. */ | |
8fcde012 | 192 | |
9d0fed7b YW |
193 | return S_ISCHR(st->st_mode); |
194 | } | |
195 | ||
196 | bool stat_is_empty(struct stat *st) { | |
197 | assert(st); | |
8fcde012 | 198 | |
9d0fed7b | 199 | return S_ISREG(st->st_mode) && st->st_size <= 0; |
8fcde012 LP |
200 | } |
201 | ||
48542eac | 202 | int null_or_empty_path_with_root(const char *fn, const char *root) { |
8fcde012 | 203 | struct stat st; |
48542eac | 204 | int r; |
8fcde012 LP |
205 | |
206 | assert(fn); | |
207 | ||
48542eac ZJS |
208 | /* A symlink to /dev/null or an empty file? |
209 | * When looking under root_dir, we can't expect /dev/ to be mounted, | |
210 | * so let's see if the path is a (possibly dangling) symlink to /dev/null. */ | |
211 | ||
1934242b | 212 | if (path_equal(path_startswith(fn, root ?: "/"), "dev/null")) |
29da4193 LP |
213 | return true; |
214 | ||
f461a28d | 215 | r = chase_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st); |
48542eac ZJS |
216 | if (r < 0) |
217 | return r; | |
8fcde012 LP |
218 | |
219 | return null_or_empty(&st); | |
220 | } | |
221 | ||
d30d4488 | 222 | int fd_is_read_only_fs(int fd) { |
c136be3f | 223 | struct statfs st; |
8fcde012 | 224 | |
dc1752ea | 225 | assert(fd >= 0); |
8fcde012 | 226 | |
c136be3f | 227 | if (fstatfs(fd, &st) < 0) |
8fcde012 LP |
228 | return -errno; |
229 | ||
c136be3f | 230 | if (st.f_flags & ST_RDONLY) |
8fcde012 LP |
231 | return true; |
232 | ||
c136be3f ZJS |
233 | if (is_network_fs(&st)) { |
234 | /* On NFS, fstatfs() might not reflect whether we can actually write to the remote share. | |
235 | * Let's try again with access(W_OK) which is more reliable, at least sometimes. */ | |
236 | if (access_fd(fd, W_OK) == -EROFS) | |
237 | return true; | |
238 | } | |
8fcde012 LP |
239 | |
240 | return false; | |
241 | } | |
242 | ||
dc1752ea | 243 | int path_is_read_only_fs(const char *path) { |
a5937dcf | 244 | _cleanup_close_ int fd = -EBADF; |
dc1752ea YW |
245 | |
246 | assert(path); | |
247 | ||
248 | fd = open(path, O_CLOEXEC | O_PATH); | |
249 | if (fd < 0) | |
250 | return -errno; | |
251 | ||
252 | return fd_is_read_only_fs(fd); | |
253 | } | |
254 | ||
563e6846 | 255 | int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags) { |
f663a15a LP |
256 | struct stat sta, stb; |
257 | int r; | |
8fcde012 | 258 | |
676ade31 | 259 | assert(fda >= 0 || fda == AT_FDCWD); |
676ade31 | 260 | assert(fdb >= 0 || fdb == AT_FDCWD); |
f663a15a LP |
261 | assert((flags & ~(AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT)) == 0); |
262 | ||
263 | /* Refuse an unset filea or fileb early unless AT_EMPTY_PATH is set */ | |
264 | if ((isempty(filea) || isempty(fileb)) && !FLAGS_SET(flags, AT_EMPTY_PATH)) | |
265 | return -EINVAL; | |
266 | ||
267 | /* Shortcut: comparing the same fd with itself means we can return true */ | |
268 | if (fda >= 0 && fda == fdb && isempty(filea) && isempty(fileb) && FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW)) | |
269 | return true; | |
270 | ||
271 | _cleanup_close_ int pin_a = -EBADF, pin_b = -EBADF; | |
272 | if (!FLAGS_SET(flags, AT_NO_AUTOMOUNT)) { | |
273 | /* Let's try to use the name_to_handle_at() AT_HANDLE_FID API to identify identical | |
274 | * inodes. We have to issue multiple calls on the same file for that (first, to acquire the | |
275 | * FID, and then to check if .st_dev is actually the same). Hence let's pin the inode in | |
276 | * between via O_PATH, unless we already have an fd for it. */ | |
277 | ||
278 | if (!isempty(filea)) { | |
279 | pin_a = openat(fda, filea, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0)); | |
280 | if (pin_a < 0) | |
281 | return -errno; | |
282 | ||
283 | fda = pin_a; | |
284 | filea = NULL; | |
285 | flags |= AT_EMPTY_PATH; | |
286 | } | |
287 | ||
288 | if (!isempty(fileb)) { | |
289 | pin_b = openat(fdb, fileb, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0)); | |
290 | if (pin_b < 0) | |
291 | return -errno; | |
292 | ||
293 | fdb = pin_b; | |
294 | fileb = NULL; | |
295 | flags |= AT_EMPTY_PATH; | |
296 | } | |
297 | ||
4b11087b | 298 | int ntha_flags = at_flags_normalize_follow(flags) & (AT_EMPTY_PATH|AT_SYMLINK_FOLLOW); |
f663a15a LP |
299 | _cleanup_free_ struct file_handle *ha = NULL, *hb = NULL; |
300 | int mntida = -1, mntidb = -1; | |
301 | ||
302 | r = name_to_handle_at_try_fid( | |
303 | fda, | |
304 | filea, | |
305 | &ha, | |
306 | &mntida, | |
307 | ntha_flags); | |
308 | if (r < 0) { | |
309 | if (is_name_to_handle_at_fatal_error(r)) | |
310 | return r; | |
311 | ||
312 | goto fallback; | |
313 | } | |
314 | ||
315 | r = name_to_handle_at_try_fid( | |
316 | fdb, | |
317 | fileb, | |
318 | &hb, | |
319 | &mntidb, | |
320 | ntha_flags); | |
321 | if (r < 0) { | |
322 | if (is_name_to_handle_at_fatal_error(r)) | |
323 | return r; | |
324 | ||
325 | goto fallback; | |
326 | } | |
327 | ||
328 | /* Now compare the two file handles */ | |
329 | if (!file_handle_equal(ha, hb)) | |
330 | return false; | |
331 | ||
332 | /* If the file handles are the same and they come from the same mount ID? Great, then we are | |
333 | * good, they are definitely the same */ | |
334 | if (mntida == mntidb) | |
335 | return true; | |
336 | ||
337 | /* File handles are the same, they are not on the same mount id. This might either be because | |
338 | * they are on two entirely different file systems, that just happen to have the same FIDs | |
339 | * (because they originally where created off the same disk images), or it could be because | |
340 | * they are located on two distinct bind mounts of the same fs. To check that, let's look at | |
341 | * .st_rdev of the inode. We simply reuse the fallback codepath for that, since it checks | |
342 | * exactly that (it checks slightly more, but we don't care.) */ | |
343 | } | |
8fcde012 | 344 | |
f663a15a LP |
345 | fallback: |
346 | if (fstatat(fda, strempty(filea), &sta, flags) < 0) | |
347 | return log_debug_errno(errno, "Cannot stat %s: %m", strna(filea)); | |
8fcde012 | 348 | |
f663a15a LP |
349 | if (fstatat(fdb, strempty(fileb), &stb, flags) < 0) |
350 | return log_debug_errno(errno, "Cannot stat %s: %m", strna(fileb)); | |
8fcde012 | 351 | |
f663a15a | 352 | return stat_inode_same(&sta, &stb); |
8fcde012 LP |
353 | } |
354 | ||
355 | bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) { | |
356 | assert(s); | |
357 | assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type)); | |
358 | ||
359 | return F_TYPE_EQUAL(s->f_type, magic_value); | |
360 | } | |
361 | ||
b196e17e | 362 | int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value) { |
6d965610 | 363 | struct statfs s; |
b196e17e | 364 | int r; |
8fcde012 | 365 | |
b196e17e DDM |
366 | r = xstatfsat(dir_fd, path, &s); |
367 | if (r < 0) | |
368 | return r; | |
8fcde012 | 369 | |
6d965610 | 370 | return is_fs_type(&s, magic_value); |
8fcde012 LP |
371 | } |
372 | ||
373 | bool is_temporary_fs(const struct statfs *s) { | |
659d1924 | 374 | return fs_in_group(s, FILESYSTEM_SET_TEMPORARY); |
77f9fa3b LP |
375 | } |
376 | ||
377 | bool is_network_fs(const struct statfs *s) { | |
659d1924 | 378 | return fs_in_group(s, FILESYSTEM_SET_NETWORK); |
8fcde012 LP |
379 | } |
380 | ||
381 | int fd_is_temporary_fs(int fd) { | |
382 | struct statfs s; | |
383 | ||
384 | if (fstatfs(fd, &s) < 0) | |
385 | return -errno; | |
386 | ||
387 | return is_temporary_fs(&s); | |
388 | } | |
ffeb8285 | 389 | |
77f9fa3b LP |
390 | int fd_is_network_fs(int fd) { |
391 | struct statfs s; | |
392 | ||
393 | if (fstatfs(fd, &s) < 0) | |
394 | return -errno; | |
395 | ||
396 | return is_network_fs(&s); | |
397 | } | |
398 | ||
ffeb8285 | 399 | int path_is_temporary_fs(const char *path) { |
15308e50 | 400 | struct statfs s; |
ffeb8285 | 401 | |
15308e50 | 402 | if (statfs(path, &s) < 0) |
ffeb8285 LP |
403 | return -errno; |
404 | ||
15308e50 | 405 | return is_temporary_fs(&s); |
ffeb8285 | 406 | } |
3cc44114 | 407 | |
4e247216 YW |
408 | int path_is_network_fs(const char *path) { |
409 | struct statfs s; | |
410 | ||
411 | if (statfs(path, &s) < 0) | |
412 | return -errno; | |
413 | ||
414 | return is_network_fs(&s); | |
415 | } | |
416 | ||
883fff25 LP |
417 | int proc_mounted(void) { |
418 | int r; | |
419 | ||
420 | /* A quick check of procfs is properly mounted */ | |
421 | ||
422 | r = path_is_fs_type("/proc/", PROC_SUPER_MAGIC); | |
423 | if (r == -ENOENT) /* not mounted at all */ | |
424 | return false; | |
425 | ||
426 | return r; | |
427 | } | |
fee5c52a | 428 | |
38db6211 LP |
429 | bool stat_inode_same(const struct stat *a, const struct stat *b) { |
430 | ||
431 | /* Returns if the specified stat structure references the same (though possibly modified) inode. Does | |
432 | * a thorough check, comparing inode nr, backing device and if the inode is still of the same type. */ | |
433 | ||
1c248d7f | 434 | return stat_is_set(a) && stat_is_set(b) && |
38db6211 LP |
435 | ((a->st_mode ^ b->st_mode) & S_IFMT) == 0 && /* same inode type */ |
436 | a->st_dev == b->st_dev && | |
437 | a->st_ino == b->st_ino; | |
438 | } | |
439 | ||
fee5c52a LP |
440 | bool stat_inode_unmodified(const struct stat *a, const struct stat *b) { |
441 | ||
442 | /* Returns if the specified stat structures reference the same, unmodified inode. This check tries to | |
443 | * be reasonably careful when detecting changes: we check both inode and mtime, to cater for file | |
444 | * systems where mtimes are fixed to 0 (think: ostree/nixos type installations). We also check file | |
445 | * size, backing device, inode type and if this refers to a device not the major/minor. | |
446 | * | |
447 | * Note that we don't care if file attributes such as ownership or access mode change, this here is | |
448 | * about contents of the file. The purpose here is to detect file contents changes, and nothing | |
449 | * else. */ | |
450 | ||
38db6211 | 451 | return stat_inode_same(a, b) && |
a59b0a9f MS |
452 | a->st_mtim.tv_sec == b->st_mtim.tv_sec && |
453 | a->st_mtim.tv_nsec == b->st_mtim.tv_nsec && | |
fee5c52a | 454 | (!S_ISREG(a->st_mode) || a->st_size == b->st_size) && /* if regular file, compare file size */ |
fee5c52a LP |
455 | (!(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 */ |
456 | } | |
ca194a2a | 457 | |
2bd315fb LP |
458 | bool statx_inode_same(const struct statx *a, const struct statx *b) { |
459 | ||
460 | /* Same as stat_inode_same() but for struct statx */ | |
461 | ||
1c248d7f | 462 | return statx_is_set(a) && statx_is_set(b) && |
2bd315fb | 463 | FLAGS_SET(a->stx_mask, STATX_TYPE|STATX_INO) && FLAGS_SET(b->stx_mask, STATX_TYPE|STATX_INO) && |
2bd315fb LP |
464 | ((a->stx_mode ^ b->stx_mode) & S_IFMT) == 0 && |
465 | a->stx_dev_major == b->stx_dev_major && | |
466 | a->stx_dev_minor == b->stx_dev_minor && | |
467 | a->stx_ino == b->stx_ino; | |
468 | } | |
469 | ||
4424e6c8 YW |
470 | bool statx_mount_same(const struct statx *a, const struct statx *b) { |
471 | if (!statx_is_set(a) || !statx_is_set(b)) | |
52f19d96 LP |
472 | return false; |
473 | ||
474 | /* if we have the mount ID, that's all we need */ | |
475 | if (FLAGS_SET(a->stx_mask, STATX_MNT_ID) && FLAGS_SET(b->stx_mask, STATX_MNT_ID)) | |
476 | return a->stx_mnt_id == b->stx_mnt_id; | |
477 | ||
478 | /* Otherwise, major/minor of backing device must match */ | |
479 | return a->stx_dev_major == b->stx_dev_major && | |
480 | a->stx_dev_minor == b->stx_dev_minor; | |
481 | } | |
482 | ||
6eec59f9 DDM |
483 | int xstatfsat(int dir_fd, const char *path, struct statfs *ret) { |
484 | _cleanup_close_ int fd = -EBADF; | |
485 | ||
486 | assert(dir_fd >= 0 || dir_fd == AT_FDCWD); | |
6eec59f9 DDM |
487 | assert(ret); |
488 | ||
99d704c6 YW |
489 | if (!isempty(path)) { |
490 | fd = xopenat(dir_fd, path, O_PATH|O_CLOEXEC|O_NOCTTY); | |
491 | if (fd < 0) | |
492 | return fd; | |
493 | dir_fd = fd; | |
494 | } | |
6eec59f9 | 495 | |
99d704c6 | 496 | return RET_NERRNO(fstatfs(dir_fd, ret)); |
6eec59f9 DDM |
497 | } |
498 | ||
0c15577a DDM |
499 | usec_t statx_timestamp_load(const struct statx_timestamp *ts) { |
500 | return timespec_load(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec }); | |
501 | } | |
502 | nsec_t statx_timestamp_load_nsec(const struct statx_timestamp *ts) { | |
503 | return timespec_load_nsec(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec }); | |
504 | } | |
505 | ||
ddfdf86f | 506 | void inode_hash_func(const struct stat *q, struct siphash *state) { |
c01a5c05 YW |
507 | siphash24_compress_typesafe(q->st_dev, state); |
508 | siphash24_compress_typesafe(q->st_ino, state); | |
ddfdf86f DDM |
509 | } |
510 | ||
511 | int inode_compare_func(const struct stat *a, const struct stat *b) { | |
512 | int r; | |
513 | ||
514 | r = CMP(a->st_dev, b->st_dev); | |
515 | if (r != 0) | |
516 | return r; | |
517 | ||
518 | return CMP(a->st_ino, b->st_ino); | |
519 | } | |
520 | ||
521 | DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(inode_hash_ops, struct stat, inode_hash_func, inode_compare_func, free); | |
d83ce136 LP |
522 | |
523 | const char* inode_type_to_string(mode_t m) { | |
524 | ||
525 | /* Returns a short string for the inode type. We use the same name as the underlying macros for each | |
526 | * inode type. */ | |
527 | ||
528 | switch (m & S_IFMT) { | |
529 | case S_IFREG: | |
530 | return "reg"; | |
531 | case S_IFDIR: | |
532 | return "dir"; | |
e83fbf4b LP |
533 | case S_IFLNK: |
534 | return "lnk"; | |
d83ce136 LP |
535 | case S_IFCHR: |
536 | return "chr"; | |
537 | case S_IFBLK: | |
538 | return "blk"; | |
539 | case S_IFIFO: | |
540 | return "fifo"; | |
541 | case S_IFSOCK: | |
542 | return "sock"; | |
543 | } | |
544 | ||
f03caa0d | 545 | /* Note anonymous inodes in the kernel will have a zero type. Hence fstat() of an eventfd() will |
7cff2b79 | 546 | * return an .st_mode where we'll return NULL here! */ |
d83ce136 LP |
547 | return NULL; |
548 | } | |
cc037880 LP |
549 | |
550 | mode_t inode_type_from_string(const char *s) { | |
551 | if (!s) | |
552 | return MODE_INVALID; | |
553 | ||
554 | if (streq(s, "reg")) | |
555 | return S_IFREG; | |
556 | if (streq(s, "dir")) | |
557 | return S_IFDIR; | |
558 | if (streq(s, "lnk")) | |
559 | return S_IFLNK; | |
560 | if (streq(s, "chr")) | |
561 | return S_IFCHR; | |
562 | if (streq(s, "blk")) | |
563 | return S_IFBLK; | |
564 | if (streq(s, "fifo")) | |
565 | return S_IFIFO; | |
566 | if (streq(s, "sock")) | |
567 | return S_IFSOCK; | |
568 | ||
569 | return MODE_INVALID; | |
570 | } |