Commit | Line | Data |
---|---|---|
18710679 JG |
1 | /* |
2 | * Copyright (C) 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along | |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #ifndef _COMPAT_DIRECTORY_HANDLE_H | |
19 | #define _COMPAT_DIRECTORY_HANDLE_H | |
20 | ||
21 | #include <common/macros.h> | |
22 | #include <common/credentials.h> | |
23 | ||
24 | /* | |
25 | * Some platforms, such as Solaris 10, do not support directory file descriptors | |
26 | * and their associated functions (*at(...)), which are defined in POSIX.2008. | |
27 | * | |
28 | * This wrapper provides a handle that is either a copy of a directory's path | |
29 | * or a directory file descriptors, depending on the platform's capabilities. | |
30 | */ | |
31 | #ifdef COMPAT_DIRFD | |
32 | struct lttng_directory_handle { | |
33 | int dirfd; | |
34 | }; | |
facc1162 JG |
35 | |
36 | static inline | |
37 | int lttng_directory_handle_get_dirfd( | |
38 | const struct lttng_directory_handle *handle) | |
39 | { | |
40 | return handle->dirfd; | |
41 | } | |
42 | ||
18710679 JG |
43 | #else |
44 | struct lttng_directory_handle { | |
45 | char *base_path; | |
46 | }; | |
47 | #endif | |
48 | ||
49 | /* | |
50 | * Initialize a directory handle to the provided path. Passing a NULL path | |
fd774fc6 | 51 | * returns a handle to the current working directory. |
18710679 JG |
52 | * |
53 | * An initialized directory handle must be finalized using | |
54 | * lttng_directory_handle_fini(). | |
55 | */ | |
56 | LTTNG_HIDDEN | |
57 | int lttng_directory_handle_init(struct lttng_directory_handle *handle, | |
58 | const char *path); | |
59 | ||
fd774fc6 JG |
60 | /* |
61 | * Initialize a new directory handle to a path relative to an existing handle. | |
62 | * | |
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. | |
67 | * | |
68 | * Passing a NULL path effectively copies the original handle. | |
69 | * | |
70 | * An initialized directory handle must be finalized using | |
71 | * lttng_directory_handle_fini(). | |
72 | */ | |
73 | LTTNG_HIDDEN | |
74 | int lttng_directory_handle_init_from_handle( | |
75 | struct lttng_directory_handle *new_handle, | |
76 | const char *path, | |
77 | const struct lttng_directory_handle *handle); | |
78 | ||
15d59b1d JG |
79 | /* |
80 | * Initialize a new directory handle from an existing directory fd. | |
81 | * | |
82 | * The new directory handle assumes the ownership of the directory fd. | |
83 | * Note that this method should only be used in very specific cases, such as | |
84 | * re-creating a directory handle from a dirfd passed over a unix socket. | |
85 | * | |
86 | * An initialized directory handle must be finalized using | |
87 | * lttng_directory_handle_fini(). | |
88 | */ | |
18710679 JG |
89 | LTTNG_HIDDEN |
90 | int lttng_directory_handle_init_from_dirfd( | |
91 | struct lttng_directory_handle *handle, int dirfd); | |
92 | ||
578e21bd JG |
93 | /* |
94 | * Copy a directory handle. | |
95 | */ | |
96 | LTTNG_HIDDEN | |
97 | int lttng_directory_handle_copy(const struct lttng_directory_handle *handle, | |
98 | struct lttng_directory_handle *new_copy); | |
99 | ||
46307ffe JG |
100 | /* |
101 | * Move a directory handle. The original directory handle may no longer be | |
102 | * used after this call. This call cannot fail; directly assign the | |
103 | * return value to the new directory handle. | |
104 | * | |
105 | * It is safe (but unnecessary) to call lttng_directory_handle_fini on the | |
106 | * original handle. | |
107 | */ | |
108 | LTTNG_HIDDEN | |
109 | struct lttng_directory_handle | |
110 | lttng_directory_handle_move(struct lttng_directory_handle *original); | |
111 | ||
18710679 JG |
112 | /* |
113 | * Release the resources of a directory handle. | |
114 | */ | |
115 | LTTNG_HIDDEN | |
116 | void lttng_directory_handle_fini(struct lttng_directory_handle *handle); | |
117 | ||
118 | /* | |
119 | * Create a subdirectory relative to a directory handle. | |
120 | */ | |
121 | LTTNG_HIDDEN | |
122 | int lttng_directory_handle_create_subdirectory( | |
123 | const struct lttng_directory_handle *handle, | |
124 | const char *subdirectory, | |
125 | mode_t mode); | |
126 | ||
127 | /* | |
128 | * Create a subdirectory relative to a directory handle | |
129 | * as a given user. | |
130 | */ | |
131 | LTTNG_HIDDEN | |
132 | int lttng_directory_handle_create_subdirectory_as_user( | |
133 | const struct lttng_directory_handle *handle, | |
134 | const char *subdirectory, | |
69e3a560 | 135 | mode_t mode, const struct lttng_credentials *creds); |
18710679 JG |
136 | |
137 | /* | |
138 | * Recursively create a directory relative to a directory handle. | |
139 | */ | |
140 | LTTNG_HIDDEN | |
141 | int lttng_directory_handle_create_subdirectory_recursive( | |
142 | const struct lttng_directory_handle *handle, | |
143 | const char *subdirectory_path, | |
144 | mode_t mode); | |
145 | ||
146 | /* | |
147 | * Recursively create a directory relative to a directory handle | |
148 | * as a given user. | |
149 | */ | |
150 | LTTNG_HIDDEN | |
151 | int lttng_directory_handle_create_subdirectory_recursive_as_user( | |
152 | const struct lttng_directory_handle *handle, | |
153 | const char *subdirectory_path, | |
69e3a560 | 154 | mode_t mode, const struct lttng_credentials *creds); |
18710679 | 155 | |
642e96c6 JG |
156 | /* |
157 | * Open a file descriptor to a path relative to a directory handle. | |
158 | */ | |
2912cead JG |
159 | LTTNG_HIDDEN |
160 | int lttng_directory_handle_open_file( | |
161 | const struct lttng_directory_handle *handle, | |
162 | const char *filename, | |
163 | int flags, mode_t mode); | |
164 | ||
642e96c6 JG |
165 | /* |
166 | * Open a file descriptor to a path relative to a directory handle | |
167 | * as a given user. | |
168 | */ | |
2912cead JG |
169 | LTTNG_HIDDEN |
170 | int lttng_directory_handle_open_file_as_user( | |
171 | const struct lttng_directory_handle *handle, | |
172 | const char *filename, | |
173 | int flags, mode_t mode, | |
174 | const struct lttng_credentials *creds); | |
175 | ||
642e96c6 JG |
176 | /* |
177 | * Unlink a file to a path relative to a directory handle. | |
178 | */ | |
2912cead JG |
179 | LTTNG_HIDDEN |
180 | int lttng_directory_handle_unlink_file( | |
181 | const struct lttng_directory_handle *handle, | |
182 | const char *filename); | |
183 | ||
642e96c6 JG |
184 | /* |
185 | * Unlink a file to a path relative to a directory handle as a given user. | |
186 | */ | |
2912cead JG |
187 | LTTNG_HIDDEN |
188 | int lttng_directory_handle_unlink_file_as_user( | |
189 | const struct lttng_directory_handle *handle, | |
190 | const char *filename, | |
191 | const struct lttng_credentials *creds); | |
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. | |
196 | */ | |
d2956687 JG |
197 | LTTNG_HIDDEN |
198 | int lttng_directory_handle_rename( | |
93bed9fe JG |
199 | const struct lttng_directory_handle *old_handle, |
200 | const char *old_name, | |
201 | const struct lttng_directory_handle *new_handle, | |
202 | const char *new_name); | |
d2956687 | 203 | |
642e96c6 JG |
204 | /* |
205 | * Rename a file from a path relative to a directory handle to a new | |
206 | * name relative to another directory handle as a given user. | |
207 | */ | |
d2956687 JG |
208 | LTTNG_HIDDEN |
209 | int lttng_directory_handle_rename_as_user( | |
93bed9fe JG |
210 | const struct lttng_directory_handle *old_handle, |
211 | const char *old_name, | |
212 | const struct lttng_directory_handle *new_handle, | |
213 | const char *new_name, | |
d2956687 JG |
214 | const struct lttng_credentials *creds); |
215 | ||
642e96c6 JG |
216 | /* |
217 | * Remove a subdirectory relative to a directory handle. | |
218 | */ | |
d2956687 | 219 | LTTNG_HIDDEN |
93bed9fe | 220 | int lttng_directory_handle_remove_subdirectory( |
d2956687 JG |
221 | const struct lttng_directory_handle *handle, |
222 | const char *name); | |
223 | ||
642e96c6 JG |
224 | /* |
225 | * Remove a subdirectory relative to a directory handle as a given user. | |
226 | */ | |
d2956687 | 227 | LTTNG_HIDDEN |
93bed9fe | 228 | int lttng_directory_handle_remove_subdirectory_as_user( |
d2956687 JG |
229 | const struct lttng_directory_handle *handle, |
230 | const char *name, | |
231 | const struct lttng_credentials *creds); | |
232 | ||
642e96c6 JG |
233 | /* |
234 | * Remove a subdirectory and remove its contents if it only | |
235 | * consists in empty directories. | |
236 | */ | |
d2956687 | 237 | LTTNG_HIDDEN |
93bed9fe | 238 | int lttng_directory_handle_remove_subdirectory_recursive( |
d2956687 JG |
239 | const struct lttng_directory_handle *handle, |
240 | const char *name); | |
241 | ||
642e96c6 JG |
242 | /* |
243 | * Remove a subdirectory and remove its contents if it only | |
244 | * consists in empty directories as a given user. | |
245 | */ | |
d2956687 | 246 | LTTNG_HIDDEN |
93bed9fe | 247 | int lttng_directory_handle_remove_subdirectory_recursive_as_user( |
d2956687 JG |
248 | const struct lttng_directory_handle *handle, |
249 | const char *name, | |
250 | const struct lttng_credentials *creds); | |
251 | ||
18710679 | 252 | #endif /* _COMPAT_PATH_HANDLE_H */ |