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

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 hits per line

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

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

3
#include <fcntl.h>
4
#include <sys/stat.h>
5

6
#include "fd-util.h"
7
#include "fs-util.h"
8
#include "log.h"
9
#include "path-util.h"
10
#include "sync-util.h"
11

12
int fsync_directory_of_file(int fd) {
1,763,249✔
13
        _cleanup_close_ int dfd = -EBADF;
1,763,249✔
14
        struct stat st;
1,763,249✔
15
        int r;
1,763,249✔
16

17
        assert(fd >= 0);
1,763,249✔
18

19
        /* We only reasonably can do this for regular files and directories, or for O_PATH fds, hence check
20
         * for the inode type first */
21
        if (fstat(fd, &st) < 0)
1,763,249✔
22
                return -errno;
×
23

24
        if (S_ISDIR(st.st_mode)) {
1,763,249✔
25
                dfd = openat(fd, "..", O_RDONLY|O_DIRECTORY|O_CLOEXEC, 0);
46✔
26
                if (dfd < 0)
46✔
27
                        return -errno;
×
28

29
        } else if (!S_ISREG(st.st_mode)) { /* Regular files are OK regardless if O_PATH or not, for all other
1,763,203✔
30
                                            * types check O_PATH flag */
31
                r = fd_is_opath(fd);
4✔
32
                if (r < 0)
4✔
33
                        return r;
34
                if (!r) /* If O_PATH this refers to the inode in the fs, in which case we can sensibly do
4✔
35
                         * what is requested. Otherwise this refers to a socket, fifo or device node, where
36
                         * the concept of a containing directory doesn't make too much sense. */
37
                        return -ENOTTY;
38
        }
39

40
        if (dfd < 0) {
41
                _cleanup_free_ char *path = NULL;
1,763,203✔
42

43
                r = fd_get_path(fd, &path);
1,763,203✔
44
                if (r < 0) {
1,763,203✔
45
                        log_debug_errno(r, "Failed to query /proc/self/fd/%d%s: %m",
×
46
                                        fd,
47
                                        r == -ENOSYS ? ", ignoring" : "");
48

49
                        if (r == -ENOSYS)
×
50
                                /* If /proc is not available, we're most likely running in some
51
                                 * chroot environment, and syncing the directory is not very
52
                                 * important in that case. Let's just silently do nothing. */
53
                                return 0;
54

55
                        return r;
×
56
                }
57

58
                if (!path_is_absolute(path))
1,763,203✔
59
                        return -EINVAL;
60

61
                dfd = open_parent(path, O_CLOEXEC|O_NOFOLLOW, 0);
1,763,203✔
62
                if (dfd < 0)
1,763,203✔
63
                        return dfd;
64
        }
65

66
        return RET_NERRNO(fsync(dfd));
1,763,249✔
67
}
68

69
int fsync_full(int fd) {
1,761,314✔
70
        int r, q;
1,761,314✔
71

72
        /* Sync both the file and the directory */
73

74
        r = RET_NERRNO(fsync(fd));
1,761,314✔
75

76
        q = fsync_directory_of_file(fd);
1,761,314✔
77
        if (r < 0) /* Return earlier error */
1,761,314✔
78
                return r;
79
        if (q == -ENOTTY) /* Ignore if the 'fd' refers to a block device or so which doesn't really have a
1,761,314✔
80
                           * parent dir */
81
                return 0;
×
82
        return q;
83
}
84

85
int fsync_path_at(int at_fd, const char *path) {
1✔
86
        _cleanup_close_ int opened_fd = -EBADF;
1✔
87
        int fd;
1✔
88

89
        if (isempty(path)) {
1✔
90
                if (at_fd == AT_FDCWD) {
×
91
                        opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
×
92
                        if (opened_fd < 0)
×
93
                                return -errno;
×
94

95
                        fd = opened_fd;
96
                } else
97
                        fd = at_fd;
98
        } else {
99
                opened_fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1✔
100
                if (opened_fd < 0)
1✔
101
                        return -errno;
×
102

103
                fd = opened_fd;
104
        }
105

106
        return RET_NERRNO(fsync(fd));
1✔
107
}
108

109
int fsync_parent_at(int at_fd, const char *path) {
132✔
110
        _cleanup_close_ int opened_fd = -EBADF;
132✔
111

112
        if (isempty(path)) {
132✔
113
                if (at_fd != AT_FDCWD)
×
114
                        return fsync_directory_of_file(at_fd);
×
115

116
                opened_fd = open("..", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
×
117
                if (opened_fd < 0)
×
118
                        return -errno;
×
119

120
                return RET_NERRNO(fsync(opened_fd));
×
121
        }
122

123
        opened_fd = openat(at_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
132✔
124
        if (opened_fd < 0)
132✔
125
                return -errno;
×
126

127
        return fsync_directory_of_file(opened_fd);
132✔
128
}
129

130
int fsync_path_and_parent_at(int at_fd, const char *path) {
32✔
131
        _cleanup_close_ int opened_fd = -EBADF;
32✔
132

133
        if (isempty(path)) {
32✔
134
                if (at_fd != AT_FDCWD)
×
135
                        return fsync_full(at_fd);
×
136

137
                opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
×
138
        } else
139
                opened_fd = openat(at_fd, path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC);
32✔
140
        if (opened_fd < 0)
32✔
141
                return -errno;
×
142

143
        return fsync_full(opened_fd);
32✔
144
}
145

146
int syncfs_path(int at_fd, const char *path) {
617✔
147
        _cleanup_close_ int fd = -EBADF;
617✔
148

149
        if (isempty(path)) {
617✔
150
                if (at_fd != AT_FDCWD)
×
151
                        return RET_NERRNO(syncfs(at_fd));
×
152

153
                fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
×
154
        } else
155
                fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
617✔
156
        if (fd < 0)
617✔
157
                return -errno;
×
158

159
        return RET_NERRNO(syncfs(fd));
617✔
160
}
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