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>
20 enum lttng_directory_handle_rmdir_recursive_flags
{
21 LTTNG_DIRECTORY_HANDLE_FAIL_NON_EMPTY_FLAG
= (1U << 0),
22 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
= (1U << 1),
26 * Some platforms, such as Solaris 10, do not support directory file descriptors
27 * and their associated functions (*at(...)), which are defined in POSIX.2008.
29 * This wrapper provides a handle that is either a copy of a directory's path
30 * or a directory file descriptors, depending on the platform's capabilities.
34 struct lttng_directory_handle
;
36 typedef void (*lttng_directory_handle_destroy_cb
)(
37 struct lttng_directory_handle
*handle
, void *data
);
39 struct lttng_directory_handle
{
41 ino_t directory_inode
;
43 lttng_directory_handle_destroy_cb destroy_cb
;
44 void *destroy_cb_data
;
48 int lttng_directory_handle_get_dirfd(
49 const struct lttng_directory_handle
*handle
)
55 struct lttng_directory_handle
{
62 * Create a directory handle to the provided path. Passing a NULL path
63 * returns a handle to the current working directory.
65 * The reference to the directory handle must be released using
66 * lttng_directory_handle_put().
68 struct lttng_directory_handle
*lttng_directory_handle_create(
72 * Create a new directory handle to a path relative to an existing handle.
74 * The provided path must already exist. Note that the creation of a
75 * subdirectory and the creation of a handle are kept as separate operations
76 * to highlight the fact that there is an inherent race between the creation of
77 * a directory and the creation of a handle to it.
79 * Passing a NULL path effectively copies the original handle.
81 * The reference to the directory handle must be released using
82 * lttng_directory_handle_put().
84 struct lttng_directory_handle
*lttng_directory_handle_create_from_handle(
86 const struct lttng_directory_handle
*ref_handle
);
89 * Create a new directory handle from an existing directory fd.
91 * The new directory handle assumes the ownership of the directory fd.
92 * Note that this method should only be used in very specific cases, such as
93 * re-creating a directory handle from a dirfd passed over a unix socket.
95 * The reference to the directory handle must be released using
96 * lttng_directory_handle_put().
98 struct lttng_directory_handle
*lttng_directory_handle_create_from_dirfd(
102 * Copy a directory handle.
104 * The reference to the directory handle must be released using
105 * 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.
113 bool lttng_directory_handle_get(struct lttng_directory_handle
*handle
);
116 * Release a reference to a directory handle.
118 void lttng_directory_handle_put(struct lttng_directory_handle
*handle
);
121 * Create a subdirectory relative to a directory handle.
123 int lttng_directory_handle_create_subdirectory(
124 const struct lttng_directory_handle
*handle
,
125 const char *subdirectory
,
129 * Create a subdirectory relative to a directory handle
132 int lttng_directory_handle_create_subdirectory_as_user(
133 const struct lttng_directory_handle
*handle
,
134 const char *subdirectory
,
135 mode_t mode
, const struct lttng_credentials
*creds
);
138 * Recursively create a directory relative to a directory handle.
140 int lttng_directory_handle_create_subdirectory_recursive(
141 const struct lttng_directory_handle
*handle
,
142 const char *subdirectory_path
,
146 * Recursively create a directory relative to a directory handle
149 int lttng_directory_handle_create_subdirectory_recursive_as_user(
150 const struct lttng_directory_handle
*handle
,
151 const char *subdirectory_path
,
152 mode_t mode
, const struct lttng_credentials
*creds
);
155 * Open a file descriptor to a path relative to a directory handle.
157 int lttng_directory_handle_open_file(
158 const struct lttng_directory_handle
*handle
,
159 const char *filename
,
160 int flags
, mode_t mode
);
163 * Open a file descriptor to a path relative to a directory handle
166 int lttng_directory_handle_open_file_as_user(
167 const struct lttng_directory_handle
*handle
,
168 const char *filename
,
169 int flags
, mode_t mode
,
170 const struct lttng_credentials
*creds
);
173 * Unlink a file to a path relative to a directory handle.
175 int lttng_directory_handle_unlink_file(
176 const struct lttng_directory_handle
*handle
,
177 const char *filename
);
180 * Unlink a file to a path relative to a directory handle as a given user.
182 int lttng_directory_handle_unlink_file_as_user(
183 const struct lttng_directory_handle
*handle
,
184 const char *filename
,
185 const struct lttng_credentials
*creds
);
188 * Rename a file from a path relative to a directory handle to a new
189 * name relative to another directory handle.
191 int lttng_directory_handle_rename(
192 const struct lttng_directory_handle
*old_handle
,
193 const char *old_name
,
194 const struct lttng_directory_handle
*new_handle
,
195 const char *new_name
);
198 * Rename a file from a path relative to a directory handle to a new
199 * name relative to another directory handle as a given user.
201 int lttng_directory_handle_rename_as_user(
202 const struct lttng_directory_handle
*old_handle
,
203 const char *old_name
,
204 const struct lttng_directory_handle
*new_handle
,
205 const char *new_name
,
206 const struct lttng_credentials
*creds
);
209 * Remove a subdirectory relative to a directory handle.
211 int lttng_directory_handle_remove_subdirectory(
212 const struct lttng_directory_handle
*handle
,
216 * Remove a subdirectory relative to a directory handle as a given user.
218 int lttng_directory_handle_remove_subdirectory_as_user(
219 const struct lttng_directory_handle
*handle
,
221 const struct lttng_credentials
*creds
);
224 * Remove a subdirectory and remove its contents if it only
225 * consists in empty directories.
226 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
228 int lttng_directory_handle_remove_subdirectory_recursive(
229 const struct lttng_directory_handle
*handle
,
230 const char *name
, int flags
);
233 * Remove a subdirectory and remove its contents if it only
234 * consists in empty directories as a given user.
235 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
237 int lttng_directory_handle_remove_subdirectory_recursive_as_user(
238 const struct lttng_directory_handle
*handle
,
240 const struct lttng_credentials
*creds
,
244 * stat() a file relative to a directory handle.
246 int lttng_directory_handle_stat(
247 const struct lttng_directory_handle
*handle
,
249 struct stat
*stat_buf
);
252 * Returns true if this directory handle is backed by a file
253 * descriptor, false otherwise.
255 bool lttng_directory_handle_uses_fd(
256 const struct lttng_directory_handle
*handle
);
259 * Compare two directory handles.
261 * Returns true if the two directory handles are equal, false otherwise.
263 bool lttng_directory_handle_equals(const struct lttng_directory_handle
*lhs
,
264 const struct lttng_directory_handle
*rhs
);
271 #endif /* _COMPAT_PATH_HANDLE_H */