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