Debian Codegraph @

src/basic/sync-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "errno-util.h"
9 #include "fd-util.h"
10 #include "fs-util.h"
11 #include "log.h"
12 #include "path-util.h"
13 #include "string-util.h"
14 #include "sync-util.h"
15
16 int fsync_directory_of_file(int fd) {
17 _cleanup_close_ int dfd = -EBADF;
18 struct stat st;
19 int r;
20
21 assert(fd >= 0);
22
23 /* We only reasonably can do this for regular files and directories, or for O_PATH fds, hence check
24 * for the inode type first */
25 if (fstat(fd, &st) < 0)
26 return -errno;
27
28 if (S_ISDIR(st.st_mode)) {
29 dfd = openat(fd, "..", O_RDONLY|O_DIRECTORY|O_CLOEXEC, 0);
30 if (dfd < 0)
31 return -errno;
32
33 } else if (!S_ISREG(st.st_mode)) { /* Regular files are OK regardless if O_PATH or not, for all other
34 * types check O_PATH flag */
35 r = fd_is_opath(fd);
36 if (r < 0)
37 return r;
38 if (!r) /* If O_PATH this refers to the inode in the fs, in which case we can sensibly do
39 * what is requested. Otherwise this refers to a socket, fifo or device node, where
40 * the concept of a containing directory doesn't make too much sense. */
41 return -ENOTTY;
42 }
43
44 if (dfd < 0) {
45 _cleanup_free_ char *path = NULL;
46
47 r = fd_get_path(fd, &path);
48 if (r < 0) {
49 log_debug_errno(r, "Failed to query /proc/self/fd/%d%s: %m",
50 fd,
51 r == -ENOSYS ? ", ignoring" : "");
52
53 if (r == -ENOSYS)
54 /* If /proc is not available, we're most likely running in some
55 * chroot environment, and syncing the directory is not very
56 * important in that case. Let's just silently do nothing. */
57 return 0;
58
59 return r;
60 }
61
62 if (!path_is_absolute(path))
63 return -EINVAL;
64
65 dfd = open_parent(path, O_CLOEXEC|O_NOFOLLOW, 0);
66 if (dfd < 0)
67 return dfd;
68 }
69
70 return RET_NERRNO(fsync(dfd));
71 }
72
73 int fsync_full(int fd) {
74 int r, q;
75
76 /* Sync both the file and the directory */
77
78 r = RET_NERRNO(fsync(fd));
79
80 q = fsync_directory_of_file(fd);
81 if (r < 0) /* Return earlier error */
82 return r;
83 if (q == -ENOTTY) /* Ignore if the 'fd' refers to a block device or so which doesn't really have a
84 * parent dir */
85 return 0;
86 return q;
87 }
88
89 int fsync_path_at(int at_fd, const char *path) {
90 _cleanup_close_ int opened_fd = -EBADF;
91 int fd;
92
93 if (isempty(path)) {
94 if (at_fd == AT_FDCWD) {
95 opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
96 if (opened_fd < 0)
97 return -errno;
98
99 fd = opened_fd;
100 } else
101 fd = at_fd;
102 } else {
103 opened_fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
104 if (opened_fd < 0)
105 return -errno;
106
107 fd = opened_fd;
108 }
109
110 return RET_NERRNO(fsync(fd));
111 }
112
113 int fsync_parent_at(int at_fd, const char *path) {
114 _cleanup_close_ int opened_fd = -EBADF;
115
116 if (isempty(path)) {
117 if (at_fd != AT_FDCWD)
118 return fsync_directory_of_file(at_fd);
119
120 opened_fd = open("..", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
121 if (opened_fd < 0)
122 return -errno;
123
124 return RET_NERRNO(fsync(opened_fd));
125 }
126
127 opened_fd = openat(at_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
128 if (opened_fd < 0)
129 return -errno;
130
131 return fsync_directory_of_file(opened_fd);
132 }
133
134 int fsync_path_and_parent_at(int at_fd, const char *path) {
135 _cleanup_close_ int opened_fd = -EBADF;
136
137 if (isempty(path)) {
138 if (at_fd != AT_FDCWD)
139 return fsync_full(at_fd);
140
141 opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
142 } else
143 opened_fd = openat(at_fd, path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC);
144 if (opened_fd < 0)
145 return -errno;
146
147 return fsync_full(opened_fd);
148 }
149
150 int syncfs_path(int at_fd, const char *path) {
151 _cleanup_close_ int fd = -EBADF;
152
153 if (isempty(path)) {
154 if (at_fd != AT_FDCWD)
155 return RET_NERRNO(syncfs(at_fd));
156
157 fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
158 } else
159 fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
160 if (fd < 0)
161 return -errno;
162
163 return RET_NERRNO(syncfs(fd));
164 }