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

systemd / systemd / 22246189288

20 Feb 2026 07:59PM UTC coverage: 72.238% (-0.2%) from 72.47%
22246189288

push

github

bluca
Add BNCF NewBook 11 ACCEL_MOUNT_MATRIX  to 60-sensor.hwdb

Corrects DE autorotation

Device description: https://www.bncfai.com/product/773/

313540 of 434040 relevant lines covered (72.24%)

1346131.39 hits per line

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

88.48
/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,194,542✔
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,194,542✔
33
        int r;
2,194,542✔
34

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

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

44
        if (fstatat(fd, strempty(path), &st,
3,124,460✔
45
                    (isempty(path) ? AT_EMPTY_PATH : 0) | (follow ? 0 : AT_SYMLINK_NOFOLLOW)) < 0)
3,459,166✔
46
                return -errno;
8,088✔
47

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

52
int stat_verify_regular(const struct stat *st) {
2,871,304✔
53
        assert(st);
2,871,304✔
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,871,304✔
59
                return -EISDIR;
60

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

64
        if (!S_ISREG(st->st_mode))
2,871,194✔
65
                return -EBADFD;
20✔
66

67
        return 0;
68
}
69

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

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

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

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

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

87
        if (!S_ISDIR(st->st_mode))
1,299,087✔
88
                return -ENOTDIR;
7✔
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) {
169✔
109
        if (IN_SET(fd, AT_FDCWD, XAT_FDROOT))
169✔
110
                return 0;
111

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

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

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

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

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

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

133
        return 0;
134
}
135

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

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

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

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

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

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

157
        return 0;
158
}
159

160
int is_socket(const char *path) {
×
161
        assert(!isempty(path));
×
162
        return verify_stat_at(AT_FDCWD, path, /* follow= */ true, stat_verify_socket, /* verify= */ false);
×
163
}
164

165
int stat_verify_linked(const struct stat *st) {
1,886,062✔
166
        assert(st);
1,886,062✔
167

168
        if (st->st_nlink <= 0)
1,886,062✔
169
                return -EIDRM; /* recognizable error. */
2✔
170

171
        return 0;
172
}
173

174
int fd_verify_linked(int fd) {
48✔
175

176
        if (fd == XAT_FDROOT)
48✔
177
                return 0;
178

179
        return verify_stat_at(fd, NULL, false, stat_verify_linked, true);
47✔
180
}
181

182
int stat_verify_device_node(const struct stat *st) {
3,482✔
183
        assert(st);
3,482✔
184

185
        if (S_ISLNK(st->st_mode))
3,482✔
186
                return -ELOOP;
187

188
        if (S_ISDIR(st->st_mode))
3,482✔
189
                return -EISDIR;
190

191
        if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
3,361✔
192
                return -ENOTTY;
5✔
193

194
        return 0;
195
}
196

197
int is_device_node(const char *path) {
130✔
198
        assert(!isempty(path));
130✔
199
        return verify_stat_at(AT_FDCWD, path, false, stat_verify_device_node, false);
130✔
200
}
201

202
int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) {
40,814✔
203
        _cleanup_close_ int fd = -EBADF;
40,814✔
204
        struct dirent *buf;
40,814✔
205
        size_t m;
40,814✔
206

207
        fd = xopenat(dir_fd, path, O_DIRECTORY|O_CLOEXEC);
40,814✔
208
        if (fd < 0)
40,814✔
209
                return fd;
210

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

218
        for (;;) {
3,475✔
219
                struct dirent *de;
3,475✔
220
                ssize_t n;
3,475✔
221

222
                n = getdents64(fd, buf, m);
3,475✔
223
                if (n < 0)
3,475✔
224
                        return -errno;
×
225
                if (n == 0)
3,475✔
226
                        break;
227

228
                assert((size_t) n <= m);
2,245✔
229
                msan_unpoison(buf, n);
2,245✔
230

231
                FOREACH_DIRENT_IN_BUFFER(de, buf, n)
6,123✔
232
                        if (!(ignore_hidden_or_backup ? hidden_or_backup_file(de->d_name) : dot_or_dot_dot(de->d_name)))
4,893✔
233
                                return 0;
234
        }
235

236
        return 1;
237
}
238

239
bool stat_may_be_dev_null(struct stat *st) {
438,168✔
240
        assert(st);
438,168✔
241

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

245
        return S_ISCHR(st->st_mode);
438,168✔
246
}
247

248
bool stat_is_empty(struct stat *st) {
421,797✔
249
        assert(st);
421,797✔
250

251
        return S_ISREG(st->st_mode) && st->st_size <= 0;
421,797✔
252
}
253

254
int null_or_empty_path_with_root(const char *fn, const char *root) {
318,387✔
255
        struct stat st;
318,387✔
256
        int r;
318,387✔
257

258
        assert(fn);
318,387✔
259

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

264
        if (path_equal(path_startswith(fn, root ?: "/"), "dev/null"))
636,774✔
265
                return true;
318,387✔
266

267
        r = chase_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st);
318,225✔
268
        if (r < 0)
318,225✔
269
                return r;
270

271
        return null_or_empty(&st);
318,109✔
272
}
273

274
static const char* statx_mask_one_to_name(unsigned mask);
275
static const char* statx_attribute_to_name(uint64_t attr);
276

277
#include "statx-attribute-to-name.inc"
278
#include "statx-mask-to-name.inc"
279

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

305
DEFINE_STATX_BITS_TO_STRING(statx_mask,       unsigned, statx_mask_one_to_name,  "0x%x");
×
306
DEFINE_STATX_BITS_TO_STRING(statx_attributes, uint64_t, statx_attribute_to_name, "0x%" PRIx64);
×
307

308
int xstatx_full(int fd,
7,668,010✔
309
                const char *path,
310
                int flags,
311
                unsigned mandatory_mask,
312
                unsigned optional_mask,
313
                uint64_t mandatory_attributes,
314
                struct statx *ret) {
315

316
        struct statx sx = {}; /* explicitly initialize the struct to make msan silent. */
7,668,010✔
317
        int r;
7,668,010✔
318

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

328
        assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
7,668,010✔
329
        assert((mandatory_mask & optional_mask) == 0);
7,668,010✔
330
        assert(ret);
7,668,010✔
331

332
        _cleanup_free_ char *p = NULL;
7,668,010✔
333
        r = resolve_xat_fdroot(&fd, &path, &p);
7,668,010✔
334
        if (r < 0)
7,668,010✔
335
                return r;
336

337
        if (statx(fd, strempty(path),
7,668,010✔
338
                  flags|(isempty(path) ? AT_EMPTY_PATH : 0),
7,841,966✔
339
                  mandatory_mask|optional_mask,
340
                  &sx) < 0)
341
                return negative_errno();
2,481✔
342

343
        if (!FLAGS_SET(sx.stx_mask, mandatory_mask)) {
7,665,529✔
344
                if (DEBUG_LOGGING) {
×
345
                        _cleanup_free_ char *mask_str = statx_mask_to_string(mandatory_mask & ~sx.stx_mask);
×
346
                        log_debug("statx() does not support '%s' mask (running on an old kernel?)", strnull(mask_str));
×
347
                }
348

349
                return -EUNATCH;
×
350
        }
351

352
        if (!FLAGS_SET(sx.stx_attributes_mask, mandatory_attributes)) {
7,665,529✔
353
                if (DEBUG_LOGGING) {
×
354
                        _cleanup_free_ char *attr_str = statx_attributes_to_string(mandatory_attributes & ~sx.stx_attributes_mask);
×
355
                        log_debug("statx() does not support '%s' attribute (running on an old kernel?)", strnull(attr_str));
×
356
                }
357

358
                return -EUNATCH;
×
359
        }
360

361
        *ret = sx;
7,665,529✔
362
        return FLAGS_SET(sx.stx_mask, optional_mask);
7,665,529✔
363
}
364

365
static int xfstatfs(int fd, struct statfs *ret) {
8,287,820✔
366
        assert(ret);
8,287,820✔
367

368
        if (fd == AT_FDCWD)
8,287,820✔
369
                return RET_NERRNO(statfs(".", ret));
×
370
        if (fd == XAT_FDROOT)
8,287,820✔
371
                return RET_NERRNO(statfs("/", ret));
2✔
372

373
        assert(fd >= 0);
8,287,818✔
374
        return RET_NERRNO(fstatfs(fd, ret));
8,287,818✔
375
}
376

377
int xstatfsat(int dir_fd, const char *path, struct statfs *ret) {
8,264,887✔
378
        _cleanup_close_ int fd = -EBADF;
8,264,887✔
379

380
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
8,264,887✔
381
        assert(ret);
8,264,887✔
382

383
        if (!isempty(path)) {
8,264,887✔
384
                fd = xopenat(dir_fd, path, O_PATH|O_CLOEXEC);
27,140✔
385
                if (fd < 0)
27,140✔
386
                        return fd;
387
                dir_fd = fd;
388
        }
389

390
        return xfstatfs(dir_fd, ret);
8,264,875✔
391
}
392

393
int fd_is_read_only_fs(int fd) {
1,806✔
394
        int r;
1,806✔
395

396
        struct statfs st;
1,806✔
397
        r = xfstatfs(fd, &st);
1,806✔
398
        if (r < 0)
1,806✔
399
                return r;
1,806✔
400

401
        if (st.f_flags & ST_RDONLY)
1,806✔
402
                return true;
403

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

409
        return false;
410
}
411

412
int path_is_read_only_fs(const char *path) {
1,510✔
413
        _cleanup_close_ int fd = -EBADF;
1,510✔
414

415
        assert(path);
1,510✔
416

417
        fd = open(path, O_CLOEXEC | O_PATH);
1,510✔
418
        if (fd < 0)
1,510✔
419
                return -errno;
152✔
420

421
        return fd_is_read_only_fs(fd);
1,358✔
422
}
423

424
int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags) {
15,362✔
425
        struct stat sta, stb;
15,362✔
426
        int r;
15,362✔
427

428
        assert(fda >= 0 || fda == AT_FDCWD);
15,362✔
429
        assert(fdb >= 0 || fdb == AT_FDCWD);
15,362✔
430
        assert((flags & ~(AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT)) == 0);
15,362✔
431

432
        /* Refuse an unset filea or fileb early unless AT_EMPTY_PATH is set */
433
        if ((isempty(filea) || isempty(fileb)) && !FLAGS_SET(flags, AT_EMPTY_PATH))
29,465✔
434
                return -EINVAL;
15,362✔
435

436
        /* Shortcut: comparing the same fd with itself means we can return true */
437
        if (fda >= 0 && fda == fdb && isempty(filea) && isempty(fileb) && FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW))
15,364✔
438
                return true;
439

440
        _cleanup_close_ int pin_a = -EBADF, pin_b = -EBADF;
30,723✔
441
        if (!FLAGS_SET(flags, AT_NO_AUTOMOUNT)) {
15,361✔
442
                /* Let's try to use the name_to_handle_at() AT_HANDLE_FID API to identify identical
443
                 * inodes. We have to issue multiple calls on the same file for that (first, to acquire the
444
                 * FID, and then to check if .st_dev is actually the same). Hence let's pin the inode in
445
                 * between via O_PATH, unless we already have an fd for it. */
446

447
                if (!isempty(filea)) {
15,361✔
448
                        pin_a = openat(fda, filea, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0));
14,103✔
449
                        if (pin_a < 0)
14,103✔
450
                                return -errno;
15,125✔
451

452
                        fda = pin_a;
13,701✔
453
                        filea = NULL;
13,701✔
454
                        flags |= AT_EMPTY_PATH;
13,701✔
455
                }
456

457
                if (!isempty(fileb)) {
14,959✔
458
                        pin_b = openat(fdb, fileb, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0));
13,699✔
459
                        if (pin_b < 0)
13,699✔
460
                                return -errno;
4✔
461

462
                        fdb = pin_b;
13,695✔
463
                        fileb = NULL;
13,695✔
464
                        flags |= AT_EMPTY_PATH;
13,695✔
465
                }
466

467
                int ntha_flags = at_flags_normalize_follow(flags) & (AT_EMPTY_PATH|AT_SYMLINK_FOLLOW);
14,955✔
468
                _cleanup_free_ struct file_handle *ha = NULL, *hb = NULL;
14,955✔
469
                uint64_t mntida, mntidb;
14,955✔
470
                int _mntida, _mntidb;
14,955✔
471

472
                r = name_to_handle_at_try_fid(
14,955✔
473
                                fda,
474
                                filea,
475
                                &ha,
476
                                &_mntida,
477
                                &mntida,
478
                                ntha_flags);
479
                if (r < 0) {
14,955✔
480
                        if (is_name_to_handle_at_fatal_error(r))
×
481
                                return r;
482

483
                        goto fallback;
×
484
                }
485
                if (r == 0)
14,955✔
486
                        mntida = _mntida;
×
487

488
                r = name_to_handle_at_try_fid(
14,955✔
489
                                fdb,
490
                                fileb,
491
                                &hb,
492
                                r > 0 ? NULL : &_mntidb, /* if we managed to get unique mnt id for a, insist on that for b */
493
                                r > 0 ? &mntidb : NULL,
494
                                ntha_flags);
495
                if (r < 0) {
14,955✔
496
                        if (is_name_to_handle_at_fatal_error(r))
×
497
                                return r;
498

499
                        goto fallback;
×
500
                }
501
                if (r == 0)
14,955✔
502
                        mntidb = _mntidb;
×
503

504
                /* Now compare the two file handles */
505
                if (!file_handle_equal(ha, hb))
14,955✔
506
                        return false;
507

508
                /* If the file handles are the same and they come from the same mount ID? Great, then we are
509
                 * good, they are definitely the same */
510
                if (mntida == mntidb)
13,731✔
511
                        return true;
512

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

521
fallback:
×
522
        if (fstatat(fda, strempty(filea), &sta, flags) < 0)
472✔
523
                return log_debug_errno(errno, "Cannot stat %s: %m", strna(filea));
×
524

525
        if (fstatat(fdb, strempty(fileb), &stb, flags) < 0)
472✔
526
                return log_debug_errno(errno, "Cannot stat %s: %m", strna(fileb));
×
527

528
        return stat_inode_same(&sta, &stb);
236✔
529
}
530

531
bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
8,269,517✔
532
        assert(s);
8,269,517✔
533
        assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
8,269,517✔
534

535
        return F_TYPE_EQUAL(s->f_type, magic_value);
8,269,517✔
536
}
537

538
int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value) {
8,264,381✔
539
        int r;
8,264,381✔
540

541
        struct statfs s;
8,264,381✔
542
        r = xstatfsat(dir_fd, path, &s);
8,264,381✔
543
        if (r < 0)
8,264,381✔
544
                return r;
8,264,381✔
545

546
        return is_fs_type(&s, magic_value);
8,264,380✔
547
}
548

549
bool is_temporary_fs(const struct statfs *s) {
10,645✔
550
        return fs_in_group(s, FILESYSTEM_SET_TEMPORARY);
10,645✔
551
}
552

553
bool is_network_fs(const struct statfs *s) {
22,527✔
554
        return fs_in_group(s, FILESYSTEM_SET_NETWORK);
22,527✔
555
}
556

557
int fd_is_temporary_fs(int fd) {
154✔
558
        int r;
154✔
559

560
        struct statfs s;
154✔
561
        r = xfstatfs(fd, &s);
154✔
562
        if (r < 0)
154✔
563
                return r;
154✔
564

565
        return is_temporary_fs(&s);
154✔
566
}
567

568
int fd_is_network_fs(int fd) {
20,985✔
569
        int r;
20,985✔
570

571
        struct statfs s;
20,985✔
572
        r = xfstatfs(fd, &s);
20,985✔
573
        if (r < 0)
20,985✔
574
                return r;
20,985✔
575

576
        return is_network_fs(&s);
20,985✔
577
}
578

579
int path_is_temporary_fs(const char *path) {
11✔
580
        struct statfs s;
11✔
581

582
        if (statfs(path, &s) < 0)
11✔
583
                return -errno;
2✔
584

585
        return is_temporary_fs(&s);
9✔
586
}
587

588
int path_is_network_fs(const char *path) {
×
589
        struct statfs s;
×
590

591
        if (statfs(path, &s) < 0)
×
592
                return -errno;
×
593

594
        return is_network_fs(&s);
×
595
}
596

597
int proc_mounted(void) {
26,508✔
598
        /* This is typically used in error path. So, it is better to not overwrite the original errno. */
599
        PROTECT_ERRNO;
26,508✔
600
        int r;
26,508✔
601

602
        /* A quick check of procfs is properly mounted */
603

604
        r = path_is_fs_type("/proc/", PROC_SUPER_MAGIC);
26,508✔
605
        if (r == -ENOENT) /* not mounted at all */
26,508✔
606
                return false;
×
607

608
        return r;
609
}
610

611
bool stat_inode_same(const struct stat *a, const struct stat *b) {
1,215,431✔
612

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

616
        return stat_is_set(a) && stat_is_set(b) &&
2,428,688✔
617
                ((a->st_mode ^ b->st_mode) & S_IFMT) == 0 &&  /* same inode type */
1,176,788✔
618
                a->st_dev == b->st_dev &&
2,351,944✔
619
                a->st_ino == b->st_ino;
1,136,513✔
620
}
621

622
bool stat_inode_unmodified(const struct stat *a, const struct stat *b) {
125,541✔
623

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

633
        return stat_inode_same(a, b) &&
125,541✔
634
                a->st_mtim.tv_sec == b->st_mtim.tv_sec &&
79,847✔
635
                a->st_mtim.tv_nsec == b->st_mtim.tv_nsec &&
79,563✔
636
                (!S_ISREG(a->st_mode) || a->st_size == b->st_size) && /* if regular file, compare file size */
205,064✔
637
                (!(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 */
79,523✔
638
}
639

640
bool statx_inode_same(const struct statx *a, const struct statx *b) {
3,815,623✔
641

642
        /* Same as stat_inode_same() but for struct statx */
643

644
        if (!statx_is_set(a) || !statx_is_set(b))
7,631,246✔
645
                return false;
646

647
        assert(FLAGS_SET(a->stx_mask, STATX_TYPE|STATX_INO));
3,815,623✔
648
        assert(FLAGS_SET(b->stx_mask, STATX_TYPE|STATX_INO));
3,815,623✔
649

650
        return
3,815,623✔
651
                ((a->stx_mode ^ b->stx_mode) & S_IFMT) == 0 &&
7,627,454✔
652
                a->stx_dev_major == b->stx_dev_major &&
3,811,831✔
653
                a->stx_dev_minor == b->stx_dev_minor &&
11,247,969✔
654
                a->stx_ino == b->stx_ino;
3,613,484✔
655
}
656

657
bool statx_mount_same(const struct statx *a, const struct statx *b) {
3,521,820✔
658
        if (!statx_is_set(a) || !statx_is_set(b))
7,043,640✔
659
                return false;
660

661
        assert(FLAGS_SET(a->stx_mask, STATX_MNT_ID));
3,521,820✔
662
        assert(FLAGS_SET(b->stx_mask, STATX_MNT_ID));
3,521,820✔
663

664
        return a->stx_mnt_id == b->stx_mnt_id;
3,521,820✔
665
}
666

667
usec_t statx_timestamp_load(const struct statx_timestamp *ts) {
5,923✔
668
        return timespec_load(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec });
5,923✔
669
}
670
nsec_t statx_timestamp_load_nsec(const struct statx_timestamp *ts) {
820✔
671
        return timespec_load_nsec(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec });
820✔
672
}
673

674
void inode_hash_func(const struct stat *q, struct siphash *state) {
37,841✔
675
        siphash24_compress_typesafe(q->st_dev, state);
37,841✔
676
        siphash24_compress_typesafe(q->st_ino, state);
37,841✔
677
}
37,841✔
678

679
int inode_compare_func(const struct stat *a, const struct stat *b) {
31,698✔
680
        int r;
31,698✔
681

682
        r = CMP(a->st_dev, b->st_dev);
31,698✔
683
        if (r != 0)
26,782✔
684
                return r;
4,993✔
685

686
        return CMP(a->st_ino, b->st_ino);
26,705✔
687
}
688

689
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(inode_hash_ops, struct stat, inode_hash_func, inode_compare_func, free);
871✔
690

691
const char* inode_type_to_string(mode_t m) {
18,516✔
692

693
        /* Returns a short string for the inode type. We use the same name as the underlying macros for each
694
         * inode type. */
695

696
        switch (m & S_IFMT) {
18,516✔
697
        case S_IFREG:
698
                return "reg";
699
        case S_IFDIR:
6,493✔
700
                return "dir";
6,493✔
701
        case S_IFLNK:
1✔
702
                return "lnk";
1✔
703
        case S_IFCHR:
1,001✔
704
                return "chr";
1,001✔
705
        case S_IFBLK:
462✔
706
                return "blk";
462✔
707
        case S_IFIFO:
462✔
708
                return "fifo";
462✔
709
        case S_IFSOCK:
606✔
710
                return "sock";
606✔
711
        }
712

713
        /* Note anonymous inodes in the kernel will have a zero type. Hence fstat() of an eventfd() will
714
         * return an .st_mode where we'll return NULL here! */
715
        return NULL;
4✔
716
}
717

718
mode_t inode_type_from_string(const char *s) {
13✔
719
        if (!s)
13✔
720
                return MODE_INVALID;
721

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

737
        return MODE_INVALID;
738
}
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