GCC Code Coverage Report


Directory: src/gate/
File: src/gate/files.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 702 887 79.1%
Functions: 57 63 90.5%
Branches: 327 602 54.3%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28 #include "gate/files.h"
29 #include "gate/results.h"
30 #include "gate/atomics.h"
31 #include "gate/debugging.h"
32 #include "gate/utilities.h"
33
34
35 #if defined(GATE_SYS_WIN)
36 # define GATE_CORE_FILES_WINAPI_IMPL 1
37 #elif defined(GATE_SYS_WASM_WEB)
38 # define GATE_CORE_FILES_WASM_IMPL 1
39 #elif defined(GATE_SYS_POSIX)
40 # define GATE_CORE_FILES_POSIX_IMPL 1
41 #elif defined(GATE_SYS_DOS)
42 # define GATE_CORE_FILES_DOS_IMPL 1
43 #elif defined(GATE_SYS_EFI)
44 # define GATE_CORE_FILES_EFI_IMPL 1
45 #else
46 # define GATE_CORE_FILES_NO_IMPL 1
47 #endif
48
49 142 static gate_bool_t gate_file_is_hidden_entry(gate_string_t const* path)
50 {
51
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
142 if (gate_string_is_empty(path))
52 {
53 ✗ return true; /* empty paths are invalid and therefore hidden */
54 }
55 else
56 {
57 142 const gate_size_t path_separator_found = gate_string_char_pos_last(
58 path, gate_file_path_separator_char);
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 if (path_separator_found == GATE_STR_NPOS)
60 {
61 /* filename only */
62 ✗ return gate_string_starts_with_char(path, '.');
63 }
64 else
65 {
66 /* path */
67 142 return path->str[path_separator_found + 1] == '.';
68 }
69 }
70 }
71
72 19 char GATE_CALL gate_file_path_separator_character(void)
73 {
74 19 return gate_file_path_separator_char;
75 }
76
77 367 gate_size_t gate_file_build_path(char* dest, gate_size_t dest_size, char const* path, gate_size_t path_size,
78 char const* subpath, gate_size_t subpath_size)
79 {
80 367 gate_size_t ret = 0;
81 gate_size_t len;
82
83
1/2
✓ Branch 0 taken 388 times.
✗ Branch 1 not taken.
388 while (path_size > 0)
84 {
85
2/2
✓ Branch 0 taken 367 times.
✓ Branch 1 taken 21 times.
388 if (path[path_size - 1] != gate_file_path_separator_char)
86 {
87 367 break;
88 }
89 21 --path_size;
90 }
91
1/2
✓ Branch 0 taken 367 times.
✗ Branch 1 not taken.
367 while (subpath_size > 0)
92 {
93
1/2
✓ Branch 0 taken 367 times.
✗ Branch 1 not taken.
367 if (subpath[0] != gate_file_path_separator_char)
94 {
95 367 break;
96 }
97 ✗ ++subpath;
98 ✗ --subpath_size;
99 }
100
101 367 ret = gate_str_print_text(dest, dest_size, path, path_size);
102 367 len = gate_str_print_text(&dest[ret], dest_size - ret, &gate_file_path_separator_char, 1);
103 367 ret += len;
104 367 len = gate_str_print_text(&dest[ret], dest_size - ret, subpath, subpath_size);
105 367 ret += len;
106 367 return ret;
107 }
108
109 18 gate_string_t* gate_file_build_path_string(gate_string_t* dest, gate_string_t const* path, gate_string_t const* subpath)
110 {
111 18 gate_string_t* ret = NULL;
112 18 gate_size_t path_len = gate_string_length(path);
113 18 gate_size_t subpath_len = gate_string_length(subpath);
114 18 gate_strbuilder_t builder = GATE_INIT_EMPTY;
115 18 gate_string_t strs[2] = GATE_INIT_EMPTY;
116 gate_result_t result;
117
118 do
119 {
120 18 gate_string_duplicate(&strs[0], path);
121 18 gate_string_duplicate(&strs[1], subpath);
122
123 18 gate_strbuilder_create(&builder, path_len + subpath_len + 2);
124
125 18 result = gate_file_build_path_components(&builder, gate_file_path_separator_char, strs, sizeof(strs) / sizeof(strs[0]));
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 GATE_BREAK_IF_FAILED(result);
127
128 18 ret = gate_strbuilder_to_string(&builder, dest);
129
130 } while (0);
131
132 18 gate_string_release(&strs[1]);
133 18 gate_string_release(&strs[0]);
134
135 18 gate_strbuilder_release(&builder);
136 18 return ret;
137 }
138
139 18 gate_result_t gate_file_build_path_components(gate_strbuilder_t* builder, char separator_char,
140 gate_string_t const* component_array, gate_size_t component_array_length)
141 {
142 18 gate_result_t ret = GATE_RESULT_OK;
143 gate_size_t ndx;
144 gate_string_t separator;
145
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (separator_char == 0)
147 {
148 ✗ separator_char = gate_file_path_separator_char;
149 }
150
151 18 gate_string_create_static_len(&separator, &separator_char, 1);
152
153
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
54 for (ndx = 0; ndx != component_array_length; ++ndx)
154 {
155 36 gate_string_t const* ptr_str = &component_array[ndx];
156 36 gate_size_t pos = 0;
157 36 gate_size_t pos_end = gate_string_find_last_not_of(ptr_str, &separator);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (pos_end == GATE_STR_NPOS)
159 {
160 ✗ continue;
161 }
162
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
36 if (ndx != 0)
163 {
164 18 pos = gate_string_find_first_not_of(ptr_str, &separator, 0);
165 18 gate_strbuilder_append_text(builder, &separator_char, 1);
166 }
167 36 gate_strbuilder_append_text(builder, gate_string_ptr(ptr_str, pos), pos_end - pos + 1);
168 }
169 18 return ret;
170 }
171
172 15 gate_result_t gate_file_split_path(gate_string_t const* src_path, gate_string_t* parent_path, gate_string_t* name)
173 {
174 15 char const* ptr = src_path->str;
175 15 gate_size_t len = src_path->length;
176 gate_size_t parent_len;
177
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 while (len > 0)
178 {
179
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
16 if (ptr[len - 1] != gate_file_path_separator_char)
180 {
181 15 break;
182 }
183 1 --len;
184 }
185
2/2
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 1 times.
178 for (parent_len = len; parent_len > 0; --parent_len)
186 {
187
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 163 times.
177 if (ptr[parent_len - 1] == gate_file_path_separator_char)
188 {
189
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3 times.
14 if (parent_path)
190 {
191 11 gate_string_substr(parent_path, src_path, 0, parent_len);
192 }
193
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3 times.
14 if (name)
194 {
195 11 gate_string_substr(name, src_path, parent_len, len - parent_len);
196 }
197 14 return GATE_RESULT_OK;
198 }
199 }
200 1 return GATE_RESULT_NOMATCH;
201 }
202
203 1 gate_size_t gate_file_split_path_components(gate_string_t const* src_path, char separator_char,
204 gate_string_t* out_array, gate_size_t out_array_capacity)
205 {
206 1 gate_size_t ret = 0;
207 1 gate_string_t separator = GATE_STRING_INIT_EMPTY;
208 gate_size_t pos;
209 gate_size_t pos_end;
210 1 gate_string_t path = GATE_STRING_INIT_EMPTY;
211
212 do
213 {
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (separator_char == 0)
215 {
216 ✗ separator_char = gate_file_path_separator_char;
217 }
218
219 1 gate_string_create_static_len(&separator, &separator_char, 1);
220 1 pos = gate_string_find_first_not_of(src_path, &separator, 0);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pos == GATE_STR_NPOS)
222 {
223 /* path-less string */
224 ✗ ret = 0;
225 ✗ break;
226 }
227 1 pos_end = gate_string_find_last_not_of(src_path, &separator);
228 1 gate_string_substr(&path, src_path, pos, pos_end - pos + 1);
229
230 1 pos = 0;
231
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 while (ret < out_array_capacity)
232 {
233 3 pos_end = gate_string_char_pos(&path, separator_char, pos);
234
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
3 if ((pos_end == GATE_STR_NPOS) || (ret == out_array_capacity - 1))
235 {
236 /* last component found */
237 1 gate_string_substr(&out_array[ret], &path, pos, GATE_STR_NPOS);
238 1 ++ret;
239 1 break;
240 }
241
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (pos_end > pos) /* ignore double separator occurences */
242 {
243 2 gate_string_substr(&out_array[ret], &path, pos, pos_end - pos);
244 2 ++ret;
245 }
246 2 pos = pos_end + 1;
247 }
248
249 } while (0);
250
251 1 gate_string_release(&path);
252 1 gate_string_release(&separator);
253
254 1 return ret;
255 }
256
257 2 gate_result_t gate_file_extract_path(char const* src_path, gate_size_t src_path_len,
258 char* name_buffer, gate_size_t name_buffer_len,
259 char* parent_buffer, gate_size_t parent_buffer_len)
260 {
261 2 char const* ptr = src_path;
262 2 gate_size_t len = src_path_len;
263 gate_size_t parent_len;
264
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 while (len > 0)
265 {
266
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptr[len - 1] != gate_file_path_separator_char)
267 {
268 2 break;
269 }
270 ✗ --len;
271 }
272
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 for (parent_len = len; parent_len > 0; --parent_len)
273 {
274
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (ptr[parent_len - 1] == gate_file_path_separator_char)
275 {
276
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (name_buffer && name_buffer_len > 0)
277 {
278 2 gate_str_print_text(name_buffer, name_buffer_len, &ptr[parent_len], len - parent_len);
279 }
280
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (parent_buffer && parent_buffer_len > 0)
281 {
282 2 gate_str_print_text(parent_buffer, parent_buffer_len, ptr, parent_len - 1);
283 }
284 2 return GATE_RESULT_OK;
285 }
286 }
287 ✗ return GATE_RESULT_NOMATCH;
288 }
289
290 1 gate_result_t gate_file_extract_path_string(gate_string_t const* src_path, gate_string_t* name, gate_string_t* parent)
291 {
292 char namebuffer[GATE_MAX_FILENAME_LENGTH];
293 char parentbuffer[GATE_MAX_FILEPATH_LENGTH];
294 gate_result_t result;
295
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_DEBUG_ASSERT(src_path != NULL);
297
298 2 result = gate_file_extract_path(
299 1 gate_string_ptr(src_path, 0), gate_string_length(src_path),
300 namebuffer, sizeof(namebuffer),
301 parentbuffer, sizeof(parentbuffer)
302 );
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_RETURN_IF_FAILED(result);
304
305
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (name != NULL)
306 {
307
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_string_create(name, namebuffer, gate_str_length_max(namebuffer, sizeof(namebuffer))))
308 {
309 ✗ return GATE_RESULT_OUTOFMEMORY;
310 }
311 }
312
313
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (parent != NULL)
314 {
315
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_string_create(parent, parentbuffer, gate_str_length_max(parentbuffer, sizeof(parentbuffer))))
316 {
317 ✗ if (name)
318 {
319 ✗ gate_string_release(name);
320 }
321 ✗ return GATE_RESULT_OUTOFMEMORY;
322 }
323 }
324 1 return GATE_RESULT_OK;
325 }
326
327 1 static gate_result_t gate_file_copy_internal(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath, gate_enumint_t flags)
328 {
329 gate_result_t ret;
330 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
331 1 gate_file_t src_handle = GATE_FILE_INVALID;
332 1 gate_file_t dst_handle = GATE_FILE_INVALID;
333 gate_size_t bytes_read;
334 gate_size_t bytes_written;
335 gate_size_t bytes_copied;
336
337 do
338 {
339
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!GATE_FLAG_ENABLED(flags, GATE_FILE_COPY_OVERWRITE))
340 {
341 1 ret = gate_file_exists(dstfilepath);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_SUCCEEDED(ret))
343 {
344 ✗ ret = GATE_RESULT_ALREADYEXISTS;
345 ✗ break;
346 }
347 }
348 1 ret = gate_file_open(srcfilepath, GATE_STREAM_OPEN_READ, &src_handle);
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
350 1 ret = gate_file_open(dstfilepath, GATE_STREAM_OPEN_WRITE, &dst_handle);
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
352
353
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 while (GATE_SUCCEEDED(ret))
354 {
355 2 bytes_read = 0;
356 2 ret = gate_file_read(src_handle, buffer, sizeof(buffer), &bytes_read);
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
358
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (bytes_read == 0)
359 {
360 1 break;
361 }
362 1 bytes_copied = 0;
363
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 while (bytes_copied < bytes_read)
364 {
365 1 ret = gate_file_write(dst_handle, &buffer[bytes_copied], bytes_read - bytes_copied, &bytes_written);
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
367 1 bytes_copied += bytes_written;
368 }
369 }
370
371 } while (0);
372
373
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (src_handle != GATE_FILE_INVALID)
374 {
375 1 gate_file_close(src_handle);
376 }
377
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (dst_handle != GATE_FILE_INVALID)
378 {
379 1 gate_file_close(dst_handle);
380 }
381 1 return ret;
382 }
383
384 ✗ static gate_result_t gate_file_move_internal(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath)
385 {
386 gate_result_t ret;
387 ✗ ret = gate_file_copy(srcfilepath, dstfilepath, 0);
388 ✗ if (GATE_SUCCEEDED(ret))
389 {
390 ✗ ret = gate_file_delete(srcfilepath);
391 }
392 ✗ return ret;
393 }
394
395
396 1 static gate_result_t gate_file_copy_list_create_path(gate_string_t const* path)
397 {
398 1 gate_string_t parent = GATE_STRING_INIT_EMPTY;
399 gate_result_t ret;
400 1 ret = gate_file_exists(path);
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(ret))
402 {
403 ✗ ret = gate_file_split_path(path, &parent, NULL);
404 ✗ if (GATE_SUCCEEDED(ret))
405 {
406 ✗ ret = gate_file_copy_list_create_path(&parent);
407 ✗ if (GATE_SUCCEEDED(ret))
408 {
409 ✗ ret = gate_file_dir_create(path, GATE_FILE_DIR_CREATE_DEFAULT);
410 }
411 ✗ gate_string_release(&parent);
412 }
413 }
414 1 return ret;
415 }
416
417 1 static gate_result_t gate_file_copy_list_entry(gate_string_t const* source, gate_string_t const* dest,
418 gate_set_t* existing_dirs, gate_enumint_t flags)
419 {
420 1 gate_result_t ret = GATE_RESULT_FAILED;
421 1 gate_string_t dest_parent = GATE_STRING_INIT_EMPTY;
422
423 do
424 {
425 1 ret = gate_file_split_path(dest, &dest_parent, NULL);
426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
427
428
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (!gate_set_iterator_valid(gate_set_get(existing_dirs, &dest_parent)))
429 {
430 1 ret = gate_file_copy_list_create_path(&dest_parent);
431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
432 1 gate_set_add(existing_dirs, &dest_parent);
433 }
434 1 ret = gate_file_copy(source, dest, flags);
435 } while (0);
436
437 1 gate_string_release(&dest_parent);
438 1 return ret;
439 }
440
441 1 gate_result_t gate_file_copy_list(gate_map_t const* path_map, gate_enumint_t flags,
442 gate_file_copy_list_callback callback, void* user_param)
443 {
444 1 gate_result_t ret = GATE_RESULT_FAILED;
445 gate_result_t result;
446 1 gate_set_t existing_dirs = GATE_INIT_EMPTY;
447 gate_map_iterator_t iter;
448 gate_string_t const* source;
449 gate_string_t const* dest;
450 char source_buffer[GATE_MAX_COPYBUFFER_LENGTH];
451 char dest_buffer[GATE_MAX_COPYBUFFER_LENGTH];
452 do
453 {
454
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_util_stringset_create(&existing_dirs))
455 {
456 ✗ ret = GATE_RESULT_OUTOFMEMORY;
457 ✗ break;
458 }
459
460 1 ret = GATE_RESULT_OK;
461
2/2
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
2 for (iter = gate_map_first(path_map); gate_map_iterator_valid(iter); iter = gate_map_iterator_next(iter))
462 {
463 1 source = (gate_string_t const*)gate_map_iterator_key(iter);
464 1 dest = (gate_string_t const*)gate_map_iterator_value(iter);
465
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (source && dest)
466 {
467 1 result = gate_file_copy_list_entry(source, dest, &existing_dirs, flags);
468
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (callback)
469 {
470 1 gate_string_to_buffer(source, source_buffer, sizeof(source_buffer));
471 1 gate_string_to_buffer(dest, dest_buffer, sizeof(dest_buffer));
472
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!callback(source_buffer, dest_buffer, result, user_param))
473 {
474 ✗ break;
475 }
476 }
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_FAILED(result))
478 {
479 ✗ if (!GATE_FLAG_ENABLED(flags, GATE_FILE_COPY_IGNORE_ERROR))
480 {
481 ✗ ret = result;
482 ✗ break;
483 }
484 }
485 }
486 }
487 } while (0);
488
489 1 gate_map_destroy(&existing_dirs);
490
491 1 return ret;
492 }
493
494 5 gate_result_t gate_file_dir_delete_recursive(gate_string_t const* dirpath)
495 {
496 gate_result_t result;
497 gate_file_dirreader_t reader;
498 char path[GATE_MAX_FILEPATH_LENGTH];
499 gate_size_t path_len;
500 5 gate_string_t entry = GATE_STRING_INIT_EMPTY;
501
502 do
503 {
504 5 result = gate_file_dir_exists(dirpath);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 GATE_BREAK_IF_FAILED(result);
506
507 5 result = gate_file_dir_open(dirpath, &reader);
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 GATE_BREAK_IF_FAILED(result);
509
510
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 5 times.
16 while (0 != (path_len = gate_file_dir_read(&reader, path, sizeof(path), false)))
511 {
512 11 gate_string_create_static_len(&entry, path, path_len);
513 11 result = gate_file_dir_exists(&entry);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (GATE_SUCCEEDED(result))
515 {
516 /* subdirectory */
517 ✗ result = gate_file_dir_delete_recursive(&entry);
518 }
519 else
520 {
521 /* file */
522 11 result = gate_file_delete(&entry);
523 }
524 11 gate_string_release(&entry);
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 GATE_BREAK_IF_FAILED(result);
526 }
527
528 5 gate_file_dir_close(&reader);
529
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 GATE_BREAK_IF_FAILED(result);
531
532 5 result = gate_file_dir_delete(dirpath);
533
534 } while (0);
535 5 return result;
536 }
537
538
539
540
541 struct gate_file_find_param
542 {
543 gate_file_filter_t const* filter;
544 gate_file_list_callback_t callback;
545 void* userparam;
546 };
547
548 36 static gate_bool_t GATE_CALL gate_file_find_callback(gate_file_entry_t const* entry, void* userparam)
549 {
550 36 struct gate_file_find_param* param = (struct gate_file_find_param*)userparam;
551 gate_string_t path;
552 36 gate_bool_t ret = true;
553 36 gate_bool_t is_dir = GATE_FLAG_ENABLED(entry->props.attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY);
554
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (param->filter)
555 {
556 36 gate_string_create_static(&path, entry->path);
557
558 do
559 {
560
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 if (!gate_string_is_empty(&param->filter->pattern))
561 {
562
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
36 if (!gate_string_like_one_of(&path, &param->filter->pattern, ';'))
563 {
564 ✗ break;
565 }
566 }
567
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1 times.
36 if (!is_dir)
568 {
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (param->filter->from_size > 0)
570 {
571 ✗ if (entry->props.size < param->filter->from_size)
572 {
573 ✗ break;
574 }
575 }
576
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 if (param->filter->to_size != 0)
577 {
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (entry->props.size > param->filter->to_size)
579 {
580 ✗ break;
581 }
582 }
583 }
584
585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (entry->props.time_modified < param->filter->from_modified)
586 {
587 ✗ break;
588 }
589
2/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
36 if ((param->filter->to_modified != 0) && (entry->props.time_modified > param->filter->to_modified))
590 {
591 ✗ break;
592 }
593
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (param->filter->from_size > 0)
595 {
596 ✗ if (entry->props.size < param->filter->from_size)
597 {
598 ✗ break;
599 }
600 }
601
602 36 ret = param->callback(entry, param->userparam);
603
604 } while (0);
605
606
3/4
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 35 times.
36 if (param->filter->recursive && is_dir)
607 {
608 1 gate_file_find(&path, param->filter, param->callback, param->userparam);
609 }
610 }
611 else
612 {
613 ✗ ret = param->callback(entry, param->userparam);
614 }
615 36 return ret;
616 }
617
618 2 gate_result_t gate_file_find(gate_string_t const* dirpath, gate_file_filter_t const* filter,
619 gate_file_list_callback_t callback, void* userparam)
620 {
621 struct gate_file_find_param param;
622 2 param.filter = filter;
623 2 param.callback = callback;
624 2 param.userparam = userparam;
625
626 2 return gate_file_list(dirpath, &gate_file_find_callback, &param);
627 }
628
629 15 gate_result_t gate_file_dir_create_all(gate_string_t const* dirpath, gate_enumint_t flags)
630 {
631 gate_result_t ret;
632 15 gate_string_t parent = GATE_STRING_INIT_EMPTY;
633 15 gate_string_t name = GATE_STRING_INIT_EMPTY;
634
635 do
636 {
637 15 ret = gate_file_dir_exists(dirpath);
638
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4 times.
15 if (GATE_SUCCEEDED(ret))
639 {
640 /* path does already exist -> nothing to do */
641 11 break;
642 }
643
644 4 ret = gate_file_split_path(dirpath, &parent, &name);
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
646
647 /* ensure that parents exist */
648 4 ret = gate_file_dir_create_all(&parent, flags);
649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATE_BREAK_IF_FAILED(ret);
650
651 /* now try to create the deepest path component */
652 4 ret = gate_file_dir_create(dirpath, flags);
653 } while (0);
654
655 15 gate_string_release(&parent);
656 15 gate_string_release(&name);
657 15 return ret;
658 }
659
660
661 87 gate_result_t gate_file_get_content_buffer(gate_string_t const* filepath, char* content_buffer, gate_size_t* content_buffer_len)
662 {
663 gate_result_t ret;
664 87 gate_file_t filehandle = GATE_FILE_INVALID;
665 87 gate_size_t copied = 0;
666 87 gate_size_t bufferlen = *content_buffer_len;
667 gate_size_t bufferused;
668
669 do
670 {
671
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 82 times.
87 GATE_BREAK_IF_FAILED(ret = gate_file_open(filepath, GATE_STREAM_OPEN_READ, &filehandle));
672
1/2
✓ Branch 0 taken 164 times.
✗ Branch 1 not taken.
164 while (copied < bufferlen)
673 {
674
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 164 times.
164 GATE_BREAK_IF_FAILED(ret = gate_file_read(filehandle, &content_buffer[copied], bufferlen - copied, &bufferused));
675
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 82 times.
164 if (bufferused == 0) break;
676 82 copied += bufferused;
677 }
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 GATE_BREAK_IF_FAILED(ret);
679
680 /* success case: */
681 82 *content_buffer_len = copied;
682 82 ret = GATE_RESULT_OK;
683 } while (0);
684
685
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 5 times.
87 if (filehandle != GATE_FILE_INVALID)
686 {
687 82 gate_file_close(filehandle);
688 }
689 87 return ret;
690 }
691
692
693 2 gate_result_t gate_file_get_content(gate_string_t const* filepath, gate_string_t* content_buffer)
694 {
695 gate_result_t ret;
696 2 gate_file_t filehandle = GATE_FILE_INVALID;
697 gate_int64_t filesize;
698 2 gate_int64_t copied = 0;
699 gate_strbuilder_t builder;
700 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
701 gate_size_t bufferused;
702
703 2 gate_strbuilder_create(&builder, 0);
704
705 do
706 {
707
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret = gate_file_open(filepath, GATE_STREAM_OPEN_READ, &filehandle));
708
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret = gate_file_size(filehandle, &filesize));
709
710 2 gate_strbuilder_create(&builder, (gate_size_t)filesize);
711
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 while (copied != filesize)
712 {
713
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret = gate_file_read(filehandle, buffer, sizeof(buffer), &bufferused));
714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (bufferused == 0) break;
715
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (0 == gate_strbuilder_append_text(&builder, buffer, bufferused))
716 {
717 ✗ ret = GATE_RESULT_OUTOFMEMORY;
718 ✗ break;
719 }
720 2 copied += bufferused;
721 }
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
723
724
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_strbuilder_to_string(&builder, content_buffer))
725 {
726 ✗ ret = GATE_RESULT_OUTOFMEMORY;
727 ✗ break;
728 }
729
730 /* success case: */
731 2 ret = GATE_RESULT_OK;
732 } while (0);
733
734
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (filehandle != GATE_FILE_INVALID)
735 {
736 2 gate_file_close(filehandle);
737 }
738 2 gate_strbuilder_release(&builder);
739
740 2 return ret;
741 }
742 1 gate_result_t gate_file_get_content_lines(gate_string_t const* filepath, gate_arraylist_t string_array)
743 {
744 1 gate_result_t ret = GATE_RESULT_FAILED;
745 char buffer[GATE_MAX_COPYBUFFER_LENGTH * 3];
746 gate_size_t bufferused;
747 1 gate_controlstream_t* stream = NULL;
748 gate_string_t line;
749
750 do
751 {
752
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (gate_arraylist_is_null(string_array))
753 {
754 ✗ ret = GATE_RESULT_NULLPOINTER;
755 ✗ break;
756 }
757
758 1 ret = gate_file_openstream(filepath, GATE_STREAM_OPEN_READ, &stream);
759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
760
761
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 while (GATE_SUCCEEDED(ret = gate_stream_read_line((gate_stream_t*)stream, buffer, sizeof(buffer), &bufferused)))
762 {
763
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (bufferused == 0)
764 {
765 1 break;
766 }
767
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (buffer[bufferused - 1] == '\n')
768 {
769 1 --bufferused;
770 }
771
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 if ((bufferused != 0) && (buffer[bufferused - 1] == '\r'))
772 {
773 1 --bufferused;
774 }
775
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (NULL != gate_string_create(&line, buffer, bufferused))
776 {
777 2 gate_util_stringarray_add(string_array, &line);
778 2 gate_string_release(&line);
779 }
780 }
781 } while (0);
782
783
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (stream != NULL)
784 {
785 1 gate_object_release(stream);
786 }
787 1 return ret;
788 }
789
790 7 static gate_result_t gate_file_write_block(gate_file_t filehandle, gate_string_t const* content_buffer)
791 {
792 7 gate_result_t ret = GATE_RESULT_OK;
793 7 gate_size_t pos = 0;
794 gate_size_t written;
795
796
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 while (pos < content_buffer->length)
797 {
798 7 ret = gate_file_write(filehandle, &content_buffer->str[pos], content_buffer->length - pos, &written);
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 GATE_BREAK_IF_FAILED(ret);
800 7 pos += written;
801 }
802 7 return ret;
803 }
804
805 3 gate_result_t gate_file_set_content(gate_string_t const* filepath, gate_string_t const* content_buffer)
806 {
807 gate_result_t ret;
808 3 gate_file_t filehandle = GATE_FILE_INVALID;
809
810
811 do
812 {
813 3 ret = gate_file_open(filepath, GATE_STREAM_OPEN_WRITE, &filehandle);
814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
815 3 ret = gate_file_write_block(filehandle, content_buffer);
816 } while (0);
817
818
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (filehandle != GATE_FILE_INVALID)
819 {
820 3 gate_file_close(filehandle);
821 }
822
823 3 return ret;
824 }
825
826 1 gate_result_t gate_file_set_content_buffer(gate_string_t const* filepath, char const* content_buffer, gate_size_t content_buffer_len)
827 {
828 gate_result_t ret;
829 1 gate_string_t text = GATE_STRING_INIT_EMPTY;
830
831 1 gate_string_create_static_len(&text, content_buffer, content_buffer_len);
832 1 ret = gate_file_set_content(filepath, &text);
833 1 gate_string_release(&text);
834
835 1 return ret;
836 }
837
838 1 gate_result_t gate_file_set_content_lines(gate_string_t const* filepath, gate_arraylist_t string_array)
839 {
840 gate_result_t ret;
841 1 gate_file_t filehandle = GATE_FILE_INVALID;
842 1 gate_size_t count = gate_arraylist_length(string_array);
843 gate_size_t index;
844 gate_string_t const* ptr;
845 static gate_string_t const new_line = { GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH, NULL };
846
847 do
848 {
849 1 ret = gate_file_open(filepath, GATE_STREAM_OPEN_WRITE, &filehandle);
850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
851
852
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (index = 0; index != count; ++index)
853 {
854 2 ptr = (gate_string_t const*)gate_arraylist_get(string_array, index);
855 2 ret = gate_file_write_block(filehandle, ptr);
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
857 2 ret = gate_file_write_block(filehandle, &new_line);
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
859 }
860 } while (0);
861
862
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (filehandle != GATE_FILE_INVALID)
863 {
864 1 gate_file_close(filehandle);
865 }
866
867 1 return ret;
868 }
869
870
871
872 #if defined(GATE_CORE_FILES_WINAPI_IMPL)
873
874 #include "gate/platforms.h"
875 #include "gate/memalloc.h"
876 #include "gate/strings.h"
877
878 #if defined(GATE_COMPILER_MSVC) && !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WINSTORE)
879 #pragma comment(lib, "Mpr.lib")
880 #endif
881
882 GATE_CORE_API char const gate_file_path_separator_char = '\\';
883 GATE_CORE_API char const* const gate_file_path_separator = "\\";
884
885 #if defined(GATE_SYS_WINSTORE)
886 # define MoveFile(lpExistingFileName, lpNewFileName) MoveFileEx(lpExistingFileName, lpNewFileName, (MOVEFILE_COPY_ALLOWED))
887 #endif
888
889 #if defined(GATE_SYS_WIN16)
890 # include <direct.h>
891 # include <tchar.h>
892
893
894 static DWORD WINAPI GetLogicalDrives()
895 {
896 DWORD ret = 0;
897 int n;
898 UINT tp;
899 for (n = 0; n < 25; ++n)
900 {
901 tp = GetDriveType(n);
902 if (tp != 0)
903 {
904 ret |= (DWORD)(1UL << n);
905 }
906 }
907 return ret;
908 }
909
910 #endif
911
912 typedef WORD GATE_FILEOP_FLAGS;
913
914 typedef struct _GATE_SHFILEOPSTRUCT
915 {
916 HWND hwnd;
917 UINT wFunc;
918 TCHAR const* pFrom; /* terminated by double zero */
919 TCHAR const* pTo; /* terminated by double zero */
920 GATE_FILEOP_FLAGS fFlags;
921 BOOL fAnyOperationsAborted;
922 LPVOID hNameMappings;
923 PCSTR lpszProgressTitle;
924 } GATE_SHFILEOPSTRUCT, * GATE_LPSHFILEOPSTRUCT;
925
926 #if defined(GATE_WIN32_UNICODE)
927 # define shell32_shfileop_name "SHFileOperationW"
928 #else
929 # define shell32_shfileop_name "SHFileOperationA"
930 #endif
931
932 typedef int(WINAPI* shell32_shfileop_t)(GATE_LPSHFILEOPSTRUCT lpFileOp);
933 static shell32_shfileop_t shell32_shfileop = NULL;
934 static gate_atomic_flag_t shell32_shfileop_loaded = GATE_ATOMIC_FLAG_INIT;
935
936 #define GATE_WIN32_SHFILEOP_MOVE 0x0001
937 #define GATE_WIN32_SHFILEOP_COPY 0x0002
938 #define GATE_WIN32_SHFILEOP_DELETE 0x0003
939 #define GATE_WIN32_SHFILEOP_RENAME 0x0004
940
941
942 #ifndef FOF_SILENT
943 #define FOF_SILENT 0x0004
944 #endif
945 #ifndef FOF_NOCONFIRMATION
946 #define FOF_NOCONFIRMATION 0x0010
947 #endif
948 #ifndef FOF_NOCONFIRMMKDIR
949 #define FOF_NOCONFIRMMKDIR 0x0200
950 #endif
951 #ifndef FOF_NOERRORUI
952 #define FOF_NOERRORUI 0x0400
953 #endif
954 #ifndef FOF_NORECURSION
955 #define FOF_NORECURSION 0x1000
956 #endif
957 #ifndef FOF_NO_UI
958 #define FOF_NO_UI (FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR)
959 #endif
960
961 static gate_bool_t load_shell32_shfileop()
962 {
963 HMODULE hmod_shell32 = gate_win32_get_shell_module();
964
965 if (hmod_shell32)
966 {
967 if (!gate_atomic_flag_set(&shell32_shfileop_loaded))
968 {
969 // execute only once:
970 gate_win32_get_proc_address(hmod_shell32, shell32_shfileop_name, &shell32_shfileop);
971 }
972 }
973 return shell32_shfileop != NULL;
974 }
975
976 static gate_bool_t gate_win32_shfileop(unsigned operation, TCHAR const* fromZZ, TCHAR const* toZZ)
977 {
978 GATE_SHFILEOPSTRUCT fileop;
979 int result;
980
981 if (!load_shell32_shfileop())
982 {
983 return false;
984 }
985
986 gate_mem_clear(&fileop, sizeof(fileop));
987 fileop.hwnd = (HWND)NULL;
988 fileop.wFunc = operation;
989 fileop.pFrom = fromZZ;
990 fileop.pTo = toZZ;
991 fileop.fFlags = FOF_NORECURSION | FOF_NO_UI;
992 result = shell32_shfileop(&fileop);
993 return result == 0;
994 }
995
996
997 #if defined(GATE_SYS_WIN16)
998
999 gate_result_t gate_file_open(gate_string_t const* filepath, gate_enumint_t flags, gate_file_t* filehandle)
1000 {
1001 char path[GATE_MAX_FILEPATH_LENGTH];
1002 gate_platform_stream_t ps;
1003 gate_result_t result;
1004
1005 gate_string_to_buffer(filepath, path, sizeof(path));
1006 result = gate_platform_stream_open(path, flags, 0, &ps);
1007 if (GATE_SUCCEEDED(result) && filehandle)
1008 {
1009 *filehandle = (gate_file_t)ps;
1010 }
1011 return result;
1012 }
1013
1014 #else /* !defined(GATE_SYS_WIN16) */
1015
1016
1017 #ifndef SECURITY_WORLD_SID_AUTHORITY
1018 #define SECURITY_WORLD_SID_AUTHORITY {0,0,0,0,0,1}
1019 #endif
1020
1021 #ifndef SECURITY_NT_AUTHORITY
1022 #define SECURITY_NT_AUTHORITY {0,0,0,0,0,5}
1023 #endif
1024
1025 static const SID_IDENTIFIER_AUTHORITY gate_win32_world_sid = { SECURITY_WORLD_SID_AUTHORITY };
1026 static const SID_IDENTIFIER_AUTHORITY gate_win32_nt_sid = { SECURITY_NT_AUTHORITY };
1027
1028 static LPSECURITY_ATTRIBUTES gate_win32_create_security_attribs(
1029 gate_enumint_t flags, LPSECURITY_ATTRIBUTES secattributes,
1030 gate_win32_sidstruct_t* sidowner, gate_win32_sidstruct_t* sidgroup, gate_win32_sidstruct_t* sideveryone,
1031 SECURITY_DESCRIPTOR* secdescr, BYTE* aclBuffer)
1032 {
1033 LPSECURITY_ATTRIBUTES ret = NULL; /* by default or in error cases -> inherit default security descriptor */
1034 gate_bool_t restrictowner = GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEOWNERRESTRICTED);
1035 gate_bool_t restrictgroup = GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEGROUPRESTRICTED);
1036 gate_bool_t unrestricted = GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEUNRESTRICTED);
1037
1038 if (!restrictowner && !restrictgroup && !unrestricted)
1039 {
1040 /* return NULL to inherit default security descriptor */
1041 GATE_UNUSED_ARG(secattributes);
1042 GATE_UNUSED_ARG(sidowner);
1043 GATE_UNUSED_ARG(sidgroup);
1044 GATE_UNUSED_ARG(sideveryone);
1045 GATE_UNUSED_ARG(secdescr);
1046 GATE_UNUSED_ARG(aclBuffer);
1047 }
1048 else
1049 {
1050 #if !defined(GATE_SYS_WINSTORE) && !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WIN16)
1051 PSID psidAll = (PSID)sideveryone;
1052 PSID psidGroup = (PSID)sidgroup;
1053 PSID psidOwner = (PSID)sidowner;
1054
1055 HANDLE hToken = (HWND)NULL;
1056 HANDLE hProc;
1057 char userBuffer[1024] = { 0 };
1058 PTOKEN_USER pUser = (PTOKEN_USER)&userBuffer[0];
1059 PACL pacl = (PACL)&aclBuffer[0];
1060
1061 DWORD dwLen = sizeof(userBuffer);
1062 DWORD dwACLFlags = GENERIC_READ | GENERIC_WRITE | DELETE | GENERIC_EXECUTE;
1063
1064 do
1065 {
1066 hProc = GetCurrentProcess();
1067
1068 gate_mem_clear(sideveryone, sizeof(gate_win32_sidstruct_t));
1069 sideveryone->Revision = SID_REVISION;
1070 sideveryone->IdentifierAuthority = gate_win32_world_sid;
1071 sideveryone->SubAuthCount = 1;
1072
1073 gate_mem_clear(sidgroup, sizeof(gate_win32_sidstruct_t));
1074 sidgroup->Revision = SID_REVISION;
1075 sidgroup->IdentifierAuthority = gate_win32_nt_sid;
1076 sidgroup->SubAuthCount = 2;
1077 sidgroup->SubAuthorities[0] = SECURITY_BUILTIN_DOMAIN_RID;
1078 sidgroup->SubAuthorities[1] = DOMAIN_ALIAS_RID_USERS;
1079
1080 gate_mem_clear(sidowner, sizeof(gate_win32_sidstruct_t));
1081 sidowner->Revision = SID_REVISION;
1082
1083 /* check, if the platform supports all required functions */
1084 if ((gate_platform.AdvOpenProcessToken == NULL)
1085 || (gate_platform.AdvGetTokenInformation == NULL)
1086 || (gate_platform.AdvInitializeSecurityDescriptor == NULL)
1087 || (gate_platform.AdvInitializeAcl == NULL)
1088 || (gate_platform.AdvAddAccessAllowedAce == NULL)
1089 || (gate_platform.AdvSetSecurityDescriptorDacl == NULL)
1090 )
1091 {
1092 break;
1093 }
1094
1095 if (!gate_platform.AdvOpenProcessToken(hProc, TOKEN_READ, &hToken))
1096 {
1097 break;
1098 }
1099
1100 if (!gate_platform.AdvGetTokenInformation(hToken, TokenUser, &userBuffer[0], sizeof(userBuffer), &dwLen))
1101 {
1102 break;
1103 }
1104
1105 gate_win32_sid_export(pUser->User.Sid, sidowner);
1106
1107 if (!gate_platform.AdvInitializeSecurityDescriptor(secdescr, SECURITY_DESCRIPTOR_REVISION))
1108 {
1109 break;
1110 }
1111 if (!gate_platform.AdvInitializeAcl(pacl, sizeof(aclBuffer), ACL_REVISION))
1112 {
1113 break;
1114 }
1115
1116 if (unrestricted)
1117 {
1118 if (!gate_platform.AdvAddAccessAllowedAce(pacl, ACL_REVISION, dwACLFlags, psidAll))
1119 {
1120 break;
1121 }
1122 }
1123 else
1124 {
1125 if (restrictowner)
1126 {
1127 if (!gate_platform.AdvAddAccessAllowedAce(pacl, ACL_REVISION, dwACLFlags, psidOwner))
1128 {
1129 break;
1130 }
1131 }
1132 if (restrictgroup)
1133 {
1134 if (!gate_platform.AdvAddAccessAllowedAce(pacl, ACL_REVISION, dwACLFlags, psidGroup))
1135 {
1136 break;
1137 }
1138 }
1139 }
1140
1141 if (!gate_platform.AdvSetSecurityDescriptorDacl(secdescr, TRUE, pacl, FALSE))
1142 {
1143 break;
1144 }
1145
1146 secattributes->nLength = sizeof(SECURITY_ATTRIBUTES);
1147 secattributes->bInheritHandle = FALSE;
1148 secattributes->lpSecurityDescriptor = secdescr;
1149
1150 ret = secattributes;
1151 } while (0);
1152
1153 gate_win32_closehandle(hToken);
1154 #endif
1155 }
1156
1157
1158 return ret;
1159 }
1160
1161 gate_result_t gate_file_open(gate_string_t const* filepath, gate_enumint_t flags, gate_file_t* filehandle)
1162 {
1163 TCHAR path[GATE_MAX_FILEPATH_LENGTH];
1164 DWORD accessmode = 0;
1165 DWORD sharemode = 0;
1166 DWORD createdispo = 0;
1167 DWORD fileattribs = FILE_ATTRIBUTE_NORMAL;
1168 gate_bool_t truncate = false;
1169 gate_bool_t append = false;
1170 HANDLE hfile;
1171 gate_result_t ret;
1172 gate_win32_sidstruct_t sideveryone;
1173 gate_win32_sidstruct_t sidowner;
1174 gate_win32_sidstruct_t sidgroup;
1175 LPSECURITY_ATTRIBUTES ptr_secattributes = NULL;
1176 SECURITY_ATTRIBUTES secattributes;
1177 SECURITY_DESCRIPTOR secdescr;
1178 BYTE aclBuffer[1024];
1179
1180 gate_win32_utf8_2_winstr(filepath->str, filepath->length, path, sizeof(path) / sizeof(path[0]));
1181
1182 switch (flags & GATE_STREAM_OPEN_FLAGS_MASK)
1183 {
1184 case GATE_STREAM_OPEN_READ:
1185 {
1186 accessmode = (GENERIC_READ);
1187 sharemode = (FILE_SHARE_READ);
1188 createdispo = (OPEN_EXISTING);
1189 break;
1190 }
1191 case GATE_STREAM_OPEN_WRITE:
1192 {
1193 accessmode = (GENERIC_WRITE);
1194 sharemode = (FILE_SHARE_READ);
1195 createdispo = (CREATE_ALWAYS);
1196 fileattribs = FILE_ATTRIBUTE_ARCHIVE;
1197 truncate = true;
1198 break;
1199 }
1200 case GATE_STREAM_OPEN_READWRITE:
1201 {
1202 accessmode = (GENERIC_READ | GENERIC_WRITE);
1203 sharemode = (FILE_SHARE_READ);
1204 createdispo = (OPEN_ALWAYS);
1205 fileattribs = FILE_ATTRIBUTE_ARCHIVE;
1206 break;
1207 }
1208 case GATE_STREAM_OPEN_APPENDWRITE:
1209 {
1210 accessmode = (GENERIC_WRITE);
1211 sharemode = (FILE_SHARE_READ);
1212 createdispo = (OPEN_ALWAYS);
1213 fileattribs = FILE_ATTRIBUTE_ARCHIVE;
1214 append = true;
1215 break;
1216 }
1217 case GATE_STREAM_OPEN_APPENDREADWRITE:
1218 {
1219 accessmode = (GENERIC_READ | GENERIC_WRITE);
1220 createdispo = (OPEN_ALWAYS);
1221 fileattribs = FILE_ATTRIBUTE_ARCHIVE;
1222 append = true;
1223 break;
1224 }
1225 default:
1226 {
1227 return GATE_RESULT_INVALIDARG;
1228 }
1229 }
1230
1231 sharemode = (FILE_SHARE_READ);
1232 if (GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_SHARED))
1233 {
1234 sharemode |= FILE_SHARE_WRITE;
1235 }
1236
1237 /* generate a security descriptor for the file, if supported by the platform, otherwise NULL: */
1238 ptr_secattributes = gate_win32_create_security_attribs(flags, &secattributes, &sidowner, &sidgroup, &sideveryone, &secdescr, aclBuffer);
1239
1240 hfile = gate_win32_createfile(path, accessmode, sharemode, createdispo, fileattribs, 0, ptr_secattributes);
1241 if (hfile == (HANDLE)INVALID_HANDLE_VALUE)
1242 {
1243 gate_win32_print_lasterror(&ret, NULL, 0);
1244 return ret;
1245 }
1246
1247 if (truncate)
1248 {
1249 gate_win32_setendoffile(hfile);
1250 }
1251
1252 if (filehandle == NULL)
1253 {
1254 gate_win32_closehandle(hfile);
1255 return GATE_RESULT_OK;
1256 }
1257
1258 *filehandle = (gate_file_t)hfile;
1259
1260 if (append)
1261 {
1262 gate_file_seek(*filehandle, 0, GATE_FILE_SEEK_ORIGIN_END, NULL);
1263 }
1264 return GATE_RESULT_OK;
1265 }
1266 #endif
1267
1268 gate_result_t gate_file_read(gate_file_t filehandle, char* buffer, gate_size_t bufferlength, gate_size_t* bufferused)
1269 {
1270 gate_result_t ret;
1271 DWORD bytesreceived = 0;
1272 HANDLE hfile = (HANDLE)filehandle;
1273 if (gate_win32_readfile(hfile, (LPVOID)buffer, (DWORD)bufferlength, &bytesreceived, NULL))
1274 {
1275 if (bufferused)
1276 {
1277 *bufferused = (gate_size_t)bytesreceived;
1278 }
1279 ret = GATE_RESULT_OK;
1280 }
1281 else
1282 {
1283 gate_win32_print_lasterror(&ret, NULL, 0);
1284 }
1285 return ret;
1286 }
1287 gate_result_t gate_file_write(gate_file_t filehandle, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
1288 {
1289 gate_result_t ret;
1290 DWORD byteswritten = 0;
1291 HANDLE hfile = (HANDLE)filehandle;
1292 if (gate_win32_writefile(hfile, (LPVOID)buffer, (DWORD)bufferlength, &byteswritten, NULL))
1293 {
1294 *written = (gate_size_t)byteswritten;
1295 ret = GATE_RESULT_OK;
1296 }
1297 else
1298 {
1299 gate_win32_print_lasterror(&ret, NULL, 0);
1300 }
1301 return ret;
1302 }
1303 gate_result_t gate_file_flush(gate_file_t filehandle)
1304 {
1305 gate_result_t ret;
1306 if (gate_win32_flushfilebuffers((HANDLE)filehandle))
1307 {
1308 ret = GATE_RESULT_OK;
1309 }
1310 else
1311 {
1312 gate_win32_print_lasterror(&ret, NULL, 0);
1313 }
1314 return ret;
1315 }
1316
1317 gate_result_t gate_file_close(gate_file_t filehandle)
1318 {
1319 gate_result_t ret;
1320 HANDLE hfile = (HANDLE)filehandle;
1321 if (gate_win32_closehandle(hfile))
1322 {
1323 ret = GATE_RESULT_OK;
1324 }
1325 else
1326 {
1327 gate_win32_print_lasterror(&ret, NULL, 0);
1328 }
1329 return ret;
1330 }
1331 gate_result_t gate_file_seek(gate_file_t filehandle, gate_int64_t position, gate_enumint_t origin, gate_int64_t* newposition)
1332 {
1333 HANDLE hfile = (HANDLE)filehandle;
1334 DWORD movemethod = 0;
1335 LARGE_INTEGER li;
1336
1337 gate_win32_write_largeint(position, &li);
1338
1339 switch (origin)
1340 {
1341 case GATE_FILE_SEEK_ORIGIN_BEGIN: movemethod = FILE_BEGIN; break;
1342 case GATE_FILE_SEEK_ORIGIN_CURPOS: movemethod = FILE_CURRENT; break;
1343 case GATE_FILE_SEEK_ORIGIN_END: movemethod = FILE_END; break;
1344 default: return GATE_RESULT_INVALIDARG;
1345 }
1346
1347 if (gate_win32_setfilepointer(hfile, li, &li, movemethod))
1348 {
1349 if (newposition != NULL)
1350 {
1351 gate_win32_read_largeint(&li, newposition);
1352 }
1353 return GATE_RESULT_OK;
1354 }
1355 return GATE_RESULT_FAILED;
1356 }
1357 gate_result_t gate_file_pos(gate_file_t filehandle, gate_int64_t* position)
1358 {
1359 HANDLE hfile = (HANDLE)filehandle;
1360 LARGE_INTEGER li;
1361 gate_win32_write_largeint(0, &li);
1362
1363 if (gate_win32_setfilepointer(hfile, li, &li, FILE_CURRENT))
1364 {
1365 if (position == NULL)
1366 {
1367 return GATE_RESULT_INVALIDARG;
1368 }
1369 gate_win32_read_largeint(&li, position);
1370 return GATE_RESULT_OK;
1371 }
1372 return GATE_RESULT_FAILED;
1373 }
1374 gate_result_t gate_file_size(gate_file_t filehandle, gate_int64_t* size)
1375 {
1376 HANDLE hfile = (HANDLE)filehandle;
1377 LARGE_INTEGER li;
1378 if (gate_win32_getfilesize(hfile, &li))
1379 {
1380 if (size == NULL)
1381 {
1382 return GATE_RESULT_INVALIDARG;
1383 }
1384 gate_win32_read_largeint(&li, size);
1385 return GATE_RESULT_OK;
1386 }
1387 return GATE_RESULT_FAILED;
1388 }
1389
1390 gate_result_t gate_file_truncate(gate_file_t filehandle)
1391 {
1392 gate_result_t ret = GATE_RESULT_FAILED;
1393 HANDLE hfile = (HANDLE)filehandle;
1394 if (gate_win32_setendoffile(hfile))
1395 {
1396 ret = GATE_RESULT_OK;
1397 }
1398 else
1399 {
1400 gate_win32_print_lasterror(&ret, NULL, 0);
1401 }
1402 return ret;
1403 }
1404
1405 static DWORD const default_lock_length_low = 0xffffffff;
1406 #if defined(GATE_WIN32_ANSI)
1407 static DWORD const default_lock_length_high = 0x00000000;
1408 #else
1409 static DWORD const default_lock_length_high = 0x7fffffff;
1410 #endif
1411
1412 #if defined(GATE_SYS_WINCE) || defined(GATE_SYS_WIN16)
1413 #if defined(__MINGW32CE__) || (_WIN32_WCE < 0x500) || defined(GATE_SYS_WIN16)
1414
1415 #ifndef LOCKFILE_EXCLUSIVE_LOCK
1416 #define LOCKFILE_EXCLUSIVE_LOCK 1
1417 #endif
1418
1419 #ifndef LOCKFILE_FAIL_IMMEDIATELY
1420 #define LOCKFILE_FAIL_IMMEDIATELY 2
1421 #endif
1422
1423 #if defined(GATE_COMPILER_MSVC)
1424 #define GATE_FILES_HELPER_API static
1425 #else
1426 #define GATE_FILES_HELPER_API
1427 #endif
1428
1429 GATE_FILES_HELPER_API BOOL LockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh, DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
1430 {
1431 GATE_UNUSED_ARG(hFile);
1432 GATE_UNUSED_ARG(dwFileOffsetLow);
1433 GATE_UNUSED_ARG(dwFileOffsetHigh);
1434 GATE_UNUSED_ARG(nNumberOfBytesToLockLow);
1435 GATE_UNUSED_ARG(nNumberOfBytesToLockHigh);
1436 return FALSE;
1437 }
1438
1439 GATE_FILES_HELPER_API BOOL LockFileEx(HANDLE hFile, DWORD dwFlags, DWORD dwReserved,
1440 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1441 LPOVERLAPPED lpOverlapped)
1442 {
1443 GATE_UNUSED_ARG(hFile);
1444 GATE_UNUSED_ARG(dwFlags);
1445 GATE_UNUSED_ARG(dwReserved);
1446 GATE_UNUSED_ARG(nNumberOfBytesToLockLow);
1447 GATE_UNUSED_ARG(nNumberOfBytesToLockHigh);
1448 GATE_UNUSED_ARG(lpOverlapped);
1449 return FALSE;
1450 }
1451
1452 GATE_FILES_HELPER_API BOOL UnlockFile(HANDLE hFile, DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh, DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
1453 {
1454 GATE_UNUSED_ARG(hFile);
1455 GATE_UNUSED_ARG(dwFileOffsetLow);
1456 GATE_UNUSED_ARG(dwFileOffsetHigh);
1457 GATE_UNUSED_ARG(nNumberOfBytesToUnlockLow);
1458 GATE_UNUSED_ARG(nNumberOfBytesToUnlockHigh);
1459 return FALSE;
1460 }
1461
1462 GATE_FILES_HELPER_API BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved,
1463 DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
1464 LPOVERLAPPED lpOverlapped)
1465 {
1466 GATE_UNUSED_ARG(hFile);
1467 GATE_UNUSED_ARG(dwReserved);
1468 GATE_UNUSED_ARG(nNumberOfBytesToLockLow);
1469 GATE_UNUSED_ARG(nNumberOfBytesToLockHigh);
1470 GATE_UNUSED_ARG(lpOverlapped);
1471 return FALSE;
1472 }
1473
1474 #endif
1475 #endif
1476
1477 #ifndef ERROR_LOCK_VIOLATION
1478 #define ERROR_LOCK_VIOLATION 0x21
1479 #endif
1480
1481 gate_result_t gate_file_lock(gate_file_t filehandle, gate_bool_t wait)
1482 {
1483 gate_result_t ret = GATE_RESULT_FAILED;
1484
1485 do
1486 {
1487 HANDLE const hfile = (HANDLE)filehandle;
1488 BOOL succeeded;
1489 gate_int64_t current_pos;
1490 ret = gate_file_pos(filehandle, &current_pos);
1491 GATE_BREAK_IF_FAILED(ret);
1492 #if defined(GATE_WIN32_ANSI)
1493 GATE_UNUSED_ARG(wait);
1494 succeeded = LockFile(hfile, 0, 0, default_lock_length_low, default_lock_length_high);
1495 #else
1496 do
1497 {
1498 OVERLAPPED ov;
1499 DWORD dwFlags = LOCKFILE_EXCLUSIVE_LOCK;
1500
1501 ret = gate_file_seek(filehandle, 0, GATE_FILE_SEEK_ORIGIN_BEGIN, NULL);
1502 GATE_BREAK_IF_FAILED(ret);
1503
1504 gate_mem_clear(&ov, sizeof(ov));
1505 if (!wait)
1506 {
1507 dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
1508 }
1509
1510 succeeded = LockFileEx(hfile, dwFlags, 0, default_lock_length_low, default_lock_length_high, &ov);
1511 gate_file_seek(filehandle, current_pos, GATE_FILE_SEEK_ORIGIN_BEGIN, NULL);
1512 } while(0);
1513 GATE_BREAK_IF_FAILED(ret);
1514 #endif
1515 if (succeeded)
1516 {
1517 ret = GATE_RESULT_OK;
1518 }
1519 else
1520 {
1521 if (gate_win32_getlasterror() == ERROR_LOCK_VIOLATION)
1522 {
1523 ret = GATE_RESULT_ACCESSDENIED;
1524 }
1525 else
1526 {
1527 gate_win32_print_lasterror(&ret, NULL, 0);
1528 }
1529 }
1530 } while (0);
1531
1532 return ret;
1533 }
1534 gate_result_t gate_file_unlock(gate_file_t filehandle)
1535 {
1536 gate_result_t ret = GATE_RESULT_FAILED;
1537 HANDLE hfile = (HANDLE)filehandle;
1538 gate_int64_t current_pos = 0;
1539 BOOL succeeded;
1540 #if !defined(GATE_WIN32_ANSI)
1541 OVERLAPPED ov;
1542 #endif
1543
1544 do
1545 {
1546 ret = gate_file_pos(filehandle, &current_pos);
1547 GATE_BREAK_IF_FAILED(ret);
1548
1549 #if defined(GATE_WIN32_ANSI)
1550 succeeded = UnlockFile(hfile, 0, 0, default_lock_length_low, default_lock_length_high);
1551 #else
1552 ret = gate_file_seek(filehandle, 0, GATE_FILE_SEEK_ORIGIN_BEGIN, NULL);
1553 GATE_BREAK_IF_FAILED(ret);
1554
1555 gate_mem_clear(&ov, sizeof(ov));
1556 succeeded = UnlockFileEx(hfile, 0, default_lock_length_low, default_lock_length_high, &ov);
1557 #endif
1558 if (succeeded)
1559 {
1560 ret = GATE_RESULT_OK;
1561 }
1562 else
1563 {
1564 gate_win32_print_lasterror(&ret, NULL, 0);
1565 }
1566
1567 gate_file_seek(filehandle, current_pos, GATE_FILE_SEEK_ORIGIN_BEGIN, NULL);
1568 } while (0);
1569
1570 return ret;
1571 }
1572
1573 static gate_bool_t gate_file_is_server_path(LPTSTR path, gate_bool_t stripshare)
1574 {
1575 #if defined(GATE_SYS_WIN16)
1576 GATE_UNUSED_ARG(path);
1577 GATE_UNUSED_ARG(stripshare);
1578 return false;
1579 #else
1580 gate_size_t pathlength = gate_win32_str_len(path);
1581 gate_size_t pos;
1582 if (pathlength < 2)
1583 {
1584 return false;
1585 }
1586 if (gate_win32_str_pos(path, pathlength, _T("\\\\"), 2, 0) != 0)
1587 {
1588 return false;
1589 }
1590
1591 /* path starts with double-backslashes */
1592 if (stripshare)
1593 {
1594 pos = gate_win32_str_pos(path, pathlength, _T("\\"), 1, 2);
1595 if (pos != GATE_STR_NPOS)
1596 {
1597 path[pos] = 0;
1598 }
1599 }
1600 return true;
1601 #endif
1602 }
1603
1604 static gate_bool_t gate_file_is_server_share_path(LPTSTR path, gate_bool_t stripsubpath)
1605 {
1606 #if defined(GATE_SYS_WIN16)
1607 GATE_UNUSED_ARG(path);
1608 GATE_UNUSED_ARG(stripsubpath);
1609 return false;
1610 #else
1611 gate_size_t pathlength = gate_win32_str_len(path);
1612 gate_size_t pos;
1613 gate_size_t shareendpos;
1614 if (pathlength < 2)
1615 {
1616 return false;
1617 }
1618 if (gate_win32_str_pos(path, pathlength, _T("\\\\"), 2, 0) != 0)
1619 {
1620 return false;
1621 }
1622
1623 /* path starts with double-backslashes */
1624
1625 pos = gate_win32_str_pos(path, pathlength, _T("\\"), 1, 2);
1626 if ((pos == GATE_STR_NPOS) || (pos + 1 == pathlength))
1627 {
1628 return false;
1629 }
1630
1631 if (stripsubpath)
1632 {
1633 shareendpos = gate_win32_str_pos(path, pathlength, _T("\\"), 1, pos);
1634 if (shareendpos != GATE_STR_NPOS)
1635 {
1636 path[shareendpos] = 0;
1637 }
1638 }
1639 return true;
1640 #endif
1641 }
1642
1643 static gate_bool_t gate_win32_is_volume_path(TCHAR const* path, gate_size_t path_len)
1644 {
1645 #if defined(GATE_SYS_WINCE)
1646 return (path_len == 1) && (path[0] == '\\');
1647 #else
1648 TCHAR drive;
1649 if ((path_len < 2) || (path_len > 3))
1650 {
1651 return false;
1652 }
1653 if (path[1] != _T(':'))
1654 {
1655 return false;
1656 }
1657 if ((path_len == 3) && (path[2] != _T('\\')))
1658 {
1659 return false;
1660 }
1661 drive = path[0];
1662 return ((drive >= _T('A')) && (drive <= _T('Z')))
1663 || ((drive >= _T('a')) && (drive <= _T('z')));
1664 #endif
1665 }
1666
1667 gate_result_t gate_file_exists(gate_string_t const* filepath)
1668 {
1669 WIN32_FIND_DATA data;
1670 HANDLE hfind;
1671 TCHAR path[GATE_MAX_FILEPATH_LENGTH];
1672 gate_size_t pathlen = gate_win32_utf8_2_winstr(filepath->str, filepath->length, path, sizeof(path) / sizeof(path[0]));
1673 if ((pathlen > 0) && (path[pathlen - 1] == '\\'))
1674 {
1675 --pathlen;
1676 path[pathlen] = 0;
1677 }
1678
1679 if (gate_file_is_server_path(path, false))
1680 {
1681 return GATE_RESULT_OK;
1682 }
1683 if (gate_file_is_server_share_path(path, false))
1684 {
1685 return GATE_RESULT_OK;
1686 }
1687
1688 #if !defined(GATE_SYS_WINCE)
1689 if (gate_win32_is_volume_path(path, pathlen))
1690 {
1691 DWORD drives = GetLogicalDrives();
1692 DWORD drivebit = (DWORD)(1 << (gate_char_uppercase(path[0]) - 'A'));
1693 if ((drives & drivebit) == 0)
1694 {
1695 return GATE_RESULT_NOTAVAILABLE;
1696 }
1697 return GATE_RESULT_OK;
1698 }
1699 #endif
1700
1701 hfind = gate_win32_findfirstfile(&path[0], &data);
1702 if ((HANDLE)INVALID_HANDLE_VALUE == hfind)
1703 {
1704 return GATE_RESULT_NOTAVAILABLE;
1705 }
1706 gate_win32_findclose(hfind);
1707 return GATE_RESULT_OK;
1708 }
1709
1710 gate_result_t gate_file_copy(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath, gate_enumint_t flags)
1711 {
1712 #if defined(GATE_SYS_WIN16)
1713 return gate_file_copy_internal(srcfilepath, dstfilepath, flags);
1714 #else
1715 gate_result_t ret;
1716 if (GATE_FLAG_ENABLED(flags, GATE_FILE_COPY_SIMPLE))
1717 {
1718 ret = gate_file_copy_internal(srcfilepath, dstfilepath, flags);
1719 }
1720 else
1721 {
1722 TCHAR srcpath[GATE_MAX_FILEPATH_LENGTH];
1723 TCHAR dstpath[GATE_MAX_FILEPATH_LENGTH];
1724 const gate_size_t srcpathlen = gate_win32_string_2_winstr(srcfilepath, srcpath, sizeof(srcpath) / sizeof(srcpath[0]));
1725 const gate_size_t dstpathlen = gate_win32_string_2_winstr(dstfilepath, dstpath, sizeof(dstpath) / sizeof(dstpath[0]));
1726
1727 if ((srcpathlen == 0) || (dstpathlen == 0))
1728 {
1729 ret = GATE_RESULT_INVALIDARG;
1730 }
1731 else
1732 {
1733 if (CopyFile(srcpath, dstpath, GATE_FLAG_ENABLED(flags, GATE_FILE_COPY_OVERWRITE) ? TRUE : FALSE))
1734 {
1735 ret = GATE_RESULT_OK;
1736 }
1737 else
1738 {
1739 gate_win32_print_lasterror(&ret, NULL, 0);
1740 }
1741 }
1742 }
1743 return ret;
1744 #endif
1745 }
1746 gate_result_t gate_file_move(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath)
1747 {
1748 #if defined(GATE_SYS_WIN16)
1749 return gate_file_move_internal(srcfilepath, dstfilepath);
1750 #else
1751 gate_result_t ret;
1752 TCHAR srcpath[GATE_MAX_FILEPATH_LENGTH];
1753 TCHAR dstpath[GATE_MAX_FILEPATH_LENGTH];
1754 gate_size_t srcpathlen = gate_win32_string_2_winstr(srcfilepath, srcpath, sizeof(srcpath) / sizeof(srcpath[0]));
1755 gate_size_t dstpathlen = gate_win32_string_2_winstr(dstfilepath, dstpath, sizeof(dstpath) / sizeof(dstpath[0]));
1756
1757 if ((srcpathlen == 0) || (dstpathlen == 0))
1758 {
1759 ret = GATE_RESULT_INVALIDARG;
1760 }
1761 else
1762 {
1763 if (MoveFile(srcpath, dstpath))
1764 {
1765 ret = GATE_RESULT_OK;
1766 }
1767 else
1768 {
1769 gate_win32_print_lasterror(&ret, NULL, 0);
1770 }
1771 }
1772 return ret;
1773 #endif
1774 }
1775
1776 #if defined(GATE_SYS_WIN16)
1777 static BOOL DeleteFile(LPCSTR lpFileName)
1778 {
1779 OFSTRUCT of;
1780 HFILE hf = OpenFile(lpFileName, &of, OF_DELETE);
1781 if (hf == HFILE_ERROR)
1782 {
1783 return FALSE;
1784 }
1785 return TRUE;
1786 }
1787 #endif
1788
1789
1790 gate_result_t gate_file_delete(gate_string_t const* filepath)
1791 {
1792 gate_result_t ret;
1793 TCHAR path[GATE_MAX_FILEPATH_LENGTH];
1794 gate_size_t pathlen = gate_win32_string_2_winstr(filepath, path, sizeof(path) / sizeof(path[0]) - 1);
1795 DWORD dw_error;
1796
1797 if (pathlen == 0)
1798 {
1799 ret = GATE_RESULT_INVALIDARG;
1800 }
1801 else
1802 {
1803 path[pathlen + 1] = 0; /* ensure double zero for shell-file-ops */
1804 if (DeleteFile(path))
1805 {
1806 ret = GATE_RESULT_OK;
1807 }
1808 else
1809 {
1810 dw_error = gate_win32_getlasterror();
1811 gate_win32_print_lasterror_code(dw_error, &ret, NULL, 0);
1812 /* try fallback */
1813 if (gate_win32_shfileop(GATE_WIN32_SHFILEOP_DELETE, path, NULL))
1814 {
1815 ret = GATE_RESULT_OK;
1816 }
1817 else
1818 {
1819 gate_win32_setlasterror(dw_error);
1820 }
1821 }
1822 }
1823 return ret;
1824 }
1825
1826
1827 #ifndef SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
1828 #define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 0x2
1829 #endif
1830
1831 #ifndef SYMBOLIC_LINK_FLAG_DIRECTORY
1832 #define SYMBOLIC_LINK_FLAG_DIRECTORY 0x1
1833 #endif
1834
1835 #ifndef INVALID_FILE_ATTRIBUTES
1836 #define INVALID_FILE_ATTRIBUTES ((DWORD)(-1))
1837 #endif
1838
1839 #if defined(GATE_SYS_WIN16)
1840 static DWORD GetFileAttributes(LPCSTR lpFileName)
1841 {
1842 DWORD ret;
1843 struct _finddata_t fd;
1844 intptr_t h = _findfirst(lpFileName, &fd);
1845 if (h != -1)
1846 {
1847 _findclose(h);
1848 ret = 0;
1849 GATE_FLAG_SET(ret, FILE_ATTRIBUTE_READONLY, GATE_FLAG_ENABLED(fd.attrib, _A_RDONLY));
1850 GATE_FLAG_SET(ret, FILE_ATTRIBUTE_SYSTEM, GATE_FLAG_ENABLED(fd.attrib, _A_SYSTEM));
1851 GATE_FLAG_SET(ret, FILE_ATTRIBUTE_HIDDEN, GATE_FLAG_ENABLED(fd.attrib, _A_HIDDEN));
1852 GATE_FLAG_SET(ret, FILE_ATTRIBUTE_DIRECTORY, GATE_FLAG_ENABLED(fd.attrib, _A_SUBDIR));
1853 GATE_FLAG_SET(ret, FILE_ATTRIBUTE_ARCHIVE, GATE_FLAG_ENABLED(fd.attrib, _A_ARCH));
1854 }
1855 else
1856 {
1857 ret = INVALID_FILE_ATTRIBUTES;
1858 }
1859 return ret;
1860 }
1861 #endif
1862
1863 static gate_size_t gate_file_build_abs_path(gate_string_t const* srcpath, TCHAR* targetpath, gate_size_t targetpathcapacity)
1864 {
1865 TCHAR tmppath[GATE_MAX_FILEPATH_LENGTH];
1866 gate_size_t tmppathlen = gate_win32_string_2_winstr(srcpath, tmppath, sizeof(tmppath) / sizeof(tmppath[0]) - 1);
1867 #if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WIN16)
1868 DWORD fulllen = GetFullPathName(tmppath, (DWORD)targetpathcapacity, targetpath, NULL);
1869 if (fulllen != 0)
1870 {
1871 return (gate_size_t)fulllen;
1872 }
1873 else
1874 #endif
1875 {
1876 return gate_win32_winstr_print_text(targetpath, targetpathcapacity, tmppath, tmppathlen);
1877 }
1878 }
1879
1880 gate_result_t gate_file_real_path(gate_string_t const* relativepath, gate_string_t* absolutepath)
1881 {
1882 TCHAR tmppath[GATE_MAX_FILEPATH_LENGTH];
1883 gate_size_t tmppathlen;
1884 if (!relativepath || !absolutepath)
1885 {
1886 return GATE_RESULT_NULLPOINTER;
1887 }
1888
1889 tmppathlen = gate_file_build_abs_path(relativepath, tmppath, sizeof(tmppath) / sizeof(tmppath[0]) - 1);
1890 if (NULL == gate_win32_winstr_2_utf8_string(tmppath, tmppathlen, absolutepath))
1891 {
1892 return GATE_RESULT_OUTOFMEMORY;
1893 }
1894 return GATE_RESULT_OK;
1895 }
1896
1897
1898 gate_result_t gate_file_create_link(gate_string_t const* targetfile, gate_string_t const* linkfile)
1899 {
1900 #if defined(GATE_SYS_WIN16) || defined(GATE_SYS_WINCE)
1901 GATE_UNUSED_ARG(targetfile);
1902 GATE_UNUSED_ARG(linkfile);
1903 return GATE_RESULT_NOTIMPLEMENTED;
1904 #else
1905 gate_result_t ret;
1906 TCHAR targetpath[GATE_MAX_FILEPATH_LENGTH];
1907 TCHAR linkpath[GATE_MAX_FILEPATH_LENGTH];
1908 gate_size_t targetpathlen = gate_file_build_abs_path(targetfile, targetpath, sizeof(targetpath) / sizeof(targetpath[0]));
1909 gate_size_t linkpathlen = gate_win32_string_2_winstr(linkfile, linkpath, sizeof(linkpath) / sizeof(linkpath[0]));
1910 DWORD dwFlags = 0;
1911 DWORD dwAttribs;
1912
1913 do
1914 {
1915 if ((targetpathlen == 0) || (linkpathlen == 0))
1916 {
1917 ret = GATE_RESULT_INVALIDARG;
1918 break;
1919 }
1920
1921 if (gate_platform.Win32CreateSymbolicLink == NULL)
1922 {
1923 /* symbolic-link API not available (pre-Vista) */
1924 ret = GATE_RESULT_NOTSUPPORTED;
1925 break;
1926 }
1927
1928 dwAttribs = GetFileAttributes(targetpath);
1929 if (INVALID_FILE_ATTRIBUTES == dwAttribs)
1930 {
1931 /* target path not available, so we cannot determine the file type */
1932 ret = GATE_RESULT_NOTAVAILABLE;
1933 break;
1934 }
1935
1936 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_DIRECTORY))
1937 {
1938 dwFlags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
1939 }
1940
1941 if (!gate_platform.Win32CreateSymbolicLink(linkpath, targetpath, dwFlags))
1942 {
1943 DWORD lasterr = gate_win32_getlasterror();
1944 if (lasterr == ERROR_PRIVILEGE_NOT_HELD)
1945 {
1946 /* try optional Win10+ support for unpriviledged link-creation */
1947 dwFlags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
1948 if (gate_platform.Win32CreateSymbolicLink(linkpath, targetpath, dwFlags))
1949 {
1950 /* Yay! Win-Devmode is active */
1951 ret = GATE_RESULT_OK;
1952 break;
1953 }
1954 else
1955 {
1956 /* reset error code for proper handling by caller */
1957 gate_win32_setlasterror(lasterr);
1958 }
1959 }
1960 gate_win32_print_lasterror_code(lasterr, &ret, NULL, 0);
1961 break;
1962 }
1963
1964 /* success case reached */
1965 ret = GATE_RESULT_OK;
1966 } while (0);
1967
1968 return ret;
1969 #endif
1970 }
1971
1972 #ifndef FILE_NAME_NORMALIZED
1973 #define FILE_NAME_NORMALIZED 0x00
1974 #endif
1975
1976 #if !defined(GATE_SYS_WIN16)
1977
1978 typedef struct _GATE_REPARSE_DATA_BUFFER {
1979 ULONG ReparseTag;
1980 USHORT ReparseDataLength;
1981 USHORT Reserved;
1982 union {
1983 struct {
1984 USHORT SubstituteNameOffset;
1985 USHORT SubstituteNameLength;
1986 USHORT PrintNameOffset;
1987 USHORT PrintNameLength;
1988 ULONG Flags;
1989 WCHAR PathBuffer[1];
1990 } SymbolicLinkReparseBuffer;
1991 struct {
1992 USHORT SubstituteNameOffset;
1993 USHORT SubstituteNameLength;
1994 USHORT PrintNameOffset;
1995 USHORT PrintNameLength;
1996 WCHAR PathBuffer[1];
1997 } MountPointReparseBuffer;
1998 struct {
1999 UCHAR DataBuffer[1];
2000 } GenericReparseBuffer;
2001 } UnionItems;
2002 } GATE_REPARSE_DATA_BUFFER, * PGATE_REPARSE_DATA_BUFFER;
2003 #endif
2004
2005 #ifndef FILE_DEVICE_FILE_SYSTEM
2006 #define FILE_DEVICE_FILE_SYSTEM 0x00000009
2007 #endif
2008
2009 #ifndef METHOD_BUFFERED
2010 #define METHOD_BUFFERED 0
2011 #endif
2012
2013 #ifndef FILE_ANY_ACCESS
2014 #define FILE_ANY_ACCESS 0
2015 #endif
2016
2017 #ifndef CTL_CODE
2018 #define CTL_CODE( DeviceType, Function, Method, Access ) ( ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) )
2019 #endif
2020
2021 #ifndef FSCTL_GET_REPARSE_POINT
2022 #define FSCTL_GET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 42, METHOD_BUFFERED, FILE_ANY_ACCESS)
2023 #endif
2024
2025 #ifndef IO_REPARSE_TAG_SYMLINK
2026 #define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
2027 #endif
2028
2029 gate_result_t gate_file_read_link(gate_string_t const* linkfile, gate_string_t* realpath)
2030 {
2031 #if defined(GATE_SYS_WINCE) || defined(GATE_SYS_WIN16)
2032 GATE_UNUSED_ARG(linkfile);
2033 GATE_UNUSED_ARG(realpath);
2034 return GATE_RESULT_NOTIMPLEMENTED;
2035 #else
2036 gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
2037 HANDLE hsearch = INVALID_HANDLE_VALUE;
2038 HANDLE hfile = INVALID_HANDLE_VALUE;
2039 TCHAR linkpath[GATE_MAX_FILEPATH_LENGTH] = GATE_INIT_EMPTY;
2040 gate_size_t linkpathlen = gate_win32_string_2_winstr(linkfile, linkpath, sizeof(linkpath) / sizeof(linkpath[0]));
2041 TCHAR targetpath[GATE_MAX_FILEPATH_LENGTH] = GATE_INIT_EMPTY;
2042 DWORD len_used = 0;
2043 BOOL privilege_enabled = FALSE;
2044 gate_bool_t reset_privilege = false;
2045 WIN32_FIND_DATA searchdata = GATE_INIT_EMPTY;
2046 gate_bool_t is_sym_link = true;
2047 DWORD bytes_returned = 0;
2048 GATE_REPARSE_DATA_BUFFER* ptr_reparse_buffer = NULL;
2049
2050 do
2051 {
2052 if (linkpathlen == 0)
2053 {
2054 ret = GATE_RESULT_INVALIDARG;
2055 break;
2056 }
2057 hsearch = gate_win32_findfirstfile(linkpath, &searchdata);
2058 if (INVALID_HANDLE_VALUE != hsearch)
2059 {
2060 switch (searchdata.dwReserved0)
2061 {
2062 case IO_REPARSE_TAG_SYMLINK:
2063 is_sym_link = true;
2064 break;
2065 default:
2066 is_sym_link = false;
2067 break;
2068 }
2069 }
2070
2071 if (gate_platform.Win32GetFinalPathNameByHandle == NULL)
2072 {
2073 ret = GATE_RESULT_NOTSUPPORTED;
2074 break;
2075 }
2076
2077 hfile = gate_win32_createfile(linkpath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
2078 FILE_ATTRIBUTE_NORMAL, 0, NULL);
2079 if ((INVALID_HANDLE_VALUE == hfile) || (NULL == hfile))
2080 {
2081 if (gate_win32_token_privilege_is_enabled(NULL, SE_BACKUP_NAME, &privilege_enabled))
2082 {
2083 if (!privilege_enabled)
2084 {
2085 if (gate_win32_token_privilege_enable(NULL, SE_BACKUP_NAME, TRUE))
2086 {
2087 reset_privilege = true;
2088 }
2089 }
2090 }
2091
2092 hfile = gate_win32_createfile(linkpath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
2093 0, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
2094 if ((INVALID_HANDLE_VALUE == hfile) || (NULL == hfile))
2095 {
2096 gate_win32_print_lasterror(&ret, NULL, 0);
2097 }
2098 else
2099 {
2100 ret = GATE_RESULT_OK;
2101 }
2102 if (reset_privilege)
2103 {
2104 gate_win32_token_privilege_enable(NULL, SE_BACKUP_NAME, FALSE);
2105 }
2106 GATE_BREAK_IF_FAILED(ret);
2107 }
2108
2109 if (is_sym_link)
2110 {
2111 /* get final path name of symlink */
2112 len_used = gate_platform.Win32GetFinalPathNameByHandle(hfile, targetpath, sizeof(targetpath) / sizeof(targetpath[0]), FILE_NAME_NORMALIZED);
2113 if (0 == len_used)
2114 {
2115 gate_win32_print_lasterror(&ret, NULL, 0);
2116 break;
2117 }
2118 else
2119 {
2120 if (NULL == gate_win32_winstr_2_utf8_string(targetpath, len_used, realpath))
2121 {
2122 ret = GATE_RESULT_OUTOFMEMORY;
2123 break;
2124 }
2125 }
2126 }
2127 else
2128 {
2129 /* get reparse point */
2130 ptr_reparse_buffer = (GATE_REPARSE_DATA_BUFFER*)&targetpath[0];
2131 if (!DeviceIoControl(hfile, FSCTL_GET_REPARSE_POINT, NULL, 0, ptr_reparse_buffer, sizeof(targetpath), &bytes_returned, NULL))
2132 {
2133 ret = GATE_RESULT_FAILED;
2134 break;
2135 }
2136
2137 if (NULL == gate_string_create_utf16(realpath,
2138 &ptr_reparse_buffer->UnionItems.MountPointReparseBuffer.PathBuffer[0],
2139 gate_str16_length(&ptr_reparse_buffer->UnionItems.MountPointReparseBuffer.PathBuffer[0])
2140 ))
2141 {
2142 ret = GATE_RESULT_OUTOFMEMORY;
2143 break;
2144 }
2145 }
2146
2147 /* reached SUCCESS case ;) */
2148 ret = GATE_RESULT_OK;
2149 } while (0);
2150
2151 gate_win32_closehandle(hfile);
2152 gate_win32_findclose(hsearch);
2153
2154 return ret;
2155 #endif
2156 }
2157 gate_result_t gate_file_create_hardlink(gate_string_t const* targetfile, gate_string_t const* linkfile)
2158 {
2159 #if defined(GATE_SYS_WIN16)
2160 GATE_UNUSED_ARG(targetfile);
2161 GATE_UNUSED_ARG(linkfile);
2162 return GATE_RESULT_NOTIMPLEMENTED;
2163 #else
2164 gate_result_t ret;
2165 TCHAR targetpath[GATE_MAX_FILEPATH_LENGTH];
2166 TCHAR linkpath[GATE_MAX_FILEPATH_LENGTH];
2167 gate_size_t targetpathlen = gate_win32_string_2_winstr(targetfile, targetpath, sizeof(targetpath) / sizeof(targetpath[0]));
2168 gate_size_t linkpathlen = gate_win32_string_2_winstr(linkfile, linkpath, sizeof(linkpath) / sizeof(linkpath[0]));
2169
2170 if ((targetpathlen == 0) || (linkpathlen == 0))
2171 {
2172 ret = GATE_RESULT_INVALIDARG;
2173 }
2174 else
2175 {
2176 if (gate_platform.Win32CreateHardLink == NULL)
2177 {
2178 ret = GATE_RESULT_NOTSUPPORTED;
2179 }
2180 else
2181 {
2182 if (gate_platform.Win32CreateHardLink(linkpath, targetpath, NULL))
2183 {
2184 ret = GATE_RESULT_OK;
2185 }
2186 else
2187 {
2188 gate_win32_print_lasterror(&ret, NULL, 0);
2189 }
2190 }
2191 }
2192 return ret;
2193 #endif
2194 }
2195
2196
2197 static gate_timestamp_t gate_win32_to_timestamp(FILETIME const* ft)
2198 {
2199 gate_uint64_t tmp = ((gate_uint64_t)ft->dwHighDateTime << 32) | (gate_uint64_t)ft->dwLowDateTime;
2200 tmp /= 10;
2201 return (gate_timestamp_t)tmp;
2202 }
2203
2204 static void gate_win32_convert_timestamp(gate_timestamp_t src, FILETIME* dest)
2205 {
2206 gate_uint64_t tmp = src * 10;
2207 dest->dwHighDateTime = (DWORD)(tmp >> 32);
2208 dest->dwLowDateTime = (DWORD)(tmp & 0xffffffff);
2209 }
2210
2211 gate_result_t gate_file_get_attributes(gate_string_t const* targetfile, gate_enumint_t* attribs, gate_enumint_t* access_bits)
2212 {
2213 gate_result_t ret;
2214 DWORD dwAttribs;
2215 TCHAR targetpath[GATE_MAX_FILEPATH_LENGTH];
2216 gate_size_t targetpathlen = gate_win32_string_2_winstr(targetfile, targetpath, sizeof(targetpath) / sizeof(targetpath[0]));
2217
2218 do
2219 {
2220 if (targetpathlen == 0)
2221 {
2222 ret = GATE_RESULT_INVALIDARG;
2223 break;
2224 }
2225
2226 if (attribs)
2227 {
2228 dwAttribs = GetFileAttributes(targetpath);
2229 if (INVALID_FILE_ATTRIBUTES == dwAttribs)
2230 {
2231 ret = GATE_RESULT_FAILED;
2232 break;
2233 }
2234
2235 if (attribs == NULL)
2236 {
2237 ret = GATE_RESULT_INVALIDARG;
2238 break;
2239 }
2240 *attribs = 0;
2241
2242 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_DIRECTORY))
2243 {
2244 *attribs |= GATE_FILEENTRY_ATTRIB_DIRECTORY;
2245 }
2246 else
2247 {
2248 *attribs |= GATE_FILEENTRY_ATTRIB_FILE;
2249 }
2250 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_ARCHIVE)) *attribs |= GATE_FILEENTRY_ATTRIB_ARCHIVE;
2251 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_HIDDEN)) *attribs |= GATE_FILEENTRY_ATTRIB_HIDDEN;
2252 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_READONLY)) *attribs |= GATE_FILEENTRY_ATTRIB_READONLY;
2253 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_SYSTEM)) *attribs |= GATE_FILEENTRY_ATTRIB_SYSTEM;
2254 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_COMPRESSED)) *attribs |= GATE_FILEENTRY_ATTRIB_COMPRESSED;
2255 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_TEMPORARY)) *attribs |= GATE_FILEENTRY_ATTRIB_TEMP;
2256 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_REPARSE_POINT)) *attribs |= GATE_FILEENTRY_ATTRIB_LINK;
2257
2258 if (gate_file_is_hidden_entry(targetfile))
2259 {
2260 *attribs |= GATE_FILEENTRY_ATTRIB_HIDDEN;
2261 }
2262 }
2263
2264 if (access_bits)
2265 {
2266 *access_bits = 0;
2267 if (gate_string_ends_with_str_ic(targetfile, ".exe"))
2268 {
2269 *access_bits |= (GATE_FILEENTRY_ACCESS_OWNEREXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_ALLEXECUTE);
2270 }
2271 }
2272
2273 ret = GATE_RESULT_OK;
2274 } while (0);
2275 return ret;
2276 }
2277
2278
2279 static void gate_win32_update_attribute_flags(DWORD* dwAttribs, gate_enumint_t attribs, gate_enumint_t mask,
2280 gate_enumint_t gate_flag, DWORD win_flag)
2281 {
2282 if (GATE_FLAG_ENABLED(mask, gate_flag))
2283 {
2284 if (GATE_FLAG_ENABLED(attribs, gate_flag))
2285 {
2286 *dwAttribs |= win_flag;
2287 }
2288 else
2289 {
2290 *dwAttribs &= ~win_flag;
2291 }
2292 }
2293 }
2294
2295 #if defined(GATE_SYS_WIN16)
2296
2297 static BOOL SetFileAttributes(LPCSTR lpFileName, DWORD dwFileAttributes)
2298 {
2299 GATE_UNUSED_ARG(lpFileName);
2300 GATE_UNUSED_ARG(dwFileAttributes);
2301 return FALSE;
2302 }
2303
2304 #endif
2305
2306 gate_result_t gate_file_set_attributes(gate_string_t const* targetfile,
2307 gate_enumint_t attribs, gate_enumint_t attribs_mask,
2308 gate_enumint_t access_bits, gate_enumint_t access_mask)
2309 {
2310 gate_result_t ret = GATE_RESULT_OK;
2311 DWORD dwAttribs = 0;
2312 TCHAR targetpath[GATE_MAX_FILEPATH_LENGTH];
2313 gate_size_t targetpathlen = gate_win32_string_2_winstr(targetfile, targetpath, sizeof(targetpath) / sizeof(targetpath[0]));
2314
2315 do
2316 {
2317 if (targetpathlen == 0)
2318 {
2319 ret = GATE_RESULT_INVALIDARG;
2320 break;
2321 }
2322
2323 if (attribs_mask != 0)
2324 {
2325 dwAttribs = GetFileAttributes(targetpath);
2326 if (INVALID_FILE_ATTRIBUTES == dwAttribs)
2327 {
2328 ret = GATE_RESULT_FAILED;
2329 break;
2330 }
2331
2332 gate_win32_update_attribute_flags(&dwAttribs, attribs, attribs_mask, GATE_FILEENTRY_ATTRIB_READONLY, FILE_ATTRIBUTE_READONLY);
2333 gate_win32_update_attribute_flags(&dwAttribs, attribs, attribs_mask, GATE_FILEENTRY_ATTRIB_SYSTEM, FILE_ATTRIBUTE_SYSTEM);
2334 gate_win32_update_attribute_flags(&dwAttribs, attribs, attribs_mask, GATE_FILEENTRY_ATTRIB_HIDDEN, GATE_FILEENTRY_ATTRIB_HIDDEN);
2335 gate_win32_update_attribute_flags(&dwAttribs, attribs, attribs_mask, GATE_FILEENTRY_ATTRIB_ARCHIVE, GATE_FILEENTRY_ATTRIB_ARCHIVE);
2336
2337 if (!SetFileAttributes(targetpath, dwAttribs))
2338 {
2339 ret = GATE_RESULT_FAILED;
2340 break;
2341 }
2342 }
2343
2344 if (access_mask != 0)
2345 {
2346 /* TODO update file ACL */
2347 GATE_UNUSED_ARG(access_bits);
2348 ret = GATE_RESULT_NOTSUPPORTED;
2349 break;
2350 }
2351
2352 } while (0);
2353
2354 return ret;
2355 }
2356
2357 gate_result_t gate_file_get_size(gate_string_t const* targetfile, gate_int64_t* filesize)
2358 {
2359 gate_result_t ret;
2360 TCHAR targetpath[GATE_MAX_FILEPATH_LENGTH];
2361 gate_size_t targetpathlen = gate_win32_string_2_winstr(targetfile, targetpath, sizeof(targetpath) / sizeof(targetpath[0]));
2362 HANDLE hfile = (HANDLE)INVALID_HANDLE_VALUE;
2363 LARGE_INTEGER large_int;
2364 #if defined(GATE_SYS_WINSTORE)
2365 WIN32_FILE_ATTRIBUTE_DATA attrData;
2366 #endif
2367
2368 do
2369 {
2370 if (targetpathlen == 0)
2371 {
2372 ret = GATE_RESULT_INVALIDARG;
2373 break;
2374 }
2375 #if defined(GATE_SYS_WINSTORE)
2376 if (!GetFileAttributesEx(targetpath, GetFileExInfoStandard, &attrData))
2377 {
2378 ret = GATE_RESULT_FAILED;
2379 break;
2380 }
2381 large_int.QuadPart = (LONGLONG)((((gate_uint64_t)attrData.nFileSizeHigh) << 32) + (gate_uint64_t)attrData.nFileSizeLow);
2382 #else /* GATE_SYS_WINSTORE */
2383 hfile = gate_win32_createfile(targetpath, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING,
2384 0, FILE_FLAG_BACKUP_SEMANTICS, NULL);
2385 if (hfile == (HANDLE)INVALID_HANDLE_VALUE)
2386 {
2387 ret = GATE_RESULT_FAILED;
2388 break;
2389 }
2390 if (!gate_win32_getfilesize(hfile, &large_int))
2391 {
2392 ret = GATE_RESULT_FAILED;
2393 break;
2394 }
2395 #endif /* GATE_SYS_WINSTORE */
2396
2397 if (filesize)
2398 {
2399 gate_win32_read_largeint(&large_int, filesize);
2400 }
2401 ret = GATE_RESULT_OK;
2402 break;
2403
2404 } while (0);
2405
2406 gate_win32_closehandle(hfile);
2407 return ret;
2408 }
2409
2410
2411 gate_result_t gate_file_get_times(gate_string_t const* targetfile, gate_timestamp_t* modified,
2412 gate_timestamp_t* accessed, gate_timestamp_t* created)
2413 {
2414 gate_result_t ret;
2415 TCHAR targetpath[GATE_MAX_FILEPATH_LENGTH];
2416 gate_size_t targetpathlen = gate_win32_string_2_winstr(targetfile, targetpath, sizeof(targetpath) / sizeof(targetpath[0]));
2417 HANDLE hfile = (HANDLE)INVALID_HANDLE_VALUE;
2418 FILETIME ftModified, ftAccessed, ftCreated;
2419 #if defined(GATE_SYS_WINCE)
2420 DWORD dwAttribs = 0;
2421 #endif
2422 #if defined(GATE_SYS_WINSTORE)
2423 WIN32_FILE_ATTRIBUTE_DATA attrData;
2424 #endif
2425
2426 do
2427 {
2428 if (targetpathlen == 0)
2429 {
2430 ret = GATE_RESULT_INVALIDARG;
2431 break;
2432 }
2433 #if defined(GATE_SYS_WINCE)
2434 dwAttribs = GetFileAttributes(targetpath);
2435 if (dwAttribs == 0xFFFFFFFF)
2436 {
2437 ret = GATE_RESULT_FAILED;
2438 break;
2439 }
2440 if (GATE_FLAG_ENABLED(dwAttribs, FILE_ATTRIBUTE_DIRECTORY))
2441 {
2442 if (created) *created = 0;
2443 if (accessed) *accessed = 0;
2444 if (modified) *modified = 0;
2445 ret = GATE_RESULT_OK;
2446 break;
2447 }
2448 #endif
2449
2450 #if defined(GATE_SYS_WINSTORE)
2451 if (!GetFileAttributesEx(targetpath, GetFileExInfoStandard, &attrData))
2452 {
2453 ret = GATE_RESULT_FAILED;
2454 break;
2455 }
2456 ftCreated = attrData.ftCreationTime;
2457 ftAccessed = attrData.ftLastAccessTime;
2458 ftModified = attrData.ftLastWriteTime;
2459 #elif defined(GATE_SYS_WIN16)
2460 ret = GATE_RESULT_NOTSUPPORTED;
2461 #else /* GATE_SYS_WINSTORE */
2462 hfile = gate_win32_createfile(targetpath, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING,
2463 0, FILE_FLAG_BACKUP_SEMANTICS, NULL);
2464 if (INVALID_HANDLE_VALUE == hfile)
2465 {
2466 ret = GATE_RESULT_FAILED;
2467 break;
2468 }
2469 if (!GetFileTime(hfile, &ftCreated, &ftAccessed, &ftModified))
2470 {
2471 ret = GATE_RESULT_FAILED;
2472 }
2473 else
2474 {
2475 ret = GATE_RESULT_OK;
2476 }
2477 #endif /* GATE_SYS_WINSTORE */
2478 if (created) *created = gate_win32_to_timestamp(&ftCreated);
2479 if (accessed) *accessed = gate_win32_to_timestamp(&ftAccessed);
2480 if (modified) *modified = gate_win32_to_timestamp(&ftModified);
2481
2482 } while (0);
2483
2484 gate_win32_closehandle(hfile);
2485 return ret;
2486 }
2487
2488
2489 gate_result_t gate_file_set_times(gate_string_t const* targetfile, gate_timestamp_t const* modified,
2490 gate_timestamp_t const* accessed, gate_timestamp_t const* created)
2491 {
2492 gate_result_t ret;
2493 TCHAR targetpath[GATE_MAX_FILEPATH_LENGTH];
2494 gate_size_t targetpathlen = gate_win32_string_2_winstr(targetfile, targetpath, sizeof(targetpath) / sizeof(targetpath[0]));
2495 HANDLE hfile = (HANDLE)INVALID_HANDLE_VALUE;
2496 FILETIME ftModified, ftAccessed, ftCreated;
2497 do
2498 {
2499 if (targetpathlen == 0)
2500 {
2501 ret = GATE_RESULT_INVALIDARG;
2502 break;
2503 }
2504
2505 if (created) gate_win32_convert_timestamp(*created, &ftCreated);
2506 if (accessed) gate_win32_convert_timestamp(*accessed, &ftAccessed);
2507 if (modified) gate_win32_convert_timestamp(*modified, &ftModified);
2508
2509 #if defined(GATE_SYS_WINSTORE) || defined(GATE_SYS_WIN16)
2510 ret = GATE_RESULT_NOTSUPPORTED;
2511 break;
2512 #else
2513 hfile = gate_win32_createfile(targetpath, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, 0, FILE_FLAG_BACKUP_SEMANTICS, NULL);
2514 if (hfile == INVALID_HANDLE_VALUE)
2515 {
2516 ret = GATE_RESULT_FAILED;
2517 break;
2518 }
2519
2520 if (!SetFileTime(hfile, (created ? &ftCreated : NULL), (accessed ? &ftAccessed : NULL), (modified ? &ftModified : NULL)))
2521 {
2522 ret = GATE_RESULT_FAILED;
2523 }
2524 else
2525 {
2526 ret = GATE_RESULT_OK;
2527 }
2528 #endif
2529 } while (0);
2530
2531 gate_win32_closehandle(hfile);
2532 return ret;
2533 }
2534
2535 #ifndef OWNER_SECURITY_INFORMATION
2536 #define OWNER_SECURITY_INFORMATION (0x00000001L)
2537 #endif
2538
2539 #ifndef GROUP_SECURITY_INFORMATION
2540 #define GROUP_SECURITY_INFORMATION (0x00000002L)
2541 #endif
2542
2543 gate_result_t gate_file_get_owner(gate_string_t const* targetpath,
2544 gate_uint32_t* owner_id, gate_string_t* owner_name)
2545 {
2546 #if defined(GATE_SYS_WINSTORE) || defined(GATE_SYS_WINCE) || defined(GATE_SYS_WIN16)
2547 GATE_UNUSED_ARG(targetpath);
2548 GATE_UNUSED_ARG(owner_id);
2549 GATE_UNUSED_ARG(owner_name);
2550 return GATE_RESULT_NOTSUPPORTED;
2551 #else
2552
2553 gate_result_t ret = GATE_RESULT_FAILED;
2554 HANDLE hfile = INVALID_HANDLE_VALUE;
2555 PSECURITY_DESCRIPTOR descriptor = NULL;
2556 DWORD sec_info = 0;
2557 PSID owner_sid = NULL;
2558 PSID group_sid = NULL;
2559 DWORD owner_rid = 0;
2560 DWORD result;
2561 TCHAR filepath[GATE_MAX_FILEPATH_LENGTH] = GATE_INIT_EMPTY;
2562 char owner_buffer[512] = GATE_INIT_EMPTY;
2563
2564 do
2565 {
2566 if (!gate_platform.AdvGetSecurityInfo)
2567 {
2568 ret = GATE_RESULT_NOTSUPPORTED;
2569 break;
2570 }
2571
2572 gate_win32_string_2_winstr(targetpath, filepath, sizeof(filepath) / sizeof(filepath[0]));
2573 hfile = gate_win32_createfile(filepath, READ_CONTROL, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, 0, FILE_FLAG_BACKUP_SEMANTICS, NULL);
2574 if (hfile == INVALID_HANDLE_VALUE)
2575 {
2576 ret = gate_platform_print_last_error(NULL, 0);
2577 break;
2578 }
2579 sec_info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION;
2580 result = gate_platform.AdvGetSecurityInfo(hfile, GATE_SE_FILE_OBJECT, sec_info, &owner_sid, &group_sid, NULL, NULL, &descriptor);
2581 if (0 != result)
2582 {
2583 ret = GATE_RESULT_FAILED;
2584 break;
2585 }
2586
2587 ret = GATE_RESULT_OK;
2588 if (owner_id)
2589 {
2590 if (gate_win32_sid_local_id(owner_sid, &owner_rid))
2591 {
2592 *owner_id = owner_rid;
2593 }
2594 else
2595 {
2596 *owner_id = 0;
2597 }
2598 }
2599 if (owner_name)
2600 {
2601 if (gate_win32_sid_resolve(owner_sid, owner_buffer, sizeof(owner_buffer)))
2602 {
2603 if (NULL == gate_string_create(owner_name, owner_buffer, gate_str_length(owner_buffer)))
2604 {
2605 ret = GATE_RESULT_OUTOFMEMORY;
2606 }
2607 }
2608 else
2609 {
2610 gate_string_create_empty(owner_name);
2611 }
2612 }
2613 } while (0);
2614
2615 if (descriptor != NULL)
2616 {
2617 LocalFree(descriptor);
2618 }
2619 gate_win32_closehandle(hfile);
2620 return ret;
2621 #endif
2622 }
2623
2624
2625
2626 #ifndef IO_REPARSE_TAG_SYMLINK
2627 #define IO_REPARSE_TAG_SYMLINK (0xA000000C)
2628 #endif
2629
2630 static void gate_win32_load_direntry(WIN32_FIND_DATA const* filedata, gate_file_entry_t* direntry,
2631 char const* filepath, gate_size_t filepathlen)
2632 {
2633 DWORD fileAttribs = filedata->dwFileAttributes;
2634 gate_string_t filepathstr;
2635
2636 #if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WINSTORE) && !defined(GATE_SYS_WIN16)
2637 /* SymLink detection code */
2638
2639 BY_HANDLE_FILE_INFORMATION handleinfo;
2640 TCHAR tmppath[GATE_MAX_FILEPATH_LENGTH];
2641 HANDLE hfile;
2642
2643 direntry->props.attribs = 0;
2644
2645 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_REPARSE_POINT))
2646 {
2647 direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_LINK;
2648 if (filedata->dwReserved0 == IO_REPARSE_TAG_SYMLINK)
2649 {
2650
2651 if (!GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_DIRECTORY))
2652 {
2653 /* load data from real file */
2654 gate_win32_utf8_2_winstr(filepath, filepathlen, tmppath, sizeof(tmppath) / sizeof(tmppath[0]));
2655 hfile = gate_win32_createfile(tmppath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
2656 OPEN_EXISTING, 0, 0, NULL);
2657 if (hfile != INVALID_HANDLE_VALUE)
2658 {
2659 if (GetFileInformationByHandle(hfile, &handleinfo))
2660 {
2661 fileAttribs = handleinfo.dwFileAttributes;
2662 direntry->props.size = ((gate_uint64_t)handleinfo.nFileSizeHigh << 32) + (gate_uint64_t)(handleinfo.nFileSizeLow);
2663 direntry->props.time_created = gate_win32_to_timestamp(&handleinfo.ftCreationTime);
2664 direntry->props.time_accessed = gate_win32_to_timestamp(&handleinfo.ftLastAccessTime);
2665 direntry->props.time_modified = gate_win32_to_timestamp(&handleinfo.ftLastWriteTime);
2666 }
2667 gate_win32_closehandle(hfile);
2668 }
2669 }
2670 }
2671 }
2672 #else
2673 direntry->props.attribs = 0;
2674 #endif
2675
2676 direntry->props.size = ((gate_uint64_t)filedata->nFileSizeHigh << 32) + (gate_uint64_t)(filedata->nFileSizeLow);
2677
2678 direntry->props.time_created = gate_win32_to_timestamp(&filedata->ftCreationTime);
2679 direntry->props.time_accessed = gate_win32_to_timestamp(&filedata->ftLastAccessTime);
2680 direntry->props.time_modified = gate_win32_to_timestamp(&filedata->ftLastWriteTime);
2681
2682 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_DIRECTORY)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_DIRECTORY;
2683 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_READONLY)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_READONLY;
2684 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_HIDDEN)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_HIDDEN;
2685 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_SYSTEM)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_SYSTEM;
2686 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_ARCHIVE)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_ARCHIVE;
2687
2688 /* windows special types: */
2689 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_TEMPORARY)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_TEMP;
2690 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_COMPRESSED)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_COMPRESSED;
2691 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_ENCRYPTED)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_ENCRYPTED;
2692 #if defined(FILE_ATTRIBUTE_DEVICE)
2693 if (GATE_FLAG_ENABLED(fileAttribs, FILE_ATTRIBUTE_DEVICE)) direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_DEVICE;
2694 #endif
2695
2696 if (0 == (fileAttribs & (FILE_ATTRIBUTE_DIRECTORY
2697 #if defined(FILE_ATTRIBUTE_DEVICE)
2698 | FILE_ATTRIBUTE_DEVICE
2699 #endif
2700 )))
2701 {
2702 /* neither a directory nor a device -> file */
2703 direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_FILE;
2704 }
2705 gate_string_create_static_len(&filepathstr, filepath, filepathlen);
2706 if (gate_file_is_hidden_entry(&filepathstr))
2707 {
2708 direntry->props.attribs |= GATE_FILEENTRY_ATTRIB_HIDDEN;
2709 }
2710
2711 if (gate_str_ends_with_ic(filepath, filepathlen, ".exe", 4))
2712 {
2713 direntry->props.access |= (GATE_FILEENTRY_ACCESS_ALLEXECUTE | GATE_FILEENTRY_ACCESS_OWNEREXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE);
2714 }
2715 }
2716
2717 gate_result_t gate_file_get_entry(gate_string_t const* srcpath, gate_file_entry_t* ptr_entry)
2718 {
2719 gate_result_t ret;
2720 TCHAR path[GATE_MAX_FILEPATH_LENGTH + 5];
2721 gate_size_t pathlen = gate_win32_string_2_winstr(srcpath, path, sizeof(path) / sizeof(path[0]));
2722 gate_size_t filenamelen;
2723 gate_size_t filepathlen;
2724
2725 HANDLE handle;
2726 WIN32_FIND_DATA filedata;
2727 do
2728 {
2729 gate_mem_clear(ptr_entry, sizeof(gate_file_entry_t));
2730 if (pathlen == 0)
2731 {
2732 ret = GATE_RESULT_INVALIDARG;
2733 break;
2734 }
2735
2736 if (gate_win32_winstr_ends_with(path, pathlen, GATE_WIN32_WINSTR("\\"), 1))
2737 {
2738 --pathlen;
2739 path[pathlen] = 0;
2740 }
2741
2742 filepathlen = gate_win32_winstr_2_utf8(path, pathlen, ptr_entry->path, sizeof(ptr_entry->path));
2743
2744 handle = gate_win32_findfirstfile(path, &filedata);
2745
2746 if (handle == (HANDLE)INVALID_HANDLE_VALUE)
2747 {
2748 gate_win32_print_lasterror(&ret, NULL, 0);
2749 break;
2750 }
2751 gate_win32_findclose(handle);
2752
2753 filenamelen = gate_win32_winstr_2_utf8(filedata.cFileName, gate_win32_winstr_length(filedata.cFileName),
2754 ptr_entry->name, sizeof(ptr_entry->name));
2755 if (filenamelen == 0)
2756 {
2757 ret = GATE_RESULT_FAILED;
2758 break;
2759 }
2760
2761
2762 gate_win32_load_direntry(&filedata, ptr_entry, ptr_entry->path, filepathlen);
2763
2764 ret = GATE_RESULT_OK;
2765 } while (0);
2766
2767 return ret;
2768 }
2769
2770 gate_result_t gate_file_root_list(gate_file_list_callback_t callback, void* userparam)
2771 {
2772 gate_result_t ret = GATE_RESULT_OK;
2773 gate_file_entry_t direntry;
2774
2775 #if !defined(GATE_SYS_WINCE)
2776 DWORD drives;
2777 DWORD ndx;
2778 char name[] = "A:";
2779 char path[] = "A:\\";
2780 #endif
2781
2782 #if defined(GATE_SYS_WINCE)
2783
2784 gate_mem_clear(&direntry, sizeof(direntry));
2785 direntry.props.attribs = GATE_FILEENTRY_ATTRIB_DIRECTORY;
2786
2787 gate_str_print_text(direntry.name, sizeof(direntry.name), "\\", 1);
2788 gate_str_print_text(direntry.path, sizeof(direntry.path), "\\", 1);
2789
2790 if (callback != NULL)
2791 {
2792 callback(&direntry, userparam);
2793 }
2794 #else
2795 drives = GetLogicalDrives();
2796
2797 for (ndx = 0; ndx < 26; ++ndx)
2798 {
2799 if ((drives & (1 << ndx)) != 0)
2800 {
2801 if (callback != NULL)
2802 {
2803 gate_mem_clear(&direntry, sizeof(direntry));
2804
2805 gate_str_print_text(direntry.name, sizeof(direntry.name), name, 2);
2806 gate_str_print_text(direntry.path, sizeof(direntry.path), path, 3);
2807
2808 direntry.props.attribs = GATE_FILEENTRY_ATTRIB_DIRECTORY | GATE_FILEENTRY_ATTRIB_VOLUME;
2809
2810 if (!callback(&direntry, userparam))
2811 {
2812 break;
2813 }
2814 }
2815 }
2816 ++name[0];
2817 ++path[0];
2818 }
2819 #endif
2820
2821 return ret;
2822 }
2823
2824 gate_result_t gate_file_dir_open(gate_string_t const* dirpath, gate_file_dirreader_t* dirhandle)
2825 {
2826 gate_result_t ret = GATE_RESULT_FAILED;
2827 TCHAR searchpath[GATE_MAX_FILEPATH_LENGTH + 5];
2828 gate_size_t searchpath_len;
2829 WIN32_FIND_DATA filedata;
2830 gate_size_t filename_len;
2831
2832 do
2833 {
2834 gate_mem_clear(dirhandle, sizeof(gate_file_dirreader_t));
2835
2836 dirhandle->bufferlen = gate_string_to_buffer(dirpath, dirhandle->buffer, sizeof(dirhandle->buffer));
2837 if (dirhandle->buffer[dirhandle->bufferlen - 1] != '\\')
2838 {
2839 dirhandle->buffer[dirhandle->bufferlen] = '\\';
2840 ++dirhandle->bufferlen;
2841 dirhandle->buffer[dirhandle->bufferlen] = '\0';
2842 }
2843
2844 searchpath_len = gate_win32_utf8_2_winstr(dirhandle->buffer, dirhandle->bufferlen, searchpath, sizeof(searchpath) / sizeof(searchpath[0]));
2845 searchpath[searchpath_len++] = (TCHAR)'*';
2846 searchpath[searchpath_len++] = (TCHAR)'.';
2847 searchpath[searchpath_len++] = (TCHAR)'*';
2848 searchpath[searchpath_len] = (TCHAR)'\0';
2849
2850 dirhandle->handle = (void*)gate_win32_findfirstfile(searchpath, &filedata);
2851 if ((HANDLE)dirhandle->handle == (HANDLE)INVALID_HANDLE_VALUE)
2852 {
2853 gate_win32_print_lasterror(&ret, NULL, 0);
2854 break;
2855 }
2856 filename_len = gate_win32_winstr_length(filedata.cFileName);
2857
2858 for (;;)
2859 {
2860 if ((0 != gate_win32_winstr_compare(filedata.cFileName, filename_len, _T("."), 1))
2861 && (0 != gate_win32_winstr_compare(filedata.cFileName, filename_len, _T(".."), 2)))
2862 {
2863 break;
2864 }
2865 if (gate_win32_findnext((HANDLE)dirhandle->handle, &filedata))
2866 {
2867 filename_len = gate_win32_winstr_length(filedata.cFileName);
2868 }
2869 else
2870 {
2871 gate_mem_clear(dirhandle->buffer, sizeof(dirhandle->buffer));
2872 dirhandle->bufferlen = 0;
2873 return GATE_RESULT_OK;
2874 }
2875 }
2876
2877 gate_win32_winstr_2_utf8(filedata.cFileName, gate_win32_winstr_length(filedata.cFileName),
2878 &dirhandle->buffer[dirhandle->bufferlen], sizeof(dirhandle->buffer) - dirhandle->bufferlen);
2879
2880 ret = GATE_RESULT_OK;
2881 } while (0);
2882
2883 return ret;
2884 }
2885 gate_size_t gate_file_dir_read(gate_file_dirreader_t* dirhandle, char* path, gate_size_t pathlen, gate_bool_t filename_only)
2886 {
2887 gate_size_t ret = 0;
2888 WIN32_FIND_DATA filedata;
2889
2890 if (filename_only)
2891 {
2892 /* return previously retrieved file name */
2893 ret = gate_str_print_text(path, pathlen,
2894 &dirhandle->buffer[dirhandle->bufferlen],
2895 gate_str_length(&dirhandle->buffer[dirhandle->bufferlen]));
2896 }
2897 else
2898 {
2899 /* return previously retrieved file path */
2900 ret = gate_str_print_text(path, pathlen,
2901 &dirhandle->buffer[0],
2902 gate_str_length(&dirhandle->buffer[0]));
2903 }
2904
2905 if (ret > 0)
2906 {
2907 if (gate_win32_findnext((HANDLE)dirhandle->handle, &filedata))
2908 {
2909 gate_win32_winstr_2_utf8(filedata.cFileName, gate_win32_winstr_length(filedata.cFileName),
2910 &dirhandle->buffer[dirhandle->bufferlen], sizeof(dirhandle->buffer) - dirhandle->bufferlen
2911 );
2912 }
2913 else
2914 {
2915 /* no more files can be retrieved -> set path buffer to zero*/
2916 gate_mem_clear(dirhandle->buffer, sizeof(dirhandle->buffer));
2917 dirhandle->bufferlen = 0;
2918 }
2919 /* find next */
2920 }
2921
2922 return ret;
2923 }
2924 gate_result_t gate_file_dir_close(gate_file_dirreader_t* dirhandle)
2925 {
2926 gate_result_t ret = GATE_RESULT_FAILED;
2927
2928 if (gate_win32_findclose((HANDLE)dirhandle->handle))
2929 {
2930 ret = GATE_RESULT_OK;
2931 }
2932 gate_mem_clear(dirhandle, sizeof(gate_file_dirreader_t));
2933
2934 return ret;
2935 }
2936
2937 #if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WINSTORE) && !defined(GATE_SYS_WIN16)
2938 # define GATE_FILES_MPR_SUPPORTED 1
2939 #endif
2940
2941
2942 #if defined(GATE_FILES_MPR_SUPPORTED)
2943
2944 typedef struct win32_mpr_functions_class
2945 {
2946 DWORD(WINAPI* MprWNetOpenEnum)(DWORD dwScope, DWORD dwType, DWORD dwUsage, LPNETRESOURCE lpNetResource, LPHANDLE lphEnum);
2947 DWORD(WINAPI* MprWNetEnumResource)(HANDLE hEnum, LPDWORD lpcCount, LPVOID lpBuffer, LPDWORD lpBufferSize);
2948 DWORD(WINAPI* MprWNetCloseEnum)(HANDLE hEnum);
2949 } win32_mpr_functions_t;
2950
2951 static win32_mpr_functions_t* win32_mpr()
2952 {
2953 static win32_mpr_functions_t mpr;
2954 static HMODULE hmpr = NULL;
2955 static gate_atomic_int_t mpr_load_state = 0;
2956 gate_int32_t state = 1;
2957
2958 do
2959 {
2960 state = gate_atomic_int_xchg_if(&mpr_load_state, 0, 1);
2961 if (state >= 2)
2962 {
2963 /* functions are loaded */
2964 break;
2965 }
2966 else if (state == 0)
2967 {
2968 hmpr = gate_win32_load_library(_T("mpr"), 0);
2969
2970 if (hmpr != NULL)
2971 {
2972 gate_win32_get_proc_address(hmpr, GATE_WIN32_DUAL_NAME("WNetOpenEnum"), &mpr.MprWNetOpenEnum);
2973 gate_win32_get_proc_address(hmpr, GATE_WIN32_DUAL_NAME("WNetEnumResource"), &mpr.MprWNetEnumResource);
2974 gate_win32_get_proc_address(hmpr, "WNetCloseEnum", &mpr.MprWNetCloseEnum);
2975 }
2976 gate_atomic_int_set(&mpr_load_state, 2);
2977 break;
2978 }
2979 else if (state < 0)
2980 {
2981 /* error, invalid state */
2982 break;
2983 }
2984 /* another thread is loading functions, -> wait/retry*/
2985 Sleep(0);
2986 } while (state == 1);
2987
2988 return &mpr;
2989 }
2990
2991 static gate_result_t gate_file_list_server_shares(LPTSTR path, gate_file_list_callback_t callback, void* userparam)
2992 {
2993 gate_result_t ret = GATE_RESULT_FAILED;
2994 gate_file_entry_t direntry;
2995 DWORD result;
2996 DWORD count;
2997 DWORD size;
2998 NETRESOURCE netres;
2999 HANDLE handle = NULL;
3000 gate_size_t filenamelen;
3001 gate_size_t filepathlen;
3002 win32_mpr_functions_t* mpr = win32_mpr();
3003 union
3004 {
3005 NETRESOURCE net;
3006 char buffer[8192];
3007 } netbuffer;
3008
3009 do
3010 {
3011 /* enumerate shares of an smb file service host */
3012 gate_mem_clear(&netres, sizeof(netres));
3013 netres.dwScope = RESOURCE_GLOBALNET;
3014 netres.dwType = RESOURCETYPE_DISK;
3015 netres.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
3016 netres.dwUsage = RESOURCEUSAGE_CONTAINER;
3017 netres.lpComment = NULL;
3018 netres.lpProvider = NULL;
3019 netres.lpLocalName = NULL;
3020 netres.lpRemoteName = path;
3021
3022 if (!mpr->MprWNetOpenEnum || !mpr->MprWNetEnumResource || !mpr->MprWNetCloseEnum)
3023 {
3024 ret = GATE_RESULT_NOTSUPPORTED;
3025 break;
3026 }
3027
3028 result = mpr->MprWNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, RESOURCEUSAGE_ALL, &netres, &handle);
3029 if (NO_ERROR != result)
3030 {
3031 gate_win32_setlasterror(result);
3032 ret = GATE_RESULT_FAILED;
3033 break;
3034 }
3035 count = 1;
3036 size = sizeof(netres);
3037 result = mpr->MprWNetEnumResource(handle, &count, &netbuffer, &size);
3038 while ((ERROR_MORE_DATA == result) || (NO_ERROR == result))
3039 {
3040 gate_mem_clear(&direntry, sizeof(direntry));
3041
3042 filenamelen = gate_win32_winstr_2_utf8(netbuffer.net.lpRemoteName, gate_win32_winstr_length(netbuffer.net.lpRemoteName),
3043 direntry.name, sizeof(direntry.name));
3044 filepathlen = gate_win32_winstr_2_utf8(path, gate_win32_winstr_length(path),
3045 direntry.path, sizeof(direntry.path));
3046 direntry.props.size = 0;
3047 direntry.props.attribs = GATE_FILEENTRY_ATTRIB_DIRECTORY;
3048 direntry.props.time_created = 0;
3049 direntry.props.time_modified = 0;
3050 direntry.props.time_accessed = 0;
3051
3052 if (callback != NULL)
3053 {
3054 if (false == callback(&direntry, userparam))
3055 {
3056 /* cancelled by user */
3057 break;
3058 }
3059 }
3060
3061 size = sizeof(netres);
3062 result = mpr->MprWNetEnumResource(handle, &count, &netbuffer, &size);
3063 }
3064 mpr->MprWNetCloseEnum(handle);
3065 } while (0);
3066
3067 return ret;
3068 }
3069 #endif /* defined(GATE_FILES_MPR_SUPPORTED) */
3070
3071
3072 gate_result_t gate_file_list(gate_string_t const* dirpath, gate_file_list_callback_t callback, void* userparam)
3073 {
3074 gate_result_t ret;
3075 TCHAR path[GATE_MAX_FILEPATH_LENGTH + 5];
3076 gate_size_t pathlen = gate_win32_string_2_winstr(dirpath, path, sizeof(path) / sizeof(path[0]));
3077 //char filename[GATE_MAX_FILENAME_LENGTH];
3078 gate_size_t filenamelen;
3079 char filepath[GATE_MAX_FILEPATH_LENGTH];
3080 gate_size_t filepathlen;
3081 gate_size_t filepathlen2;
3082 HANDLE hsearch = (HANDLE)NULL;
3083 WIN32_FIND_DATA filedata;
3084 gate_file_entry_t direntry;
3085
3086 do
3087 {
3088 if (pathlen == 0)
3089 {
3090 ret = GATE_RESULT_INVALIDARG;
3091 break;
3092 }
3093
3094 #if defined(GATE_FILES_MPR_SUPPORTED)
3095 if (!gate_file_is_server_share_path(path, false))
3096 {
3097 if (gate_file_is_server_path(path, true))
3098 {
3099 ret = gate_file_list_server_shares(path, callback, userparam);
3100 break;
3101 }
3102 }
3103 #endif /* defined(GATE_FILES_MPR_SUPPORTED) */
3104
3105 /* file search required '*.*' token at the end of the directory path */
3106 if (gate_win32_winstr_ends_with(path, pathlen, GATE_WIN32_WINSTR("\\"), 1))
3107 {
3108 gate_win32_winstr_print_text(&path[pathlen], sizeof(path) / sizeof(path[0]) - pathlen, GATE_WIN32_WINSTR("*.*"), 3);
3109 pathlen += 3;
3110
3111 }
3112 else if (!gate_win32_winstr_ends_with(path, pathlen, GATE_WIN32_WINSTR("\\*.*"), 4))
3113 {
3114 gate_win32_winstr_print_text(&path[pathlen], sizeof(path) / sizeof(path[0]) - pathlen, GATE_WIN32_WINSTR("\\*.*"), 4);
3115 pathlen += 4;
3116 }
3117
3118 filepathlen = gate_win32_winstr_2_utf8(path, pathlen - 3, filepath, sizeof(filepath));
3119
3120 hsearch = gate_win32_findfirstfile(path, &filedata);
3121 if ((HANDLE)INVALID_HANDLE_VALUE == hsearch)
3122 {
3123 #if defined(GATE_SYS_WINCE)
3124 /* empty directories return *.*-error in wince */
3125 path[pathlen - 3] = '\0';
3126 pathlen -= 3;
3127 hsearch = gate_win32_findfirstfile(path, &filedata);
3128 if (INVALID_HANDLE_VALUE != hsearch)
3129 {
3130 /* directory exists -> so it's empty */
3131 ret = GATE_RESULT_OK;
3132 break;
3133 }
3134 #endif
3135 gate_win32_print_lasterror(&ret, NULL, 0);
3136 break;
3137 }
3138 do
3139 {
3140 gate_mem_clear(&direntry, sizeof(direntry));
3141 filenamelen = gate_win32_winstr_2_utf8(filedata.cFileName, gate_win32_winstr_length(filedata.cFileName),
3142 direntry.name, sizeof(direntry.name));
3143 if (filenamelen == 0)
3144 {
3145 /* failed to convert filename -> skip */
3146 continue;
3147 }
3148 if (0 == gate_str_compare(direntry.name, filenamelen, "..", 2))
3149 {
3150 /* skip parent-reference */
3151 continue;
3152 }
3153 if (0 == gate_str_compare(direntry.name, filenamelen, ".", 1))
3154 {
3155 /* skip self-reference */
3156 continue;
3157 }
3158
3159 filepathlen2 = gate_str_print_text(&filepath[filepathlen], sizeof(filepath) - filepathlen, direntry.name, filenamelen);
3160
3161 GATE_DEBUG_ASSERT(filenamelen == filepathlen2);
3162
3163 gate_str_print_text(direntry.path, sizeof(direntry.path), filepath, filepathlen + filepathlen2);
3164
3165 gate_win32_load_direntry(&filedata, &direntry, filepath, filepathlen + filepathlen2);
3166
3167 if (callback != NULL)
3168 {
3169 if (false == callback(&direntry, userparam))
3170 {
3171 /* canceled by user */
3172 break;
3173 }
3174 }
3175
3176 } while (gate_win32_findnext(hsearch, &filedata));
3177 ret = GATE_RESULT_OK;
3178 } while (0);
3179 gate_win32_findclose(hsearch);
3180 return ret;
3181 }
3182
3183 #if defined(GATE_SYS_WIN16)
3184 static BOOL CreateDirectory(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
3185 {
3186 GATE_UNUSED_ARG(lpSecurityAttributes);
3187 if (0 == _mkdir(lpPathName))
3188 {
3189 return TRUE;
3190 }
3191 else
3192 {
3193 return FALSE;
3194 }
3195 }
3196 #endif
3197
3198 gate_result_t gate_file_dir_create(gate_string_t const* dirpath, gate_enumint_t flags)
3199 {
3200 gate_result_t ret;
3201 TCHAR path[GATE_MAX_FILEPATH_LENGTH];
3202 gate_size_t pathlen = gate_win32_string_2_winstr(dirpath, path, sizeof(path) / sizeof(path[0]));
3203 GATE_UNUSED_ARG(flags);
3204 if (pathlen == 0)
3205 {
3206 ret = GATE_RESULT_INVALIDARG;
3207 }
3208 else
3209 {
3210 if (path[pathlen - 1] == _T('\\'))
3211 {
3212 path[--pathlen] = 0;
3213 }
3214 if (CreateDirectory(path, NULL))
3215 {
3216 ret = GATE_RESULT_OK;
3217 }
3218 else
3219 {
3220 gate_win32_print_lasterror(&ret, NULL, 0);
3221 }
3222 }
3223 return ret;
3224 }
3225
3226 #if defined(GATE_SYS_WIN16)
3227 static BOOL RemoveDirectory(LPCSTR lpPathName)
3228 {
3229 if (0 == _rmdir(lpPathName))
3230 {
3231 return TRUE;
3232 }
3233 return FALSE;
3234 }
3235 #endif
3236
3237 gate_result_t gate_file_dir_delete(gate_string_t const* dirpath)
3238 {
3239 gate_result_t ret;
3240 DWORD dw_error;
3241 TCHAR path[GATE_MAX_FILEPATH_LENGTH];
3242 gate_size_t pathlen = gate_win32_string_2_winstr(dirpath, path, sizeof(path) / sizeof(path[0]) - 1);
3243 if (pathlen == 0)
3244 {
3245 ret = GATE_RESULT_INVALIDARG;
3246 }
3247 else
3248 {
3249 path[pathlen + 1] = 0; /* ensure double zero for shell-file-ops */
3250 if (RemoveDirectory(path))
3251 {
3252 ret = GATE_RESULT_OK;
3253 }
3254 else
3255 {
3256 dw_error = gate_win32_getlasterror();
3257 gate_win32_print_lasterror_code(dw_error, &ret, NULL, 0);
3258 /* try fallback */
3259 if (gate_win32_shfileop(GATE_WIN32_SHFILEOP_DELETE, path, NULL))
3260 {
3261 ret = GATE_RESULT_OK;
3262 }
3263 else
3264 {
3265 gate_win32_setlasterror(dw_error);
3266 }
3267 }
3268 }
3269 return ret;
3270 }
3271
3272 gate_result_t gate_file_dir_exists(gate_string_t const* dirpath)
3273 {
3274 WIN32_FIND_DATA data;
3275 DWORD attribs;
3276 HANDLE hfind;
3277 TCHAR path[GATE_MAX_FILEPATH_LENGTH];
3278 gate_size_t pathlen = gate_win32_utf8_2_winstr(dirpath->str, dirpath->length, path, sizeof(path) / sizeof(path[0]));
3279 if (path[pathlen - 1] == '\\')
3280 {
3281 --pathlen;
3282 path[pathlen] = 0;
3283 }
3284
3285 #if !defined(GATE_SYS_WIN16)
3286 if (gate_file_is_server_path(path, false))
3287 {
3288 return GATE_RESULT_OK;
3289 }
3290 if (gate_file_is_server_share_path(path, false))
3291 {
3292 return GATE_RESULT_OK;
3293 }
3294 #endif
3295
3296 attribs = GetFileAttributes(path);
3297 if (attribs != INVALID_FILE_ATTRIBUTES)
3298 {
3299 if (GATE_FLAG_ENABLED(attribs, FILE_ATTRIBUTE_DIRECTORY))
3300 {
3301 return GATE_RESULT_OK;
3302 }
3303 else
3304 {
3305 return GATE_RESULT_NOMATCH;
3306 }
3307 }
3308
3309 hfind = gate_win32_findfirstfile(path, &data);
3310 if (hfind != (HANDLE)INVALID_HANDLE_VALUE)
3311 {
3312 gate_win32_findclose(hfind);
3313 if (GATE_FLAG_ENABLED(data.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY))
3314 {
3315 return GATE_RESULT_OK;
3316 }
3317 else
3318 {
3319 return GATE_RESULT_NOMATCH;
3320 }
3321 }
3322 else
3323 {
3324 return GATE_RESULT_NOTAVAILABLE;
3325 }
3326 }
3327
3328
3329 #endif /* GATE_CORE_FILES_WINAPI_IMPL */
3330
3331
3332 #if defined(GATE_CORE_FILES_POSIX_IMPL)
3333
3334 #include "gate/platforms.h"
3335 #include <sys/stat.h>
3336 #include <sys/types.h>
3337 #include <stdio.h>
3338 #include <errno.h>
3339 #include <fcntl.h>
3340 #include <dirent.h>
3341 #include <utime.h>
3342 #include <stdlib.h>
3343
3344 char const gate_file_path_separator_char = '/';
3345 char const* const gate_file_path_separator = "/";
3346
3347
3348 323 gate_result_t gate_file_exists(gate_string_t const* filepath)
3349 {
3350 gate_result_t ret;
3351 char pathbuffer[GATE_MAX_FILEPATH_LENGTH];
3352 struct stat filestat;
3353 323 gate_string_to_buffer(filepath, pathbuffer, sizeof(pathbuffer));
3354
2/2
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 191 times.
323 if (0 == stat(pathbuffer, &filestat))
3355 {
3356 132 ret = GATE_RESULT_OK;
3357 }
3358 else
3359 {
3360 191 gate_posix_errno(&ret);
3361 }
3362 323 return ret;
3363 }
3364 73 gate_result_t gate_file_dir_exists(gate_string_t const* dirpath)
3365 {
3366 gate_result_t ret;
3367 char pathbuffer[GATE_MAX_FILEPATH_LENGTH];
3368 struct stat filestat;
3369 73 gate_size_t len = gate_string_to_buffer(dirpath, pathbuffer, sizeof(pathbuffer));
3370
4/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 55 times.
73 if ((len > 1) && (pathbuffer[len - 1] == '/'))
3371 {
3372 17 --len;
3373 17 pathbuffer[len] = 0;
3374 }
3375
3376
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 31 times.
73 if (0 == stat(pathbuffer, &filestat))
3377 {
3378
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 12 times.
42 if (S_ISDIR(filestat.st_mode))
3379 {
3380 30 ret = GATE_RESULT_OK;
3381 }
3382 else
3383 {
3384 12 ret = GATE_RESULT_INVALIDSTATE;
3385 }
3386 }
3387 else
3388 {
3389 31 gate_posix_errno(&ret);
3390 }
3391 73 return ret;
3392 }
3393
3394 3 gate_result_t gate_file_copy(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath, gate_enumint_t flags)
3395 {
3396 gate_size_t ret;
3397
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (GATE_FLAG_ENABLED(flags, GATE_FILE_COPY_SIMPLE))
3398 {
3399 1 ret = gate_file_copy_internal(srcfilepath, dstfilepath, flags);
3400 }
3401 else
3402 {
3403 2 gate_file_t srcfile = GATE_FILE_INVALID;
3404 2 gate_file_t dstfile = GATE_FILE_INVALID;
3405 gate_size_t bufferused;
3406 gate_size_t pos;
3407 gate_size_t written;
3408 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
3409 struct stat srcstat;
3410 struct utimbuf utm;
3411 2 gate_bool_t srcstatreceived = false;
3412
3413 do
3414 {
3415
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!GATE_FLAG_ENABLED(flags, GATE_FILE_COPY_OVERWRITE))
3416 {
3417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (GATE_SUCCEEDED(gate_file_exists(dstfilepath)))
3418 {
3419 ✗ ret = GATE_RESULT_ALREADYEXISTS;
3420 ✗ break;
3421 }
3422 }
3423
3424 2 ret = gate_file_open(srcfilepath, GATE_STREAM_OPEN_READ, &srcfile);
3425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(ret))
3426 {
3427 ✗ break;
3428 }
3429
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (0 == fstat((int)(gate_intptr_t)srcfile, &srcstat))
3430 {
3431 2 utm.actime = srcstat.st_atime;
3432 2 utm.modtime = srcstat.st_mtime;
3433 2 srcstatreceived = true;
3434 }
3435 2 ret = gate_file_open(dstfilepath, GATE_STREAM_OPEN_WRITE, &dstfile);
3436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(ret))
3437 {
3438 ✗ break;
3439 }
3440 2 ret = gate_file_read(srcfile, buffer, sizeof(buffer), &bufferused);
3441
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 while (GATE_SUCCEEDED(ret))
3442 {
3443
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (bufferused == 0)
3444 {
3445 /* end of file */
3446 2 break;
3447 }
3448 2 pos = 0;
3449
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 while (pos < bufferused)
3450 {
3451 2 ret = gate_file_write(dstfile, &buffer[pos], bufferused - pos, &written);
3452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(ret))
3453 {
3454 ✗ break;
3455 }
3456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (written == 0)
3457 {
3458 ✗ ret = GATE_RESULT_INCORRECTSIZE;
3459 ✗ break;
3460 }
3461 2 pos += written;
3462 }
3463
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_SUCCEEDED(ret))
3464 {
3465 2 ret = gate_file_read(srcfile, buffer, sizeof(buffer), &bufferused);
3466 }
3467 }
3468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (GATE_FAILED(ret))
3469 {
3470 ✗ break;
3471 }
3472 } while (0);
3473
3474
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (srcfile != GATE_FILE_INVALID)
3475 {
3476 2 gate_file_close(srcfile);
3477 }
3478
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (dstfile != GATE_FILE_INVALID)
3479 {
3480 2 gate_file_close(dstfile);
3481 }
3482
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_SUCCEEDED(ret))
3483 {
3484
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (srcstatreceived)
3485 {
3486 2 gate_string_to_buffer(dstfilepath, buffer, sizeof(buffer));
3487 2 utime(buffer, &utm);
3488 }
3489 }
3490 }
3491 3 return ret;
3492 }
3493 1 gate_result_t gate_file_move(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath)
3494 {
3495 gate_result_t ret;
3496 char srcpath[GATE_MAX_FILEPATH_LENGTH];
3497 char dstpath[GATE_MAX_FILEPATH_LENGTH];
3498 1 gate_string_to_buffer(srcfilepath, srcpath, sizeof(srcpath));
3499 1 gate_string_to_buffer(dstfilepath, dstpath, sizeof(dstpath));
3500
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (rename(srcpath, dstpath) == 0)
3501 {
3502 1 ret = GATE_RESULT_OK;
3503 }
3504 else
3505 {
3506 ✗ gate_posix_errno(&ret);
3507 }
3508 1 return ret;
3509 }
3510 20 gate_result_t gate_file_delete(gate_string_t const* filepath)
3511 {
3512 gate_result_t ret;
3513 char pathbuffer[GATE_MAX_FILEPATH_LENGTH];
3514 20 gate_string_to_buffer(filepath, pathbuffer, sizeof(pathbuffer));
3515
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (0 == unlink(pathbuffer))
3516 {
3517 20 ret = GATE_RESULT_OK;
3518 }
3519 else
3520 {
3521 ✗ gate_posix_errno(&ret);
3522 }
3523 20 return ret;
3524 }
3525 1 gate_result_t gate_file_real_path(gate_string_t const* relativepath, gate_string_t* absolutepath)
3526 {
3527 gate_result_t ret;
3528 char relpath[GATE_MAX_FILEPATH_LENGTH];
3529 char abspath[PATH_MAX + 1];
3530 1 gate_string_to_buffer(relativepath, relpath, sizeof(relpath));
3531
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (realpath(relpath, abspath) != NULL)
3532 {
3533
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_string_create(absolutepath, abspath, gate_str_length(abspath)))
3534 {
3535 ✗ ret = GATE_RESULT_OUTOFMEMORY;
3536 }
3537 else
3538 {
3539 1 ret = GATE_RESULT_OK;
3540 }
3541 }
3542 else
3543 {
3544 ✗ gate_posix_errno(&ret);
3545 }
3546 1 return ret;
3547 }
3548 1 gate_result_t gate_file_create_link(gate_string_t const* targetpath, gate_string_t const* linkfile)
3549 {
3550 gate_result_t ret;
3551 char target[PATH_MAX];
3552 char link[GATE_MAX_FILEPATH_LENGTH];
3553 1 gate_string_to_buffer(targetpath, link, sizeof(link));
3554
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == realpath(link, target))
3555 {
3556 ✗ gate_posix_errno(&ret);
3557 }
3558 else
3559 {
3560 1 gate_string_to_buffer(linkfile, link, sizeof(link));
3561
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (symlink(target, link) == 0)
3562 {
3563 1 ret = GATE_RESULT_OK;
3564 }
3565 else
3566 {
3567 ✗ gate_posix_errno(&ret);
3568 }
3569 }
3570 1 return ret;
3571 }
3572 1 gate_result_t gate_file_read_link(gate_string_t const* filepath, gate_string_t* realpath)
3573 {
3574 gate_result_t ret;
3575 char linkpath[GATE_MAX_FILEPATH_LENGTH];
3576
3577 1 gate_string_to_buffer(filepath, linkpath, sizeof(linkpath));
3578 for (;;)
3579 1 {
3580 char destpath[GATE_MAX_FILEPATH_LENGTH];
3581 2 ssize_t rlresult = readlink(linkpath, destpath, sizeof(destpath) / sizeof(destpath[0]) - 1);
3582
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (rlresult <= 0)
3583 {
3584 1 int err = gate_posix_errno(&ret);
3585
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (err == EINVAL)
3586 {
3587 /* real path found (not another link), return it */
3588
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_string_create(realpath, destpath, gate_str_length(destpath)))
3589 {
3590 ✗ ret = GATE_RESULT_OUTOFMEMORY;
3591 }
3592 else
3593 {
3594 1 ret = GATE_RESULT_OK;
3595 }
3596 }
3597 else
3598 {
3599 /* all other cases are real errors during resolution */
3600 }
3601 1 break;
3602 }
3603 else
3604 {
3605 /* a destination path could be resolved.
3606 make it a new source to check
3607 */
3608 1 destpath[rlresult] = 0;
3609 1 gate_str_print_text(linkpath, sizeof(linkpath), destpath, gate_str_length(destpath));
3610 }
3611 }
3612 1 return ret;
3613 }
3614 1 gate_result_t gate_file_create_hardlink(gate_string_t const* targetfile, gate_string_t const* linkfile)
3615 {
3616 gate_result_t ret;
3617 char target[GATE_MAX_FILEPATH_LENGTH];
3618 char linkpath[GATE_MAX_FILEPATH_LENGTH];
3619 1 gate_string_to_buffer(targetfile, target, sizeof(target));
3620 1 gate_string_to_buffer(linkfile, linkpath, sizeof(linkpath));
3621
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (link(target, linkpath) == 0)
3622 {
3623 1 ret = GATE_RESULT_OK;
3624 }
3625 else
3626 {
3627 ✗ gate_posix_errno(&ret);
3628 }
3629 1 return ret;
3630 }
3631
3632 76 static gate_bool_t gate_file_parent_stat(gate_string_t const* filepath, mode_t* ptrmode, uid_t* ptruid, gid_t* ptrgid)
3633 {
3634 76 gate_size_t pos = gate_str_char_pos_last(filepath->str, filepath->length, gate_file_path_separator_char);
3635
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 27 times.
76 if (pos != GATE_STR_NPOS)
3636 {
3637 char parent[GATE_MAX_FILEPATH_LENGTH];
3638 struct stat dirstat;
3639 49 gate_str_print_text(parent, sizeof(parent), filepath->str, pos);
3640
2/2
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 3 times.
49 if (0 == lstat(parent, &dirstat))
3641 {
3642 46 mode_t mode = dirstat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3643
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (ptrmode != NULL) *ptrmode = mode;
3644
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (ptruid != NULL) *ptruid = dirstat.st_uid;
3645
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (ptrgid != NULL) *ptrgid = dirstat.st_gid;
3646 46 return true;
3647 }
3648 }
3649 30 return false;
3650 }
3651
3652 186 gate_result_t gate_file_open(gate_string_t const* filepath, gate_enumint_t flags, gate_file_t* filehandle)
3653 {
3654 gate_result_t ret;
3655
3656 do
3657 {
3658 char path[GATE_MAX_FILEPATH_LENGTH];
3659 int openflags;
3660 int fd;
3661 mode_t mode;
3662 186 uid_t open_as_uid = gate_posix_getuid();
3663 186 uid_t open_as_gid = gate_posix_getgid();
3664
3665
2/4
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 186 times.
186 if (gate_string_is_empty(filepath) || (filehandle == NULL))
3666 {
3667 ✗ ret = GATE_RESULT_INVALIDARG;
3668 50 break;
3669 }
3670 186 gate_string_to_buffer(filepath, path, sizeof(path));
3671
3672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if (GATE_FLAG_ENABLED(flags, GATE_STREAM_OPEN_APPENDREADWRITE)) { openflags = (O_CREAT | O_RDWR | O_APPEND); }
3673
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 169 times.
186 else if (GATE_FLAG_ENABLED(flags, GATE_STREAM_OPEN_APPENDWRITE)) { openflags = (O_CREAT | O_WRONLY | O_APPEND); }
3674
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 161 times.
169 else if (GATE_FLAG_ENABLED(flags, GATE_STREAM_OPEN_READWRITE)) { openflags = (O_CREAT | O_RDWR); }
3675
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 118 times.
161 else if (GATE_FLAG_ENABLED(flags, GATE_STREAM_OPEN_WRITE)) { openflags = (O_CREAT | O_WRONLY | O_TRUNC); }
3676
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 else if (GATE_FLAG_ENABLED(flags, GATE_STREAM_OPEN_READ)) { openflags = (O_RDONLY); }
3677 else
3678 {
3679 ✗ ret = GATE_RESULT_INVALIDARG;
3680 ✗ break;
3681 }
3682
3683 #ifdef O_CLOEXEC
3684 186 openflags |= O_CLOEXEC;
3685 #endif
3686
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 118 times.
186 if (GATE_FLAG_ENABLED(openflags, O_CREAT))
3687 {
3688 /* if a new file is created we have a chance to influence its permissions
3689 */
3690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEOWNERRESTRICTED))
3691 {
3692 /* only the owner has read/write access */
3693 ✗ mode = S_IRUSR | S_IWUSR;
3694 ✗ if (GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEEXECUTABLE))
3695 {
3696 ✗ mode |= S_IXUSR;
3697 }
3698 }
3699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 else if (GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEGROUPRESTRICTED))
3700 {
3701 /* owner and owner's primary group have read/write access */
3702 ✗ mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
3703 ✗ if (GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEEXECUTABLE))
3704 {
3705 ✗ mode |= S_IXUSR | S_IXGRP;
3706 }
3707 }
3708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 else if (GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEUNRESTRICTED))
3709 {
3710 /* everyone has read/write access */
3711 ✗ mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3712 ✗ if (GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEEXECUTABLE))
3713 {
3714 ✗ mode |= S_IXUSR | S_IXGRP;
3715 }
3716 }
3717 else
3718 {
3719 /* in case of no specification, we try to apply
3720 the windows convention, where permissions are
3721 inherited from the parent directory
3722 */
3723 68 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
3724
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 27 times.
68 if (gate_file_parent_stat(filepath, &mode, &open_as_uid, &open_as_gid))
3725 {
3726
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 if (open_as_uid == 0)
3727 {
3728 41 open_as_uid = gate_posix_getuid();
3729 }
3730
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 if (open_as_gid == 0)
3731 {
3732 41 open_as_gid = gate_posix_getgid();
3733 }
3734 /* remove directory-X-bits */
3735 41 mode &= ~(mode_t)(S_IXUSR | S_IXGRP | S_IXOTH);
3736 }
3737
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 67 times.
68 if (GATE_FLAG_ENABLED(flags, GATE_FILE_OPEN_CREATEEXECUTABLE))
3738 {
3739
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_FLAG_ENABLED(mode, S_IRUSR)) mode |= S_IXUSR;
3740
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_FLAG_ENABLED(mode, S_IRGRP)) mode |= S_IXGRP;
3741
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_FLAG_ENABLED(mode, S_IROTH)) mode |= S_IXOTH;
3742 }
3743 }
3744
3745 /* try to open exclusive and fail, if file existed */
3746 68 fd = open(path, openflags | O_EXCL, mode);
3747
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 23 times.
68 if (fd != -1)
3748 {
3749 int result;
3750 /* open() succeeded */
3751 45 *filehandle = (gate_file_t)(intptr_t)fd;
3752
3753 /* We cannot trust the given mode,
3754 due to umask and multithreading issues,
3755 so let's try to patch permissions.
3756 If it fails, we just go on as this step is optional
3757 */
3758 45 result = fchmod(fd, mode);
3759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if(result != 0)
3760 {
3761 ✗ GATE_DEBUG_TRACE("fchmod() failed");
3762 }
3763 #if !defined(GATE_SYS_WASM_WASI)
3764 /* In case of permission inheritance, we
3765 also need to change owner and group.
3766 Concept: If we are allowed to create a new file in the directory,
3767 then the directory's owner or group will allow the current process
3768 user to access its contents and we can safely change the ownership
3769 of our new created file.
3770 If this fails, we continue as this step is optional
3771 */
3772 45 result = fchown(fd, open_as_uid, open_as_gid);
3773
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if(result != 0)
3774 {
3775 ✗ GATE_DEBUG_TRACE("fchown() failed");
3776 }
3777 #endif
3778
3779 45 ret = GATE_RESULT_OK;
3780 45 break;
3781 }
3782 /* in case of an error, the file may exist,
3783 so continue with the default opening-procedure
3784 */
3785 }
3786
3787 141 mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3788 141 fd = open(path, openflags, mode);
3789
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 136 times.
141 if (fd == -1)
3790 {
3791 /* open() failed */
3792 5 gate_posix_errno(&ret);
3793 5 break;
3794 }
3795
3796 136 *filehandle = (gate_file_t)(intptr_t)fd;
3797 136 ret = GATE_RESULT_OK;
3798 } while (0);
3799
3800 186 return ret;
3801 }
3802 45513 gate_result_t gate_file_read(gate_file_t filehandle, char* buffer, gate_size_t bufferlength, gate_size_t* bufferused)
3803 {
3804 gate_result_t ret;
3805 45513 int fd = (int)(gate_intptr_t)filehandle;
3806 45513 ssize_t returned = read(fd, buffer, bufferlength);
3807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45513 times.
45513 if (returned < 0)
3808 {
3809 ✗ gate_posix_errno(&ret);
3810 }
3811 else
3812 {
3813
1/2
✓ Branch 0 taken 45513 times.
✗ Branch 1 not taken.
45513 if (bufferused != NULL)
3814 {
3815 45513 *bufferused = (gate_size_t)returned;
3816 }
3817 45513 ret = GATE_RESULT_OK;
3818 }
3819 45513 return ret;
3820 }
3821 7393 gate_result_t gate_file_write(gate_file_t filehandle, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
3822 {
3823 gate_result_t ret;
3824 7393 int fd = (int)(gate_intptr_t)filehandle;
3825 7393 ssize_t processed = write(fd, buffer, bufferlength);
3826
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7393 times.
7393 if (processed < 0)
3827 {
3828 ✗ gate_posix_errno(&ret);
3829 }
3830 else
3831 {
3832
1/2
✓ Branch 0 taken 7393 times.
✗ Branch 1 not taken.
7393 if (written != NULL)
3833 {
3834 7393 *written = (gate_size_t)processed;
3835 }
3836 7393 ret = GATE_RESULT_OK;
3837 }
3838 7393 return ret;
3839
3840 }
3841 14 gate_result_t gate_file_flush(gate_file_t filehandle)
3842 {
3843 gate_result_t ret;
3844 14 int fd = (int)(gate_intptr_t)filehandle;
3845
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (fsync(fd) == -1)
3846 {
3847 ✗ gate_posix_errno(&ret);
3848 }
3849 else
3850 {
3851 14 ret = GATE_RESULT_OK;
3852 }
3853 14 return ret;
3854 }
3855 183 gate_result_t gate_file_close(gate_file_t filehandle)
3856 {
3857 gate_result_t ret;
3858 183 int fd = (int)(gate_intptr_t)filehandle;
3859
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 181 times.
183 if (-1 == close(fd))
3860 {
3861 2 gate_posix_errno(&ret);
3862 }
3863 else
3864 {
3865 181 ret = GATE_RESULT_OK;
3866 }
3867 183 return ret;
3868 }
3869 24 gate_result_t gate_file_seek(gate_file_t filehandle, gate_int64_t position, gate_enumint_t origin, gate_int64_t* newposition)
3870 {
3871 gate_result_t ret;
3872 do
3873 {
3874 24 int fd = (int)(gate_intptr_t)filehandle;
3875 off_t offset;
3876 int whence;
3877
3878
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 14 times.
24 if (origin == GATE_FILE_SEEK_ORIGIN_BEGIN) { whence = SEEK_SET; }
3879
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 else if (origin == GATE_FILE_SEEK_ORIGIN_CURPOS) { whence = SEEK_CUR; }
3880
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 else if (origin == GATE_FILE_SEEK_ORIGIN_END) { whence = SEEK_END; }
3881 else
3882 {
3883 ✗ ret = GATE_RESULT_INVALIDARG;
3884 ✗ break;
3885 }
3886 24 offset = lseek(fd, (off_t)position, whence);
3887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (offset == (off_t)-1)
3888 {
3889 ✗ gate_posix_errno(&ret);
3890 ✗ break;
3891 }
3892
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 14 times.
24 if (newposition != NULL)
3893 {
3894 10 *newposition = (gate_int64_t)offset;
3895 }
3896 24 ret = GATE_RESULT_OK;
3897 } while (0);
3898
3899 24 return ret;
3900 }
3901 2 gate_result_t gate_file_pos(gate_file_t filehandle, gate_int64_t* position)
3902 {
3903 gate_result_t ret;
3904 2 int fd = (int)(gate_intptr_t)filehandle;
3905 2 off_t offset = lseek(fd, 0, SEEK_CUR);
3906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (offset == (off_t)-1)
3907 {
3908 ✗ gate_posix_errno(&ret);
3909 }
3910 else
3911 {
3912
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (position != NULL)
3913 {
3914 2 *position = (gate_int64_t)offset;
3915 }
3916 2 ret = GATE_RESULT_OK;
3917 }
3918 2 return ret;
3919 }
3920 4 gate_result_t gate_file_size(gate_file_t filehandle, gate_int64_t* size)
3921 {
3922 gate_result_t ret;
3923 4 int fd = (int)(gate_intptr_t)filehandle;
3924 struct stat filestat;
3925
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (fstat(fd, &filestat) == -1)
3926 {
3927 ✗ gate_posix_errno(&ret);
3928 }
3929 else
3930 {
3931
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (size != NULL)
3932 {
3933 4 *size = (gate_int64_t)filestat.st_size;
3934 }
3935 4 ret = GATE_RESULT_OK;
3936 }
3937 4 return ret;
3938 }
3939
3940 8 gate_result_t gate_file_truncate(gate_file_t filehandle)
3941 {
3942 gate_result_t ret;
3943 8 int fd = (int)(gate_intptr_t)filehandle;
3944 8 off_t offset = lseek(fd, 0, SEEK_CUR);
3945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (offset == (off_t)-1)
3946 {
3947 ✗ gate_posix_errno(&ret);
3948 }
3949 else
3950 {
3951
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (0 != ftruncate(fd, offset))
3952 {
3953 ✗ gate_posix_errno(&ret);
3954 }
3955 else
3956 {
3957 8 ret = GATE_RESULT_OK;
3958 }
3959 }
3960 8 return ret;
3961 }
3962
3963
3964 16 gate_result_t gate_file_lock(gate_file_t filehandle, gate_bool_t wait)
3965 {
3966 16 gate_result_t ret = GATE_RESULT_FAILED;
3967 16 int fd = (int)(gate_intptr_t)filehandle;
3968
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 gate_posix_flock_operation op = wait ? gate_posix_flock_exclusive : gate_posix_flock_exclusive_test;
3969 16 int result = gate_posix_flock(fd, op);
3970
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1 times.
16 if (result == 0)
3971 {
3972 15 ret = GATE_RESULT_OK;
3973 }
3974 16 return ret;
3975 }
3976 15 gate_result_t gate_file_unlock(gate_file_t filehandle)
3977 {
3978 15 gate_result_t ret = GATE_RESULT_FAILED;
3979 15 int fd = (int)(gate_intptr_t)filehandle;
3980 15 int result = gate_posix_flock(fd, gate_posix_flock_unlock);
3981
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (result == 0)
3982 {
3983 15 ret = GATE_RESULT_OK;
3984 }
3985 15 return ret;
3986 }
3987
3988
3989
3990 156 static void gate_unix_mode_to_attributes(mode_t mode, gate_enumint_t* ptr_attribs, gate_enumint_t* ptr_accessbits)
3991 {
3992
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (ptr_accessbits)
3993 {
3994 156 *ptr_accessbits = 0;
3995
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (GATE_FLAG_ENABLED(mode, S_IRUSR)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_OWNERREAD;
3996
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (GATE_FLAG_ENABLED(mode, S_IWUSR)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_OWNERWRITE;
3997
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 10 times.
156 if (GATE_FLAG_ENABLED(mode, S_IXUSR)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_OWNEREXECUTE;
3998
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 7 times.
156 if (GATE_FLAG_ENABLED(mode, S_IRGRP)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_GROUPREAD;
3999
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 51 times.
156 if (GATE_FLAG_ENABLED(mode, S_IWGRP)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_GROUPWRITE;
4000
2/2
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 11 times.
156 if (GATE_FLAG_ENABLED(mode, S_IXGRP)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_GROUPEXECUTE;
4001
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 7 times.
156 if (GATE_FLAG_ENABLED(mode, S_IROTH)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_ALLREAD;
4002
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 55 times.
156 if (GATE_FLAG_ENABLED(mode, S_IWOTH)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_ALLWRITE;
4003
2/2
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 11 times.
156 if (GATE_FLAG_ENABLED(mode, S_IXOTH)) *ptr_accessbits |= GATE_FILEENTRY_ACCESS_ALLEXECUTE;
4004 }
4005
4006
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 14 times.
156 if (ptr_attribs)
4007 {
4008 142 *ptr_attribs = 0;
4009 #if defined(S_IFLNK)
4010
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 41 times.
142 if (GATE_FLAG_ENABLED(mode, S_IFLNK)) *ptr_attribs |= GATE_FILEENTRY_ATTRIB_LINK;
4011 #endif
4012
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 141 times.
142 if (GATE_FLAG_ENABLED(mode, S_IFDIR)) *ptr_attribs |= GATE_FILEENTRY_ATTRIB_DIRECTORY;
4013
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 1 times.
142 if (GATE_FLAG_ENABLED(mode, S_IFREG)) *ptr_attribs |= GATE_FILEENTRY_ATTRIB_FILE;
4014
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 if (GATE_FLAG_ENABLED(mode, S_IFBLK)) *ptr_attribs |= GATE_FILEENTRY_ATTRIB_DEVICE;
4015
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 41 times.
142 if (GATE_FLAG_ENABLED(mode, S_IFCHR)) *ptr_attribs |= GATE_FILEENTRY_ATTRIB_DEVICE;
4016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 if (GATE_FLAG_ENABLED(mode, S_IFIFO)) *ptr_attribs |= GATE_FILEENTRY_ATTRIB_DEVICE;
4017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 if (GATE_FLAG_ENABLED(mode, S_IFSOCK)) *ptr_attribs |= GATE_FILEENTRY_ATTRIB_DEVICE;
4018 }
4019 156 }
4020
4021 140 static gate_bool_t gate_fstat_to_direntry(char const* path, gate_file_entry_t* entry)
4022 {
4023 do
4024 {
4025 struct stat filestat;
4026 140 gate_string_t tmp = GATE_STRING_INIT_EMPTY;
4027
4028
1/2
✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
140 if (0 == lstat(path, &filestat))
4029 {
4030 140 entry->props.size = filestat.st_size;
4031 140 entry->props.time_created = 0;
4032 140 entry->props.time_modified = 0;
4033 140 entry->props.time_accessed = 0;
4034 140 gate_time_from_unix(filestat.st_ctime, &entry->props.time_created); /* this is not the creation time, but may be close to it */
4035 140 gate_time_from_unix(filestat.st_mtime, &entry->props.time_modified);
4036 140 gate_time_from_unix(filestat.st_atime, &entry->props.time_accessed);
4037
4038 140 gate_unix_mode_to_attributes(filestat.st_mode, &entry->props.attribs, &entry->props.access);
4039
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 39 times.
140 if (GATE_FLAG_ENABLED(entry->props.attribs, GATE_FILEENTRY_ATTRIB_LINK))
4040 {
4041
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
101 if (0 != stat(path, &filestat))
4042 {
4043 ✗ break;
4044 }
4045 }
4046 140 gate_string_create_static(&tmp, path);
4047
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
140 if (gate_file_is_hidden_entry(&tmp))
4048 {
4049 ✗ entry->props.attribs |= GATE_FILEENTRY_ATTRIB_HIDDEN;
4050 }
4051
4052
4053 140 return true;
4054 }
4055 } while (0);
4056 ✗ return false;
4057 }
4058
4059 #if 0
4060 static gate_bool_t gate_readdir(DIR* dirp, struct dirent* entry)
4061 {
4062 struct dirent* returned_entry = NULL;
4063 int result = readdir_r(dirp, entry, &returned_entry);
4064 if (result == 0)
4065 {
4066 return entry != NULL;
4067 }
4068 return false;
4069 }
4070 #else
4071 225 static gate_bool_t gate_readdir(DIR* dirp, struct dirent* entry)
4072 {
4073 225 struct dirent* ptr_entry = readdir(dirp);
4074
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 13 times.
225 if (ptr_entry)
4075 {
4076 212 gate_mem_copy(entry, ptr_entry, sizeof(struct dirent));
4077 212 return true;
4078 }
4079 13 return false;
4080 }
4081 #endif
4082
4083 9 gate_result_t gate_file_list(gate_string_t const* dirpath, gate_file_list_callback_t callback, void* userparam)
4084 {
4085 gate_result_t ret;
4086 9 DIR* dir = NULL;
4087
4088 do
4089 {
4090 char path[GATE_MAX_FILEPATH_LENGTH];
4091 char file[GATE_MAX_FILEPATH_LENGTH];
4092 gate_size_t namepos;
4093 9 struct dirent entry = GATE_INIT_EMPTY;
4094
4095
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
9 if (gate_string_is_empty(dirpath) || callback == NULL)
4096 {
4097 ✗ ret = GATE_RESULT_INVALIDARG;
4098 ✗ break;
4099 }
4100
4101 9 gate_string_to_buffer(dirpath, path, sizeof(path));
4102 9 namepos = gate_string_to_buffer(dirpath, file, sizeof(file));
4103
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (file[namepos - 1] != gate_file_path_separator_char)
4104 {
4105 6 file[namepos] = gate_file_path_separator_char;
4106 6 ++namepos;
4107 }
4108
4109 9 dir = opendir(path);
4110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (NULL == dir)
4111 {
4112 ✗ ret = GATE_RESULT_FAILED;
4113 ✗ break;
4114 }
4115
4116 9 ret = GATE_RESULT_OK;
4117
2/2
✓ Branch 1 taken 157 times.
✓ Branch 2 taken 6 times.
163 while (gate_readdir(dir, &entry))
4118 {
4119 gate_size_t namelen;
4120 gate_file_entry_t direntry;
4121
4122
4/4
✓ Branch 1 taken 148 times.
✓ Branch 2 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 139 times.
157 if ((gate_str_comp(entry.d_name, "..") == 0) || (gate_str_comp(entry.d_name, ".") == 0))
4123 {
4124 18 continue;
4125 }
4126
4127 139 namelen = gate_str_print_text(&file[namepos], sizeof(file) - namepos, entry.d_name, gate_str_length(entry.d_name));
4128 139 gate_str_print_text(direntry.name, sizeof(direntry.name), entry.d_name, gate_str_length(entry.d_name));
4129 139 gate_str_print_text(direntry.path, sizeof(direntry.path), file, namepos + namelen);
4130 139 direntry.props.size = 0;
4131 139 direntry.props.attribs = 0;
4132 139 direntry.props.time_created = 0;
4133 139 direntry.props.time_modified = 0;
4134 139 direntry.props.time_accessed = 0;
4135 139 gate_fstat_to_direntry(direntry.path, &direntry);
4136
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 136 times.
139 if (!callback(&direntry, userparam))
4137 {
4138 3 break;
4139 }
4140 }
4141 } while (0);
4142
4143
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (dir != NULL)
4144 {
4145 9 closedir(dir);
4146 }
4147 9 return ret;
4148 }
4149
4150 14 gate_result_t gate_file_dir_open(gate_string_t const* dirpath, gate_file_dirreader_t* dirhandle)
4151 {
4152 14 gate_result_t ret = GATE_RESULT_FAILED;
4153 do
4154 {
4155 14 gate_mem_clear(dirhandle, sizeof(gate_file_dirreader_t));
4156 14 dirhandle->bufferlen = gate_str_print_text(dirhandle->buffer, sizeof(dirhandle->buffer),
4157 gate_string_ptr(dirpath, 0), gate_string_length(dirpath));
4158
4159 14 dirhandle->handle = opendir(dirhandle->buffer);
4160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (dirhandle->handle == NULL)
4161 {
4162 ✗ gate_posix_errno(&ret);
4163 }
4164 else
4165 {
4166 14 ret = GATE_RESULT_OK;
4167 }
4168
4169 } while (0);
4170
4171 14 return ret;
4172 }
4173 34 gate_size_t gate_file_dir_read(gate_file_dirreader_t* dirhandle, char* path, gate_size_t pathlen, gate_bool_t filename_only)
4174 {
4175 34 struct dirent entry = GATE_INIT_EMPTY;
4176 34 gate_size_t ret = 0;
4177
2/2
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 7 times.
62 while (gate_readdir((DIR*)dirhandle->handle, &entry))
4178 {
4179 55 gate_size_t name_len = gate_str_length(entry.d_name);
4180
4/4
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 14 times.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 14 times.
55 if ((gate_str_compare(entry.d_name, name_len, ".", 1) != 0) && (gate_str_compare(entry.d_name, name_len, "..", 2) != 0))
4181 {
4182
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 26 times.
27 if (filename_only)
4183 {
4184 1 ret = gate_str_print_text(path, pathlen, entry.d_name, name_len);
4185 }
4186 else
4187 {
4188 26 ret = gate_file_build_path(path, pathlen,
4189 26 dirhandle->buffer, dirhandle->bufferlen,
4190 entry.d_name, gate_str_length_max(entry.d_name, NAME_MAX));
4191 }
4192
4193
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (ret > 0)
4194 {
4195 27 break;
4196 }
4197 }
4198 }
4199 34 return ret;
4200 }
4201 14 gate_result_t gate_file_dir_close(gate_file_dirreader_t* dirhandle)
4202 {
4203 14 gate_result_t ret = GATE_RESULT_OK;
4204 14 closedir((DIR*)dirhandle->handle);
4205 14 return ret;
4206 }
4207
4208 3 gate_result_t gate_file_root_list(gate_file_list_callback_t callback, void* userparam)
4209 {
4210 3 gate_result_t ret = GATE_RESULT_OK;
4211 gate_file_entry_t direntry;
4212 3 gate_mem_clear(&direntry, sizeof(direntry));
4213 3 gate_str_print_text(direntry.name, sizeof(direntry.name), "/", 1);
4214 3 gate_str_print_text(direntry.path, sizeof(direntry.path), "/", 1);
4215
4216 3 direntry.props.attribs = GATE_FILEENTRY_ATTRIB_DIRECTORY | GATE_FILEENTRY_ATTRIB_VOLUME;
4217
4218 3 callback(&direntry, userparam);
4219
4220 3 return ret;
4221 }
4222
4223 1 gate_result_t gate_file_get_entry(gate_string_t const* filepath, gate_file_entry_t* ptr_entry)
4224 {
4225 gate_result_t ret;
4226
4227 do
4228 {
4229 char path[GATE_MAX_FILEPATH_LENGTH];
4230 1 gate_size_t pathlength = gate_string_to_buffer(filepath, path, sizeof(path));
4231 gate_size_t pos;
4232
4233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pathlength == 0)
4234 {
4235 ✗ ret = GATE_RESULT_INVALIDARG;
4236 ✗ break;
4237 }
4238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (path[pathlength - 1] == gate_file_path_separator_char)
4239 {
4240 ✗ path[--pathlength] = '\0';
4241 }
4242 1 pos = gate_str_char_pos_last(path, pathlength, gate_file_path_separator_char);
4243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pos == GATE_STR_NPOS)
4244 {
4245 ✗ pos = 0;
4246 }
4247 else
4248 {
4249 1 ++pos;
4250 }
4251 1 gate_mem_clear(ptr_entry, sizeof(gate_file_entry_t));
4252
4253 1 gate_mem_copy(ptr_entry->path, path, pathlength);
4254 1 ptr_entry->path[pathlength] = 0;
4255 1 gate_str_print_text(ptr_entry->name, sizeof(ptr_entry->name), &ptr_entry->path[pos], pathlength - pos);
4256
4257
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!gate_fstat_to_direntry(path, ptr_entry))
4258 {
4259 ✗ ret = GATE_RESULT_FAILED;
4260 ✗ break;
4261 }
4262 1 ret = GATE_RESULT_OK;
4263 } while (0);
4264
4265 1 return ret;
4266 }
4267 8 gate_result_t gate_file_dir_create(gate_string_t const* dirpath, gate_enumint_t flags)
4268 {
4269 gate_result_t ret;
4270
4271 do
4272 {
4273 char path[GATE_MAX_FILEPATH_LENGTH];
4274 8 gate_size_t pathlen = gate_string_to_buffer(dirpath, path, sizeof(path));
4275 8 mode_t mode = 0;
4276 8 uid_t uid = 0;
4277 8 gid_t gid = 0;
4278 8 gate_bool_t parent_stat = false;
4279
4280
3/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 5 times.
8 if ((pathlen != 0) && (path[pathlen - 1] == '/'))
4281 {
4282 3 path[--pathlen] = 0;
4283 }
4284
4285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (GATE_FLAG_ENABLED(flags, GATE_FILE_DIR_CREATE_OWNERRESTRICTED))
4286 {
4287 ✗ mode = (S_IRUSR | S_IWUSR | S_IXUSR);
4288 }
4289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 else if (GATE_FLAG_ENABLED(flags, GATE_FILE_DIR_CREATE_GROUPRESTRICTED))
4290 {
4291 ✗ mode = (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP);
4292 }
4293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 else if (GATE_FLAG_ENABLED(flags, GATE_FILE_DIR_CREATE_UNRESTRICTED))
4294 {
4295 ✗ mode = (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
4296 }
4297 else
4298 {
4299 /* default */
4300 8 parent_stat = gate_file_parent_stat(dirpath, &mode, &uid, &gid);
4301
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 if (!parent_stat)
4302 {
4303 3 mode = 0;
4304 }
4305 8 mode |= (S_IRUSR | S_IWUSR | S_IXUSR);
4306 }
4307
4308
4309
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (0 != mkdir(path, mode))
4310 {
4311 ✗ gate_posix_errno(&ret);
4312 ✗ break;
4313 }
4314
4315 8 ret = GATE_RESULT_OK;
4316
4317
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 if (parent_stat)
4318 {
4319 int result;
4320 5 result = chmod(path, mode);
4321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (result != 0)
4322 {
4323 ✗ GATE_DEBUG_TRACE("chmod() failed");
4324 }
4325 #if !defined(GATE_SYS_WASM_WASI)
4326 5 result = chown(path, uid, gid);
4327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (result != 0)
4328 {
4329 ✗ GATE_DEBUG_TRACE("chown() failed");
4330 }
4331 #endif
4332 (void)result;
4333 }
4334 } while (0);
4335
4336 8 return ret;
4337 }
4338 6 gate_result_t gate_file_dir_delete(gate_string_t const* dirpath)
4339 {
4340 gate_result_t ret;
4341 char path[GATE_MAX_FILEPATH_LENGTH];
4342
4343 6 gate_string_to_buffer(dirpath, path, sizeof(path));
4344
4345
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (0 != rmdir(path))
4346 {
4347 ✗ gate_posix_errno(&ret);
4348 }
4349 else
4350 {
4351 6 ret = GATE_RESULT_OK;
4352 }
4353 6 return ret;
4354 }
4355
4356 16 gate_result_t gate_file_get_attributes(gate_string_t const* targetfile, gate_enumint_t* attribs, gate_enumint_t* access_bits)
4357 {
4358 gate_result_t ret;
4359 char path[GATE_MAX_FILEPATH_LENGTH];
4360 struct stat filestat;
4361 16 gate_string_to_buffer(targetfile, path, sizeof(path));
4362
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (0 != lstat(path, &filestat))
4363 {
4364 ✗ ret = GATE_RESULT_FAILED;
4365 }
4366 else
4367 {
4368 16 ret = GATE_RESULT_OK;
4369 16 gate_unix_mode_to_attributes(filestat.st_mode, attribs, access_bits);
4370
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
16 if (attribs)
4371 {
4372
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (gate_file_is_hidden_entry(targetfile))
4373 {
4374 ✗ *attribs |= GATE_FILEENTRY_ATTRIB_HIDDEN;
4375 }
4376 }
4377 }
4378 16 return ret;
4379 }
4380
4381 ✗ gate_result_t gate_file_get_size(gate_string_t const* targetfile, gate_int64_t* filesize)
4382 {
4383 gate_result_t ret;
4384 char path[GATE_MAX_FILEPATH_LENGTH];
4385 struct stat filestat;
4386 ✗ gate_string_to_buffer(targetfile, path, sizeof(path));
4387 ✗ if (0 != lstat(path, &filestat))
4388 {
4389 ✗ ret = GATE_RESULT_FAILED;
4390 }
4391 else
4392 {
4393 ✗ ret = GATE_RESULT_OK;
4394 ✗ if (filesize)
4395 {
4396 ✗ *filesize = (gate_int64_t)filestat.st_size;
4397 }
4398 }
4399 ✗ return ret;
4400 }
4401
4402 ✗ gate_result_t gate_file_get_owner(gate_string_t const* targetpath,
4403 gate_uint32_t* owner_id, gate_string_t* owner_name)
4404 {
4405 gate_result_t ret;
4406 char path[GATE_MAX_FILEPATH_LENGTH];
4407 struct stat filestat;
4408
4409 ✗ gate_string_to_buffer(targetpath, path, sizeof(path));
4410 ✗ if (0 != lstat(path, &filestat))
4411 {
4412 ✗ ret = GATE_RESULT_FAILED;
4413 }
4414 else
4415 {
4416 ✗ if (NULL != owner_id)
4417 {
4418 ✗ *owner_id = (gate_uint32_t)filestat.st_uid;
4419 }
4420 ✗ if (NULL != owner_name)
4421 {
4422 ✗ gate_posix_user_info_t user_info = GATE_INIT_EMPTY;
4423 ✗ ret = gate_posix_get_user_info(filestat.st_uid, &user_info);
4424 ✗ if (GATE_SUCCEEDED(ret))
4425 {
4426 ✗ gate_string_create(owner_name, user_info.name, gate_str_length(user_info.name));
4427 }
4428 else
4429 {
4430 ✗ gate_string_create_empty(owner_name);
4431 }
4432 }
4433 ✗ ret = GATE_RESULT_OK;
4434 }
4435 ✗ return ret;
4436 }
4437
4438
4439 #define GATE_FILE_MODE_SET(mode_var, mask, attribs, attrib, native_mode) \
4440 do { \
4441 if(GATE_FLAG_ENABLED(mask, attrib)) { \
4442 if(GATE_FLAG_ENABLED(attribs, attrib)) { \
4443 mode_var |= native_mode; \
4444 } \
4445 else { \
4446 mode_var &= ~native_mode; \
4447 } \
4448 } \
4449 } while(0)
4450
4451 ✗ gate_result_t gate_file_set_attributes(gate_string_t const* targetfile,
4452 gate_enumint_t attribs, gate_enumint_t attribs_mask,
4453 gate_enumint_t access_bits, gate_enumint_t access_mask)
4454 {
4455 gate_result_t ret;
4456 char path[GATE_MAX_FILEPATH_LENGTH];
4457 struct stat filestat;
4458
4459 ✗ gate_string_to_buffer(targetfile, path, sizeof(path));
4460 ✗ if (0 != lstat(path, &filestat))
4461 {
4462 ✗ ret = GATE_RESULT_FAILED;
4463 }
4464 else
4465 {
4466 ✗ mode_t mode = filestat.st_mode;
4467 ✗ if (access_mask != 0)
4468 {
4469 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_OWNERREAD, S_IRUSR);
4470 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_OWNERWRITE, S_IWUSR);
4471 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_OWNEREXECUTE, S_IXUSR);
4472 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_OWNERSETID, S_ISUID);
4473 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_GROUPREAD, S_IRGRP);
4474 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_GROUPWRITE, S_IWGRP);
4475 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_GROUPEXECUTE, S_IXGRP);
4476 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_GROUPSETID, S_ISGID);
4477 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_ALLREAD, S_IROTH);
4478 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_ALLWRITE, S_IWOTH);
4479 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_ALLEXECUTE, S_IXOTH);
4480 ✗ GATE_FILE_MODE_SET(mode, access_mask, access_bits, GATE_FILEENTRY_ACCESS_NODELETE, S_ISVTX);
4481 }
4482
4483 ✗ if (0 != chmod(path, mode))
4484 {
4485 ✗ ret = GATE_RESULT_FAILED;
4486 }
4487 else
4488 {
4489 ✗ ret = GATE_RESULT_OK;
4490 }
4491 }
4492 ✗ return ret;
4493 }
4494
4495 ✗ gate_result_t gate_file_get_times(gate_string_t const* targetfile, gate_timestamp_t* modified,
4496 gate_timestamp_t* accessed, gate_timestamp_t* created)
4497 {
4498 gate_result_t ret;
4499 char path[GATE_MAX_FILEPATH_LENGTH];
4500 struct stat filestat;
4501
4502 ✗ gate_string_to_buffer(targetfile, path, sizeof(path));
4503
4504 ✗ if (0 != lstat(path, &filestat))
4505 {
4506 ✗ ret = GATE_RESULT_FAILED;
4507 }
4508 else
4509 {
4510 ✗ ret = GATE_RESULT_OK;
4511 ✗ if (created) gate_time_from_unix(filestat.st_ctime, created);
4512 ✗ if (modified)gate_time_from_unix(filestat.st_mtime, modified);
4513 ✗ if (accessed)gate_time_from_unix(filestat.st_atime, accessed);
4514 }
4515 ✗ return ret;
4516 }
4517 ✗ gate_result_t gate_file_set_times(gate_string_t const* targetfile, gate_timestamp_t const* modified,
4518 gate_timestamp_t const* accessed, gate_timestamp_t const* created)
4519 {
4520 gate_result_t ret;
4521 char path[GATE_MAX_FILEPATH_LENGTH];
4522 struct utimbuf ft;
4523 ✗ gate_timestamp_t orig_modified = 0, orig_accessed = 0;
4524 ✗ gate_int64_t accesstime = 0;
4525 ✗ gate_int64_t modifiedtime = 0;
4526
4527 ✗ if (!modified || !accessed)
4528 {
4529 ✗ gate_file_get_times(targetfile, &orig_modified, &orig_accessed, NULL);
4530 ✗ if (!modified) modified = &orig_modified;
4531 ✗ if (!accessed) accessed = &orig_accessed;
4532 }
4533
4534 ✗ gate_time_to_unix(*modified, &modifiedtime);
4535 ✗ gate_time_to_unix(*accessed, &accesstime);
4536
4537 ✗ gate_string_to_buffer(targetfile, path, sizeof(path));
4538
4539 ✗ ft.actime = (time_t)accesstime;
4540 ✗ ft.modtime = (time_t)modifiedtime;
4541 ✗ if (0 != utime(path, &ft))
4542 {
4543 ✗ ret = GATE_RESULT_FAILED;
4544 }
4545 else
4546 {
4547 ✗ ret = GATE_RESULT_OK;
4548 }
4549 ✗ return ret;
4550 }
4551
4552 #endif /* GATE_CORE_FILES_POSIX_IMPL */
4553
4554
4555
4556 #if defined(GATE_CORE_FILES_DOS_IMPL)
4557
4558 #include "gate/platforms.h"
4559 #include <dos.h>
4560 #include <io.h>
4561 #include <direct.h>
4562
4563 char const gate_file_path_separator_char = '\\';
4564
4565 char const* const gate_file_path_separator = "\\";
4566
4567 gate_result_t gate_file_exists(gate_string_t const* filepath)
4568 {
4569 gate_result_t ret = GATE_RESULT_FAILED;
4570
4571 do
4572 {
4573 char path[GATE_MAX_FILEPATH_LENGTH];
4574 struct find_t f;
4575 unsigned result;
4576 gate_string_to_buffer(filepath, path, sizeof(path));
4577 result = _dos_findfirst(path, _A_NORMAL | _A_RDONLY | _A_HIDDEN | _A_SYSTEM, &f);
4578 if (0 != result)
4579 {
4580 ret = GATE_RESULT_NOTAVAILABLE;
4581 break;
4582 }
4583 ret = GATE_RESULT_OK;
4584 } while (0);
4585 return ret;
4586 }
4587 gate_result_t gate_file_copy(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath, gate_enumint_t flags)
4588 {
4589 return gate_file_copy_internal(srcfilepath, dstfilepath, flags);
4590 }
4591 gate_result_t gate_file_move(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath)
4592 {
4593 return gate_file_move_internal(srcfilepath, dstfilepath);
4594 }
4595 gate_result_t gate_file_delete(gate_string_t const* filepath)
4596 {
4597 union REGS regs;
4598 struct SREGS segregs;
4599 char path[GATE_MAX_FILEPATH_LENGTH];
4600 gate_string_to_buffer(filepath, path, sizeof(path));
4601
4602 segregs.ds = segregs.es = FP_SEG(&path[0]);
4603 regs.h.ah = 0x41;
4604 regs.w.bx = 0;
4605 regs.w.cx = 0;
4606 regs.w.dx = FP_OFF(&path[0]);
4607
4608 intdosx(&regs, &regs, &segregs);
4609 if (regs.w.cflag & INTR_CF)
4610 {
4611 return GATE_RESULT_FAILED;
4612 }
4613 return GATE_RESULT_OK;
4614
4615 /*
4616 if(0 == unlink(path))
4617 {
4618 return GATE_RESULT_OK;
4619 }
4620 return GATE_RESULT_FAILED;
4621 */
4622 }
4623 gate_result_t gate_file_real_path(gate_string_t const* relativepath, gate_string_t* absolutepath)
4624 {
4625 /* TODO */
4626 GATE_UNUSED_ARG(relativepath);
4627 GATE_UNUSED_ARG(absolutepath);
4628 return GATE_RESULT_NOTSUPPORTED;
4629 }
4630 gate_result_t gate_file_create_link(gate_string_t const* targetpath, gate_string_t const* linkfile)
4631 {
4632 GATE_UNUSED_ARG(targetpath);
4633 GATE_UNUSED_ARG(linkfile);
4634 return GATE_RESULT_NOTSUPPORTED;
4635 }
4636 gate_result_t gate_file_read_link(gate_string_t const* filepath, gate_string_t* realpath)
4637 {
4638 GATE_UNUSED_ARG(filepath);
4639 GATE_UNUSED_ARG(realpath);
4640 return GATE_RESULT_NOTSUPPORTED;
4641 }
4642 gate_result_t gate_file_create_hardlink(gate_string_t const* targetfile, gate_string_t const* linkfile)
4643 {
4644 GATE_UNUSED_ARG(targetfile);
4645 GATE_UNUSED_ARG(linkfile);
4646 return GATE_RESULT_NOTSUPPORTED;
4647 }
4648 gate_result_t gate_file_get_attributes(gate_string_t const* targetpath, gate_enumint_t* attribs, gate_enumint_t* access_bits)
4649 {
4650 gate_result_t ret = GATE_RESULT_FAILED;
4651
4652 do
4653 {
4654 struct find_t f;
4655 char path[GATE_MAX_FILEPATH_LENGTH];
4656 unsigned result;
4657
4658 gate_string_to_buffer(targetpath, path, sizeof(path));
4659
4660 result = _dos_findfirst(path, _A_NORMAL | _A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR | _A_ARCH, &f);
4661 if (0 != result)
4662 {
4663 ret = GATE_RESULT_NOTAVAILABLE;
4664 break;
4665 }
4666 if (attribs)
4667 {
4668 *attribs = 0;
4669 if (GATE_FLAG_ENABLED(f.attrib, _A_SUBDIR)) *attribs |= GATE_FILEENTRY_ATTRIB_DIRECTORY;
4670 else *attribs |= GATE_FILEENTRY_ATTRIB_FILE;
4671 if (GATE_FLAG_ENABLED(f.attrib, _A_RDONLY)) *attribs |= GATE_FILEENTRY_ATTRIB_READONLY;
4672 if (GATE_FLAG_ENABLED(f.attrib, _A_HIDDEN)) *attribs |= GATE_FILEENTRY_ATTRIB_HIDDEN;
4673 if (GATE_FLAG_ENABLED(f.attrib, _A_SYSTEM)) *attribs |= GATE_FILEENTRY_ATTRIB_SYSTEM;
4674 if (GATE_FLAG_ENABLED(f.attrib, _A_ARCH)) *attribs |= GATE_FILEENTRY_ATTRIB_ARCHIVE;
4675
4676 }
4677 if (access_bits)
4678 {
4679 gate_bool_t is_readonly = GATE_FLAG_ENABLED(f.attrib, _A_RDONLY);
4680 gate_bool_t is_executable = gate_string_ends_with_str_ic(targetpath, ".exe")
4681 || gate_string_ends_with_str_ic(targetpath, ".com")
4682 || gate_string_ends_with_str_ic(targetpath, ".bat");
4683
4684 /* all access granted */
4685 *access_bits = GATE_FILEENTRY_ACCESS_OWNERREAD | GATE_FILEENTRY_ACCESS_GROUPREAD | GATE_FILEENTRY_ACCESS_ALLREAD;
4686 if (!is_readonly)
4687 {
4688 *access_bits |= GATE_FILEENTRY_ACCESS_OWNERWRITE | GATE_FILEENTRY_ACCESS_GROUPWRITE | GATE_FILEENTRY_ACCESS_ALLWRITE;
4689 }
4690 if (is_executable)
4691 {
4692 *access_bits |= GATE_FILEENTRY_ACCESS_OWNEREXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_ALLEXECUTE;
4693 }
4694 }
4695
4696 ret = GATE_RESULT_OK;
4697 } while (0);
4698 return ret;
4699 }
4700 gate_result_t gate_file_set_attributes(gate_string_t const* targetpath,
4701 gate_enumint_t attribs, gate_enumint_t attribs_mask,
4702 gate_enumint_t access_bits, gate_enumint_t access_mask)
4703 {
4704 char path[GATE_MAX_FILEPATH_LENGTH];
4705
4706 GATE_UNUSED_ARG(access_bits);
4707 GATE_UNUSED_ARG(access_mask);
4708
4709 gate_string_to_buffer(targetpath, path, sizeof(path));
4710
4711 if (attribs_mask)
4712 {
4713 unsigned dosattribs = 0;
4714 unsigned result = _dos_getfileattr(path, &dosattribs);
4715 if (result != 0)
4716 {
4717 return GATE_RESULT_NOTAVAILABLE;
4718 }
4719
4720 if (GATE_FLAG_ENABLED(attribs_mask, GATE_FILEENTRY_ATTRIB_READONLY)) GATE_FLAG_SET(dosattribs, _A_RDONLY, GATE_FLAG_ENABLED(attribs, GATE_FILEENTRY_ATTRIB_READONLY));
4721 if (GATE_FLAG_ENABLED(attribs_mask, GATE_FILEENTRY_ATTRIB_HIDDEN)) GATE_FLAG_SET(dosattribs, _A_HIDDEN, GATE_FLAG_ENABLED(attribs, GATE_FILEENTRY_ATTRIB_HIDDEN));
4722 if (GATE_FLAG_ENABLED(attribs_mask, GATE_FILEENTRY_ATTRIB_SYSTEM)) GATE_FLAG_SET(dosattribs, _A_SYSTEM, GATE_FLAG_ENABLED(attribs, GATE_FILEENTRY_ATTRIB_SYSTEM));
4723 if (GATE_FLAG_ENABLED(attribs_mask, GATE_FILEENTRY_ATTRIB_ARCHIVE)) GATE_FLAG_SET(dosattribs, _A_ARCH, GATE_FLAG_ENABLED(attribs, GATE_FILEENTRY_ATTRIB_ARCHIVE));
4724
4725 result = _dos_setfileattr(path, dosattribs);
4726 if (result != 0)
4727 {
4728 return GATE_RESULT_FAILED;
4729 }
4730 }
4731
4732 return GATE_RESULT_OK;
4733 }
4734
4735 typedef struct {
4736 unsigned short twosecs : 5; /* seconds / 2 */
4737 unsigned short minutes : 6; /* minutes (0,59) */
4738 unsigned short hours : 5; /* hours (0,23) */
4739 } ftime_t;
4740
4741 typedef struct {
4742 unsigned short day : 5; /* day (1,31) */
4743 unsigned short month : 4; /* month (1,12) */
4744 unsigned short year : 7; /* 0 is 1980 */
4745 } fdate_t;
4746
4747 void gate_file_convert_time(unsigned short fdate, unsigned short ftime, gate_timestamp_t* ts)
4748 {
4749 fdate_t* ptr_date = (fdate_t*)&fdate;
4750 ftime_t* ptr_time = (ftime_t*)&ftime;
4751 gate_datetime_t dt;
4752 gate_time_t tm;
4753
4754 dt.date.year = 1980 + ptr_date->year;
4755 dt.date.month = ptr_date->month;
4756 dt.date.day = ptr_date->day;
4757 dt.time.hour = ptr_time->hours;
4758 dt.time.minute = ptr_time->minutes;
4759 dt.time.second = ptr_time->twosecs * 2;
4760 dt.time.microsecond = 0;
4761 gate_date_to_time(&dt, &tm);
4762 if (ts)
4763 {
4764 *ts = tm.timestamp;
4765 }
4766 }
4767
4768 gate_result_t gate_file_get_times(gate_string_t const* targetpath, gate_timestamp_t* modified, gate_timestamp_t* accessed, gate_timestamp_t* created)
4769 {
4770 gate_result_t ret = GATE_RESULT_FAILED;
4771
4772 do
4773 {
4774 struct find_t f;
4775 char path[GATE_MAX_FILEPATH_LENGTH];
4776 unsigned result;
4777
4778 gate_string_to_buffer(targetpath, path, sizeof(path));
4779
4780 result = _dos_findfirst(path, _A_NORMAL | _A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR | _A_ARCH, &f);
4781 if (0 != result)
4782 {
4783 ret = GATE_RESULT_NOTAVAILABLE;
4784 break;
4785 }
4786 if (modified)
4787 {
4788 gate_file_convert_time(f.wr_date, f.wr_time, modified);
4789 }
4790 if (accessed)
4791 {
4792 gate_file_convert_time(f.wr_date, f.wr_time, accessed);
4793 }
4794 if (created)
4795 {
4796 *created = 0;
4797 }
4798 ret = GATE_RESULT_OK;
4799 } while (0);
4800 return ret;
4801 }
4802 gate_result_t gate_file_set_times(gate_string_t const* targetpath, gate_timestamp_t const* modified, gate_timestamp_t const* accessed, gate_timestamp_t const* created)
4803 {
4804 GATE_UNUSED_ARG(targetpath);
4805 GATE_UNUSED_ARG(modified);
4806 GATE_UNUSED_ARG(accessed);
4807 GATE_UNUSED_ARG(created);
4808 /* TODO */
4809 return GATE_RESULT_NOTSUPPORTED;
4810 }
4811 gate_result_t gate_file_get_size(gate_string_t const* targetfile, gate_int64_t* filesize)
4812 {
4813 gate_result_t ret = GATE_RESULT_FAILED;
4814
4815 do
4816 {
4817 struct find_t f;
4818 char path[GATE_MAX_FILEPATH_LENGTH];
4819 unsigned result;
4820
4821 gate_string_to_buffer(targetfile, path, sizeof(path));
4822
4823 result = _dos_findfirst(path, _A_NORMAL | _A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_ARCH, &f);
4824 if (0 != result)
4825 {
4826 ret = GATE_RESULT_NOTAVAILABLE;
4827 break;
4828 }
4829 if (filesize)
4830 {
4831 *filesize = (gate_int64_t)f.size;
4832 }
4833 ret = GATE_RESULT_OK;
4834 } while (0);
4835 return ret;
4836 }
4837 gate_result_t gate_file_get_owner(gate_string_t const* targetfile,
4838 gate_uint32_t* owner_id, gate_string_t* owner_name)
4839 {
4840 GATE_UNUSED_ARG(targetfile);
4841 GATE_UNUSED_ARG(owner_id);
4842 GATE_UNUSED_ARG(owner_name);
4843 return GATE_RESULT_NOTSUPPORTED;
4844 }
4845
4846 gate_result_t gate_file_open(gate_string_t const* filepath, gate_enumint_t flags, gate_file_t* filehandle)
4847 {
4848 gate_result_t ret;
4849 int platform_flags;
4850 gate_bool_t seek_end = false;
4851 gate_platform_stream_t stream;
4852
4853 switch (flags)
4854 {
4855 case GATE_STREAM_OPEN_READ:
4856 platform_flags = GATE_PLATFORM_STREAM_OPEN_READ;
4857 break;
4858 case GATE_STREAM_OPEN_WRITE:
4859 platform_flags = GATE_PLATFORM_STREAM_OPEN_WRITE;
4860 break;
4861 case GATE_STREAM_OPEN_READWRITE:
4862 platform_flags = GATE_PLATFORM_STREAM_OPEN_READWRITE;
4863 break;
4864 case GATE_STREAM_OPEN_APPENDWRITE:
4865 case GATE_STREAM_OPEN_APPENDREADWRITE:
4866 platform_flags = GATE_PLATFORM_STREAM_OPEN_READWRITE;
4867 seek_end = true;
4868 break;
4869 default:
4870 return GATE_RESULT_INVALIDARG;
4871 }
4872
4873 do
4874 {
4875 char path[GATE_MAX_FILEPATH_LENGTH];
4876 gate_string_to_buffer(filepath, path, sizeof(path));
4877 ret = gate_platform_stream_open(path, platform_flags, 0, &stream);
4878 GATE_BREAK_IF_FAILED(ret);
4879
4880 *filehandle = (gate_file_t)stream;
4881 ret = GATE_RESULT_OK;
4882 } while (0);
4883 return ret;
4884 }
4885 gate_result_t gate_file_read(gate_file_t filehandle, char* buffer, gate_size_t bufferlength, gate_size_t* bufferused)
4886 {
4887 gate_platform_stream_t stream = (gate_platform_stream_t)filehandle;
4888 return gate_platform_stream_read(stream, buffer, bufferlength, bufferused);
4889 }
4890 gate_result_t gate_file_write(gate_file_t filehandle, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
4891 {
4892 gate_platform_stream_t stream = (gate_platform_stream_t)filehandle;
4893 return gate_platform_stream_write(stream, buffer, bufferlength, written);
4894 }
4895 gate_result_t gate_file_flush(gate_file_t filehandle)
4896 {
4897 int handle = (int)(gate_intptr_t)(gate_platform_stream_t)filehandle;
4898 unsigned result = _dos_commit(handle);
4899 if (result != 0)
4900 {
4901 return GATE_RESULT_FAILED;
4902 }
4903 else
4904 {
4905 return GATE_RESULT_OK;
4906 }
4907 }
4908 gate_result_t gate_file_close(gate_file_t filehandle)
4909 {
4910 gate_platform_stream_t stream = (gate_platform_stream_t)filehandle;
4911 return gate_platform_stream_close(&stream);
4912 }
4913 gate_result_t gate_file_seek(gate_file_t filehandle, gate_int64_t position, gate_enumint_t origin, gate_int64_t* newposition)
4914 {
4915 gate_platform_stream_t stream = (gate_platform_stream_t)filehandle;
4916 int native_origin = GATE_PLATFORM_STREAM_ORIGIN_BEGIN;
4917 switch (origin)
4918 {
4919 case GATE_FILE_SEEK_ORIGIN_BEGIN: native_origin = GATE_PLATFORM_STREAM_ORIGIN_BEGIN; break;
4920 case GATE_FILE_SEEK_ORIGIN_CURPOS: native_origin = GATE_PLATFORM_STREAM_ORIGIN_CURRENT; break;
4921 case GATE_FILE_SEEK_ORIGIN_END: native_origin = GATE_PLATFORM_STREAM_ORIGIN_END; break;
4922 default: return GATE_RESULT_INVALIDARG;
4923 }
4924 return gate_platform_stream_seek(stream, position, native_origin, newposition);
4925 }
4926 gate_result_t gate_file_pos(gate_file_t filehandle, gate_int64_t* position)
4927 {
4928 gate_platform_stream_t stream = (gate_platform_stream_t)filehandle;
4929 return gate_platform_stream_seek(stream, 0, GATE_PLATFORM_STREAM_ORIGIN_CURRENT, position);
4930 }
4931 gate_result_t gate_file_size(gate_file_t filehandle, gate_int64_t* size)
4932 {
4933 gate_result_t ret = GATE_RESULT_FAILED;
4934 gate_platform_stream_t stream = (gate_platform_stream_t)filehandle;
4935 gate_int64_t cur_pos = 0;
4936 do
4937 {
4938 ret = gate_platform_stream_seek(stream, 0, GATE_PLATFORM_STREAM_ORIGIN_CURRENT, &cur_pos);
4939 GATE_BREAK_IF_FAILED(ret);
4940
4941 ret = gate_platform_stream_seek(stream, 0, GATE_PLATFORM_STREAM_ORIGIN_END, size);
4942 gate_platform_stream_seek(stream, cur_pos, GATE_PLATFORM_STREAM_ORIGIN_BEGIN, NULL);
4943 } while (0);
4944 return ret;
4945 }
4946 gate_result_t gate_file_lock(gate_file_t filehandle, gate_bool_t wait)
4947 {
4948 GATE_UNUSED_ARG(filehandle);
4949 GATE_UNUSED_ARG(wait);
4950 return GATE_RESULT_NOTSUPPORTED;
4951 }
4952 gate_result_t gate_file_unlock(gate_file_t filehandle)
4953 {
4954 GATE_UNUSED_ARG(filehandle);
4955 return GATE_RESULT_NOTSUPPORTED;
4956 }
4957 gate_result_t gate_file_truncate(gate_file_t filehandle)
4958 {
4959 GATE_UNUSED_ARG(filehandle);
4960 return GATE_RESULT_NOTSUPPORTED;
4961 }
4962 gate_result_t gate_file_root_list(gate_file_list_callback_t callback, void* userparam)
4963 {
4964 gate_result_t ret = GATE_RESULT_OK;
4965 gate_size_t n;
4966 for (n = 0; n < 26; ++n)
4967 {
4968 gate_file_entry_t entry;
4969 gate_dos_gpregs_t regs = GATE_INIT_EMPTY;
4970 regs.a.h = 0x32;
4971 regs.d.x = (n + 1);
4972 gate_dos_intr(0x21, &regs, NULL);
4973 if (regs.a.l != 0)
4974 {
4975 continue;
4976 }
4977 gate_mem_clear(&entry, sizeof(entry));
4978 entry.path[0] = 'A' + n;
4979 entry.path[1] = ':';
4980 entry.path[2] = '\\';
4981 entry.path[3] = 0;
4982 entry.name[0] = 'A' + n;
4983 entry.name[1] = ':';
4984 entry.name[2] = 0;
4985 entry.props.attribs = GATE_FILEENTRY_ATTRIB_VOLUME;
4986 if (!callback(&entry, userparam))
4987 {
4988 break;
4989 }
4990 }
4991 return ret;
4992 }
4993
4994 static void dos_find_to_gate_file_entry(struct find_t* ptr_find, gate_file_entry_t* ptr_entry)
4995 {
4996 gate_size_t namelen = gate_str_length(ptr_find->name);
4997 gate_bool_t is_executable = gate_str_ends_with_ic(ptr_find->name, namelen, ".exe", 4)
4998 || gate_str_ends_with_ic(ptr_find->name, namelen, ".com", 4)
4999 || gate_str_ends_with_ic(ptr_find->name, namelen, ".bat", 4);
5000 gate_str_print_text(&ptr_entry->name[0], sizeof(ptr_entry->name), ptr_find->name, namelen);
5001 ptr_entry->props.size = ptr_find->size;
5002 ptr_entry->props.attribs = 0;
5003 ptr_entry->props.access = GATE_FILEENTRY_ACCESS_OWNERREAD | GATE_FILEENTRY_ACCESS_GROUPREAD | GATE_FILEENTRY_ACCESS_ALLREAD;
5004 GATE_FLAG_SET(ptr_entry->props.attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY, (ptr_find->attrib & _A_SUBDIR));
5005 GATE_FLAG_SET(ptr_entry->props.attribs, GATE_FILEENTRY_ATTRIB_ARCHIVE, (ptr_find->attrib & _A_ARCH));
5006 GATE_FLAG_SET(ptr_entry->props.attribs, GATE_FILEENTRY_ATTRIB_HIDDEN, (ptr_find->attrib & _A_HIDDEN));
5007 GATE_FLAG_SET(ptr_entry->props.attribs, GATE_FILEENTRY_ATTRIB_READONLY, (ptr_find->attrib & _A_RDONLY));
5008 GATE_FLAG_SET(ptr_entry->props.attribs, GATE_FILEENTRY_ATTRIB_SYSTEM, (ptr_find->attrib & _A_SYSTEM));
5009 if (!GATE_FLAG_ENABLED(ptr_entry->props.attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY))
5010 {
5011 ptr_entry->props.attribs |= GATE_FILEENTRY_ATTRIB_FILE;
5012 }
5013 if (!GATE_FLAG_ENABLED(ptr_entry->props.attribs, GATE_FILEENTRY_ATTRIB_READONLY))
5014 {
5015 ptr_entry->props.access |= GATE_FILEENTRY_ACCESS_OWNERWRITE | GATE_FILEENTRY_ACCESS_GROUPWRITE | GATE_FILEENTRY_ACCESS_ALLWRITE;
5016 }
5017 if (is_executable)
5018 {
5019 ptr_entry->props.access |= GATE_FILEENTRY_ACCESS_OWNEREXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_ALLEXECUTE;
5020 }
5021 gate_file_convert_time(ptr_find->wr_date, ptr_find->wr_time, &ptr_entry->props.time_modified);
5022 ptr_entry->props.time_accessed = ptr_entry->props.time_modified;
5023 ptr_entry->props.time_created = ptr_entry->props.time_modified;
5024 }
5025
5026 gate_result_t gate_file_list(gate_string_t const* dirpath, gate_file_list_callback_t callback, void* userparam)
5027 {
5028 gate_result_t ret = GATE_RESULT_OK;
5029 char path[GATE_MAX_FILEPATH_LENGTH];
5030 gate_size_t pathlen;
5031 gate_size_t namelen;
5032 struct find_t found_file;
5033 gate_file_entry_t file_entry;
5034 unsigned errorstatus;
5035
5036 pathlen = gate_string_to_buffer(dirpath, path, sizeof(path) - 5);
5037 if (pathlen != 0)
5038 {
5039 if (path[pathlen - 1] != '\\')
5040 {
5041 path[pathlen] = '\\';
5042 ++pathlen;
5043 }
5044 }
5045 path[pathlen + 0] = '*';
5046 path[pathlen + 1] = '.';
5047 path[pathlen + 2] = '*';
5048 path[pathlen + 3] = '\0';
5049
5050 errorstatus = _dos_findfirst(path, _A_SUBDIR | _A_ARCH | _A_HIDDEN | _A_NORMAL | _A_RDONLY | _A_SYSTEM, &found_file);
5051 if (errorstatus)
5052 {
5053 return GATE_RESULT_FAILED;
5054 }
5055 while (errorstatus == 0)
5056 {
5057 namelen = gate_str_length(found_file.name);
5058 if ((gate_str_compare(found_file.name, namelen, ".", 1) != 0)
5059 && (gate_str_compare(found_file.name, namelen, "..", 2) != 0)
5060 )
5061 {
5062 gate_str_print_text(&file_entry.path[0], sizeof(file_entry.path), path, pathlen);
5063 gate_str_print_text(&file_entry.path[pathlen], sizeof(file_entry.path) - pathlen, found_file.name, namelen);
5064
5065 dos_find_to_gate_file_entry(&found_file, &file_entry);
5066 if (callback)
5067 {
5068 if (!callback(&file_entry, userparam))
5069 {
5070 /* skip all other iterations */
5071 while (errorstatus == 0)
5072 {
5073 errorstatus = _dos_findnext(&found_file);
5074 }
5075 break;
5076 }
5077 }
5078 }
5079 errorstatus = _dos_findnext(&found_file);
5080 }
5081 return ret;
5082 }
5083 gate_result_t gate_file_get_entry(gate_string_t const* filepath, gate_file_entry_t* ptr_entry)
5084 {
5085 gate_result_t ret = GATE_RESULT_FAILED;
5086
5087 do
5088 {
5089 char path[GATE_MAX_FILEPATH_LENGTH];
5090 struct find_t found_file;
5091 gate_size_t pathlen;
5092 unsigned search_attribs = _A_NORMAL | _A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_SUBDIR | _A_ARCH;
5093 unsigned result;
5094
5095 gate_mem_clear(ptr_entry, sizeof(gate_file_entry_t));
5096 pathlen = gate_string_to_buffer(filepath, path, sizeof(path));
5097
5098 if (pathlen == 0)
5099 {
5100 ret = GATE_RESULT_INVALIDARG;
5101 break;
5102 }
5103
5104 if (pathlen == 3)
5105 {
5106 if (path[1] == ':' && path[2] == '\\')
5107 {
5108 /* path is a folder */
5109 path[3] = '*';
5110 path[4] = '.';
5111 path[5] = '*';
5112 path[6] = '\0';
5113 result = _dos_findfirst(path, _A_SUBDIR | _A_ARCH | _A_HIDDEN | _A_NORMAL | _A_VOLID | _A_RDONLY | _A_SYSTEM, &found_file);
5114 if (result == 0)
5115 {
5116 gate_string_to_buffer(filepath, ptr_entry->path, sizeof(ptr_entry->path));
5117 gate_string_to_buffer(filepath, ptr_entry->name, sizeof(ptr_entry->name));
5118 ptr_entry->props.attribs = GATE_FILEENTRY_ATTRIB_DIRECTORY | GATE_FILEENTRY_ATTRIB_VOLUME;
5119 ptr_entry->props.access = GATE_FILEENTRY_ACCESS_ALLREAD | GATE_FILEENTRY_ACCESS_ALLWRITE;
5120 ptr_entry->props.size = 0;
5121 ret = GATE_RESULT_OK;
5122 break;
5123 }
5124 }
5125 }
5126
5127 if (path[pathlen - 1] == '\\')
5128 {
5129 --pathlen;
5130 path[pathlen] = '\0';
5131 if (pathlen == 0)
5132 {
5133 ret = GATE_RESULT_INVALIDARG;
5134 break;
5135 }
5136 }
5137 gate_mem_clear(&found_file, sizeof(found_file));
5138
5139 result = _dos_findfirst(path, search_attribs, &found_file);
5140 if (0 != result)
5141 {
5142 ret = GATE_RESULT_NOTAVAILABLE;
5143 break;
5144 }
5145
5146 if (ptr_entry)
5147 {
5148 gate_string_to_buffer(filepath, ptr_entry->path, sizeof(ptr_entry->path));
5149 dos_find_to_gate_file_entry(&found_file, ptr_entry);
5150 }
5151 ret = GATE_RESULT_OK;
5152 } while (0);
5153
5154 return ret;
5155 }
5156 gate_result_t gate_file_dir_create(gate_string_t const* dirpath, gate_enumint_t flags)
5157 {
5158 char path[GATE_MAX_FILEPATH_LENGTH];
5159 GATE_UNUSED_ARG(flags);
5160 gate_string_to_buffer(dirpath, path, sizeof(path));
5161 if (0 != _mkdir(path))
5162 {
5163 return GATE_RESULT_FAILED;
5164 }
5165 return GATE_RESULT_OK;
5166 }
5167 gate_result_t gate_file_dir_delete(gate_string_t const* dirpath)
5168 {
5169 char path[GATE_MAX_FILEPATH_LENGTH];
5170 gate_string_to_buffer(dirpath, path, sizeof(path));
5171 if (0 != _rmdir(path))
5172 {
5173 return GATE_RESULT_FAILED;
5174 }
5175 return GATE_RESULT_OK;
5176 }
5177 gate_result_t gate_file_dir_exists(gate_string_t const* dirpath)
5178 {
5179 DIR* ptr_dir;
5180 char path[GATE_MAX_FILEPATH_LENGTH];
5181 gate_string_to_buffer(dirpath, path, sizeof(path));
5182 ptr_dir = opendir(path);
5183 if (ptr_dir == NULL)
5184 {
5185 return GATE_RESULT_NOTAVAILABLE;
5186 }
5187 closedir(ptr_dir);
5188 return GATE_RESULT_OK;
5189 }
5190 gate_result_t gate_file_dir_open(gate_string_t const* dirpath, gate_file_dirreader_t* dirhandle)
5191 {
5192 DIR* ptr_dir;
5193
5194 if (!dirpath || !dirhandle)
5195 {
5196 return GATE_RESULT_INVALIDARG;
5197 }
5198
5199 gate_mem_clear(dirhandle, sizeof(gate_file_dirreader_t));
5200
5201 dirhandle->bufferlen = gate_string_to_buffer(dirpath, dirhandle->buffer, sizeof(dirhandle->buffer) - 1);
5202 if (dirhandle->bufferlen == 0)
5203 {
5204 return GATE_RESULT_FAILED;
5205 }
5206 if (dirhandle->buffer[dirhandle->bufferlen - 1] != '\\')
5207 {
5208 dirhandle->buffer[dirhandle->bufferlen] = '\\';
5209 ++dirhandle->bufferlen;
5210 dirhandle->buffer[dirhandle->bufferlen] = 0;
5211 }
5212
5213 ptr_dir = opendir(dirhandle->buffer);
5214 if (ptr_dir == NULL)
5215 {
5216 return GATE_RESULT_NOTAVAILABLE;
5217 }
5218 dirhandle->handle = ptr_dir;
5219 return GATE_RESULT_OK;
5220 }
5221 gate_size_t gate_file_dir_read(gate_file_dirreader_t* dirhandle, char* path, gate_size_t pathlen, gate_bool_t filename_only)
5222 {
5223 gate_size_t ret = 0;
5224
5225 if (dirhandle && dirhandle->handle && path && pathlen)
5226 {
5227 DIR* ptr_dir = (DIR*)dirhandle->handle;
5228 struct dirent* ptr_dirent = readdir(ptr_dir);
5229 while (ptr_dirent)
5230 {
5231 if ((gate_str_comp(ptr_dirent->d_name, "..") == 0) || (gate_str_comp(ptr_dirent->d_name, ".") == 0))
5232 {
5233 ptr_dirent = readdir(ptr_dir);
5234 }
5235 else
5236 {
5237 if (filename_only)
5238 {
5239 ret = gate_str_print_text(path, pathlen, ptr_dirent->d_name, gate_str_length(ptr_dirent->d_name));
5240 }
5241 else
5242 {
5243 ret = gate_str_print_text(path, pathlen, dirhandle->buffer, dirhandle->bufferlen);
5244 path += ret;
5245 pathlen -= ret;
5246 ret += gate_str_print_text(path, pathlen, ptr_dirent->d_name, gate_str_length(ptr_dirent->d_name));
5247 }
5248 break;
5249 }
5250 }
5251 }
5252
5253 return ret;
5254 }
5255 gate_result_t gate_file_dir_close(gate_file_dirreader_t* dirhandle)
5256 {
5257 if (dirhandle && dirhandle->handle)
5258 {
5259 DIR* ptr_dir = (DIR*)dirhandle->handle;
5260 closedir(ptr_dir);
5261 dirhandle->handle = NULL;
5262 }
5263 return GATE_RESULT_OK;
5264 }
5265
5266 #endif /* GATE_CORE_FILES_DOS_IMPL */
5267
5268
5269
5270
5271 #if defined(GATE_CORE_FILES_EFI_IMPL)
5272
5273 #include "gate/platform/efi/efi_gate.h"
5274
5275 char const gate_file_path_separator_char = '\\';
5276
5277 char const* const gate_file_path_separator = "\\";
5278
5279 static void update_volumes(void** device_handles, gate_size_t device_handles_count)
5280 {
5281 gate_size_t ndx;
5282 int dummy = 0;
5283
5284 for (ndx = 0; ndx != device_handles_count; ++ndx)
5285 {
5286 gate_platform_efi_register_volume(device_handles[ndx], &dummy);
5287 }
5288 }
5289
5290 static gate_result_t update_all_volumes()
5291 {
5292 void* device_handles[256] = GATE_INIT_EMPTY;
5293 gate_size_t device_handles_count = sizeof(device_handles) / sizeof(device_handles[0]);
5294 gate_result_t ret;
5295 ret = gate_platform_efi_find_volume_device_handles(device_handles, &device_handles_count);
5296 if (GATE_SUCCEEDED(ret))
5297 {
5298 update_volumes(device_handles, device_handles_count);
5299 }
5300 return ret;
5301 }
5302
5303 static gate_result_t open_volume_root(gate_string_t const* dirpath, void** ptr_volume_root, gate_string_t* subpath)
5304 {
5305 gate_result_t ret;
5306 gate_string_t drive = GATE_STRING_INIT_EMPTY;
5307 do
5308 {
5309 static gate_bool_t is_initialized = false;
5310 if (!is_initialized)
5311 {
5312 is_initialized = true;
5313 update_all_volumes();
5314 }
5315
5316 if (gate_string_starts_with_str(dirpath, gate_file_path_separator))
5317 {
5318 /* open boot device */
5319 ret = gate_platform_efi_open_app_volume_root(ptr_volume_root);
5320 GATE_BREAK_IF_FAILED(ret);
5321 gate_string_substr(subpath, dirpath, 1, GATE_STR_NPOS);
5322 }
5323 else if (gate_string_starts_with_str(dirpath, "FS") || gate_string_starts_with_str(dirpath, "fs"))
5324 {
5325 /* check for UEFI shell paths FSx:\\ */
5326 wchar_t fspath[8];
5327 void* volume_handle = NULL;
5328
5329 gate_string_substr(&drive, dirpath, 0, 5);
5330 if (!gate_string_ends_with_str(&drive, ":\\"))
5331 {
5332 ret = GATE_RESULT_INVALIDARG;
5333 break;
5334 }
5335
5336 /* it is a shell path: */
5337 fspath[0] = 'f';
5338 fspath[1] = 's';
5339 fspath[2] = (wchar_t)dirpath->str[2];
5340 fspath[3] = ':';
5341 fspath[4] = 0;
5342
5343 ret = gate_platform_efi_shell_get_volume_from_path(fspath, &volume_handle);
5344 GATE_BREAK_IF_FAILED(ret);
5345
5346 ret = gate_platform_efi_resolve_volume(volume_handle, NULL);
5347 if (GATE_FAILED(ret))
5348 {
5349 ret = update_all_volumes();
5350 GATE_BREAK_IF_FAILED(ret);
5351 ret = gate_platform_efi_resolve_volume(volume_handle, NULL);
5352 GATE_BREAK_IF_FAILED(ret);
5353 }
5354
5355 ret = gate_platform_efi_open_volume_device(volume_handle, ptr_volume_root);
5356 GATE_BREAK_IF_FAILED(ret);
5357
5358 gate_string_substr(subpath, dirpath, 5, GATE_STR_NPOS);
5359 }
5360 else
5361 {
5362 /* check for GATE- or DOS- style drive letters: */
5363 char letter;
5364 int volume_id = 0;
5365 void* volume_handle = NULL;
5366
5367 gate_string_substr(&drive, dirpath, 0, 3);
5368 if (!gate_string_ends_with_str(&drive, ":\\"))
5369 {
5370 ret = GATE_RESULT_INVALIDARG;
5371 break;
5372 }
5373 letter = gate_string_char_at(&drive, 0);
5374 if ((letter >= 'A') && (letter <= 'Z'))
5375 {
5376 volume_id = letter - 'A' + 1;
5377 }
5378 else if ((letter >= 'a') && (letter <= 'z'))
5379 {
5380 volume_id = letter - 'a' + 1;
5381 }
5382 else
5383 {
5384 ret = GATE_RESULT_INVALIDARG;
5385 break;
5386 }
5387
5388 ret = gate_platform_efi_get_volume(volume_id, &volume_handle);
5389 if (GATE_FAILED(ret))
5390 {
5391 ret = update_all_volumes();
5392 GATE_BREAK_IF_FAILED(ret);
5393 ret = gate_platform_efi_get_volume(volume_id, &volume_handle);
5394 }
5395 if (GATE_FAILED(ret))
5396 {
5397 ret = GATE_RESULT_NOMATCH;
5398 break;
5399 }
5400
5401 ret = gate_platform_efi_open_volume_device(volume_handle, ptr_volume_root);
5402 GATE_BREAK_IF_FAILED(ret);
5403
5404 gate_string_substr(subpath, dirpath, 3, GATE_STR_NPOS);
5405 }
5406 /* code */
5407 } while (0);
5408
5409 gate_string_release(&drive);
5410
5411 return ret;
5412 }
5413
5414
5415 gate_result_t open_volume_file(gate_string_t const* filepath, gate_enumint_t efi_open_type, void** ptr_file_handle)
5416 {
5417 gate_result_t ret;
5418 void* volume_root = NULL;
5419 void* file_handle = NULL;
5420 gate_string_t subpath = GATE_STRING_INIT_EMPTY;
5421 do
5422 {
5423 ret = open_volume_root(filepath, &volume_root, &subpath);
5424 GATE_BREAK_IF_FAILED(ret);
5425
5426 if (gate_string_is_empty(&subpath))
5427 {
5428 /* open root path itself */
5429 if (ptr_file_handle)
5430 {
5431 *ptr_file_handle = volume_root;
5432 volume_root = NULL;
5433 }
5434 }
5435 else
5436 {
5437 wchar_t native_path[GATE_MAX_FILEPATH_LENGTH];
5438 gate_str_utf8_2_utf16(gate_string_ptr(&subpath, 0), gate_string_length(&subpath),
5439 native_path, sizeof(native_path) / sizeof(native_path[0]));
5440
5441 ret = gate_platform_efi_open_file(volume_root, native_path, GATE_PLATFORM_EFI_OPEN_READ, &file_handle);
5442 GATE_BREAK_IF_FAILED(ret);
5443
5444 if (ptr_file_handle)
5445 {
5446 *ptr_file_handle = file_handle;
5447 file_handle = NULL;
5448 }
5449 }
5450
5451 } while (0);
5452
5453 if (file_handle != NULL)
5454 {
5455 gate_platform_efi_close_file(file_handle);
5456 }
5457 if (volume_root != NULL)
5458 {
5459 gate_platform_efi_close_file(volume_root);
5460 }
5461 gate_string_release(&subpath);
5462
5463 return ret;
5464 }
5465
5466 gate_result_t gate_file_exists(gate_string_t const* filepath)
5467 {
5468 return open_volume_file(filepath, GATE_PLATFORM_EFI_OPEN_READ, NULL);
5469 }
5470 gate_result_t gate_file_copy(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath, gate_enumint_t flags)
5471 {
5472 return gate_file_copy_internal(srcfilepath, dstfilepath, flags);
5473 }
5474 gate_result_t gate_file_move(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath)
5475 {
5476 return gate_file_move_internal(srcfilepath, dstfilepath);
5477 }
5478 gate_result_t gate_file_delete(gate_string_t const* filepath)
5479 {
5480 void* file_handle = NULL;
5481 gate_result_t ret = open_volume_file(filepath, GATE_PLATFORM_EFI_OPEN_READ, &file_handle);
5482 if (GATE_SUCCEEDED(ret))
5483 {
5484 ret = gate_platform_efi_delete_file(file_handle);
5485 gate_platform_efi_close_file(file_handle);
5486 }
5487 return ret;
5488 }
5489 gate_result_t gate_file_real_path(gate_string_t const* relativepath, gate_string_t* absolutepath)
5490 {
5491 return GATE_RESULT_NOTSUPPORTED;
5492 }
5493 gate_result_t gate_file_create_link(gate_string_t const* targetpath, gate_string_t const* linkfile)
5494 {
5495 return GATE_RESULT_NOTSUPPORTED;
5496 }
5497 gate_result_t gate_file_read_link(gate_string_t const* filepath, gate_string_t* realpath)
5498 {
5499 return GATE_RESULT_NOTSUPPORTED;
5500 }
5501 gate_result_t gate_file_create_hardlink(gate_string_t const* targetfile, gate_string_t const* linkfile)
5502 {
5503 return GATE_RESULT_NOTSUPPORTED;
5504 }
5505 gate_result_t gate_file_get_attributes(gate_string_t const* targetpath, gate_enumint_t* attribs, gate_enumint_t* access_bits)
5506 {
5507 return GATE_RESULT_NOTSUPPORTED;
5508 }
5509 gate_result_t gate_file_set_attributes(gate_string_t const* targetpath,
5510 gate_enumint_t attribs, gate_enumint_t attribs_mask,
5511 gate_enumint_t access, gate_enumint_t access_mask)
5512 {
5513 return GATE_RESULT_NOTSUPPORTED;
5514 }
5515
5516 gate_result_t gate_file_get_times(gate_string_t const* targetpath, gate_timestamp_t* modified, gate_timestamp_t* accessed, gate_timestamp_t* created)
5517 {
5518 return GATE_RESULT_NOTSUPPORTED;
5519 }
5520 gate_result_t gate_file_set_times(gate_string_t const* targetpath, gate_timestamp_t const* modified, gate_timestamp_t const* accessed, gate_timestamp_t const* created)
5521 {
5522 return GATE_RESULT_NOTSUPPORTED;
5523 }
5524 gate_result_t gate_file_get_size(gate_string_t const* targetfile, gate_int64_t* filesize)
5525 {
5526 return GATE_RESULT_NOTSUPPORTED;
5527 }
5528 gate_result_t gate_file_get_owner(gate_string_t const* targetfile,
5529 gate_uint32_t* owner_id, gate_string_t* owner_name)
5530 {
5531 return GATE_RESULT_NOTSUPPORTED;
5532 }
5533
5534 gate_result_t gate_file_open(gate_string_t const* filepath, gate_enumint_t flags, gate_file_t* filehandle)
5535 {
5536 gate_result_t ret = GATE_RESULT_FAILED;
5537 int efi_open_flags = 0;
5538 void* efi_handle = NULL;
5539 gate_enumint_t attribs = 0;
5540
5541 do
5542 {
5543 if (GATE_FLAG_ENABLED(flags, GATE_STREAM_OPEN_READ))
5544 {
5545 efi_open_flags |= GATE_PLATFORM_EFI_OPEN_READ;
5546 }
5547 if (GATE_FLAG_ENABLED(flags, GATE_STREAM_OPEN_WRITE))
5548 {
5549 efi_open_flags |= GATE_PLATFORM_EFI_OPEN_WRITE;
5550 }
5551
5552 ret = open_volume_file(filepath, efi_open_flags, &efi_handle);
5553 GATE_BREAK_IF_FAILED(ret);
5554
5555 ret = gate_platform_efi_get_file_info(efi_handle,
5556 NULL, NULL, NULL,
5557 &attribs,
5558 NULL, NULL, NULL);
5559
5560 GATE_BREAK_IF_FAILED(ret);
5561
5562 if (GATE_FLAG_ENABLED(attribs, GATE_PLATFORM_EFI_FILEATTR_DIRECTORY))
5563 {
5564 /* directories cannot be opened as a file! */
5565 ret = GATE_RESULT_INVALIDSTATE;
5566 break;
5567 }
5568
5569 /* success case */
5570 ret = GATE_RESULT_OK;
5571 if (filehandle)
5572 {
5573 *filehandle = efi_handle;
5574 efi_handle = NULL;
5575 }
5576 } while (0);
5577
5578 if (efi_handle != NULL)
5579 {
5580 gate_platform_efi_close_file(efi_handle);
5581 }
5582
5583 return ret;
5584 }
5585 gate_result_t gate_file_read(gate_file_t filehandle, char* buffer, gate_size_t bufferlength, gate_size_t* bufferused)
5586 {
5587 gate_result_t ret = GATE_RESULT_FAILED;
5588 do
5589 {
5590 if (!filehandle)
5591 {
5592 ret = GATE_RESULT_INVALIDARG;
5593 break;
5594 }
5595 if ((bufferlength != 0) && (buffer == NULL))
5596 {
5597 ret = GATE_RESULT_INVALIDARG;
5598 break;
5599 }
5600 ret = gate_platform_efi_read_file(filehandle, (char*)buffer, bufferlength, bufferused);
5601 } while (0);
5602 return ret;
5603 }
5604 gate_result_t gate_file_write(gate_file_t filehandle, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
5605 {
5606 gate_result_t ret = GATE_RESULT_FAILED;
5607 do
5608 {
5609 if (!filehandle)
5610 {
5611 ret = GATE_RESULT_INVALIDARG;
5612 break;
5613 }
5614 if ((bufferlength != 0) && (buffer == NULL))
5615 {
5616 ret = GATE_RESULT_INVALIDARG;
5617 break;
5618 }
5619 ret = gate_platform_efi_read_file(filehandle, (char*)buffer, bufferlength, written);
5620 } while (0);
5621 return ret;
5622 }
5623 gate_result_t gate_file_flush(gate_file_t filehandle)
5624 {
5625 return gate_platform_efi_flush_file(filehandle);
5626 }
5627 gate_result_t gate_file_close(gate_file_t filehandle)
5628 {
5629 if (filehandle)
5630 {
5631 gate_platform_efi_close_file(filehandle);
5632 return GATE_RESULT_OK;
5633 }
5634 else
5635 {
5636 return GATE_RESULT_INVALIDARG;
5637 }
5638 }
5639 gate_result_t gate_file_seek(gate_file_t filehandle, gate_int64_t position, gate_enumint_t origin, gate_int64_t* newposition)
5640 {
5641 gate_result_t result = GATE_RESULT_OK;
5642 gate_int64_t target_pos = 0;
5643
5644 switch (origin)
5645 {
5646 case GATE_FILE_SEEK_ORIGIN_BEGIN:
5647 {
5648 target_pos = position;
5649 break;
5650 }
5651 case GATE_FILE_SEEK_ORIGIN_CURPOS:
5652 {
5653 result = gate_platform_efi_get_file_position(filehandle, &target_pos);
5654 target_pos += position;
5655 break;
5656 }
5657 case GATE_FILE_SEEK_ORIGIN_END:
5658 {
5659 result = gate_file_size(filehandle, &target_pos);
5660 target_pos += position;
5661 break;
5662 }
5663 default:
5664 {
5665 result = GATE_RESULT_INVALIDARG;
5666 break;
5667 }
5668 }
5669
5670 if (GATE_SUCCEEDED(result))
5671 {
5672 result = gate_platform_efi_set_file_position(filehandle, target_pos);
5673 if (newposition)
5674 {
5675 result = gate_platform_efi_get_file_position(filehandle, newposition);
5676 }
5677 }
5678 return result;
5679 }
5680 gate_result_t gate_file_pos(gate_file_t filehandle, gate_int64_t* position)
5681 {
5682 return gate_platform_efi_get_file_position(filehandle, position);
5683 }
5684 gate_result_t gate_file_size(gate_file_t filehandle, gate_int64_t* size)
5685 {
5686 gate_uint64_t sz = 0;
5687 gate_uint64_t f_sz = 0;
5688 gate_result_t ret = gate_platform_efi_get_file_info(filehandle, &sz, &f_sz, NULL, NULL, NULL, NULL, NULL);
5689 if (size) *size = f_sz;
5690 return ret;
5691 }
5692
5693 gate_result_t gate_file_truncate(gate_file_t filehandle)
5694 {
5695 gate_result_t ret = GATE_RESULT_NOTSUPPORTED;
5696 return ret;
5697 }
5698
5699
5700 gate_result_t gate_file_lock(gate_file_t filehandle, gate_bool_t wait)
5701 {
5702 return GATE_RESULT_NOTSUPPORTED;
5703 }
5704 gate_result_t gate_file_unlock(gate_file_t filehandle)
5705 {
5706 return GATE_RESULT_NOTSUPPORTED;
5707 }
5708
5709 gate_result_t gate_file_root_list(gate_file_list_callback_t callback, void* userparam)
5710 {
5711 gate_result_t ret;
5712 gate_size_t ndx;
5713 int volume_ids[26];
5714 gate_size_t volumes_count;
5715 gate_file_entry_t file_entry;
5716
5717 do
5718 {
5719 ret = update_all_volumes();
5720 GATE_BREAK_IF_FAILED(ret);
5721
5722 if (!callback)
5723 {
5724 break;
5725 }
5726
5727 ret = GATE_RESULT_OK;
5728 volumes_count = gate_platform_efi_list_volumes(volume_ids, sizeof(volume_ids) / sizeof(volume_ids[0]));
5729 for (ndx = 0; ndx != volumes_count; ++ndx)
5730 {
5731 int vol_id = volume_ids[ndx];
5732 gate_mem_clear(&file_entry, sizeof(file_entry));
5733 file_entry.path[0] = (char)('@' + vol_id);
5734 file_entry.path[1] = ':';
5735 file_entry.path[2] = '\\';
5736 file_entry.path[3] = 0;
5737
5738 file_entry.name[0] = (char)('@' + vol_id);
5739 file_entry.name[1] = ':';
5740 file_entry.name[2] = 0;
5741
5742 file_entry.props.attribs = GATE_FILEENTRY_ATTRIB_DIRECTORY | GATE_FILEENTRY_ATTRIB_VOLUME;
5743 if (!callback(&file_entry, userparam))
5744 {
5745 break;
5746 }
5747 }
5748 } while (0);
5749 return ret;
5750 }
5751
5752 gate_result_t gate_file_list(gate_string_t const* dirpath, gate_file_list_callback_t callback, void* userparam)
5753 {
5754 gate_result_t ret = GATE_RESULT_FAILED;
5755 void* file_handle = NULL;
5756
5757 do
5758 {
5759 gate_file_entry_t entry;
5760 gate_size_t parent_length;
5761
5762 ret = open_volume_file(dirpath, GATE_PLATFORM_EFI_OPEN_READ, &file_handle);
5763 GATE_BREAK_IF_FAILED(ret);
5764
5765 gate_mem_clear(&entry, sizeof(entry));
5766
5767 parent_length = gate_str_print_text(entry.path, sizeof(entry.path) - 1, gate_string_ptr(dirpath, 0), gate_string_length(dirpath));
5768 if (parent_length == 0)
5769 {
5770 ret = GATE_RESULT_INVALIDDATA;
5771 break;
5772 }
5773 if (entry.path[parent_length - 1] != gate_file_path_separator_char)
5774 {
5775 entry.path[parent_length] = gate_file_path_separator_char;
5776 ++parent_length;
5777 }
5778
5779 while (GATE_SUCCEEDED(ret))
5780 {
5781 wchar_t filename[256];
5782 gate_size_t filename_len = sizeof(filename) / sizeof(filename[0]);
5783 gate_size_t filename_used;
5784 gate_uint64_t filesize = 0;
5785 gate_uint32_t attribs = 0;
5786 gate_int64_t times[3];
5787 gate_bool_t is_readonly;
5788 gate_bool_t is_executable;
5789
5790 ret = gate_platform_efi_read_dir(file_handle,
5791 filename, &filename_len,
5792 NULL, &filesize, NULL,
5793 &attribs,
5794 &times[0],
5795 &times[1],
5796 &times[2]
5797 );
5798 if (ret == GATE_RESULT_ENDOFSTREAM)
5799 {
5800 ret = GATE_RESULT_OK;
5801 break;
5802 }
5803 GATE_BREAK_IF_FAILED(ret);
5804
5805 /* print filename */
5806 filename_used = gate_str_utf16_2_utf8(filename, filename_len, &entry.name[0], sizeof(entry.name));
5807 /* print filename after parent path */
5808 gate_str_print_text(&entry.path[parent_length], sizeof(entry.path) - parent_length, entry.name, filename_used);
5809
5810 entry.props.size = filesize;
5811 entry.props.attribs = 0;
5812 GATE_FLAG_SET(entry.props.attribs, GATE_FILEENTRY_ATTRIB_READONLY, GATE_FLAG_ENABLED(attribs, GATE_PLATFORM_EFI_FILEATTR_READONLY));
5813 GATE_FLAG_SET(entry.props.attribs, GATE_FILEENTRY_ATTRIB_HIDDEN, GATE_FLAG_ENABLED(attribs, GATE_PLATFORM_EFI_FILEATTR_HIDDEN));
5814 GATE_FLAG_SET(entry.props.attribs, GATE_FILEENTRY_ATTRIB_SYSTEM, GATE_FLAG_ENABLED(attribs, GATE_PLATFORM_EFI_FILEATTR_SYSTEM));
5815 GATE_FLAG_SET(entry.props.attribs, GATE_FILEENTRY_ATTRIB_ARCHIVE, GATE_FLAG_ENABLED(attribs, GATE_PLATFORM_EFI_FILEATTR_ARCHIVE));
5816 GATE_FLAG_SET(entry.props.attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY, GATE_FLAG_ENABLED(attribs, GATE_PLATFORM_EFI_FILEATTR_DIRECTORY));
5817
5818 is_readonly = GATE_FLAG_ENABLED(attribs, GATE_PLATFORM_EFI_FILEATTR_READONLY);
5819 is_executable = gate_str_ends_with_ic(entry.name, filename_used, ".efi", 4);
5820
5821 entry.props.access = GATE_FILEENTRY_ACCESS_OWNERREAD
5822 | GATE_FILEENTRY_ACCESS_GROUPREAD
5823 | GATE_FILEENTRY_ACCESS_ALLREAD
5824 ;
5825
5826 if (!is_readonly)
5827 {
5828 entry.props.access |= GATE_FILEENTRY_ACCESS_OWNERWRITE
5829 | GATE_FILEENTRY_ACCESS_GROUPWRITE
5830 | GATE_FILEENTRY_ACCESS_ALLWRITE
5831 ;
5832 }
5833
5834 if (is_executable)
5835 {
5836 entry.props.access |= GATE_FILEENTRY_ACCESS_OWNEREXECUTE
5837 | GATE_FILEENTRY_ACCESS_GROUPEXECUTE
5838 | GATE_FILEENTRY_ACCESS_ALLEXECUTE
5839 ;
5840 }
5841
5842 gate_time_from_unix(times[0] / 1000, &entry.props.time_created);
5843 gate_time_from_unix(times[1] / 1000, &entry.props.time_accessed);
5844 gate_time_from_unix(times[2] / 1000, &entry.props.time_modified);
5845
5846 if (callback)
5847 {
5848 const gate_bool_t continue_iteration = callback(&entry, userparam);
5849 if (!continue_iteration)
5850 {
5851 break;
5852 }
5853 }
5854 }
5855 } while (0);
5856
5857 if (file_handle != NULL)
5858 {
5859 gate_platform_efi_close_file(file_handle);
5860 }
5861
5862 return ret;
5863
5864 return GATE_RESULT_NOTSUPPORTED;
5865 }
5866 gate_result_t gate_file_get_entry(gate_string_t const* filepath, gate_file_entry_t* ptrtonewentry)
5867 {
5868 return GATE_RESULT_NOTSUPPORTED;
5869 }
5870 gate_result_t gate_file_dir_create(gate_string_t const* dirpath, gate_enumint_t flags)
5871 {
5872 return GATE_RESULT_NOTSUPPORTED;
5873 }
5874 gate_result_t gate_file_dir_delete(gate_string_t const* dirpath)
5875 {
5876 return GATE_RESULT_NOTSUPPORTED;
5877 }
5878 gate_result_t gate_file_dir_exists(gate_string_t const* dirpath)
5879 {
5880 return GATE_RESULT_NOTSUPPORTED;
5881 }
5882 gate_result_t gate_file_dir_open(gate_string_t const* dirpath, gate_file_dirreader_t* dirhandle)
5883 {
5884 return GATE_RESULT_NOTSUPPORTED;
5885 }
5886 gate_size_t gate_file_dir_read(gate_file_dirreader_t* dirhandle, char* path, gate_size_t pathlen, gate_bool_t filename_only)
5887 {
5888 return GATE_RESULT_NOTSUPPORTED;
5889 }
5890 gate_result_t gate_file_dir_close(gate_file_dirreader_t* dirhandle)
5891 {
5892 return GATE_RESULT_NOTSUPPORTED;
5893 }
5894
5895 #endif /* GATE_CORE_FILES_EFI_IMPL */
5896
5897
5898 #if defined(GATE_CORE_FILES_WASM_IMPL)
5899
5900 #include <emscripten.h>
5901 #include "gate/strings.h"
5902 #include "gate/platform/wasm/wasm_gate.h"
5903
5904
5905 char const gate_file_path_separator_char = '/';
5906
5907 char const* const gate_file_path_separator = "/";
5908
5909 gate_result_t gate_file_exists(gate_string_t const* filepath)
5910 {
5911 return GATE_RESULT_NOTSUPPORTED;
5912 }
5913 gate_result_t gate_file_copy(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath, gate_enumint_t flags)
5914 {
5915 return gate_file_copy_internal(srcfilepath, dstfilepath, flags);
5916 }
5917 gate_result_t gate_file_move(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath)
5918 {
5919 return gate_file_move_internal(srcfilepath, dstfilepath);
5920 }
5921 gate_result_t gate_file_delete(gate_string_t const* filepath)
5922 {
5923 return GATE_RESULT_NOTSUPPORTED;
5924 }
5925 gate_result_t gate_file_real_path(gate_string_t const* relativepath, gate_string_t* absolutepath)
5926 {
5927 return GATE_RESULT_NOTSUPPORTED;
5928 }
5929 gate_result_t gate_file_create_link(gate_string_t const* targetpath, gate_string_t const* linkfile)
5930 {
5931 return GATE_RESULT_NOTSUPPORTED;
5932 }
5933 gate_result_t gate_file_read_link(gate_string_t const* filepath, gate_string_t* realpath)
5934 {
5935 return GATE_RESULT_NOTSUPPORTED;
5936 }
5937 gate_result_t gate_file_create_hardlink(gate_string_t const* targetfile, gate_string_t const* linkfile)
5938 {
5939 return GATE_RESULT_NOTSUPPORTED;
5940 }
5941 gate_result_t gate_file_get_attributes(gate_string_t const* targetpath, gate_enumint_t* attribs, gate_enumint_t* access_bits)
5942 {
5943 return GATE_RESULT_NOTSUPPORTED;
5944
5945 }
5946 gate_result_t gate_file_set_attributes(gate_string_t const* targetpath,
5947 gate_enumint_t attribs, gate_enumint_t attribs_mask,
5948 gate_enumint_t access_bits, gate_enumint_t access_mask)
5949 {
5950 return GATE_RESULT_NOTSUPPORTED;
5951 }
5952 gate_result_t gate_file_get_times(gate_string_t const* targetpath, gate_timestamp_t* modified, gate_timestamp_t* accessed, gate_timestamp_t* created)
5953 {
5954 return GATE_RESULT_NOTSUPPORTED;
5955 }
5956 gate_result_t gate_file_set_times(gate_string_t const* targetpath, gate_timestamp_t const* modified, gate_timestamp_t const* accessed, gate_timestamp_t const* created)
5957 {
5958 return GATE_RESULT_NOTSUPPORTED;
5959 }
5960 gate_result_t gate_file_get_size(gate_string_t const* targetfile, gate_int64_t* filesize)
5961 {
5962 return GATE_RESULT_NOTSUPPORTED;
5963 }
5964 gate_result_t gate_file_get_owner(gate_string_t const* targetfile,
5965 gate_uint32_t* owner_id, gate_string_t* owner_name)
5966 {
5967 return GATE_RESULT_NOTSUPPORTED;
5968 }
5969
5970 gate_result_t gate_file_open(gate_string_t const* filepath, gate_enumint_t flags, gate_file_t* filehandle)
5971 {
5972 gate_result_t ret;
5973 gate_cstrbuffer_t buffer = GATE_INIT_EMPTY;
5974
5975 gate_cstrbuffer_create_string(&buffer, filepath, false);
5976 do
5977 {
5978 int fd = gate_wasm_open(gate_cstrbuffer_get(&buffer), flags);
5979 if (fd < 0)
5980 {
5981 ret = GATE_RESULT_FAILED;
5982 break;
5983 }
5984 *filehandle = (gate_file_t)fd;
5985 ret = GATE_RESULT_OK;
5986 } while (0);
5987
5988 gate_cstrbuffer_destroy(&buffer);
5989 return ret;
5990 }
5991 gate_result_t gate_file_read(gate_file_t filehandle, char* buffer, gate_size_t bufferlength, gate_size_t* bufferused)
5992 {
5993 int fd = (int)filehandle;
5994 gate_intptr_t result = gate_wasm_read(fd, buffer, bufferlength);
5995 if (result < 0)
5996 {
5997 return GATE_RESULT_FAILED;
5998 }
5999 else
6000 {
6001 if (bufferused)
6002 {
6003 *bufferused = (gate_size_t)result;
6004 }
6005 return GATE_RESULT_OK;
6006 }
6007 }
6008 gate_result_t gate_file_write(gate_file_t filehandle, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
6009 {
6010 int fd = (int)filehandle;
6011 gate_intptr_t result = gate_wasm_write(fd, buffer, bufferlength);
6012 if (result < 0)
6013 {
6014 return GATE_RESULT_FAILED;
6015 }
6016 else
6017 {
6018 if (written)
6019 {
6020 *written = (gate_size_t)result;
6021 }
6022 return GATE_RESULT_OK;
6023 }
6024 }
6025 gate_result_t gate_file_flush(gate_file_t filehandle)
6026 {
6027 return GATE_RESULT_NOTSUPPORTED;
6028 }
6029 gate_result_t gate_file_close(gate_file_t filehandle)
6030 {
6031 int fd = (int)filehandle;
6032 return gate_wasm_close(fd);
6033 }
6034 gate_result_t gate_file_seek(gate_file_t filehandle, gate_int64_t position, gate_enumint_t origin, gate_int64_t* newposition)
6035 {
6036 int fd = (int)filehandle;
6037 return gate_wasm_seek(fd, position, origin, newposition);
6038 }
6039 gate_result_t gate_file_pos(gate_file_t filehandle, gate_int64_t* position)
6040 {
6041 return GATE_RESULT_NOTSUPPORTED;
6042 }
6043 gate_result_t gate_file_size(gate_file_t filehandle, gate_int64_t* size)
6044 {
6045 return GATE_RESULT_NOTSUPPORTED;
6046 }
6047 gate_result_t gate_file_lock(gate_file_t filehandle, gate_bool_t wait)
6048 {
6049 return GATE_RESULT_NOTSUPPORTED;
6050 }
6051 gate_result_t gate_file_unlock(gate_file_t filehandle)
6052 {
6053 return GATE_RESULT_NOTSUPPORTED;
6054 }
6055 gate_result_t gate_file_root_list(gate_file_list_callback_t callback, void* userparam)
6056 {
6057 return GATE_RESULT_NOTSUPPORTED;
6058 }
6059 gate_result_t gate_file_list(gate_string_t const* dirpath, gate_file_list_callback_t callback, void* userparam)
6060 {
6061 gate_result_t ret;
6062 gate_cstrbuffer8_t buffer;
6063
6064 do
6065 {
6066 gate_string_t filenames[1024];
6067 gate_size_t used, n;
6068 gate_file_entry_t entry;
6069 char const* path_ptr;
6070 gate_size_t path_len;
6071
6072 if (NULL == gate_cstrbuffer_create_string(&buffer, dirpath, false))
6073 {
6074 ret = GATE_RESULT_OUTOFMEMORY;
6075 break;
6076 }
6077
6078 path_ptr = gate_cstrbuffer_get(&buffer);
6079 path_len = gate_cstrbuffer_length(&buffer);
6080
6081 used = gate_wasm_fs_readdir(path_ptr, filenames, sizeof(filenames) / sizeof(filenames[0]));
6082
6083 gate_mem_clear(&entry, sizeof(entry));
6084
6085 path_len = gate_str_print_text(entry.path, sizeof(entry.path) - 2, path_ptr, path_len);
6086 if (entry.path[path_len - 1] != gate_file_path_separator_char)
6087 {
6088 entry.path[path_len] = gate_file_path_separator_char;
6089 ++path_len;
6090 }
6091
6092 for (n = 0; n != used; ++n)
6093 {
6094 char const* name = gate_string_ptr(&filenames[n], 0);
6095 gate_size_t namelen = gate_string_length(&filenames[n]);
6096 gate_str_print_text(entry.name, sizeof(entry.name), name, namelen);
6097 gate_str_print_text(&entry.path[path_len], sizeof(entry.path) - path_len, name, namelen);
6098 entry.props.attribs = GATE_FILEENTRY_ATTRIB_FILE;
6099 entry.props.size = 1;
6100 if (!callback(&entry, userparam))
6101 {
6102 break;
6103 }
6104 }
6105 ret = GATE_RESULT_OK;
6106 for (n = 0; n != used; ++n)
6107 {
6108 gate_string_release(&filenames[n]);
6109 }
6110 } while (0);
6111 return ret;
6112 }
6113 gate_result_t gate_file_get_entry(gate_string_t const* filepath, gate_file_entry_t* ptrtonewentry)
6114 {
6115 return GATE_RESULT_NOTSUPPORTED;
6116 }
6117 gate_result_t gate_file_dir_create(gate_string_t const* dirpath, gate_enumint_t flags)
6118 {
6119 return GATE_RESULT_NOTSUPPORTED;
6120 }
6121 gate_result_t gate_file_dir_delete(gate_string_t const* dirpath)
6122 {
6123 return GATE_RESULT_NOTSUPPORTED;
6124 }
6125 gate_result_t gate_file_dir_exists(gate_string_t const* dirpath)
6126 {
6127 return GATE_RESULT_NOTSUPPORTED;
6128 }
6129 gate_result_t gate_file_dir_open(gate_string_t const* dirpath, gate_file_dirreader_t* dirhandle)
6130 {
6131 return GATE_RESULT_NOTSUPPORTED;
6132 }
6133 gate_size_t gate_file_dir_read(gate_file_dirreader_t* dirhandle, char* path, gate_size_t pathlen, gate_bool_t filename_only)
6134 {
6135 return GATE_RESULT_NOTSUPPORTED;
6136 }
6137 gate_result_t gate_file_dir_close(gate_file_dirreader_t* dirhandle)
6138 {
6139 return GATE_RESULT_NOTSUPPORTED;
6140 }
6141 gate_result_t gate_file_truncate(gate_file_t filehandle)
6142 {
6143 return GATE_RESULT_NOTSUPPORTED;
6144 }
6145
6146
6147 #endif /* GATE_CORE_FILES_WASM_IMPL */
6148
6149
6150
6151
6152 #if defined(GATE_CORE_FILES_NO_IMPL)
6153
6154 char const gate_file_path_separator_char = '/';
6155
6156 char const* const gate_file_path_separator = "/";
6157
6158 gate_result_t gate_file_exists(gate_string_t const* filepath)
6159 {
6160 return GATE_RESULT_NOTSUPPORTED;
6161 }
6162 gate_result_t gate_file_copy(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath, gate_enumint_t flags)
6163 {
6164 return GATE_RESULT_NOTSUPPORTED;
6165 }
6166 gate_result_t gate_file_move(gate_string_t const* srcfilepath, gate_string_t const* dstfilepath)
6167 {
6168 return GATE_RESULT_NOTSUPPORTED;
6169 }
6170 gate_result_t gate_file_delete(gate_string_t const* filepath)
6171 {
6172 return GATE_RESULT_NOTSUPPORTED;
6173 }
6174 gate_result_t gate_file_real_path(gate_string_t const* relativepath, gate_string_t* absolutepath)
6175 {
6176 return GATE_RESULT_NOTSUPPORTED;
6177 }
6178 gate_result_t gate_file_create_link(gate_string_t const* targetpath, gate_string_t const* linkfile)
6179 {
6180 return GATE_RESULT_NOTSUPPORTED;
6181 }
6182 gate_result_t gate_file_read_link(gate_string_t const* filepath, gate_string_t* realpath)
6183 {
6184 return GATE_RESULT_NOTSUPPORTED;
6185 }
6186 gate_result_t gate_file_create_hardlink(gate_string_t const* targetfile, gate_string_t const* linkfile)
6187 {
6188 return GATE_RESULT_NOTSUPPORTED;
6189 }
6190 gate_result_t gate_file_get_attributes(gate_string_t const* targetpath, gate_enumint_t* attribs, gate_enumint_t* access_bits)
6191 {
6192 return GATE_RESULT_NOTSUPPORTED;
6193
6194 }
6195 gate_result_t gate_file_set_attributes(gate_string_t const* targetpath,
6196 gate_enumint_t attribs, gate_enumint_t attribs_mask,
6197 gate_enumint_t access_bits, gate_enumint_t access_mask)
6198 {
6199 return GATE_RESULT_NOTSUPPORTED;
6200 }
6201 gate_result_t gate_file_get_times(gate_string_t const* targetpath, gate_timestamp_t* modified, gate_timestamp_t* accessed, gate_timestamp_t* created)
6202 {
6203 return GATE_RESULT_NOTSUPPORTED;
6204 }
6205 gate_result_t gate_file_set_times(gate_string_t const* targetpath, gate_timestamp_t const* modified, gate_timestamp_t const* accessed, gate_timestamp_t const* created)
6206 {
6207 return GATE_RESULT_NOTSUPPORTED;
6208 }
6209 gate_result_t gate_file_get_size(gate_string_t const* targetfile, gate_int64_t* filesize)
6210 {
6211 return GATE_RESULT_NOTSUPPORTED;
6212 }
6213 gate_result_t gate_file_get_owner(gate_string_t const* targetfile,
6214 gate_uint32_t* owner_id, gate_string_t* owner_name)
6215 {
6216 return GATE_RESULT_NOTSUPPORTED;
6217 }
6218
6219 gate_result_t gate_file_open(gate_string_t const* filepath, gate_enumint_t flags, gate_file_t* filehandle)
6220 {
6221 return GATE_RESULT_NOTSUPPORTED;
6222 }
6223 gate_result_t gate_file_read(gate_file_t filehandle, char* buffer, gate_size_t bufferlength, gate_size_t* bufferused)
6224 {
6225 return GATE_RESULT_NOTSUPPORTED;
6226 }
6227 gate_result_t gate_file_write(gate_file_t filehandle, char const* buffer, gate_size_t bufferlength, gate_size_t* written)
6228 {
6229 return GATE_RESULT_NOTSUPPORTED;
6230 }
6231 gate_result_t gate_file_flush(gate_file_t filehandle)
6232 {
6233 return GATE_RESULT_NOTSUPPORTED;
6234 }
6235 gate_result_t gate_file_close(gate_file_t filehandle)
6236 {
6237 return GATE_RESULT_NOTSUPPORTED;
6238 }
6239 gate_result_t gate_file_seek(gate_file_t filehandle, gate_int64_t position, gate_enumint_t origin, gate_int64_t* newposition)
6240 {
6241 return GATE_RESULT_NOTSUPPORTED;
6242 }
6243 gate_result_t gate_file_pos(gate_file_t filehandle, gate_int64_t* position)
6244 {
6245 return GATE_RESULT_NOTSUPPORTED;
6246 }
6247 gate_result_t gate_file_size(gate_file_t filehandle, gate_int64_t* size)
6248 {
6249 return GATE_RESULT_NOTSUPPORTED;
6250 }
6251 gate_result_t gate_file_lock(gate_file_t filehandle, gate_bool_t wait)
6252 {
6253 return GATE_RESULT_NOTSUPPORTED;
6254 }
6255 gate_result_t gate_file_unlock(gate_file_t filehandle)
6256 {
6257 return GATE_RESULT_NOTSUPPORTED;
6258 }
6259 gate_result_t gate_file_root_list(gate_file_list_callback_t callback, void* userparam)
6260 {
6261 return GATE_RESULT_NOTSUPPORTED;
6262 }
6263 gate_result_t gate_file_list(gate_string_t const* dirpath, gate_file_list_callback_t callback, void* userparam)
6264 {
6265 return GATE_RESULT_NOTSUPPORTED;
6266 }
6267 gate_result_t gate_file_get_entry(gate_string_t const* filepath, gate_file_entry_t* ptrtonewentry)
6268 {
6269 return GATE_RESULT_NOTSUPPORTED;
6270 }
6271 gate_result_t gate_file_dir_create(gate_string_t const* dirpath, gate_enumint_t flags)
6272 {
6273 return GATE_RESULT_NOTSUPPORTED;
6274 }
6275 gate_result_t gate_file_dir_delete(gate_string_t const* dirpath)
6276 {
6277 return GATE_RESULT_NOTSUPPORTED;
6278 }
6279 gate_result_t gate_file_dir_exists(gate_string_t const* dirpath)
6280 {
6281 return GATE_RESULT_NOTSUPPORTED;
6282 }
6283 gate_result_t gate_file_dir_open(gate_string_t const* dirpath, gate_file_dirreader_t* dirhandle)
6284 {
6285 return GATE_RESULT_NOTSUPPORTED;
6286 }
6287 gate_size_t gate_file_dir_read(gate_file_dirreader_t* dirhandle, char* path, gate_size_t pathlen, gate_bool_t filename_only)
6288 {
6289 return GATE_RESULT_NOTSUPPORTED;
6290 }
6291 gate_result_t gate_file_dir_close(gate_file_dirreader_t* dirhandle)
6292 {
6293 return GATE_RESULT_NOTSUPPORTED;
6294 }
6295 gate_result_t gate_file_truncate(gate_file_t filehandle)
6296 {
6297 return GATE_RESULT_NOTSUPPORTED;
6298 }
6299
6300 #endif /* GATE_CORE_FILES_NO_IMPL */
6301
6302