Debian Codegraph @

crt/aws-c-common/source/file.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/byte_buf.h>
7 #include <aws/common/file.h>
8 #include <aws/common/linked_list.h>
9 #include <aws/common/logging.h>
10 #include <aws/common/string.h>
11
12 #include <errno.h>
13 #include <inttypes.h>
14
15 /* For "special files", the OS often lies about size.
16 * For example, on Amazon Linux 2:
17 * /proc/cpuinfo: size is 0, but contents are several KB of data.
18 * /sys/devices/virtual/dmi/id/product_name: size is 4096, but contents are "c5.2xlarge"
19 *
20 * Therefore, we may need to grow the buffer as we read until EOF.
21 * This is the min/max step size for growth. */
22 #define MIN_BUFFER_GROWTH_READING_FILES 32
23 #define MAX_BUFFER_GROWTH_READING_FILES 4096
24
25 AWS_STATIC_STRING_FROM_LITERAL(s_readonly_bytes_mode, "rb");
26
27 FILE *aws_fopen(const char *file_path, const char *mode) {
28 if (!file_path || strlen(file_path) == 0) {
29 AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to open file. path is empty");
30 aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
31 return NULL;
32 }
33
34 if (!mode || strlen(mode) == 0) {
35 AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to open file. mode is empty");
36 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
37 return NULL;
38 }
39
40 struct aws_string *file_path_str = aws_string_new_from_c_str(aws_default_allocator(), file_path);
41 struct aws_string *mode_str = aws_string_new_from_c_str(aws_default_allocator(), mode);
42
43 FILE *file = aws_fopen_safe(file_path_str, mode_str);
44 aws_string_destroy(mode_str);
45 aws_string_destroy(file_path_str);
46
47 return file;
48 }
49
50 /* Helper function used by aws_byte_buf_init_from_file() and aws_byte_buf_init_from_file_with_size_hint() */
51 static int s_byte_buf_init_from_file_impl(
52 struct aws_byte_buf *out_buf,
53 struct aws_allocator *alloc,
54 const char *filename,
55 bool use_file_size_as_hint,
56 size_t size_hint) {
57
58 AWS_ZERO_STRUCT(*out_buf);
59 FILE *fp = aws_fopen(filename, "rb");
60 if (fp == NULL) {
61 goto error;
62 }
63
64 if (use_file_size_as_hint) {
65 int64_t len64 = 0;
66 if (aws_file_get_length(fp, &len64)) {
67 AWS_LOGF_ERROR(
68 AWS_LS_COMMON_IO,
69 "static: Failed to get file length. file:'%s' error:%s",
70 filename,
71 aws_error_name(aws_last_error()));
72 goto error;
73 }
74
75 if (len64 >= SIZE_MAX) {
76 aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
77 AWS_LOGF_ERROR(
78 AWS_LS_COMMON_IO,
79 "static: File too large to read into memory. file:'%s' error:%s",
80 filename,
81 aws_error_name(aws_last_error()));
82 goto error;
83 }
84
85 /* Leave space for null terminator at end of buffer */
86 size_hint = (size_t)len64 + 1;
87 }
88
89 aws_byte_buf_init(out_buf, alloc, size_hint);
90
91 /* Read in a loop until we hit EOF */
92 while (true) {
93 /* Expand buffer if necessary (at a reasonable rate) */
94 if (out_buf->len == out_buf->capacity) {
95 size_t additional_capacity = out_buf->capacity;
96 additional_capacity = aws_max_size(MIN_BUFFER_GROWTH_READING_FILES, additional_capacity);
97 additional_capacity = aws_min_size(MAX_BUFFER_GROWTH_READING_FILES, additional_capacity);
98 if (aws_byte_buf_reserve_relative(out_buf, additional_capacity)) {
99 AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to grow buffer for file:'%s'", filename);
100 goto error;
101 }
102 }
103
104 size_t space_available = out_buf->capacity - out_buf->len;
105 size_t bytes_read = fread(out_buf->buffer + out_buf->len, 1, space_available, fp);
106 out_buf->len += bytes_read;
107
108 /* If EOF, we're done! */
109 if (feof(fp)) {
110 break;
111 }
112
113 /* If no EOF but we read 0 bytes, there's been an error or at least we need
114 * to treat it like one because we can't just infinitely loop. */
115 if (bytes_read == 0) {
116 int errno_value = ferror(fp) ? errno : 0; /* Always cache errno before potential side-effect */
117 aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_FILE_READ_FAILURE);
118 AWS_LOGF_ERROR(
119 AWS_LS_COMMON_IO,
120 "static: Failed reading file:'%s' errno:%d aws-error:%s",
121 filename,
122 errno_value,
123 aws_error_name(aws_last_error()));
124 goto error;
125 }
126 }
127
128 /* A null terminator is appended, but is not included as part of the length field. */
129 if (out_buf->len == out_buf->capacity) {
130 if (aws_byte_buf_reserve_relative(out_buf, 1)) {
131 AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to grow buffer for file:'%s'", filename);
132 goto error;
133 }
134 }
135 out_buf->buffer[out_buf->len] = 0;
136
137 fclose(fp);
138 return AWS_OP_SUCCESS;
139
140 error:
141 if (fp) {
142 fclose(fp);
143 }
144 aws_byte_buf_clean_up_secure(out_buf);
145 return AWS_OP_ERR;
146 }
147
148 int aws_byte_buf_init_from_file(struct aws_byte_buf *out_buf, struct aws_allocator *alloc, const char *filename) {
149 return s_byte_buf_init_from_file_impl(out_buf, alloc, filename, true /*use_file_size_as_hint*/, 0 /*size_hint*/);
150 }
151
152 int aws_byte_buf_init_from_file_with_size_hint(
153 struct aws_byte_buf *out_buf,
154 struct aws_allocator *alloc,
155 const char *filename,
156 size_t size_hint) {
157
158 return s_byte_buf_init_from_file_impl(out_buf, alloc, filename, false /*use_file_size_as_hint*/, size_hint);
159 }
160
161 bool aws_is_any_directory_separator(char value) {
162 return value == '\\' || value == '/';
163 }
164
165 void aws_normalize_directory_separator(struct aws_byte_buf *path) {
166 AWS_PRECONDITION(aws_byte_buf_is_valid(path));
167
168 const char local_platform_separator = aws_get_platform_directory_separator();
169 for (size_t i = 0; i < path->len; ++i) {
170 if (aws_is_any_directory_separator((char)path->buffer[i])) {
171 path->buffer[i] = local_platform_separator;
172 }
173 }
174
175 AWS_POSTCONDITION(aws_byte_buf_is_valid(path));
176 }
177
178 struct aws_directory_iterator {
179 struct aws_linked_list list_data;
180 struct aws_allocator *allocator;
181 struct aws_linked_list_node *current_node;
182 };
183
184 struct directory_entry_value {
185 struct aws_directory_entry entry;
186 struct aws_byte_buf path;
187 struct aws_byte_buf relative_path;
188 struct aws_linked_list_node node;
189 };
190
191 static bool s_directory_iterator_directory_entry(const struct aws_directory_entry *entry, void *user_data) {
192 struct aws_directory_iterator *iterator = user_data;
193 struct directory_entry_value *value = aws_mem_calloc(iterator->allocator, 1, sizeof(struct directory_entry_value));
194
195 value->entry = *entry;
196 aws_byte_buf_init_copy_from_cursor(&value->path, iterator->allocator, entry->path);
197 value->entry.path = aws_byte_cursor_from_buf(&value->path);
198 aws_byte_buf_init_copy_from_cursor(&value->relative_path, iterator->allocator, entry->relative_path);
199 value->entry.relative_path = aws_byte_cursor_from_buf(&value->relative_path);
200 aws_linked_list_push_back(&iterator->list_data, &value->node);
201
202 return true;
203 }
204
205 struct aws_directory_iterator *aws_directory_entry_iterator_new(
206 struct aws_allocator *allocator,
207 const struct aws_string *path) {
208 struct aws_directory_iterator *iterator = aws_mem_acquire(allocator, sizeof(struct aws_directory_iterator));
209 iterator->allocator = allocator;
210 aws_linked_list_init(&iterator->list_data);
211
212 /* the whole point of this iterator is to avoid recursion, so let's do that by passing recurse as false. */
213 if (AWS_OP_SUCCESS ==
214 aws_directory_traverse(allocator, path, false, s_directory_iterator_directory_entry, iterator)) {
215 if (!aws_linked_list_empty(&iterator->list_data)) {
216 iterator->current_node = aws_linked_list_front(&iterator->list_data);
217 }
218 return iterator;
219 }
220
221 aws_mem_release(allocator, iterator);
222 return NULL;
223 }
224
225 int aws_directory_entry_iterator_next(struct aws_directory_iterator *iterator) {
226 struct aws_linked_list_node *node = iterator->current_node;
227
228 if (!node || node->next == aws_linked_list_end(&iterator->list_data)) {
229 return aws_raise_error(AWS_ERROR_LIST_EMPTY);
230 }
231
232 iterator->current_node = aws_linked_list_next(node);
233
234 return AWS_OP_SUCCESS;
235 }
236
237 int aws_directory_entry_iterator_previous(struct aws_directory_iterator *iterator) {
238 struct aws_linked_list_node *node = iterator->current_node;
239
240 if (!node || node == aws_linked_list_begin(&iterator->list_data)) {
241 return aws_raise_error(AWS_ERROR_LIST_EMPTY);
242 }
243
244 iterator->current_node = aws_linked_list_prev(node);
245
246 return AWS_OP_SUCCESS;
247 }
248
249 void aws_directory_entry_iterator_destroy(struct aws_directory_iterator *iterator) {
250 while (!aws_linked_list_empty(&iterator->list_data)) {
251 struct aws_linked_list_node *node = aws_linked_list_pop_front(&iterator->list_data);
252 struct directory_entry_value *value = AWS_CONTAINER_OF(node, struct directory_entry_value, node);
253
254 aws_byte_buf_clean_up(&value->path);
255 aws_byte_buf_clean_up(&value->relative_path);
256
257 aws_mem_release(iterator->allocator, value);
258 }
259
260 aws_mem_release(iterator->allocator, iterator);
261 }
262
263 const struct aws_directory_entry *aws_directory_entry_iterator_get_value(
264 const struct aws_directory_iterator *iterator) {
265 struct aws_linked_list_node *node = iterator->current_node;
266
267 if (!iterator->current_node) {
268 return NULL;
269 }
270
271 struct directory_entry_value *value = AWS_CONTAINER_OF(node, struct directory_entry_value, node);
272 return &value->entry;
273 }
274
275 int aws_file_path_read_from_offset(
276 const struct aws_string *file_path,
277 uint64_t offset,
278 size_t max_read_length,
279 struct aws_byte_buf *output_buf,
280 size_t *out_actual_read) {
281
282 int rt_code = AWS_OP_ERR;
283 size_t available_len = aws_sub_size_saturating(output_buf->capacity, output_buf->len);
284 size_t length = aws_min_size(available_len, max_read_length);
285 if (length == 0) {
286 return AWS_OP_SUCCESS; /* Nothing to do. */
287 }
288 FILE *file_stream = aws_fopen_safe(file_path, s_readonly_bytes_mode);
289 if (file_stream == NULL) {
290 AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "Failed to open file %s", aws_string_c_str(file_path));
291 return AWS_OP_ERR;
292 }
293
294 /* seek to the right position and then read */
295 if (aws_fseek(file_stream, (int64_t)offset, SEEK_SET)) {
296 AWS_LOGF_ERROR(
297 AWS_LS_COMMON_GENERAL,
298 "Failed to seek to position %" PRIu64 " in file %s",
299 offset,
300 aws_string_c_str(file_path));
301 goto cleanup;
302 }
303
304 size_t actually_read = fread(output_buf->buffer + output_buf->len, 1, length, file_stream);
305 if (actually_read == 0 && ferror(file_stream)) {
306 AWS_LOGF_ERROR(
307 AWS_LS_COMMON_GENERAL, "Failed to read %zu bytes from file %s", length, aws_string_c_str(file_path));
308 aws_raise_error(aws_translate_and_raise_io_error(errno));
309 goto cleanup;
310 }
311 /* If we cannot fill the length ask for, which means we hit the EOF. */
312 *out_actual_read = actually_read;
313 output_buf->len += actually_read;
314 rt_code = AWS_OP_SUCCESS;
315 cleanup:
316 if (file_stream != NULL) {
317 fclose(file_stream);
318 }
319 return rt_code;
320 }