2 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef _COMPAT_DIRECTORY_HANDLE_H
9 #define _COMPAT_DIRECTORY_HANDLE_H
11 #include <common/credentials.h>
12 #include <common/macros.h>
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),
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.
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.
30 struct lttng_directory_handle
;
32 typedef void (*lttng_directory_handle_destroy_cb
)(
33 struct lttng_directory_handle
*handle
, void *data
);
35 struct lttng_directory_handle
{
37 ino_t directory_inode
;
39 lttng_directory_handle_destroy_cb destroy_cb
;
40 void *destroy_cb_data
;
44 int lttng_directory_handle_get_dirfd(
45 const struct lttng_directory_handle
*handle
)
51 struct lttng_directory_handle
{
58 * Create a directory handle to the provided path. Passing a NULL path
59 * returns a handle to the current working directory.
61 * The reference to the directory handle must be released using
62 * lttng_directory_handle_put().
65 struct lttng_directory_handle
*lttng_directory_handle_create(
69 * Create a new directory handle to a path relative to an existing handle.
71 * The provided path must already exist. Note that the creation of a
72 * subdirectory and the creation of a handle are kept as separate operations
73 * to highlight the fact that there is an inherent race between the creation of
74 * a directory and the creation of a handle to it.
76 * Passing a NULL path effectively copies the original handle.
78 * The reference to the directory handle must be released using
79 * lttng_directory_handle_put().
82 struct lttng_directory_handle
*lttng_directory_handle_create_from_handle(
84 const struct lttng_directory_handle
*ref_handle
);
87 * Create a new directory handle from an existing directory fd.
89 * The new directory handle assumes the ownership of the directory fd.
90 * Note that this method should only be used in very specific cases, such as
91 * re-creating a directory handle from a dirfd passed over a unix socket.
93 * The reference to the directory handle must be released using
94 * lttng_directory_handle_put().
97 struct lttng_directory_handle
*lttng_directory_handle_create_from_dirfd(
101 * Copy a directory handle.
103 * The reference to the directory handle must be released using
104 * lttng_directory_handle_put().
107 struct lttng_directory_handle
*lttng_directory_handle_copy(
108 const struct lttng_directory_handle
*handle
);
111 * Acquire a reference to a directory handle.
114 bool lttng_directory_handle_get(struct lttng_directory_handle
*handle
);
117 * Release a reference to a directory handle.
120 void lttng_directory_handle_put(struct lttng_directory_handle
*handle
);
123 * Create a subdirectory relative to a directory handle.
126 int lttng_directory_handle_create_subdirectory(
127 const struct lttng_directory_handle
*handle
,
128 const char *subdirectory
,
132 * Create a subdirectory relative to a directory handle
136 int lttng_directory_handle_create_subdirectory_as_user(
137 const struct lttng_directory_handle
*handle
,
138 const char *subdirectory
,
139 mode_t mode
, const struct lttng_credentials
*creds
);
142 * Recursively create a directory relative to a directory handle.
145 int lttng_directory_handle_create_subdirectory_recursive(
146 const struct lttng_directory_handle
*handle
,
147 const char *subdirectory_path
,
151 * Recursively create a directory relative to a directory handle
155 int lttng_directory_handle_create_subdirectory_recursive_as_user(
156 const struct lttng_directory_handle
*handle
,
157 const char *subdirectory_path
,
158 mode_t mode
, const struct lttng_credentials
*creds
);
161 * Open a file descriptor to a path relative to a directory handle.
164 int lttng_directory_handle_open_file(
165 const struct lttng_directory_handle
*handle
,
166 const char *filename
,
167 int flags
, mode_t mode
);
170 * Open a file descriptor to a path relative to a directory handle
174 int lttng_directory_handle_open_file_as_user(
175 const struct lttng_directory_handle
*handle
,
176 const char *filename
,
177 int flags
, mode_t mode
,
178 const struct lttng_credentials
*creds
);
181 * Unlink a file to a path relative to a directory handle.
184 int lttng_directory_handle_unlink_file(
185 const struct lttng_directory_handle
*handle
,
186 const char *filename
);
189 * Unlink a file to a path relative to a directory handle as a given user.
192 int lttng_directory_handle_unlink_file_as_user(
193 const struct lttng_directory_handle
*handle
,
194 const char *filename
,
195 const struct lttng_credentials
*creds
);
198 * Rename a file from a path relative to a directory handle to a new
199 * name relative to another directory handle.
202 int lttng_directory_handle_rename(
203 const struct lttng_directory_handle
*old_handle
,
204 const char *old_name
,
205 const struct lttng_directory_handle
*new_handle
,
206 const char *new_name
);
209 * Rename a file from a path relative to a directory handle to a new
210 * name relative to another directory handle as a given user.
213 int lttng_directory_handle_rename_as_user(
214 const struct lttng_directory_handle
*old_handle
,
215 const char *old_name
,
216 const struct lttng_directory_handle
*new_handle
,
217 const char *new_name
,
218 const struct lttng_credentials
*creds
);
221 * Remove a subdirectory relative to a directory handle.
224 int lttng_directory_handle_remove_subdirectory(
225 const struct lttng_directory_handle
*handle
,
229 * Remove a subdirectory relative to a directory handle as a given user.
232 int lttng_directory_handle_remove_subdirectory_as_user(
233 const struct lttng_directory_handle
*handle
,
235 const struct lttng_credentials
*creds
);
238 * Remove a subdirectory and remove its contents if it only
239 * consists in empty directories.
240 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
243 int lttng_directory_handle_remove_subdirectory_recursive(
244 const struct lttng_directory_handle
*handle
,
245 const char *name
, int flags
);
248 * Remove a subdirectory and remove its contents if it only
249 * consists in empty directories as a given user.
250 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
253 int lttng_directory_handle_remove_subdirectory_recursive_as_user(
254 const struct lttng_directory_handle
*handle
,
256 const struct lttng_credentials
*creds
,
260 * stat() a file relative to a directory handle.
263 int lttng_directory_handle_stat(
264 const struct lttng_directory_handle
*handle
,
266 struct stat
*stat_buf
);
269 * Returns true if this directory handle is backed by a file
270 * descriptor, false otherwise.
273 bool lttng_directory_handle_uses_fd(
274 const struct lttng_directory_handle
*handle
);
277 * Compare two directory handles.
279 * Returns true if the two directory handles are equal, false otherwise.
282 bool lttng_directory_handle_equals(const struct lttng_directory_handle
*lhs
,
283 const struct lttng_directory_handle
*rhs
);
285 #endif /* _COMPAT_PATH_HANDLE_H */