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

systemd / systemd / 21192089104

20 Jan 2026 11:35PM UTC coverage: 72.524% (-0.3%) from 72.818%
21192089104

push

github

yuwata
mkdir: reset mtime *after* fchown()

Follow-up for 34c3d5747

Also, drop pointless shortcut.

1 of 2 new or added lines in 1 file covered. (50.0%)

2960 existing lines in 48 files now uncovered.

309808 of 427181 relevant lines covered (72.52%)

1236537.64 hits per line

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

92.23
/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 "chase.h"
10
#include "dirent-util.h"
11
#include "errno-util.h"
12
#include "fd-util.h"
13
#include "filesystems.h"
14
#include "fs-util.h"
15
#include "hash-funcs.h"
16
#include "log.h"
17
#include "mountpoint-util.h"
18
#include "path-util.h"
19
#include "siphash24.h"
20
#include "stat-util.h"
21
#include "string-util.h"
22
#include "time-util.h"
23

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

31
        struct stat st;
2,107,597✔
32
        int r;
2,107,597✔
33

34
        assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
2,107,597✔
35
        assert(!isempty(path) || !follow);
2,107,597✔
36
        assert(verify_func);
2,107,597✔
37

38
        _cleanup_free_ char *p = NULL;
2,107,597✔
39
        if (fd == XAT_FDROOT) {
2,107,597✔
40
                fd = AT_FDCWD;
×
41

42
                if (isempty(path))
×
43
                        path = "/";
44
                else if (!path_is_absolute(path)) {
×
45
                        p = strjoin("/", path);
×
46
                        if (!p)
×
47
                                return -ENOMEM;
48

49
                        path = p;
50
                }
51
        }
52

53
        if (fstatat(fd, strempty(path), &st,
5,074,992✔
54
                    (isempty(path) ? AT_EMPTY_PATH : 0) | (follow ? 0 : AT_SYMLINK_NOFOLLOW)) < 0)
2,107,597✔
55
                return -errno;
7,423✔
56

57
        r = verify_func(&st);
2,100,174✔
58
        return verify ? r : r >= 0;
2,100,174✔
59
}
60

61
int stat_verify_regular(const struct stat *st) {
2,727,090✔
62
        assert(st);
2,727,090✔
63

64
        /* Checks whether the specified stat() structure refers to a regular file. If not returns an
65
         * appropriate error code. */
66

67
        if (S_ISDIR(st->st_mode))
2,727,090✔
68
                return -EISDIR;
69

70
        if (S_ISLNK(st->st_mode))
2,724,897✔
71
                return -ELOOP;
72

73
        if (!S_ISREG(st->st_mode))
2,724,860✔
74
                return -EBADFD;
17✔
75

76
        return 0;
77
}
78

79
int verify_regular_at(int fd, const char *path, bool follow) {
855,297✔
80
        return verify_stat_at(fd, path, follow, stat_verify_regular, true);
855,297✔
81
}
82

83
int fd_verify_regular(int fd) {
855,014✔
84
        if (IN_SET(fd, AT_FDCWD, XAT_FDROOT))
855,014✔
85
                return -EISDIR;
86

87
        return verify_regular_at(fd, /* path= */ NULL, /* follow= */ false);
855,014✔
88
}
89

90
int stat_verify_directory(const struct stat *st) {
1,249,947✔
91
        assert(st);
1,249,947✔
92

93
        if (S_ISLNK(st->st_mode))
1,249,947✔
94
                return -ELOOP;
95

96
        if (!S_ISDIR(st->st_mode))
1,249,937✔
97
                return -ENOTDIR;
4✔
98

99
        return 0;
100
}
101

102
int fd_verify_directory(int fd) {
61✔
103
        if (IN_SET(fd, AT_FDCWD, XAT_FDROOT))
61✔
104
                return 0;
105

106
        return verify_stat_at(fd, NULL, false, stat_verify_directory, true);
60✔
107
}
108

109
int is_dir_at(int fd, const char *path, bool follow) {
1,230,159✔
110
        return verify_stat_at(fd, path, follow, stat_verify_directory, false);
1,230,159✔
111
}
112

113
int is_dir(const char *path, bool follow) {
444,533✔
114
        assert(!isempty(path));
444,533✔
115
        return is_dir_at(AT_FDCWD, path, follow);
444,533✔
116
}
117

118
int stat_verify_symlink(const struct stat *st) {
21,904✔
119
        assert(st);
21,904✔
120

121
        if (S_ISDIR(st->st_mode))
21,904✔
122
                return -EISDIR;
123

124
        if (!S_ISLNK(st->st_mode))
21,884✔
125
                return -ENOLINK;
290✔
126

127
        return 0;
128
}
129

130
int fd_verify_symlink(int fd) {
4,677✔
131
        return verify_stat_at(fd, /* path= */ NULL, /* follow= */ false, stat_verify_symlink, /* verify= */ true);
4,677✔
132
}
133

134
int is_symlink(const char *path) {
17,227✔
135
        assert(!isempty(path));
17,227✔
136
        return verify_stat_at(AT_FDCWD, path, false, stat_verify_symlink, false);
17,227✔
137
}
138

139
int stat_verify_linked(const struct stat *st) {
1,811,048✔
140
        assert(st);
1,811,048✔
141

142
        if (st->st_nlink <= 0)
1,811,048✔
143
                return -EIDRM; /* recognizable error. */
2✔
144

145
        return 0;
146
}
147

148
int fd_verify_linked(int fd) {
48✔
149

150
        if (fd == XAT_FDROOT)
48✔
151
                return 0;
152

153
        return verify_stat_at(fd, NULL, false, stat_verify_linked, true);
47✔
154
}
155

156
int stat_verify_device_node(const struct stat *st) {
3,587✔
157
        assert(st);
3,587✔
158

159
        if (S_ISLNK(st->st_mode))
3,587✔
160
                return -ELOOP;
161

162
        if (S_ISDIR(st->st_mode))
3,587✔
163
                return -EISDIR;
164

165
        if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
3,466✔
166
                return -ENOTTY;
5✔
167

168
        return 0;
169
}
170

171
int is_device_node(const char *path) {
130✔
172
        assert(!isempty(path));
130✔
173
        return verify_stat_at(AT_FDCWD, path, false, stat_verify_device_node, false);
130✔
174
}
175

176
int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) {
38,207✔
177
        _cleanup_close_ int fd = -EBADF;
38,207✔
178
        struct dirent *buf;
38,207✔
179
        size_t m;
38,207✔
180

181
        fd = xopenat(dir_fd, path, O_DIRECTORY|O_CLOEXEC);
38,207✔
182
        if (fd < 0)
38,207✔
183
                return fd;
184

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

192
        for (;;) {
3,534✔
193
                struct dirent *de;
3,534✔
194
                ssize_t n;
3,534✔
195

196
                n = getdents64(fd, buf, m);
3,534✔
197
                if (n < 0)
3,534✔
198
                        return -errno;
×
199
                if (n == 0)
3,534✔
200
                        break;
201

202
                assert((size_t) n <= m);
2,171✔
203
                msan_unpoison(buf, n);
2,171✔
204

205
                FOREACH_DIRENT_IN_BUFFER(de, buf, n)
6,067✔
206
                        if (!(ignore_hidden_or_backup ? hidden_or_backup_file(de->d_name) : dot_or_dot_dot(de->d_name)))
4,704✔
207
                                return 0;
208
        }
209

210
        return 1;
211
}
212

213
bool stat_may_be_dev_null(struct stat *st) {
427,594✔
214
        assert(st);
427,594✔
215

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

219
        return S_ISCHR(st->st_mode);
427,594✔
220
}
221

222
bool stat_is_empty(struct stat *st) {
410,781✔
223
        assert(st);
410,781✔
224

225
        return S_ISREG(st->st_mode) && st->st_size <= 0;
410,781✔
226
}
227

228
int null_or_empty_path_with_root(const char *fn, const char *root) {
308,031✔
229
        struct stat st;
308,031✔
230
        int r;
308,031✔
231

232
        assert(fn);
308,031✔
233

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

238
        if (path_equal(path_startswith(fn, root ?: "/"), "dev/null"))
616,062✔
239
                return true;
308,031✔
240

241
        r = chase_and_stat(fn, root, CHASE_PREFIX_ROOT, NULL, &st);
307,845✔
242
        if (r < 0)
307,845✔
243
                return r;
244

245
        return null_or_empty(&st);
307,729✔
246
}
247

248
static int xfstatfs(int fd, struct statfs *ret) {
8,356,380✔
249
        assert(ret);
8,356,380✔
250

251
        if (fd == AT_FDCWD)
8,356,380✔
252
                return RET_NERRNO(statfs(".", ret));
×
253
        if (fd == XAT_FDROOT)
8,356,380✔
254
                return RET_NERRNO(statfs("/", ret));
2✔
255

256
        assert(fd >= 0);
8,356,378✔
257
        return RET_NERRNO(fstatfs(fd, ret));
8,356,378✔
258
}
259

260
int xstatfsat(int dir_fd, const char *path, struct statfs *ret) {
8,333,247✔
261
        _cleanup_close_ int fd = -EBADF;
8,333,247✔
262

263
        assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
8,333,247✔
264
        assert(ret);
8,333,247✔
265

266
        if (!isempty(path)) {
8,333,247✔
267
                fd = xopenat(dir_fd, path, O_PATH|O_CLOEXEC);
28,068✔
268
                if (fd < 0)
28,068✔
269
                        return fd;
270
                dir_fd = fd;
271
        }
272

273
        return xfstatfs(dir_fd, ret);
8,333,238✔
274
}
275

276
int fd_is_read_only_fs(int fd) {
1,854✔
277
        int r;
1,854✔
278

279
        struct statfs st;
1,854✔
280
        r = xfstatfs(fd, &st);
1,854✔
281
        if (r < 0)
1,854✔
282
                return r;
1,854✔
283

284
        if (st.f_flags & ST_RDONLY)
1,854✔
285
                return true;
286

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

292
        return false;
293
}
294

295
int path_is_read_only_fs(const char *path) {
1,576✔
296
        _cleanup_close_ int fd = -EBADF;
1,576✔
297

298
        assert(path);
1,576✔
299

300
        fd = open(path, O_CLOEXEC | O_PATH);
1,576✔
301
        if (fd < 0)
1,576✔
302
                return -errno;
170✔
303

304
        return fd_is_read_only_fs(fd);
1,406✔
305
}
306

307
int inode_same_at(int fda, const char *filea, int fdb, const char *fileb, int flags) {
13,978✔
308
        struct stat sta, stb;
13,978✔
309
        int r;
13,978✔
310

311
        assert(fda >= 0 || fda == AT_FDCWD);
13,978✔
312
        assert(fdb >= 0 || fdb == AT_FDCWD);
13,978✔
313
        assert((flags & ~(AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT)) == 0);
13,978✔
314

315
        /* Refuse an unset filea or fileb early unless AT_EMPTY_PATH is set */
316
        if ((isempty(filea) || isempty(fileb)) && !FLAGS_SET(flags, AT_EMPTY_PATH))
27,609✔
317
                return -EINVAL;
13,978✔
318

319
        /* Shortcut: comparing the same fd with itself means we can return true */
320
        if (fda >= 0 && fda == fdb && isempty(filea) && isempty(fileb) && FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW))
13,980✔
321
                return true;
322

323
        _cleanup_close_ int pin_a = -EBADF, pin_b = -EBADF;
27,955✔
324
        if (!FLAGS_SET(flags, AT_NO_AUTOMOUNT)) {
13,977✔
325
                /* Let's try to use the name_to_handle_at() AT_HANDLE_FID API to identify identical
326
                 * inodes. We have to issue multiple calls on the same file for that (first, to acquire the
327
                 * FID, and then to check if .st_dev is actually the same). Hence let's pin the inode in
328
                 * between via O_PATH, unless we already have an fd for it. */
329

330
                if (!isempty(filea)) {
13,977✔
331
                        pin_a = openat(fda, filea, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0));
13,631✔
332
                        if (pin_a < 0)
13,631✔
333
                                return -errno;
13,739✔
334

335
                        fda = pin_a;
13,260✔
336
                        filea = NULL;
13,260✔
337
                        flags |= AT_EMPTY_PATH;
13,260✔
338
                }
339

340
                if (!isempty(fileb)) {
13,606✔
341
                        pin_b = openat(fdb, fileb, O_PATH|O_CLOEXEC|(FLAGS_SET(flags, AT_SYMLINK_NOFOLLOW) ? O_NOFOLLOW : 0));
13,258✔
342
                        if (pin_b < 0)
13,258✔
343
                                return -errno;
4✔
344

345
                        fdb = pin_b;
13,254✔
346
                        fileb = NULL;
13,254✔
347
                        flags |= AT_EMPTY_PATH;
13,254✔
348
                }
349

350
                int ntha_flags = at_flags_normalize_follow(flags) & (AT_EMPTY_PATH|AT_SYMLINK_FOLLOW);
13,602✔
351
                _cleanup_free_ struct file_handle *ha = NULL, *hb = NULL;
13,602✔
352
                int mntida = -1, mntidb = -1;
13,602✔
353

354
                r = name_to_handle_at_try_fid(
13,602✔
355
                                fda,
356
                                filea,
357
                                &ha,
358
                                &mntida,
359
                                ntha_flags);
360
                if (r < 0) {
13,602✔
361
                        if (is_name_to_handle_at_fatal_error(r))
×
362
                                return r;
363

364
                        goto fallback;
×
365
                }
366

367
                r = name_to_handle_at_try_fid(
13,602✔
368
                                fdb,
369
                                fileb,
370
                                &hb,
371
                                &mntidb,
372
                                ntha_flags);
373
                if (r < 0) {
13,602✔
374
                        if (is_name_to_handle_at_fatal_error(r))
×
375
                                return r;
376

377
                        goto fallback;
×
378
                }
379

380
                /* Now compare the two file handles */
381
                if (!file_handle_equal(ha, hb))
13,602✔
382
                        return false;
383

384
                /* If the file handles are the same and they come from the same mount ID? Great, then we are
385
                 * good, they are definitely the same */
386
                if (mntida == mntidb)
13,257✔
387
                        return true;
388

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

397
fallback:
×
398
        if (fstatat(fda, strempty(filea), &sta, flags) < 0)
476✔
399
                return log_debug_errno(errno, "Cannot stat %s: %m", strna(filea));
×
400

401
        if (fstatat(fdb, strempty(fileb), &stb, flags) < 0)
476✔
402
                return log_debug_errno(errno, "Cannot stat %s: %m", strna(fileb));
×
403

404
        return stat_inode_same(&sta, &stb);
238✔
405
}
406

407
bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
8,337,898✔
408
        assert(s);
8,337,898✔
409
        assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
8,337,898✔
410

411
        return F_TYPE_EQUAL(s->f_type, magic_value);
8,337,898✔
412
}
413

414
int is_fs_type_at(int dir_fd, const char *path, statfs_f_type_t magic_value) {
8,332,786✔
415
        int r;
8,332,786✔
416

417
        struct statfs s;
8,332,786✔
418
        r = xstatfsat(dir_fd, path, &s);
8,332,786✔
419
        if (r < 0)
8,332,786✔
420
                return r;
8,332,786✔
421

422
        return is_fs_type(&s, magic_value);
8,332,785✔
423
}
424

425
bool is_temporary_fs(const struct statfs *s) {
9,876✔
426
        return fs_in_group(s, FILESYSTEM_SET_TEMPORARY);
9,876✔
427
}
428

429
bool is_network_fs(const struct statfs *s) {
22,721✔
430
        return fs_in_group(s, FILESYSTEM_SET_NETWORK);
22,721✔
431
}
432

433
int fd_is_temporary_fs(int fd) {
157✔
434
        int r;
157✔
435

436
        struct statfs s;
157✔
437
        r = xfstatfs(fd, &s);
157✔
438
        if (r < 0)
157✔
439
                return r;
157✔
440

441
        return is_temporary_fs(&s);
157✔
442
}
443

444
int fd_is_network_fs(int fd) {
21,131✔
445
        int r;
21,131✔
446

447
        struct statfs s;
21,131✔
448
        r = xfstatfs(fd, &s);
21,131✔
449
        if (r < 0)
21,131✔
450
                return r;
21,131✔
451

452
        return is_network_fs(&s);
21,131✔
453
}
454

455
int path_is_temporary_fs(const char *path) {
11✔
456
        struct statfs s;
11✔
457

458
        if (statfs(path, &s) < 0)
11✔
459
                return -errno;
2✔
460

461
        return is_temporary_fs(&s);
9✔
462
}
463

464
int path_is_network_fs(const char *path) {
×
465
        struct statfs s;
×
466

467
        if (statfs(path, &s) < 0)
×
468
                return -errno;
×
469

470
        return is_network_fs(&s);
×
471
}
472

473
int proc_mounted(void) {
27,481✔
474
        /* This is typically used in error path. So, it is better to not overwrite the original errno. */
475
        PROTECT_ERRNO;
27,481✔
476
        int r;
27,481✔
477

478
        /* A quick check of procfs is properly mounted */
479

480
        r = path_is_fs_type("/proc/", PROC_SUPER_MAGIC);
27,481✔
481
        if (r == -ENOENT) /* not mounted at all */
27,481✔
UNCOV
482
                return false;
×
483

484
        return r;
485
}
486

487
bool stat_inode_same(const struct stat *a, const struct stat *b) {
1,190,350✔
488

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

492
        return stat_is_set(a) && stat_is_set(b) &&
2,378,538✔
493
                ((a->st_mode ^ b->st_mode) & S_IFMT) == 0 &&  /* same inode type */
1,149,848✔
494
                a->st_dev == b->st_dev &&
2,296,510✔
495
                a->st_ino == b->st_ino;
1,106,160✔
496
}
497

498
bool stat_inode_unmodified(const struct stat *a, const struct stat *b) {
126,974✔
499

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

509
        return stat_inode_same(a, b) &&
126,974✔
510
                a->st_mtim.tv_sec == b->st_mtim.tv_sec &&
79,436✔
511
                a->st_mtim.tv_nsec == b->st_mtim.tv_nsec &&
79,136✔
512
                (!S_ISREG(a->st_mode) || a->st_size == b->st_size) && /* if regular file, compare file size */
206,078✔
513
                (!(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,104✔
514
}
515

516
bool statx_inode_same(const struct statx *a, const struct statx *b) {
4,484,714✔
517

518
        /* Same as stat_inode_same() but for struct statx */
519

520
        if (!statx_is_set(a) || !statx_is_set(b))
8,969,428✔
521
                return false;
522

523
        assert(FLAGS_SET(a->stx_mask, STATX_TYPE|STATX_INO));
4,484,714✔
524
        assert(FLAGS_SET(b->stx_mask, STATX_TYPE|STATX_INO));
4,484,714✔
525

526
        return
4,484,714✔
527
                ((a->stx_mode ^ b->stx_mode) & S_IFMT) == 0 &&
8,965,579✔
528
                a->stx_dev_major == b->stx_dev_major &&
4,480,865✔
529
                a->stx_dev_minor == b->stx_dev_minor &&
13,260,062✔
530
                a->stx_ino == b->stx_ino;
4,288,060✔
531
}
532

533
bool statx_mount_same(const struct statx *a, const struct statx *b) {
4,202,122✔
534
        if (!statx_is_set(a) || !statx_is_set(b))
8,404,244✔
535
                return false;
536

537
        assert(FLAGS_SET(a->stx_mask, STATX_MNT_ID));
4,202,122✔
538
        assert(FLAGS_SET(b->stx_mask, STATX_MNT_ID));
4,202,122✔
539

540
        return a->stx_mnt_id == b->stx_mnt_id;
4,202,122✔
541
}
542

543
usec_t statx_timestamp_load(const struct statx_timestamp *ts) {
5,160✔
544
        return timespec_load(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec });
5,160✔
545
}
546
nsec_t statx_timestamp_load_nsec(const struct statx_timestamp *ts) {
820✔
547
        return timespec_load_nsec(&(const struct timespec) { .tv_sec = ts->tv_sec, .tv_nsec = ts->tv_nsec });
820✔
548
}
549

550
void inode_hash_func(const struct stat *q, struct siphash *state) {
37,735✔
551
        siphash24_compress_typesafe(q->st_dev, state);
37,735✔
552
        siphash24_compress_typesafe(q->st_ino, state);
37,735✔
553
}
37,735✔
554

555
int inode_compare_func(const struct stat *a, const struct stat *b) {
31,442✔
556
        int r;
31,442✔
557

558
        r = CMP(a->st_dev, b->st_dev);
31,442✔
559
        if (r != 0)
26,565✔
560
                return r;
4,977✔
561

562
        return CMP(a->st_ino, b->st_ino);
26,465✔
563
}
564

565
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(inode_hash_ops, struct stat, inode_hash_func, inode_compare_func, free);
864✔
566

567
const char* inode_type_to_string(mode_t m) {
13,664✔
568

569
        /* Returns a short string for the inode type. We use the same name as the underlying macros for each
570
         * inode type. */
571

572
        switch (m & S_IFMT) {
13,664✔
573
        case S_IFREG:
574
                return "reg";
575
        case S_IFDIR:
5,944✔
576
                return "dir";
5,944✔
577
        case S_IFLNK:
1✔
578
                return "lnk";
1✔
579
        case S_IFCHR:
1,007✔
580
                return "chr";
1,007✔
581
        case S_IFBLK:
463✔
582
                return "blk";
463✔
583
        case S_IFIFO:
463✔
584
                return "fifo";
463✔
585
        case S_IFSOCK:
607✔
586
                return "sock";
607✔
587
        }
588

589
        /* Note anonymous inodes in the kernel will have a zero type. Hence fstat() of an eventfd() will
590
         * return an .st_mode where we'll return NULL here! */
591
        return NULL;
4✔
592
}
593

594
mode_t inode_type_from_string(const char *s) {
13✔
595
        if (!s)
13✔
596
                return MODE_INVALID;
597

598
        if (streq(s, "reg"))
13✔
599
                return S_IFREG;
600
        if (streq(s, "dir"))
10✔
601
                return S_IFDIR;
602
        if (streq(s, "lnk"))
7✔
603
                return S_IFLNK;
604
        if (streq(s, "chr"))
6✔
605
                return S_IFCHR;
606
        if (streq(s, "blk"))
5✔
607
                return S_IFBLK;
608
        if (streq(s, "fifo"))
4✔
609
                return S_IFIFO;
610
        if (streq(s, "sock"))
2✔
611
                return S_IFSOCK;
2✔
612

613
        return MODE_INVALID;
614
}
615

616
int statx_warn_mount_root(const struct statx *sx, int log_level) {
82,082✔
617
        assert(sx);
82,082✔
618

619
        /* The STATX_ATTR_MOUNT_ROOT flag is supported since kernel v5.8. */
620
        if (!FLAGS_SET(sx->stx_attributes_mask, STATX_ATTR_MOUNT_ROOT))
82,082✔
UNCOV
621
                return log_full_errno(log_level, SYNTHETIC_ERRNO(ENOSYS),
×
622
                                      "statx() did not set STATX_ATTR_MOUNT_ROOT, running on an old kernel?");
623

624
        return 0;
625
}
626

627
int statx_warn_mount_id(const struct statx *sx, int log_level) {
6,931✔
628
        assert(sx);
6,931✔
629

630
        /* The STATX_MNT_ID flag is supported since kernel v5.10. */
631
        if (!FLAGS_SET(sx->stx_mask, STATX_MNT_ID))
6,931✔
UNCOV
632
                return log_full_errno(log_level, SYNTHETIC_ERRNO(ENOSYS),
×
633
                                      "statx() does not support STATX_MNT_ID, running on an old kernel?");
634

635
        return 0;
636
}
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