Commit | Line | Data |
---|---|---|
18710679 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
18710679 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
18710679 | 5 | * |
18710679 JG |
6 | */ |
7 | ||
8 | #ifndef _COMPAT_DIRECTORY_HANDLE_H | |
9 | #define _COMPAT_DIRECTORY_HANDLE_H | |
10 | ||
18710679 | 11 | #include <common/credentials.h> |
57b14318 JG |
12 | #include <common/macros.h> |
13 | #include <sys/stat.h> | |
cbf53d23 | 14 | #include <urcu/ref.h> |
18710679 | 15 | |
f75c5439 MD |
16 | enum lttng_directory_handle_rmdir_recursive_flags { |
17 | LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG = (1U << 0), | |
18 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG = (1U << 1), | |
19 | }; | |
20 | ||
18710679 JG |
21 | /* |
22 | * Some platforms, such as Solaris 10, do not support directory file descriptors | |
23 | * and their associated functions (*at(...)), which are defined in POSIX.2008. | |
24 | * | |
25 | * This wrapper provides a handle that is either a copy of a directory's path | |
26 | * or a directory file descriptors, depending on the platform's capabilities. | |
27 | */ | |
28 | #ifdef COMPAT_DIRFD | |
dd95933f JG |
29 | |
30 | struct lttng_directory_handle; | |
31 | ||
32 | typedef void (*lttng_directory_handle_destroy_cb)( | |
33 | struct lttng_directory_handle *handle, void *data); | |
34 | ||
18710679 | 35 | struct lttng_directory_handle { |
cbf53d23 | 36 | struct urcu_ref ref; |
0e985513 | 37 | ino_t directory_inode; |
18710679 | 38 | int dirfd; |
dd95933f JG |
39 | lttng_directory_handle_destroy_cb destroy_cb; |
40 | void *destroy_cb_data; | |
18710679 | 41 | }; |
facc1162 JG |
42 | |
43 | static inline | |
44 | int lttng_directory_handle_get_dirfd( | |
45 | const struct lttng_directory_handle *handle) | |
46 | { | |
47 | return handle->dirfd; | |
48 | } | |
49 | ||
18710679 JG |
50 | #else |
51 | struct lttng_directory_handle { | |
cbf53d23 | 52 | struct urcu_ref ref; |
18710679 JG |
53 | char *base_path; |
54 | }; | |
55 | #endif | |
56 | ||
57 | /* | |
cbf53d23 | 58 | * Create a directory handle to the provided path. Passing a NULL path |
fd774fc6 | 59 | * returns a handle to the current working directory. |
18710679 | 60 | * |
cbf53d23 JG |
61 | * The reference to the directory handle must be released using |
62 | * lttng_directory_handle_put(). | |
18710679 JG |
63 | */ |
64 | LTTNG_HIDDEN | |
cbf53d23 | 65 | struct lttng_directory_handle *lttng_directory_handle_create( |
18710679 JG |
66 | const char *path); |
67 | ||
fd774fc6 | 68 | /* |
cbf53d23 | 69 | * Create a new directory handle to a path relative to an existing handle. |
fd774fc6 JG |
70 | * |
71 | * The provided path must already exist. Note that the creation of a | |
72 | * subdirectory and the creation of a handle are kept as separate operations | |
73 | * to highlight the fact that there is an inherent race between the creation of | |
74 | * a directory and the creation of a handle to it. | |
75 | * | |
76 | * Passing a NULL path effectively copies the original handle. | |
77 | * | |
cbf53d23 JG |
78 | * The reference to the directory handle must be released using |
79 | * lttng_directory_handle_put(). | |
fd774fc6 JG |
80 | */ |
81 | LTTNG_HIDDEN | |
cbf53d23 | 82 | struct lttng_directory_handle *lttng_directory_handle_create_from_handle( |
fd774fc6 | 83 | const char *path, |
cbf53d23 | 84 | const struct lttng_directory_handle *ref_handle); |
fd774fc6 | 85 | |
15d59b1d | 86 | /* |
cbf53d23 | 87 | * Create a new directory handle from an existing directory fd. |
15d59b1d JG |
88 | * |
89 | * The new directory handle assumes the ownership of the directory fd. | |
90 | * Note that this method should only be used in very specific cases, such as | |
91 | * re-creating a directory handle from a dirfd passed over a unix socket. | |
92 | * | |
cbf53d23 JG |
93 | * The reference to the directory handle must be released using |
94 | * lttng_directory_handle_put(). | |
15d59b1d | 95 | */ |
18710679 | 96 | LTTNG_HIDDEN |
cbf53d23 JG |
97 | struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd( |
98 | int dirfd); | |
18710679 | 99 | |
578e21bd JG |
100 | /* |
101 | * Copy a directory handle. | |
cbf53d23 JG |
102 | * |
103 | * The reference to the directory handle must be released using | |
104 | * lttng_directory_handle_put(). | |
578e21bd JG |
105 | */ |
106 | LTTNG_HIDDEN | |
cbf53d23 JG |
107 | struct lttng_directory_handle *lttng_directory_handle_copy( |
108 | const struct lttng_directory_handle *handle); | |
578e21bd | 109 | |
46307ffe | 110 | /* |
cbf53d23 | 111 | * Acquire a reference to a directory handle. |
46307ffe JG |
112 | */ |
113 | LTTNG_HIDDEN | |
cbf53d23 | 114 | bool lttng_directory_handle_get(struct lttng_directory_handle *handle); |
46307ffe | 115 | |
18710679 | 116 | /* |
cbf53d23 | 117 | * Release a reference to a directory handle. |
18710679 JG |
118 | */ |
119 | LTTNG_HIDDEN | |
cbf53d23 | 120 | void lttng_directory_handle_put(struct lttng_directory_handle *handle); |
18710679 JG |
121 | |
122 | /* | |
123 | * Create a subdirectory relative to a directory handle. | |
124 | */ | |
125 | LTTNG_HIDDEN | |
126 | int lttng_directory_handle_create_subdirectory( | |
127 | const struct lttng_directory_handle *handle, | |
128 | const char *subdirectory, | |
129 | mode_t mode); | |
130 | ||
131 | /* | |
132 | * Create a subdirectory relative to a directory handle | |
133 | * as a given user. | |
134 | */ | |
135 | LTTNG_HIDDEN | |
136 | int lttng_directory_handle_create_subdirectory_as_user( | |
137 | const struct lttng_directory_handle *handle, | |
138 | const char *subdirectory, | |
69e3a560 | 139 | mode_t mode, const struct lttng_credentials *creds); |
18710679 JG |
140 | |
141 | /* | |
142 | * Recursively create a directory relative to a directory handle. | |
143 | */ | |
144 | LTTNG_HIDDEN | |
145 | int lttng_directory_handle_create_subdirectory_recursive( | |
146 | const struct lttng_directory_handle *handle, | |
147 | const char *subdirectory_path, | |
148 | mode_t mode); | |
149 | ||
150 | /* | |
151 | * Recursively create a directory relative to a directory handle | |
152 | * as a given user. | |
153 | */ | |
154 | LTTNG_HIDDEN | |
155 | int lttng_directory_handle_create_subdirectory_recursive_as_user( | |
156 | const struct lttng_directory_handle *handle, | |
157 | const char *subdirectory_path, | |
69e3a560 | 158 | mode_t mode, const struct lttng_credentials *creds); |
18710679 | 159 | |
642e96c6 JG |
160 | /* |
161 | * Open a file descriptor to a path relative to a directory handle. | |
162 | */ | |
2912cead JG |
163 | LTTNG_HIDDEN |
164 | int lttng_directory_handle_open_file( | |
165 | const struct lttng_directory_handle *handle, | |
166 | const char *filename, | |
167 | int flags, mode_t mode); | |
168 | ||
642e96c6 JG |
169 | /* |
170 | * Open a file descriptor to a path relative to a directory handle | |
171 | * as a given user. | |
172 | */ | |
2912cead JG |
173 | LTTNG_HIDDEN |
174 | int lttng_directory_handle_open_file_as_user( | |
175 | const struct lttng_directory_handle *handle, | |
176 | const char *filename, | |
177 | int flags, mode_t mode, | |
178 | const struct lttng_credentials *creds); | |
179 | ||
642e96c6 JG |
180 | /* |
181 | * Unlink a file to a path relative to a directory handle. | |
182 | */ | |
2912cead JG |
183 | LTTNG_HIDDEN |
184 | int lttng_directory_handle_unlink_file( | |
185 | const struct lttng_directory_handle *handle, | |
186 | const char *filename); | |
187 | ||
642e96c6 JG |
188 | /* |
189 | * Unlink a file to a path relative to a directory handle as a given user. | |
190 | */ | |
2912cead JG |
191 | LTTNG_HIDDEN |
192 | int lttng_directory_handle_unlink_file_as_user( | |
193 | const struct lttng_directory_handle *handle, | |
194 | const char *filename, | |
195 | const struct lttng_credentials *creds); | |
196 | ||
642e96c6 JG |
197 | /* |
198 | * Rename a file from a path relative to a directory handle to a new | |
199 | * name relative to another directory handle. | |
200 | */ | |
d2956687 JG |
201 | LTTNG_HIDDEN |
202 | int lttng_directory_handle_rename( | |
93bed9fe JG |
203 | const struct lttng_directory_handle *old_handle, |
204 | const char *old_name, | |
205 | const struct lttng_directory_handle *new_handle, | |
206 | const char *new_name); | |
d2956687 | 207 | |
642e96c6 JG |
208 | /* |
209 | * Rename a file from a path relative to a directory handle to a new | |
210 | * name relative to another directory handle as a given user. | |
211 | */ | |
d2956687 JG |
212 | LTTNG_HIDDEN |
213 | int lttng_directory_handle_rename_as_user( | |
93bed9fe JG |
214 | const struct lttng_directory_handle *old_handle, |
215 | const char *old_name, | |
216 | const struct lttng_directory_handle *new_handle, | |
217 | const char *new_name, | |
d2956687 JG |
218 | const struct lttng_credentials *creds); |
219 | ||
642e96c6 JG |
220 | /* |
221 | * Remove a subdirectory relative to a directory handle. | |
222 | */ | |
d2956687 | 223 | LTTNG_HIDDEN |
93bed9fe | 224 | int lttng_directory_handle_remove_subdirectory( |
d2956687 JG |
225 | const struct lttng_directory_handle *handle, |
226 | const char *name); | |
227 | ||
642e96c6 JG |
228 | /* |
229 | * Remove a subdirectory relative to a directory handle as a given user. | |
230 | */ | |
d2956687 | 231 | LTTNG_HIDDEN |
93bed9fe | 232 | int lttng_directory_handle_remove_subdirectory_as_user( |
d2956687 JG |
233 | const struct lttng_directory_handle *handle, |
234 | const char *name, | |
235 | const struct lttng_credentials *creds); | |
236 | ||
642e96c6 JG |
237 | /* |
238 | * Remove a subdirectory and remove its contents if it only | |
239 | * consists in empty directories. | |
f75c5439 | 240 | * @flags: enum lttng_directory_handle_rmdir_recursive_flags |
642e96c6 | 241 | */ |
d2956687 | 242 | LTTNG_HIDDEN |
93bed9fe | 243 | int lttng_directory_handle_remove_subdirectory_recursive( |
d2956687 | 244 | const struct lttng_directory_handle *handle, |
f75c5439 | 245 | const char *name, int flags); |
d2956687 | 246 | |
642e96c6 JG |
247 | /* |
248 | * Remove a subdirectory and remove its contents if it only | |
249 | * consists in empty directories as a given user. | |
f75c5439 | 250 | * @flags: enum lttng_directory_handle_rmdir_recursive_flags |
642e96c6 | 251 | */ |
d2956687 | 252 | LTTNG_HIDDEN |
93bed9fe | 253 | int lttng_directory_handle_remove_subdirectory_recursive_as_user( |
d2956687 JG |
254 | const struct lttng_directory_handle *handle, |
255 | const char *name, | |
f75c5439 MD |
256 | const struct lttng_credentials *creds, |
257 | int flags); | |
d2956687 | 258 | |
57b14318 JG |
259 | /* |
260 | * stat() a file relative to a directory handle. | |
261 | */ | |
262 | LTTNG_HIDDEN | |
263 | int lttng_directory_handle_stat( | |
264 | const struct lttng_directory_handle *handle, | |
265 | const char *name, | |
266 | struct stat *stat_buf); | |
267 | ||
9a1a997f JG |
268 | /* |
269 | * Returns true if this directory handle is backed by a file | |
270 | * descriptor, false otherwise. | |
271 | */ | |
272 | LTTNG_HIDDEN | |
273 | bool lttng_directory_handle_uses_fd( | |
274 | const struct lttng_directory_handle *handle); | |
275 | ||
0e985513 JG |
276 | /* |
277 | * Compare two directory handles. | |
278 | * | |
279 | * Returns true if the two directory handles are equal, false otherwise. | |
280 | */ | |
281 | LTTNG_HIDDEN | |
282 | bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs, | |
283 | const struct lttng_directory_handle *rhs); | |
284 | ||
18710679 | 285 | #endif /* _COMPAT_PATH_HANDLE_H */ |