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