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