Debian Codegraph @

crt/aws-c-common/include/aws/common/file.h

raw symbols SCIP
1 #ifndef AWS_COMMON_FILE_H
2 #define AWS_COMMON_FILE_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7 #include <aws/common/byte_buf.h>
8 #include <aws/common/common.h>
9 #include <aws/common/platform.h>
10 #include <stdio.h>
11
12 AWS_PUSH_SANE_WARNING_LEVEL
13
14 #ifdef AWS_OS_WINDOWS
15 # define AWS_PATH_DELIM '\\'
16 # define AWS_PATH_DELIM_STR "\\"
17 #else
18 # define AWS_PATH_DELIM '/'
19 # define AWS_PATH_DELIM_STR "/"
20 #endif
21
22 struct aws_string;
23 struct aws_directory_iterator;
24
25 enum aws_file_type {
26 AWS_FILE_TYPE_FILE = 1,
27 AWS_FILE_TYPE_SYM_LINK = 2,
28 AWS_FILE_TYPE_DIRECTORY = 4,
29 };
30
31 struct aws_directory_entry {
32 /**
33 * Absolute path to the entry from the current process root.
34 */
35 struct aws_byte_cursor path;
36 /**
37 * Path to the entry relative to the current working directory.
38 */
39 struct aws_byte_cursor relative_path;
40 /**
41 * Bit-field of enum aws_file_type
42 */
43 int file_type;
44 /**
45 * Size of the file on disk.
46 */
47 int64_t file_size;
48 };
49
50 /**
51 * Invoked during calls to aws_directory_traverse() as an entry is encountered. entry will contain
52 * the parsed directory entry info.
53 *
54 * Return true to continue the traversal, or alternatively, if you have a reason to abort the traversal, return false.
55 */
56 typedef bool(aws_on_directory_entry)(const struct aws_directory_entry *entry, void *user_data);
57
58 AWS_EXTERN_C_BEGIN
59
60 /**
61 * Deprecated - Use aws_fopen_safe() instead, avoid const char * in public APIs.
62 * Opens file at file_path using mode. Returns the FILE pointer if successful.
63 * Otherwise, aws_last_error() will contain the error that occurred
64 */
65 AWS_COMMON_API FILE *aws_fopen(const char *file_path, const char *mode);
66
67 /**
68 * Opens file at file_path using mode. Returns the FILE pointer if successful.
69 * Otherwise, aws_last_error() will contain the error that occurred
70 */
71 AWS_COMMON_API FILE *aws_fopen_safe(const struct aws_string *file_path, const struct aws_string *mode);
72
73 /**
74 * Creates a directory if it doesn't currently exist. If the directory already exists, it's ignored and assumed
75 * successful.
76 *
77 * Returns AWS_OP_SUCCESS on success. Otherwise, check aws_last_error().
78 */
79 AWS_COMMON_API int aws_directory_create(const struct aws_string *dir_path);
80 /**
81 * Returns true if the directory currently exists. Otherwise, it returns false.
82 */
83 AWS_COMMON_API bool aws_directory_exists(const struct aws_string *dir_path);
84 /**
85 * Deletes a directory. If the directory is not empty, this will fail unless the recursive parameter is set to true.
86 * If recursive is true then the entire directory and all of its contents will be deleted. If it is set to false,
87 * the directory will be deleted only if it is empty. Returns AWS_OP_SUCCESS if the operation was successful. Otherwise,
88 * aws_last_error() will contain the error that occurred. If the directory doesn't exist, AWS_OP_SUCCESS is still
89 * returned.
90 */
91 AWS_COMMON_API int aws_directory_delete(const struct aws_string *dir_path, bool recursive);
92 /**
93 * Deletes a file. Returns AWS_OP_SUCCESS if the operation was successful. Otherwise,
94 * aws_last_error() will contain the error that occurred. If the file doesn't exist, AWS_OP_SUCCESS is still returned.
95 */
96 AWS_COMMON_API int aws_file_delete(const struct aws_string *file_path);
97
98 /**
99 * Moves directory at from to to.
100 * Returns AWS_OP_SUCCESS if the operation was successful. Otherwise,
101 * aws_last_error() will contain the error that occurred.
102 */
103 AWS_COMMON_API int aws_directory_or_file_move(const struct aws_string *from, const struct aws_string *to);
104
105 /**
106 * Traverse a directory starting at path.
107 *
108 * If you want the traversal to recurse the entire directory, pass recursive as true. Passing false for this parameter
109 * will only iterate the contents of the directory, but will not descend into any directories it encounters.
110 *
111 * If recursive is set to true, the traversal is performed post-order, depth-first
112 * (for practical reasons such as deleting a directory that contains subdirectories or files).
113 *
114 * returns AWS_OP_SUCCESS(0) on success.
115 */
116 AWS_COMMON_API int aws_directory_traverse(
117 struct aws_allocator *allocator,
118 const struct aws_string *path,
119 bool recursive,
120 aws_on_directory_entry *on_entry,
121 void *user_data);
122
123 /**
124 * Creates a read-only iterator of a directory starting at path. If path is invalid or there's any other error
125 * condition, NULL will be returned. Call aws_last_error() for the exact error in that case.
126 */
127 AWS_COMMON_API struct aws_directory_iterator *aws_directory_entry_iterator_new(
128 struct aws_allocator *allocator,
129 const struct aws_string *path);
130
131 /**
132 * Moves the iterator to the next entry. Returns AWS_OP_SUCCESS if another entry is available, or AWS_OP_ERR with
133 * AWS_ERROR_LIST_EMPTY as the value for aws_last_error() if no more entries are available.
134 */
135 AWS_COMMON_API int aws_directory_entry_iterator_next(struct aws_directory_iterator *iterator);
136
137 /**
138 * Moves the iterator to the previous entry. Returns AWS_OP_SUCCESS if another entry is available, or AWS_OP_ERR with
139 * AWS_ERROR_LIST_EMPTY as the value for aws_last_error() if no more entries are available.
140 */
141 AWS_COMMON_API int aws_directory_entry_iterator_previous(struct aws_directory_iterator *iterator);
142
143 /**
144 * Cleanup and deallocate iterator
145 */
146 AWS_COMMON_API void aws_directory_entry_iterator_destroy(struct aws_directory_iterator *iterator);
147
148 /**
149 * Gets the aws_directory_entry value for iterator at the current position. Returns NULL if the iterator contains no
150 * entries.
151 */
152 AWS_COMMON_API const struct aws_directory_entry *aws_directory_entry_iterator_get_value(
153 const struct aws_directory_iterator *iterator);
154
155 /**
156 * Returns true iff the character is a directory separator on ANY supported platform.
157 */
158 AWS_COMMON_API
159 bool aws_is_any_directory_separator(char value);
160
161 /**
162 * Returns the directory separator used by the local platform
163 */
164 AWS_COMMON_API
165 char aws_get_platform_directory_separator(void);
166
167 /**
168 * Normalizes the path by replacing any directory separator with the local platform's directory separator.
169 * @param path path to normalize. Must be writeable.
170 */
171 AWS_COMMON_API
172 void aws_normalize_directory_separator(struct aws_byte_buf *path);
173
174 /**
175 * Returns the current user's home directory.
176 */
177 AWS_COMMON_API
178 struct aws_string *aws_get_home_directory(struct aws_allocator *allocator);
179
180 /**
181 * Returns true if a file or path exists, otherwise, false.
182 */
183 AWS_COMMON_API
184 bool aws_path_exists(const struct aws_string *path);
185
186 /*
187 * Wrapper for highest-resolution platform-dependent seek implementation.
188 * Maps to:
189 *
190 * _fseeki64() on windows
191 * fseeko() on linux
192 *
193 * whence can either be SEEK_SET or SEEK_END
194 *
195 * Returns AWS_OP_SUCCESS, or AWS_OP_ERR (after an error has been raised).
196 */
197 AWS_COMMON_API
198 int aws_fseek(FILE *file, int64_t offset, int whence);
199
200 /*
201 * Wrapper for os-specific file length query. We can't use fseek(END, 0)
202 * because support for it is not technically required.
203 *
204 * Unix flavors call fstat, while Windows variants use GetFileSize on a
205 * HANDLE queried from the libc FILE pointer.
206 */
207 AWS_COMMON_API
208 int aws_file_get_length(FILE *file, int64_t *length);
209
210 /*
211 * Read from the file using the file path with DIRECT I/O, starting at offset to fill the output_buf or to the
212 * max_read_length.
213 * Using direct IO to bypass the OS cache. Helpful when the disk I/O outperform the kernel cache.
214 * If O_DIRECT is not supported, returns AWS_ERROR_UNSUPPORTED_OPERATION.
215 * Notes:
216 * - ONLY supports linux for now and raise AWS_ERROR_UNSUPPORTED_OPERATION on all other platforms (eg: FreeBSD).
217 * - check the NOTES for O_DIRECT in https://man7.org/linux/man-pages/man2/openat.2.html
218 * - The offset, length and output_buf->buffer all need to be aligned with the page size. Otherwise,
219 * AWS_ERROR_INVALID_ARGUMENT will be raised.
220 *
221 * @param file_path The file path to read from.
222 * @param offset The offset in the file to start reading from.
223 * @param max_read_length The maximum number of bytes to read.
224 * @param output_buf The buffer read to.
225 * @param out_actual_read The actual number of bytes read.
226 *
227 * Returns AWS_OP_SUCCESS, or AWS_OP_ERR (after an error has been raised).
228 */
229 AWS_COMMON_API
230 int aws_file_path_read_from_offset_direct_io(
231 const struct aws_string *file_path,
232 uint64_t offset,
233 size_t max_read_length,
234 struct aws_byte_buf *output_buf,
235 size_t *out_actual_read);
236
237 /*
238 * Read from the file using the file path, starting at offset to fill the output_buf or to the
239 * max_read_length.
240 *
241 * @param file_path The file path to read from.
242 * @param offset The offset in the file to start reading from.
243 * @param max_read_length The maximum number of bytes to read.
244 * @param output_buf The buffer read to.
245 * @param out_actual_read The actual number of bytes read.
246 *
247 * Returns AWS_OP_SUCCESS, or AWS_OP_ERR (after an error has been raised).
248 */
249 AWS_COMMON_API
250 int aws_file_path_read_from_offset(
251 const struct aws_string *file_path,
252 uint64_t offset,
253 size_t max_read_length,
254 struct aws_byte_buf *output_buf,
255 size_t *out_actual_read);
256
257 /*
258 * The function serve the same purpose as `aws_file_path_read_from_offset_direct_io`.
259 * Please use `aws_file_path_read_from_offset_direct_io` directly.
260 */
261 AWS_COMMON_API
262 int aws_file_path_read_from_offset_direct_io_with_chunk_size(
263 const struct aws_string *file_path,
264 uint64_t offset,
265 size_t max_read_length,
266 size_t max_chunk_size,
267 struct aws_byte_buf *output_buf,
268 size_t *out_actual_read);
269
270 /*
271 * Write to a file using DIRECT I/O at the given offset.
272 * Using direct IO to bypass the OS cache. Helpful when the disk I/O outperform the kernel cache.
273 * If O_DIRECT is not supported, returns AWS_ERROR_UNSUPPORTED_OPERATION.
274 *
275 * The file must already exist; the caller is responsible for creating it.
276 *
277 * For aligned writes (offset and length both aligned to page size), O_DIRECT is used for the
278 * entire write.
279 *
280 * Notes:
281 * - ONLY supports linux for now and raises AWS_ERROR_UNSUPPORTED_OPERATION on all other platforms.
282 * - The offset, data.len, and data.ptr all need to be aligned with the page size (a multiple of page size).
283 * Otherwise, AWS_ERROR_INVALID_ARGUMENT will be raised.
284 * - check the NOTES for O_DIRECT in https://man7.org/linux/man-pages/man2/openat.2.html
285 *
286 * @param file_path The file path to write to.
287 * @param offset The offset in the file to start writing at.
288 * @param data The buffer to write from (data.len bytes will be written).
289 *
290 * Returns AWS_OP_SUCCESS, or AWS_OP_ERR (after an error has been raised).
291 */
292 AWS_COMMON_API
293 int aws_file_path_write_to_offset_direct_io(
294 const struct aws_string *file_path,
295 uint64_t offset,
296 struct aws_byte_cursor data);
297
298 /**
299 * Returns true if direct I/O (O_DIRECT) is supported on the current platform.
300 *
301 * Currently only Linux supports direct I/O. On unsupported platforms,
302 * aws_file_path_read_from_offset_direct_io() and aws_file_path_write_to_offset_direct_io()
303 * will raise AWS_ERROR_UNSUPPORTED_OPERATION.
304 *
305 * Use this to check at init time whether direct I/O is viable, rather than calling the
306 * read/write functions and handling the error reactively.
307 */
308 AWS_COMMON_API
309 bool aws_file_direct_io_is_supported(void);
310
311 AWS_EXTERN_C_END AWS_POP_SANE_WARNING_LEVEL
312
313 #endif /* AWS_COMMON_FILE_H */