docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / common / compat / directory-handle.hpp
1 /*
2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #ifndef _COMPAT_DIRECTORY_HANDLE_H
9 #define _COMPAT_DIRECTORY_HANDLE_H
10
11 #include <common/credentials.hpp>
12 #include <common/macros.hpp>
13 #include <sys/stat.h>
14 #include <urcu/ref.h>
15
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
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 HAVE_DIRFD
29
30 struct lttng_directory_handle;
31
32 using lttng_directory_handle_destroy_cb = void (*)(struct lttng_directory_handle *, void *);
33
34 struct lttng_directory_handle {
35 struct urcu_ref ref;
36 ino_t directory_inode;
37 int dirfd;
38 lttng_directory_handle_destroy_cb destroy_cb;
39 void *destroy_cb_data;
40 };
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
49 #else
50 struct lttng_directory_handle {
51 struct urcu_ref ref;
52 char *base_path;
53 };
54 #endif
55
56 /*
57 * Create a directory handle to the provided path. Passing a NULL path
58 * returns a handle to the current working directory.
59 *
60 * The reference to the directory handle must be released using
61 * lttng_directory_handle_put().
62 */
63 struct lttng_directory_handle *lttng_directory_handle_create(
64 const char *path);
65
66 /*
67 * Create a new directory handle to a path relative to an existing handle.
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 *
76 * The reference to the directory handle must be released using
77 * lttng_directory_handle_put().
78 */
79 struct lttng_directory_handle *lttng_directory_handle_create_from_handle(
80 const char *path,
81 const struct lttng_directory_handle *ref_handle);
82
83 /*
84 * Create a new directory handle from an existing directory fd.
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 *
90 * The reference to the directory handle must be released using
91 * lttng_directory_handle_put().
92 */
93 struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd(
94 int dirfd);
95
96 /*
97 * Copy a directory handle.
98 *
99 * The reference to the directory handle must be released using
100 * lttng_directory_handle_put().
101 */
102 struct lttng_directory_handle *lttng_directory_handle_copy(
103 const struct lttng_directory_handle *handle);
104
105 /*
106 * Acquire a reference to a directory handle.
107 */
108 bool lttng_directory_handle_get(struct lttng_directory_handle *handle);
109
110 /*
111 * Release a reference to a directory handle.
112 */
113 void lttng_directory_handle_put(struct lttng_directory_handle *handle);
114
115 /*
116 * Create a subdirectory relative to a directory handle.
117 */
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 */
127 int lttng_directory_handle_create_subdirectory_as_user(
128 const struct lttng_directory_handle *handle,
129 const char *subdirectory,
130 mode_t mode, const struct lttng_credentials *creds);
131
132 /*
133 * Recursively create a directory relative to a directory handle.
134 */
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 */
144 int lttng_directory_handle_create_subdirectory_recursive_as_user(
145 const struct lttng_directory_handle *handle,
146 const char *subdirectory_path,
147 mode_t mode, const struct lttng_credentials *creds);
148
149 /*
150 * Open a file descriptor to a path relative to a directory handle.
151 */
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
157 /*
158 * Open a file descriptor to a path relative to a directory handle
159 * as a given user.
160 */
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
167 /*
168 * Unlink a file to a path relative to a directory handle.
169 */
170 int lttng_directory_handle_unlink_file(
171 const struct lttng_directory_handle *handle,
172 const char *filename);
173
174 /*
175 * Unlink a file to a path relative to a directory handle as a given user.
176 */
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
182 /*
183 * Rename a file from a path relative to a directory handle to a new
184 * name relative to another directory handle.
185 */
186 int lttng_directory_handle_rename(
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);
191
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 */
196 int lttng_directory_handle_rename_as_user(
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,
201 const struct lttng_credentials *creds);
202
203 /*
204 * Remove a subdirectory relative to a directory handle.
205 */
206 int lttng_directory_handle_remove_subdirectory(
207 const struct lttng_directory_handle *handle,
208 const char *name);
209
210 /*
211 * Remove a subdirectory relative to a directory handle as a given user.
212 */
213 int lttng_directory_handle_remove_subdirectory_as_user(
214 const struct lttng_directory_handle *handle,
215 const char *name,
216 const struct lttng_credentials *creds);
217
218 /*
219 * Remove a subdirectory and remove its contents if it only
220 * consists in empty directories.
221 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
222 */
223 int lttng_directory_handle_remove_subdirectory_recursive(
224 const struct lttng_directory_handle *handle,
225 const char *name, int flags);
226
227 /*
228 * Remove a subdirectory and remove its contents if it only
229 * consists in empty directories as a given user.
230 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
231 */
232 int lttng_directory_handle_remove_subdirectory_recursive_as_user(
233 const struct lttng_directory_handle *handle,
234 const char *name,
235 const struct lttng_credentials *creds,
236 int flags);
237
238 /*
239 * stat() a file relative to a directory handle.
240 */
241 int lttng_directory_handle_stat(
242 const struct lttng_directory_handle *handle,
243 const char *name,
244 struct stat *stat_buf);
245
246 /*
247 * Returns true if this directory handle is backed by a file
248 * descriptor, false otherwise.
249 */
250 bool lttng_directory_handle_uses_fd(
251 const struct lttng_directory_handle *handle);
252
253 /*
254 * Compare two directory handles.
255 *
256 * Returns true if the two directory handles are equal, false otherwise.
257 */
258 bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs,
259 const struct lttng_directory_handle *rhs);
260
261
262 #endif /* _COMPAT_PATH_HANDLE_H */
This page took 0.040861 seconds and 4 git commands to generate.