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.
29 struct lttng_directory_handle
{
31 ino_t directory_inode
;
36 int lttng_directory_handle_get_dirfd(
37 const struct lttng_directory_handle
*handle
)
43 struct lttng_directory_handle
{
50 * Create a directory handle to the provided path. Passing a NULL path
51 * returns a handle to the current working directory.
53 * The reference to the directory handle must be released using
54 * lttng_directory_handle_put().
57 struct lttng_directory_handle
*lttng_directory_handle_create(
61 * Create a new directory handle to a path relative to an existing handle.
63 * The provided path must already exist. Note that the creation of a
64 * subdirectory and the creation of a handle are kept as separate operations
65 * to highlight the fact that there is an inherent race between the creation of
66 * a directory and the creation of a handle to it.
68 * Passing a NULL path effectively copies the original handle.
70 * The reference to the directory handle must be released using
71 * lttng_directory_handle_put().
74 struct lttng_directory_handle
*lttng_directory_handle_create_from_handle(
76 const struct lttng_directory_handle
*ref_handle
);
79 * Create a new directory handle from an existing directory fd.
81 * The new directory handle assumes the ownership of the directory fd.
82 * Note that this method should only be used in very specific cases, such as
83 * re-creating a directory handle from a dirfd passed over a unix socket.
85 * The reference to the directory handle must be released using
86 * lttng_directory_handle_put().
89 struct lttng_directory_handle
*lttng_directory_handle_create_from_dirfd(
93 * Copy a directory handle.
95 * The reference to the directory handle must be released using
96 * lttng_directory_handle_put().
99 struct lttng_directory_handle
*lttng_directory_handle_copy(
100 const struct lttng_directory_handle
*handle
);
103 * Acquire a reference to a directory handle.
106 bool lttng_directory_handle_get(struct lttng_directory_handle
*handle
);
109 * Release a reference to a directory handle.
112 void lttng_directory_handle_put(struct lttng_directory_handle
*handle
);
115 * Create a subdirectory relative to a directory handle.
118 int lttng_directory_handle_create_subdirectory(
119 const struct lttng_directory_handle
*handle
,
120 const char *subdirectory
,
124 * Create a subdirectory relative to a directory handle
128 int 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
);
134 * Recursively create a directory relative to a directory handle.
137 int lttng_directory_handle_create_subdirectory_recursive(
138 const struct lttng_directory_handle
*handle
,
139 const char *subdirectory_path
,
143 * Recursively create a directory relative to a directory handle
147 int lttng_directory_handle_create_subdirectory_recursive_as_user(
148 const struct lttng_directory_handle
*handle
,
149 const char *subdirectory_path
,
150 mode_t mode
, const struct lttng_credentials
*creds
);
153 * Open a file descriptor to a path relative to a directory handle.
156 int lttng_directory_handle_open_file(
157 const struct lttng_directory_handle
*handle
,
158 const char *filename
,
159 int flags
, mode_t mode
);
162 * 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.
176 int lttng_directory_handle_unlink_file(
177 const struct lttng_directory_handle
*handle
,
178 const char *filename
);
181 * Unlink a file to a path relative to a directory handle as a given user.
184 int lttng_directory_handle_unlink_file_as_user(
185 const struct lttng_directory_handle
*handle
,
186 const char *filename
,
187 const struct lttng_credentials
*creds
);
190 * Rename a file from a path relative to a directory handle to a new
191 * name relative to another directory handle.
194 int lttng_directory_handle_rename(
195 const struct lttng_directory_handle
*old_handle
,
196 const char *old_name
,
197 const struct lttng_directory_handle
*new_handle
,
198 const char *new_name
);
201 * Rename a file from a path relative to a directory handle to a new
202 * name relative to another directory handle as a given user.
205 int lttng_directory_handle_rename_as_user(
206 const struct lttng_directory_handle
*old_handle
,
207 const char *old_name
,
208 const struct lttng_directory_handle
*new_handle
,
209 const char *new_name
,
210 const struct lttng_credentials
*creds
);
213 * Remove a subdirectory relative to a directory handle.
216 int lttng_directory_handle_remove_subdirectory(
217 const struct lttng_directory_handle
*handle
,
221 * Remove a subdirectory relative to a directory handle as a given user.
224 int lttng_directory_handle_remove_subdirectory_as_user(
225 const struct lttng_directory_handle
*handle
,
227 const struct lttng_credentials
*creds
);
230 * Remove a subdirectory and remove its contents if it only
231 * consists in empty directories.
232 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
235 int lttng_directory_handle_remove_subdirectory_recursive(
236 const struct lttng_directory_handle
*handle
,
237 const char *name
, int flags
);
240 * Remove a subdirectory and remove its contents if it only
241 * consists in empty directories as a given user.
242 * @flags: enum lttng_directory_handle_rmdir_recursive_flags
245 int lttng_directory_handle_remove_subdirectory_recursive_as_user(
246 const struct lttng_directory_handle
*handle
,
248 const struct lttng_credentials
*creds
,
252 * stat() a file relative to a directory handle.
255 int lttng_directory_handle_stat(
256 const struct lttng_directory_handle
*handle
,
258 struct stat
*stat_buf
);
261 * Returns true if this directory handle is backed by a file
262 * descriptor, false otherwise.
265 bool lttng_directory_handle_uses_fd(
266 const struct lttng_directory_handle
*handle
);
269 * Compare two directory handles.
271 * Returns true if the two directory handles are equal, false otherwise.
274 bool lttng_directory_handle_equals(const struct lttng_directory_handle
*lhs
,
275 const struct lttng_directory_handle
*rhs
);
277 #endif /* _COMPAT_PATH_HANDLE_H */