Debian Codegraph @

crt/aws-c-common/source/windows/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/environment.h>
7 #include <aws/common/file.h>
8 #include <aws/common/logging.h>
9 #include <aws/common/string.h>
10
11 #include <errno.h>
12 #include <io.h>
13 #include <shlwapi.h>
14 #include <stdio.h>
15 #include <windows.h>
16
17 static bool s_is_string_empty(const struct aws_string *str) {
18 return str == NULL || str->len == 0;
19 }
20
21 static bool s_is_wstring_empty(const struct aws_wstring *str) {
22 return str == NULL || str->len == 0;
23 }
24
25 FILE *aws_fopen_safe(const struct aws_string *file_path, const struct aws_string *mode) {
26 if (s_is_string_empty(file_path)) {
27 AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to open file. path is empty");
28 aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
29 return NULL;
30 }
31
32 if (s_is_string_empty(mode)) {
33 AWS_LOGF_ERROR(AWS_LS_COMMON_IO, "static: Failed to open file. mode is empty");
34 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
35 return NULL;
36 }
37
38 struct aws_wstring *w_file_path = aws_string_convert_to_wstring(aws_default_allocator(), file_path);
39 struct aws_wstring *w_mode = aws_string_convert_to_wstring(aws_default_allocator(), mode);
40
41 FILE *file = NULL;
42 errno_t error = _wfopen_s(&file, aws_wstring_c_str(w_file_path), aws_wstring_c_str(w_mode));
43 aws_wstring_destroy(w_mode);
44 aws_wstring_destroy(w_file_path);
45
46 if (error) {
47 aws_translate_and_raise_io_error_or(error, AWS_ERROR_FILE_OPEN_FAILURE);
48 AWS_LOGF_ERROR(
49 AWS_LS_COMMON_IO,
50 "static: Failed to open file. path:'%s' mode:'%s' errno:%d aws-error:%d(%s)",
51 aws_string_c_str(file_path),
52 aws_string_c_str(mode),
53 error,
54 aws_last_error(),
55 aws_error_name(aws_last_error()));
56 }
57
58 return file;
59 }
60
61 struct aws_wstring *s_to_long_path(struct aws_allocator *allocator, const struct aws_wstring *path) {
62 if (s_is_wstring_empty(path)) {
63 return NULL;
64 }
65
66 wchar_t prefix[] = L"\\\\?\\";
67 size_t prefix_size = sizeof(prefix);
68 if (aws_wstring_num_chars(path) > MAX_PATH - prefix_size) {
69
70 struct aws_byte_buf new_path;
71 aws_byte_buf_init(&new_path, allocator, sizeof(prefix) + path->len + 2);
72
73 struct aws_byte_cursor prefix_cur = aws_byte_cursor_from_array((uint8_t *)prefix, sizeof(prefix) - 2);
74 aws_byte_buf_append_dynamic(&new_path, &prefix_cur);
75
76 struct aws_byte_cursor path_cur = aws_byte_cursor_from_array((uint8_t *)aws_wstring_c_str(path), path->len);
77 aws_byte_buf_append_dynamic(&new_path, &path_cur);
78
79 struct aws_wstring *long_path =
80 aws_wstring_new_from_array(allocator, (wchar_t *)new_path.buffer, new_path.len / sizeof(wchar_t));
81 aws_byte_buf_clean_up(&new_path);
82
83 return long_path;
84 }
85
86 return aws_wstring_new_from_array(allocator, aws_wstring_c_str(path), aws_wstring_num_chars(path));
87 }
88
89 int aws_directory_create(const struct aws_string *dir_path) {
90 if (s_is_string_empty(dir_path)) {
91 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
92 }
93
94 struct aws_wstring *w_dir_path = aws_string_convert_to_wstring(aws_default_allocator(), dir_path);
95 struct aws_wstring *long_dir_path = s_to_long_path(aws_default_allocator(), w_dir_path);
96 aws_wstring_destroy(w_dir_path);
97
98 BOOL create_dir_res = CreateDirectoryW(aws_wstring_c_str(long_dir_path), NULL);
99 aws_wstring_destroy(long_dir_path);
100
101 int error = GetLastError();
102 if (!create_dir_res) {
103 if (error == ERROR_ALREADY_EXISTS) {
104 return AWS_OP_SUCCESS;
105 }
106
107 if (error == ERROR_PATH_NOT_FOUND) {
108 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
109 }
110
111 if (error == ERROR_ACCESS_DENIED) {
112 return aws_raise_error(AWS_ERROR_NO_PERMISSION);
113 }
114
115 return aws_raise_error(AWS_ERROR_UNKNOWN);
116 }
117
118 return AWS_OP_SUCCESS;
119 }
120
121 bool aws_directory_exists(const struct aws_string *dir_path) {
122 if (s_is_string_empty(dir_path)) {
123 return false;
124 }
125
126 struct aws_wstring *w_dir_path = aws_string_convert_to_wstring(aws_default_allocator(), dir_path);
127 struct aws_wstring *long_dir_path = s_to_long_path(aws_default_allocator(), w_dir_path);
128 aws_wstring_destroy(w_dir_path);
129
130 DWORD attributes = GetFileAttributesW(aws_wstring_c_str(long_dir_path));
131 aws_wstring_destroy(long_dir_path);
132
133 return (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY));
134 }
135
136 static bool s_delete_file_or_directory(const struct aws_directory_entry *entry, void *user_data) {
137 (void)user_data;
138
139 struct aws_allocator *allocator = aws_default_allocator();
140
141 struct aws_string *path_str = aws_string_new_from_cursor(allocator, &entry->relative_path);
142 int ret_val = AWS_OP_SUCCESS;
143
144 if (entry->file_type & AWS_FILE_TYPE_FILE) {
145 ret_val = aws_file_delete(path_str);
146 }
147
148 if (entry->file_type & AWS_FILE_TYPE_DIRECTORY) {
149 ret_val = aws_directory_delete(path_str, false);
150 }
151
152 aws_string_destroy(path_str);
153 return ret_val == AWS_OP_SUCCESS;
154 }
155
156 int aws_directory_delete(const struct aws_string *dir_path, bool recursive) {
157 if (s_is_string_empty(dir_path)) {
158 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
159 }
160 if (!aws_directory_exists(dir_path)) {
161 return AWS_OP_SUCCESS;
162 }
163
164 int ret_val = AWS_OP_SUCCESS;
165
166 if (recursive) {
167 ret_val = aws_directory_traverse(aws_default_allocator(), dir_path, true, s_delete_file_or_directory, NULL);
168 }
169
170 if (ret_val && aws_last_error() == AWS_ERROR_FILE_INVALID_PATH) {
171 aws_reset_error();
172 return AWS_OP_SUCCESS;
173 }
174
175 if (ret_val) {
176 return AWS_OP_ERR;
177 }
178
179 struct aws_wstring *w_dir_path = aws_string_convert_to_wstring(aws_default_allocator(), dir_path);
180 struct aws_wstring *long_dir_path = s_to_long_path(aws_default_allocator(), w_dir_path);
181 aws_wstring_destroy(w_dir_path);
182
183 BOOL remove_dir_res = RemoveDirectoryW(aws_wstring_c_str(long_dir_path));
184 aws_wstring_destroy(long_dir_path);
185
186 if (!remove_dir_res) {
187 int error = GetLastError();
188 if (error == ERROR_DIR_NOT_EMPTY) {
189 return aws_raise_error(AWS_ERROR_DIRECTORY_NOT_EMPTY);
190 }
191
192 if (error == ERROR_ACCESS_DENIED) {
193 return aws_raise_error(AWS_ERROR_NO_PERMISSION);
194 }
195
196 return aws_raise_error(AWS_ERROR_UNKNOWN);
197 }
198
199 return AWS_OP_SUCCESS;
200 }
201
202 int aws_file_delete(const struct aws_string *file_path) {
203 if (s_is_string_empty(file_path)) {
204 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
205 }
206
207 struct aws_wstring *w_file_path = aws_string_convert_to_wstring(aws_default_allocator(), file_path);
208 struct aws_wstring *long_file_path = s_to_long_path(aws_default_allocator(), w_file_path);
209 aws_wstring_destroy(w_file_path);
210
211 BOOL remove_file_res = DeleteFileW(aws_wstring_c_str(long_file_path));
212 aws_wstring_destroy(long_file_path);
213
214 if (!remove_file_res) {
215 int error = GetLastError();
216 if (error == ERROR_FILE_NOT_FOUND) {
217 return AWS_OP_SUCCESS;
218 }
219
220 if (error == ERROR_ACCESS_DENIED) {
221 return aws_raise_error(AWS_ERROR_NO_PERMISSION);
222 }
223
224 return aws_raise_error(AWS_ERROR_UNKNOWN);
225 }
226
227 return AWS_OP_SUCCESS;
228 }
229
230 int aws_directory_or_file_move(const struct aws_string *from, const struct aws_string *to) {
231 if (s_is_string_empty(from) || s_is_string_empty(to)) {
232 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
233 }
234
235 struct aws_wstring *w_from_path = aws_string_convert_to_wstring(aws_default_allocator(), from);
236 struct aws_wstring *long_from_path = s_to_long_path(aws_default_allocator(), w_from_path);
237 aws_wstring_destroy(w_from_path);
238
239 struct aws_wstring *w_to_path = aws_string_convert_to_wstring(aws_default_allocator(), to);
240 struct aws_wstring *long_to_path = s_to_long_path(aws_default_allocator(), w_to_path);
241 aws_wstring_destroy(w_to_path);
242
243 BOOL move_res = MoveFileW(aws_wstring_c_str(long_from_path), aws_wstring_c_str(long_to_path));
244 aws_wstring_destroy(long_from_path);
245 aws_wstring_destroy(long_to_path);
246
247 if (!move_res) {
248 int error = GetLastError();
249 if (error == ERROR_FILE_NOT_FOUND) {
250 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
251 }
252
253 if (error == ERROR_ACCESS_DENIED) {
254 return aws_raise_error(AWS_ERROR_NO_PERMISSION);
255 }
256
257 return aws_raise_error(AWS_ERROR_UNKNOWN);
258 }
259
260 return AWS_OP_SUCCESS;
261 }
262
263 int aws_directory_traverse(
264 struct aws_allocator *allocator,
265 const struct aws_string *path,
266 bool recursive,
267 aws_on_directory_entry *on_entry,
268 void *user_data) {
269
270 if (s_is_string_empty(path)) {
271 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
272 }
273
274 struct aws_wstring *w_path_wchar = aws_string_convert_to_wstring(allocator, path);
275 struct aws_wstring *long_path_wchar = s_to_long_path(allocator, w_path_wchar);
276 aws_wstring_destroy(w_path_wchar);
277
278 /* windows doesn't fail in FindFirstFile if it's not a directory. Do the check here. We don't call the perfectly
279 good function for this check because the string is already converted to utf-16 and it's trivial to reuse it. */
280 DWORD attributes = GetFileAttributesW(aws_wstring_c_str(long_path_wchar));
281 if (!(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
282 aws_wstring_destroy(long_path_wchar);
283 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
284 }
285
286 WIN32_FIND_DATAW ffd;
287 HANDLE find_handle = FindFirstFileW(aws_wstring_c_str(long_path_wchar), &ffd);
288
289 if (find_handle == INVALID_HANDLE_VALUE) {
290 aws_wstring_destroy(long_path_wchar);
291
292 int error = GetLastError();
293
294 if (error == ERROR_FILE_NOT_FOUND) {
295 return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
296 }
297
298 return aws_raise_error(AWS_ERROR_UNKNOWN);
299 }
300
301 FindClose(find_handle);
302
303 /* create search path string */
304 struct aws_byte_cursor path_wchar_cur =
305 aws_byte_cursor_from_array(aws_wstring_c_str(long_path_wchar), aws_wstring_size_bytes(long_path_wchar));
306 struct aws_byte_buf search_wchar_buf;
307 aws_byte_buf_init_copy_from_cursor(&search_wchar_buf, allocator, path_wchar_cur);
308
309 wchar_t search_wchar_pattern[] = L"\\*";
310 struct aws_byte_cursor search_char_wchar =
311 aws_byte_cursor_from_array((uint8_t *)search_wchar_pattern, sizeof(search_wchar_pattern));
312
313 aws_byte_buf_append_dynamic(&search_wchar_buf, &search_char_wchar);
314 struct aws_byte_cursor search_wchar_cur = aws_byte_cursor_from_buf(&search_wchar_buf);
315 /* it's already converted to wide string */
316 struct aws_wstring *search_wchar_string = aws_wstring_new_from_cursor(allocator, &search_wchar_cur);
317
318 find_handle = FindFirstFileW(aws_wstring_c_str(search_wchar_string), &ffd);
319 aws_wstring_destroy(search_wchar_string);
320 aws_byte_buf_clean_up(&search_wchar_buf);
321
322 int ret_val = AWS_OP_SUCCESS;
323
324 /* iterate each entry in the directory. Do a bunch of utf-16 conversions. Figure out the paths etc....
325 invoke the visitor, and continue recursing if the flag was set. */
326 do {
327 struct aws_string *name_component_multi_char_str =
328 aws_string_convert_from_wchar_c_str(allocator, ffd.cFileName);
329 struct aws_byte_cursor name_component_multi_char = aws_byte_cursor_from_string(name_component_multi_char_str);
330
331 /* disgard . and .. */
332 char *ascend_mark = "..";
333 char *cd_mark = ".";
334 struct aws_byte_cursor ascend_mark_cur = aws_byte_cursor_from_c_str(ascend_mark);
335 struct aws_byte_cursor cd_mark_cur = aws_byte_cursor_from_c_str(cd_mark);
336 if (aws_byte_cursor_eq(&name_component_multi_char, &ascend_mark_cur) ||
337 aws_byte_cursor_eq(&name_component_multi_char, &cd_mark_cur)) {
338 aws_string_destroy(name_component_multi_char_str);
339 continue;
340 }
341
342 /* get the relative path as utf-16, so we can talk to windows. */
343 struct aws_byte_buf relative_path_wchar;
344 aws_byte_buf_init_copy_from_cursor(&relative_path_wchar, allocator, path_wchar_cur);
345
346 wchar_t unicode_delim[] = L"\\";
347 struct aws_byte_cursor delimiter_cur =
348 aws_byte_cursor_from_array((uint8_t *)unicode_delim, sizeof(unicode_delim) - 2);
349 aws_byte_buf_append_dynamic(&relative_path_wchar, &delimiter_cur);
350 struct aws_byte_cursor name_str =
351 aws_byte_cursor_from_array(ffd.cFileName, wcsnlen(ffd.cFileName, sizeof(ffd.cFileName)) * sizeof(wchar_t));
352 aws_byte_buf_append_dynamic(&relative_path_wchar, &name_str);
353 aws_byte_buf_append_byte_dynamic(&relative_path_wchar, 0);
354 aws_byte_buf_append_byte_dynamic(&relative_path_wchar, 0);
355
356 relative_path_wchar.len -= 2;
357
358 /* now get the absolute path from the relative path we just computed. */
359 DWORD path_res = GetFullPathNameW((wchar_t *)relative_path_wchar.buffer, 0, NULL, NULL);
360
361 AWS_FATAL_ASSERT(path_res > 0);
362 struct aws_byte_buf full_path_wchar_buf;
363 aws_byte_buf_init(&full_path_wchar_buf, allocator, (size_t)path_res * sizeof(wchar_t) + 2);
364
365 full_path_wchar_buf.len = full_path_wchar_buf.capacity - 2;
366 path_res = GetFullPathNameW(
367 (wchar_t *)relative_path_wchar.buffer, (DWORD)path_res + 1, (wchar_t *)full_path_wchar_buf.buffer, NULL);
368 AWS_FATAL_ASSERT(path_res > 0);
369
370 aws_byte_buf_append_byte_dynamic(&full_path_wchar_buf, 0);
371 aws_byte_buf_append_byte_dynamic(&full_path_wchar_buf, 0);
372
373 /* now we have the data, convert the utf-16 strings we used to communicate with windows back to
374 utf-8 for the user to actually consume. */
375 struct aws_string *full_path_name_multi_char =
376 aws_string_convert_from_wchar_c_str(allocator, (wchar_t *)full_path_wchar_buf.buffer);
377 aws_byte_buf_clean_up(&full_path_wchar_buf);
378
379 struct aws_string *relative_path_multi_char =
380 aws_string_convert_from_wchar_c_str(allocator, (wchar_t *)relative_path_wchar.buffer);
381
382 struct aws_directory_entry entry;
383 AWS_ZERO_STRUCT(entry);
384 entry.relative_path = aws_byte_cursor_from_string(relative_path_multi_char);
385 entry.path = aws_byte_cursor_from_string(full_path_name_multi_char);
386
387 LARGE_INTEGER file_size;
388 file_size.HighPart = ffd.nFileSizeHigh;
389 file_size.LowPart = ffd.nFileSizeLow;
390 entry.file_size = (int64_t)file_size.QuadPart;
391
392 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
393 entry.file_type |= AWS_FILE_TYPE_DIRECTORY;
394 } else {
395 entry.file_type |= AWS_FILE_TYPE_FILE;
396 }
397
398 if (recursive && entry.file_type & AWS_FILE_TYPE_DIRECTORY) {
399 ret_val = aws_directory_traverse(allocator, relative_path_multi_char, recursive, on_entry, user_data);
400 }
401
402 /* post order traversal, if a node below us ended the traversal, don't call the visitor again. */
403 if (ret_val && aws_last_error() == AWS_ERROR_OPERATION_INTERUPTED) {
404 goto cleanup;
405 }
406
407 if (!on_entry(&entry, user_data)) {
408 ret_val = aws_raise_error(AWS_ERROR_OPERATION_INTERUPTED);
409 goto cleanup;
410 }
411
412 if (ret_val) {
413 goto cleanup;
414 }
415
416 cleanup:
417 aws_string_destroy(relative_path_multi_char);
418 aws_string_destroy(full_path_name_multi_char);
419 aws_byte_buf_clean_up(&relative_path_wchar);
420 aws_string_destroy(name_component_multi_char_str);
421
422 } while (ret_val == AWS_OP_SUCCESS && FindNextFileW(find_handle, &ffd));
423
424 aws_wstring_destroy(long_path_wchar);
425 if (find_handle != INVALID_HANDLE_VALUE) {
426 FindClose(find_handle);
427 }
428
429 return ret_val;
430 }
431
432 char aws_get_platform_directory_separator(void) {
433 return '\\';
434 }
435
436 AWS_STATIC_STRING_FROM_LITERAL(s_userprofile_env_var, "USERPROFILE");
437 AWS_STATIC_STRING_FROM_LITERAL(s_homedrive_env_var, "HOMEDRIVE");
438 AWS_STATIC_STRING_FROM_LITERAL(s_homepath_env_var, "HOMEPATH");
439
440 AWS_STATIC_STRING_FROM_LITERAL(s_home_env_var, "HOME");
441
442 struct aws_string *aws_get_home_directory(struct aws_allocator *allocator) {
443
444 /*
445 * 1. Check HOME
446 */
447 struct aws_string *home_env_var_value = NULL;
448 if (aws_get_environment_value(allocator, s_home_env_var, &home_env_var_value) == 0 && home_env_var_value != NULL) {
449 return home_env_var_value;
450 }
451
452 /*
453 * 2. (Windows) Check USERPROFILE
454 */
455 struct aws_string *userprofile_env_var_value = NULL;
456 if (aws_get_environment_value(allocator, s_userprofile_env_var, &userprofile_env_var_value) == 0 &&
457 userprofile_env_var_value != NULL) {
458 return userprofile_env_var_value;
459 }
460
461 /*
462 * 3. (Windows) Check HOMEDRIVE ++ HOMEPATH
463 */
464 struct aws_string *final_path = NULL;
465 struct aws_string *homedrive_env_var_value = NULL;
466 if (aws_get_environment_value(allocator, s_homedrive_env_var, &homedrive_env_var_value) == 0 &&
467 homedrive_env_var_value != NULL) {
468 struct aws_string *homepath_env_var_value = NULL;
469 if (aws_get_environment_value(allocator, s_homepath_env_var, &homepath_env_var_value) == 0 &&
470 homepath_env_var_value != NULL) {
471 struct aws_byte_buf concatenated_dir;
472 aws_byte_buf_init(
473 &concatenated_dir, allocator, homedrive_env_var_value->len + homepath_env_var_value->len + 1);
474
475 struct aws_byte_cursor drive_cursor = aws_byte_cursor_from_string(homedrive_env_var_value);
476 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_string(homepath_env_var_value);
477
478 aws_byte_buf_append(&concatenated_dir, &drive_cursor);
479 aws_byte_buf_append(&concatenated_dir, &path_cursor);
480
481 final_path = aws_string_new_from_buf(allocator, &concatenated_dir);
482
483 aws_byte_buf_clean_up(&concatenated_dir);
484 aws_string_destroy(homepath_env_var_value);
485 }
486
487 aws_string_destroy(homedrive_env_var_value);
488 }
489
490 if (final_path != NULL) {
491 return final_path;
492 }
493
494 return NULL;
495 }
496
497 bool aws_path_exists(const struct aws_string *path) {
498 if (s_is_string_empty(path)) {
499 return false;
500 }
501
502 struct aws_wstring *wchar_path = aws_string_convert_to_wstring(aws_default_allocator(), path);
503 bool ret_val = PathFileExistsW(aws_wstring_c_str(wchar_path)) == TRUE;
504 aws_wstring_destroy(wchar_path);
505 return ret_val;
506 }
507
508 int aws_fseek(FILE *file, int64_t offset, int whence) {
509 if (_fseeki64(file, offset, whence)) {
510 int errno_value = errno; /* Always cache errno before potential side-effect */
511 return aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_STREAM_UNSEEKABLE);
512 }
513
514 return AWS_OP_SUCCESS;
515 }
516
517 int aws_file_get_length(FILE *file, int64_t *length) {
518 if (file == NULL) {
519 return aws_raise_error(AWS_ERROR_INVALID_FILE_HANDLE);
520 }
521
522 int fd = _fileno(file);
523 if (fd == -1) {
524 return aws_raise_error(AWS_ERROR_INVALID_FILE_HANDLE);
525 }
526
527 HANDLE os_file = (HANDLE)_get_osfhandle(fd);
528 if (os_file == INVALID_HANDLE_VALUE) {
529 int errno_value = errno; /* Always cache errno before potential side-effect */
530 return aws_translate_and_raise_io_error(errno_value);
531 }
532
533 LARGE_INTEGER os_size;
534 if (!GetFileSizeEx(os_file, &os_size)) {
535 return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
536 }
537
538 int64_t size = os_size.QuadPart;
539 if (size < 0) {
540 return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
541 }
542
543 *length = size;
544
545 return AWS_OP_SUCCESS;
546 }