src/common: use single Makefile for parallel builds
[lttng-tools.git] / src / common / compat / directory-handle.h
CommitLineData
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
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
16enum 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
30struct lttng_directory_handle;
31
32typedef void (*lttng_directory_handle_destroy_cb)(
33 struct lttng_directory_handle *handle, void *data);
34
18710679 35struct 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
43static inline
44int lttng_directory_handle_get_dirfd(
45 const struct lttng_directory_handle *handle)
46{
47 return handle->dirfd;
48}
49
18710679
JG
50#else
51struct 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 63 */
cbf53d23 64struct lttng_directory_handle *lttng_directory_handle_create(
18710679
JG
65 const char *path);
66
fd774fc6 67/*
cbf53d23 68 * Create a new directory handle to a path relative to an existing handle.
fd774fc6
JG
69 *
70 * The provided path must already exist. Note that the creation of a
71 * subdirectory and the creation of a handle are kept as separate operations
72 * to highlight the fact that there is an inherent race between the creation of
73 * a directory and the creation of a handle to it.
74 *
75 * Passing a NULL path effectively copies the original handle.
76 *
cbf53d23
JG
77 * The reference to the directory handle must be released using
78 * lttng_directory_handle_put().
fd774fc6 79 */
cbf53d23 80struct lttng_directory_handle *lttng_directory_handle_create_from_handle(
fd774fc6 81 const char *path,
cbf53d23 82 const struct lttng_directory_handle *ref_handle);
fd774fc6 83
15d59b1d 84/*
cbf53d23 85 * Create a new directory handle from an existing directory fd.
15d59b1d
JG
86 *
87 * The new directory handle assumes the ownership of the directory fd.
88 * Note that this method should only be used in very specific cases, such as
89 * re-creating a directory handle from a dirfd passed over a unix socket.
90 *
cbf53d23
JG
91 * The reference to the directory handle must be released using
92 * lttng_directory_handle_put().
15d59b1d 93 */
cbf53d23
JG
94struct lttng_directory_handle *lttng_directory_handle_create_from_dirfd(
95 int dirfd);
18710679 96
578e21bd
JG
97/*
98 * Copy a directory handle.
cbf53d23
JG
99 *
100 * The reference to the directory handle must be released using
101 * lttng_directory_handle_put().
578e21bd 102 */
cbf53d23
JG
103struct lttng_directory_handle *lttng_directory_handle_copy(
104 const struct lttng_directory_handle *handle);
578e21bd 105
46307ffe 106/*
cbf53d23 107 * Acquire a reference to a directory handle.
46307ffe 108 */
cbf53d23 109bool lttng_directory_handle_get(struct lttng_directory_handle *handle);
46307ffe 110
18710679 111/*
cbf53d23 112 * Release a reference to a directory handle.
18710679 113 */
cbf53d23 114void lttng_directory_handle_put(struct lttng_directory_handle *handle);
18710679
JG
115
116/*
117 * Create a subdirectory relative to a directory handle.
118 */
18710679
JG
119int lttng_directory_handle_create_subdirectory(
120 const struct lttng_directory_handle *handle,
121 const char *subdirectory,
122 mode_t mode);
123
124/*
125 * Create a subdirectory relative to a directory handle
126 * as a given user.
127 */
18710679
JG
128int lttng_directory_handle_create_subdirectory_as_user(
129 const struct lttng_directory_handle *handle,
130 const char *subdirectory,
69e3a560 131 mode_t mode, const struct lttng_credentials *creds);
18710679
JG
132
133/*
134 * Recursively create a directory relative to a directory handle.
135 */
18710679
JG
136int lttng_directory_handle_create_subdirectory_recursive(
137 const struct lttng_directory_handle *handle,
138 const char *subdirectory_path,
139 mode_t mode);
140
141/*
142 * Recursively create a directory relative to a directory handle
143 * as a given user.
144 */
18710679
JG
145int lttng_directory_handle_create_subdirectory_recursive_as_user(
146 const struct lttng_directory_handle *handle,
147 const char *subdirectory_path,
69e3a560 148 mode_t mode, const struct lttng_credentials *creds);
18710679 149
642e96c6
JG
150/*
151 * Open a file descriptor to a path relative to a directory handle.
152 */
2912cead
JG
153int lttng_directory_handle_open_file(
154 const struct lttng_directory_handle *handle,
155 const char *filename,
156 int flags, mode_t mode);
157
642e96c6
JG
158/*
159 * Open a file descriptor to a path relative to a directory handle
160 * as a given user.
161 */
2912cead
JG
162int lttng_directory_handle_open_file_as_user(
163 const struct lttng_directory_handle *handle,
164 const char *filename,
165 int flags, mode_t mode,
166 const struct lttng_credentials *creds);
167
642e96c6
JG
168/*
169 * Unlink a file to a path relative to a directory handle.
170 */
2912cead
JG
171int lttng_directory_handle_unlink_file(
172 const struct lttng_directory_handle *handle,
173 const char *filename);
174
642e96c6
JG
175/*
176 * Unlink a file to a path relative to a directory handle as a given user.
177 */
2912cead
JG
178int lttng_directory_handle_unlink_file_as_user(
179 const struct lttng_directory_handle *handle,
180 const char *filename,
181 const struct lttng_credentials *creds);
182
642e96c6
JG
183/*
184 * Rename a file from a path relative to a directory handle to a new
185 * name relative to another directory handle.
186 */
d2956687 187int lttng_directory_handle_rename(
93bed9fe
JG
188 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);
d2956687 192
642e96c6
JG
193/*
194 * Rename a file from a path relative to a directory handle to a new
195 * name relative to another directory handle as a given user.
196 */
d2956687 197int lttng_directory_handle_rename_as_user(
93bed9fe
JG
198 const struct lttng_directory_handle *old_handle,
199 const char *old_name,
200 const struct lttng_directory_handle *new_handle,
201 const char *new_name,
d2956687
JG
202 const struct lttng_credentials *creds);
203
642e96c6
JG
204/*
205 * Remove a subdirectory relative to a directory handle.
206 */
93bed9fe 207int lttng_directory_handle_remove_subdirectory(
d2956687
JG
208 const struct lttng_directory_handle *handle,
209 const char *name);
210
642e96c6
JG
211/*
212 * Remove a subdirectory relative to a directory handle as a given user.
213 */
93bed9fe 214int lttng_directory_handle_remove_subdirectory_as_user(
d2956687
JG
215 const struct lttng_directory_handle *handle,
216 const char *name,
217 const struct lttng_credentials *creds);
218
642e96c6
JG
219/*
220 * Remove a subdirectory and remove its contents if it only
221 * consists in empty directories.
f75c5439 222 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
642e96c6 223 */
93bed9fe 224int lttng_directory_handle_remove_subdirectory_recursive(
d2956687 225 const struct lttng_directory_handle *handle,
f75c5439 226 const char *name, int flags);
d2956687 227
642e96c6
JG
228/*
229 * Remove a subdirectory and remove its contents if it only
230 * consists in empty directories as a given user.
f75c5439 231 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
642e96c6 232 */
93bed9fe 233int lttng_directory_handle_remove_subdirectory_recursive_as_user(
d2956687
JG
234 const struct lttng_directory_handle *handle,
235 const char *name,
f75c5439
MD
236 const struct lttng_credentials *creds,
237 int flags);
d2956687 238
57b14318
JG
239/*
240 * stat() a file relative to a directory handle.
241 */
57b14318
JG
242int lttng_directory_handle_stat(
243 const struct lttng_directory_handle *handle,
244 const char *name,
245 struct stat *stat_buf);
246
9a1a997f
JG
247/*
248 * Returns true if this directory handle is backed by a file
249 * descriptor, false otherwise.
250 */
9a1a997f
JG
251bool lttng_directory_handle_uses_fd(
252 const struct lttng_directory_handle *handle);
253
0e985513
JG
254/*
255 * Compare two directory handles.
256 *
257 * Returns true if the two directory handles are equal, false otherwise.
258 */
0e985513
JG
259bool lttng_directory_handle_equals(const struct lttng_directory_handle *lhs,
260 const struct lttng_directory_handle *rhs);
261
7966af57 262
18710679 263#endif /* _COMPAT_PATH_HANDLE_H */
This page took 0.048303 seconds and 4 git commands to generate.