• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.3
/src/basic/stat-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <linux/magic.h>
5
#include <sys/statvfs.h>
6
#include <unistd.h>
7

8
#include "alloc-util.h"
9
#include "bitfield.h"
10
#include "chase.h"
11
#include "dirent-util.h"
12
#include "errno-util.h"
13
#include "fd-util.h"
14
#include "filesystems.h"
15
#include "fs-util.h"
16
#include "hash-funcs.h"
17
#include "log.h"
18
#include "mountpoint-util.h"
19
#include "path-util.h"
20
#include "siphash24.h"
21
#include "stat-util.h"
22
#include "string-util.h"
23
#include "time-util.h"
24

25
static int verify_stat_at(
2,159,170✔
26
                int fd,
27
                const char *path,
28
                bool follow,
29
                int (*verify_func)(const struct stat *st),
30
                bool verify) {
31

32
        struct stat st;
2,159,170✔
33
        int r;
2,159,170✔
34

35
        assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
2,159,170✔
36
        assert(!isempty(path) || !follow);
2,159,170✔
37
        assert(verify_func);
2,159,170✔
38

39
        _cleanup_free_ char *p = NULL;
2,159,170✔
40
        r = resolve_xat_fdroot(&fd, &path, &p);
2,159,170✔
41
        if (r < 0)
2,159,170✔
42
                return r;
43

44
        if (fstatat(fd, strempty(path), &st,
3,055,903✔
45
                    (isempty(path) ? AT_EMPTY_PATH : 0) | (follow ? 0 : AT_SYMLINK_NOFOLLOW)) < 0)
3,421,607✔
46
                return -errno;
7,960✔
47

48
        r = verify_func(&st);
2,151,210✔
49
        return verify ? r : r >= 0;
2,151,210✔
50
}
51

52
int stat_verify_regular(const struct stat *st) {
2,983,533✔
53
        assert(st);
2,983,533✔
54

55
        /* Checks whether the specified stat() structure refers to a regular file. If not returns an
56
         * appropriate error code. */
57

58
        if (S_ISDIR(st->st_mode))
2,983,533✔
59
                return -EISDIR;
60

61
        if (S_ISLNK(st->st_mode))
2,983,462✔
62
                return -ELOOP;
63

64
        if (!S_ISREG(st->st_mode))
2,983,427✔
65
                return -EBADFD;
19✔
66

67
        return 0;
68
}
69

70
int verify_regular_at(int fd, const char *path, bool follow) {
892,146✔
71
        return verify_stat_at(fd, path, follow, stat_verify_regular, true);
892,146✔
72
}
73

74
int fd_verify_regular(int fd) {
891,867✔
75
        if (IN_SET(fd, AT_FDCWD, XAT_FDROOT))
891,867✔
76
                return -EISDIR;
77

78
        return verify_regular_at(fd, /* path= */ NULL, /* follow= */ false);
891,867✔
79
}
80

81
int stat_verify_directory(const struct stat *st) {
1,294,689✔
82
        assert(st);
1,294,689✔
83

84
        if (S_ISLNK(st->st_mode))
1,294,689✔
85
                return -ELOOP;
86

87
        if (!S_ISDIR(st->st_mode))
1,294,679✔
88
                return -ENOTDIR;
4✔
89

90
        return 0;
91
}
92

93
int statx_verify_directory(const struct statx *stx) {
×
94
        assert(stx);
×
95

96
        if (!FLAGS_SET(stx->stx_mask, STATX_TYPE))
×
97
                return -ENODATA;
98

99
        if (S_ISLNK(stx->stx_mode))
×
100
                return -ELOOP;
101

102
        if (!S_ISDIR(stx->stx_mode))
×
103
                return -ENOTDIR;
×
104

105
        return 0;
106
}
107

108
int fd_verify_directory(int fd) {
143✔
109
        if (IN_SET(fd, AT_FDCWD, XAT_FDROOT))
143✔
110
                return 0;
111

112
        return verify_stat_at(fd, NULL, false, stat_verify_directory, true);
142✔
113
}
114

115
int is_dir_at(int fd, const char *path, bool follow) {
1,244,636✔
116
        return verify_stat_at(fd, path, follow, stat_verify_directory, false);
1,244,636✔
117
}
118

119
int is_dir(const char *path, bool follow) {
447,200✔
120
        assert(!isempty(path));
447,200✔
121
        return is_dir_at(AT_FDCWD, path, follow);
447,200✔
122
}
123

124
int stat_verify_symlink(const struct stat *st) {
22,069✔
125
        assert(st);
22,069✔
126

127
        if (S_ISDIR(st->st_mode))
22,069✔
128
                return -EISDIR;
129

130
        if (!S_ISLNK(st->st_mode))
22,052✔
131
                return -ENOLINK;
275✔
132

133
        return 0;
134
}
135

136
int fd_verify_symlink(int fd) {
4,677✔
137
        return verify_stat_at(fd, /* path= */ NULL, /* follow= */ false, stat_verify_symlink, /* verify= */ true);
4,677✔
138
}
139

140
int is_symlink(const char *path) {
17,392✔
141
        assert(!isempty(path));
17,392✔
142
        return verify_stat_at(AT_FDCWD, path, false, stat_verify_symlink, false);
17,392✔
143
}
144

145
int stat_verify_socket(const struct stat *st) {
63✔
146
        assert(st);
63✔
147

148
        if (S_ISLNK(st->st_mode))
63✔
149
                return -ELOOP;
150

151
        if (S_ISDIR(st->st_mode))
63✔
152
                return -EISDIR;
153

154
        if (!S_ISSOCK(st->st_mode))
63✔
155
                return -ENOTSOCK;
×
156

157
        return 0;
158
}
159

160
int stat_verify_linked(const struct stat *st) {
2,031,757✔
161
        assert(st);
2,031,757✔
162

163
        if (st->st_nlink <= 0)
2,031,757✔
164
                return -EIDRM; /* recognizable error. */
2✔
165

166
        return 0;
167
}
168

169
int fd_verify_linked(int fd) {
48✔
170

171
        if (fd == XAT_FDROOT)
48✔
172
                return 0;
173

174
        return verify_stat_at(fd, NULL, false, stat_verify_linked, true);
47✔
175
}
176

177
int stat_verify_device_node(const struct stat *st) {
3,490✔
178
        assert(st);
3,490✔
179

180
        if (S_ISLNK(st->st_mode))
3,490✔
181
                return -ELOOP;
182

183
        if (S_ISDIR(st->st_mode))
3,490✔
184
                return -EISDIR;
185

186
        if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
3,369✔
187
                return -ENOTTY;
5✔
188

189
        return 0;
190
}
191

192
int is_device_node(const char *path) {
130✔
193
        assert(!isempty(path));
130✔
194
        return verify_stat_at(AT_FDCWD, path, false, stat_verify_device_node, false);
130✔
195
}
196

197
int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) {
38,371✔
198
        _cleanup_close_ int fd = -EBADF;
38,371✔
199
        struct dirent *buf;
38,371✔
200
        size_t m;
38,371✔
201

202
        fd = xopenat(dir_fd, path, O_DIRECTORY|O_CLOEXEC);
38,371✔
203
        if (fd < 0)
38,371✔
204
                return fd;
205

206
        /* Allocate space for at least 3 full dirents, since every dir has at least two entries ("."  +
207
         * ".."), and only once we have seen if there's a third we know whether the dir is empty or not. If
208
         * 'ignore_hidden_or_backup' is true we'll allocate a bit more, since we might skip over a bunch of
209
         * entries that we end up ignoring. */
210
        m = (ignore_hidden_or_backup ? 16 : 3) * DIRENT_SIZE_MAX;
2,268✔
211
        buf = alloca(m);
2,268✔
212

213
        for (;;) {
3,531✔
214
                struct dirent *de;
3,531✔
215
                ssize_t n;
3,531✔
216

217
                n = getdents64(fd, buf, m);
3,531✔
218
                if (n < 0)
3,531✔
219
                        return -errno;
×
220
                if (n == 0)
3,531✔
221
                        break;
222

223
                assert((size_t) n <= m);
2,268✔
224
                msan_unpoison(buf, n);
2,268✔
225

226
                FOREACH_DIRENT_IN_BUFFER(de, buf, n)
6,371✔
227
                        if (!(ignore_hidden_or_backup ? hidden_or_backup_file(de->d_name) : dot_or_dot_dot(de->d_name)))
5,108✔
228
                                return 0;
229
        }
230

231
        return 1;
232
}
233

234
bool stat_may_be_dev_null(struct stat *st) {
443,241✔
235
        assert(st);
443,241✔
236

237
        /* We don't want to hardcode the major/minor of /dev/null, hence we do a simpler "is this a character
238
         * device node?" check. */
239

240
        return S_ISCHR(st->st_mode);
443,241✔
241
}
242

243
bool stat_is_empty(struct stat *st) {
426,508✔
244
        assert(st);
426,508✔
245

246
        return S_ISREG(st->st_mode) && st->st_size <= 0;
426,508✔
247
}
248

249
int null_or_empty_path_with_root(const char *fn, const char *root) {
322,284✔
250
        struct stat st;
322,284✔
251
        int r;
322,284✔
252

253
        assert(fn);
322,284✔
254

255
        /* A symlink to /dev/null or an empty file?
256
         * When looking under root_dir, we can't expect /dev/ to be mounted,
257
         * so let's see if the path is a (possibly dangling) symlink to /dev/null. */
258

259
        if (path_equal(path_startswith(fn, root ?: "/"), "dev/null"))
644,568✔
260
                return true;
322,284✔
261

262
        r = chase_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st);
322,122✔
263
        if (r < 0)
322,122✔
264
                return r;
265

266
        return null_or_empty(&st);
322,003✔
267
}
268

269
static const char* statx_mask_one_to_name(unsigned mask);
270
static const char* statx_attribute_to_name(uint64_t attr);
271

272
#include "statx-attribute-to-name.inc"
273
#include "statx-mask-to-name.inc"
274

275
#define DEFINE_STATX_BITS_TO_STRING(prefix, type, func, format_str)             \
276
        static char* prefix##_to_string(type v) {                               \
277
                if (v == 0)                                                     \
278
                        return strdup("");                                      \
279
                                                                                \
280
                _cleanup_free_ char *s = NULL;                                  \
281
                                                                                \
282
                BIT_FOREACH(i, v) {                                             \
283
                        type f = 1 << i;                                        \
284
                                                                                \
285
                        const char *n = func(f);                                \
286
                        if (!n)                                                 \
287
                                continue;                                       \
288
                                                                                \
289
                        if (!strextend_with_separator(&s, "|", n))              \
290
                                return NULL;                                    \
291
                        v &= ~f;                                                \
292
                }                                                               \
293
                                                                                \
294
                if (v != 0 && strextendf_with_separator(&s, "|", format_str, v) < 0) \
295
                        return NULL;                                            \
296
                                                                                \
297
                return TAKE_PTR(s);                                             \
298
        }
299

300
DEFINE_STATX_BITS_TO_STRING(statx_mask,       unsigned, statx_mask_one_to_name,  "0x%x");
×
301
DEFINE_STATX_BITS_TO_STRING(statx_attributes, uint64_t, statx_attribute_to_name, "0x%" PRIx64);
×
302

303
int xstatx_full(int fd,
7,551,772✔
304
                const char *path,
305
                int flags,
306
                unsigned mandatory_mask,
307
                unsigned optional_mask,
308
                uint64_t mandatory_attributes,
309
                struct statx *ret) {
310

311
        struct statx sx = {}; /* explicitly initialize the struct to make msan silent. */
7,551,772✔
312
        int r;
7,551,772✔
313

314
        /* Wrapper around statx(), with additional bells and whistles:
315
         *
316
         * 1. AT_EMPTY_PATH is implied on empty path
317
         * 2. Supports XAT_FDROOT
318
         * 3. Takes separate mandatory and optional mask params, plus mandatory attributes.
319
         *    Returns -EUNATCH if statx() does not return all masks specified as mandatory,
320
         *    > 0 if all optional masks are supported, 0 otherwise.
321
         */
322

323
        assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
7,551,772✔
324
        assert((mandatory_mask & optional_mask) == 0);
7,551,772✔
325
        assert(ret);
7,551,772✔
326

327
        _cleanup_free_ char *p = NULL;
7,551,772✔
328
        r = resolve_xat_fdroot(&fd, &path, &p);
7,551,772✔
329
        if (r < 0)
7,551,772✔
330
                return r;
331

332
        if (statx(fd, strempty(path),
7,551,772✔
333
                  flags|(isempty(path) ? AT_EMPTY_PATH : 0),
7,717,961✔
334
                  mandatory_mask|optional_mask,
335
                  &sx) < 0)
336
                return negative_errno();
2,469✔
337

338
        if (!FLAGS_SET(sx.stx_mask, mandatory_mask)) {
7,549,303✔
339
                if (DEBUG_LOGGING) {
×
340
                        _cleanup_free_ char *mask_str = statx_mask_to_string(mandatory_mask & ~sx.stx_mask);
×
341
                        log_debug("statx() does not support '%s' mask (running on an old kernel?)", strnull(mask_str));
×
342
                }
343

344
                return -EUNATCH;
×
345
        }
346

347
        if (!FLAGS_SET(sx.stx_attributes_mask, mandatory_attributes)) {
7,549,303✔
348
                if (DEBUG_LOGGING) {
×
349
                        _cleanup_free_ char *attr_str = statx_attributes_to_string(mandatory_attributes & ~sx.stx_attributes_mask);
×
350
                        log_debug("statx() does not support '%s' attribute (running on an old kernel?)", strnull(attr_str));
×
351
                }
352

353
                return -EUNATCH;
×
354
        }
355

356
        *ret = sx;
7,549,303✔
357
        return FLAGS_SET(sx.stx_mask, optional_mask);
7,549,303✔
358
}
359

360
static int xfstatfs(int fd, struct statfs *ret) {
8,302,988✔
361
        assert(ret);
8,302,988✔
362

363
        if (fd == AT_FDCWD)
8,302,988✔
364
                return RET_NERRNO(statfs(".", ret));
×
365
        if (fd == XAT_FDROOT)
8,302,988✔
366
                return RET_NERRNO(statfs("/", ret));
2✔
367

368
        assert(fd >= 0);
8,302,986✔
369
        return RET_NERRNO(fstatfs(fd, ret));
8,302,986✔
370
}
371

372
int xstatfsat(int dir_fd, const char *path, struct statfs *ret) {
8,280,128✔
373
        _cleanup_close_ int fd = -EBADF;
8,280,128✔
374

375
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
8,280,128✔
376
        assert(ret);
8,280,128✔
377

378
        if (!isempty(path)) {
8,280,128✔
379
                fd = xopenat(dir_fd, path, O_PATH|O_CLOEXEC);
24,213✔
380
                if (fd < 0)
24,213✔
381
                        return fd;
382
                dir_fd = fd;
383
        }
384

385
        return xfstatfs(dir_fd, ret);
8,280,116✔
386
}
387

388
int fd_is_read_only_fs(int fd) {
1,808✔
389
        int r;
1,808✔
390

391
        struct statfs st;
1,808✔
392
        r = xfstatfs(fd, &st);
1,808✔
393
        if (r < 0)
1,808✔
394
                return r;
1,808✔
395

396
        if (st.f_flags & ST_RDONLY)
1,808✔
397
                return true;
398

399
        if (is_network_fs(&st))
1,436✔
400
                /* On NFS, fstatfs() might not reflect whether we can actually write to the remote share.
401
                 * Let's try again with access(W_OK) which is more reliable, at least sometimes. */
402
                return access_fd(fd, W_OK) == -EROFS;
×
403

404
        return false;
405
}
406

407
int path_is_read_only_fs(const char *path) {
1,512✔
408
        _cleanup_close_ int fd = -EBADF;
1,512✔
409

410
        assert(path);
1,512✔
411

412
        fd = open(path, O_CLOEXEC | O_PATH);
1,512✔
413
        if (fd < 0)
1,512✔
414
                return -errno;
152✔
415

416
        return fd_is_read_only_fs(fd);
1,360✔
417
}
418

419
int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags) {
14,166✔
420
        struct stat sta, stb;
14,166✔
421
        int r;
14,166✔
422

423
        assert(fda >= 0 || fda == AT_FDCWD);
14,166✔
424
        assert(fdb >= 0 || fdb == AT_FDCWD);
14,166✔
425
        assert((flags & ~(AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT)) == 0);
14,166✔
426

427
        /* Refuse an unset filea or fileb early unless AT_EMPTY_PATH is set */
428
        if ((isempty(filea) || isempty(fileb)) && !FLAGS_SET(flags, AT_EMPTY_PATH))
27,810✔
429
                return -EINVAL;
14,166✔
430

431
        /* Shortcut: comparing the same fd with itself means we can return true */
432
        if (fda >= 0 && fda == fdb && isempty(filea) && isempty(fileb) && FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW))
14,168✔
433
                return true;
434

435
        _cleanup_close_ int pin_a = -EBADF, pin_b = -EBADF;
28,331✔
436
        if (!FLAGS_SET(flags, AT_NO_AUTOMOUNT)) {
14,165✔
437
                /* Let's try to use the name_to_handle_at() AT_HANDLE_FID API to identify identical
438
                 * inodes. We have to issue multiple calls on the same file for that (first, to acquire the
439
                 * FID, and then to check if .st_dev is actually the same). Hence let's pin the inode in
440
                 * between via O_PATH, unless we already have an fd for it. */
441

442
                if (!isempty(filea)) {
14,165✔
443
                        pin_a = openat(fda, filea, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0));
13,644✔
444
                        if (pin_a < 0)
13,644✔
445
                                return -errno;
13,930✔
446

447
                        fda = pin_a;
13,243✔
448
                        filea = NULL;
13,243✔
449
                        flags |= AT_EMPTY_PATH;
13,243✔
450
                }
451

452
                if (!isempty(fileb)) {
13,764✔
453
                        pin_b = openat(fdb, fileb, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0));
13,241✔
454
                        if (pin_b < 0)
13,241✔
455
                                return -errno;
4✔
456

457
                        fdb = pin_b;
13,237✔
458
                        fileb = NULL;
13,237✔
459
                        flags |= AT_EMPTY_PATH;
13,237✔
460
                }
461

462
                int ntha_flags = at_flags_normalize_follow(flags) & (AT_EMPTY_PATH|AT_SYMLINK_FOLLOW);
13,760✔
463
                _cleanup_free_ struct file_handle *ha = NULL, *hb = NULL;
13,760✔
464
                uint64_t mntida, mntidb;
13,760✔
465
                int _mntida, _mntidb;
13,760✔
466

467
                r = name_to_handle_at_try_fid(
13,760✔
468
                                fda,
469
                                filea,
470
                                &ha,
471
                                &_mntida,
472
                                &mntida,
473
                                ntha_flags);
474
                if (r < 0) {
13,760✔
475
                        if (is_name_to_handle_at_fatal_error(r))
×
476
                                return r;
477

478
                        goto fallback;
×
479
                }
480
                if (r == 0)
13,760✔
481
                        mntida = _mntida;
×
482

483
                r = name_to_handle_at_try_fid(
13,760✔
484
                                fdb,
485
                                fileb,
486
                                &hb,
487
                                r > 0 ? NULL : &_mntidb, /* if we managed to get unique mnt id for a, insist on that for b */
488
                                r > 0 ? &mntidb : NULL,
489
                                ntha_flags);
490
                if (r < 0) {
13,760✔
491
                        if (is_name_to_handle_at_fatal_error(r))
×
492
                                return r;
493

494
                        goto fallback;
×
495
                }
496
                if (r == 0)
13,760✔
497
                        mntidb = _mntidb;
×
498

499
                /* Now compare the two file handles */
500
                if (!file_handle_equal(ha, hb))
13,760✔
501
                        return false;
502

503
                /* If the file handles are the same and they come from the same mount ID? Great, then we are
504
                 * good, they are definitely the same */
505
                if (mntida == mntidb)
13,245✔
506
                        return true;
507

508
                /* File handles are the same, they are not on the same mount id. This might either be because
509
                 * they are on two entirely different file systems, that just happen to have the same FIDs
510
                 * (because they originally where created off the same disk images), or it could be because
511
                 * they are located on two distinct bind mounts of the same fs. To check that, let's look at
512
                 * .st_rdev of the inode. We simply reuse the fallback codepath for that, since it checks
513
                 * exactly that (it checks slightly more, but we don't care.) */
514
        }
515

516
fallback:
×
517
        if (fstatat(fda, strempty(filea), &sta, flags) < 0)
470✔
518
                return log_debug_errno(errno, "Cannot stat %s: %m", strna(filea));
×
519

520
        if (fstatat(fdb, strempty(fileb), &stb, flags) < 0)
470✔
521
                return log_debug_errno(errno, "Cannot stat %s: %m", strna(fileb));
×
522

523
        return stat_inode_same(&sta, &stb);
235✔
524
}
525

526
bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
8,284,754✔
527
        assert(s);
8,284,754✔
528
        assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
8,284,754✔
529

530
        return F_TYPE_EQUAL(s->f_type, magic_value);
8,284,754✔
531
}
532

533
int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value) {
8,279,622✔
534
        int r;
8,279,622✔
535

536
        struct statfs s;
8,279,622✔
537
        r = xstatfsat(dir_fd, path, &s);
8,279,622✔
538
        if (r < 0)
8,279,622✔
539
                return r;
8,279,622✔
540

541
        return is_fs_type(&s, magic_value);
8,279,621✔
542
}
543

544
bool is_temporary_fs(const struct statfs *s) {
10,345✔
545
        return fs_in_group(s, FILESYSTEM_SET_TEMPORARY);
10,345✔
546
}
547

548
bool is_network_fs(const struct statfs *s) {
22,453✔
549
        return fs_in_group(s, FILESYSTEM_SET_NETWORK);
22,453✔
550
}
551

552
int fd_is_temporary_fs(int fd) {
154✔
553
        int r;
154✔
554

555
        struct statfs s;
154✔
556
        r = xfstatfs(fd, &s);
154✔
557
        if (r < 0)
154✔
558
                return r;
154✔
559

560
        return is_temporary_fs(&s);
154✔
561
}
562

563
int fd_is_network_fs(int fd) {
20,910✔
564
        int r;
20,910✔
565

566
        struct statfs s;
20,910✔
567
        r = xfstatfs(fd, &s);
20,910✔
568
        if (r < 0)
20,910✔
569
                return r;
20,910✔
570

571
        return is_network_fs(&s);
20,910✔
572
}
573

574
int path_is_temporary_fs(const char *path) {
11✔
575
        struct statfs s;
11✔
576

577
        if (statfs(path, &s) < 0)
11✔
578
                return -errno;
2✔
579

580
        return is_temporary_fs(&s);
9✔
581
}
582

583
int path_is_network_fs(const char *path) {
×
584
        struct statfs s;
×
585

586
        if (statfs(path, &s) < 0)
×
587
                return -errno;
×
588

589
        return is_network_fs(&s);
×
590
}
591

592
int proc_mounted(void) {
23,581✔
593
        /* This is typically used in error path. So, it is better to not overwrite the original errno. */
594
        PROTECT_ERRNO;
23,581✔
595
        int r;
23,581✔
596

597
        /* A quick check of procfs is properly mounted */
598

599
        r = path_is_fs_type("/proc/", PROC_SUPER_MAGIC);
23,581✔
600
        if (r == -ENOENT) /* not mounted at all */
23,581✔
601
                return false;
×
602

603
        return r;
604
}
605

606
bool stat_inode_same(const struct stat *a, const struct stat *b) {
1,195,170✔
607

608
        /* Returns if the specified stat structure references the same (though possibly modified) inode. Does
609
         * a thorough check, comparing inode nr, backing device and if the inode is still of the same type. */
610

611
        return stat_is_set(a) && stat_is_set(b) &&
2,388,170✔
612
                ((a->st_mode ^ b->st_mode) & S_IFMT) == 0 &&  /* same inode type */
1,155,219✔
613
                a->st_dev == b->st_dev &&
2,310,573✔
614
                a->st_ino == b->st_ino;
1,115,403✔
615
}
616

617
bool stat_inode_unmodified(const struct stat *a, const struct stat *b) {
128,291✔
618

619
        /* Returns if the specified stat structures reference the same, unmodified inode. This check tries to
620
         * be reasonably careful when detecting changes: we check both inode and mtime, to cater for file
621
         * systems where mtimes are fixed to 0 (think: ostree/nixos type installations). We also check file
622
         * size, backing device, inode type and if this refers to a device not the major/minor.
623
         *
624
         * Note that we don't care if file attributes such as ownership or access mode change, this here is
625
         * about contents of the file. The purpose here is to detect file contents changes, and nothing
626
         * else. */
627

628
        return stat_inode_same(a, b) &&
128,291✔
629
                a->st_mtim.tv_sec == b->st_mtim.tv_sec &&
81,303✔
630
                a->st_mtim.tv_nsec == b->st_mtim.tv_nsec &&
80,989✔
631
                (!S_ISREG(a->st_mode) || a->st_size == b->st_size) && /* if regular file, compare file size */
209,258✔
632
                (!(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 */
80,967✔
633
}
634

635
bool statx_inode_same(const struct statx *a, const struct statx *b) {
3,758,214✔
636

637
        /* Same as stat_inode_same() but for struct statx */
638

639
        if (!statx_is_set(a) || !statx_is_set(b))
7,516,428✔
640
                return false;
641

642
        assert(FLAGS_SET(a->stx_mask, STATX_TYPE|STATX_INO));
3,758,214✔
643
        assert(FLAGS_SET(b->stx_mask, STATX_TYPE|STATX_INO));
3,758,214✔
644

645
        return
3,758,214✔
646
                ((a->stx_mode ^ b->stx_mode) & S_IFMT) == 0 &&
7,512,628✔
647
                a->stx_dev_major == b->stx_dev_major &&
3,754,414✔
648
                a->stx_dev_minor == b->stx_dev_minor &&
11,095,887✔
649
                a->stx_ino == b->stx_ino;
3,576,221✔
650
}
651

652
bool statx_mount_same(const struct statx *a, const struct statx *b) {
3,489,831✔
653
        if (!statx_is_set(a) || !statx_is_set(b))
6,979,662✔
654
                return false;
655

656
        assert(FLAGS_SET(a->stx_mask, STATX_MNT_ID));
3,489,831✔
657
        assert(FLAGS_SET(b->stx_mask, STATX_MNT_ID));
3,489,831✔
658

659
        return a->stx_mnt_id == b->stx_mnt_id;
3,489,831✔
660
}
661

662
usec_t statx_timestamp_load(const struct statx_timestamp *ts) {
5,301✔
663
        return timespec_load(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec });
5,301✔
664
}
665
nsec_t statx_timestamp_load_nsec(const struct statx_timestamp *ts) {
820✔
666
        return timespec_load_nsec(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec });
820✔
667
}
668

669
void inode_hash_func(const struct stat *q, struct siphash *state) {
37,729✔
670
        siphash24_compress_typesafe(q->st_dev, state);
37,729✔
671
        siphash24_compress_typesafe(q->st_ino, state);
37,729✔
672
}
37,729✔
673

674
int inode_compare_func(const struct stat *a, const struct stat *b) {
31,419✔
675
        int r;
31,419✔
676

677
        r = CMP(a->st_dev, b->st_dev);
31,419✔
678
        if (r != 0)
26,466✔
679
                return r;
5,046✔
680

681
        return CMP(a->st_ino, b->st_ino);
26,373✔
682
}
683

684
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(inode_hash_ops, struct stat, inode_hash_func, inode_compare_func, free);
851✔
685

686
const char* inode_type_to_string(mode_t m) {
13,775✔
687

688
        /* Returns a short string for the inode type. We use the same name as the underlying macros for each
689
         * inode type. */
690

691
        switch (m & S_IFMT) {
13,775✔
692
        case S_IFREG:
693
                return "reg";
694
        case S_IFDIR:
5,912✔
695
                return "dir";
5,912✔
696
        case S_IFLNK:
1✔
697
                return "lnk";
1✔
698
        case S_IFCHR:
1,003✔
699
                return "chr";
1,003✔
700
        case S_IFBLK:
463✔
701
                return "blk";
463✔
702
        case S_IFIFO:
463✔
703
                return "fifo";
463✔
704
        case S_IFSOCK:
606✔
705
                return "sock";
606✔
706
        }
707

708
        /* Note anonymous inodes in the kernel will have a zero type. Hence fstat() of an eventfd() will
709
         * return an .st_mode where we'll return NULL here! */
710
        return NULL;
4✔
711
}
712

713
mode_t inode_type_from_string(const char *s) {
13✔
714
        if (!s)
13✔
715
                return MODE_INVALID;
716

717
        if (streq(s, "reg"))
13✔
718
                return S_IFREG;
719
        if (streq(s, "dir"))
10✔
720
                return S_IFDIR;
721
        if (streq(s, "lnk"))
7✔
722
                return S_IFLNK;
723
        if (streq(s, "chr"))
6✔
724
                return S_IFCHR;
725
        if (streq(s, "blk"))
5✔
726
                return S_IFBLK;
727
        if (streq(s, "fifo"))
4✔
728
                return S_IFIFO;
729
        if (streq(s, "sock"))
2✔
730
                return S_IFSOCK;
2✔
731

732
        return MODE_INVALID;
733
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc