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