GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_files.cpp
Date: 2026-02-03 22:06:38
Exec Total Coverage
Lines: 377 407 92.6%
Functions: 97 102 95.1%
Branches: 108 242 44.6%

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
29 #include "gate/files.hpp"
30 #include "gate/results.h"
31 #include "gate/utilities.h"
32 #include "gate/utilities.hpp"
33 #include "gate/exceptions.hpp"
34 #include "gate/strings.hpp"
35
36 namespace gate
37 {
38 enumint_t const File::Open_Read = GATE_STREAM_OPEN_READ;
39 enumint_t const File::Open_Write = GATE_STREAM_OPEN_WRITE;
40 enumint_t const File::Open_ReadWrite = GATE_STREAM_OPEN_READWRITE;
41 enumint_t const File::Open_Append = GATE_STREAM_OPEN_APPEND;
42 enumint_t const File::Open_AppendWrite = GATE_STREAM_OPEN_APPENDWRITE;
43 enumint_t const File::Open_AppendReadWrite = GATE_STREAM_OPEN_APPENDREADWRITE;
44 enumint_t const File::Open_CreateOwnerRestricted = GATE_FILE_OPEN_CREATEOWNERRESTRICTED;
45 enumint_t const File::Open_CreateGroupRestricted = GATE_FILE_OPEN_CREATEGROUPRESTRICTED;
46 enumint_t const File::Open_CreateUnrestricted = GATE_FILE_OPEN_CREATEUNRESTRICTED;
47 enumint_t const File::Open_CreateExecutable = GATE_FILE_OPEN_CREATEEXECUTABLE;
48 enumint_t const File::Open_Shared = GATE_FILE_OPEN_SHARED;
49
50 12 static gate_controlstream_t* open_filestream(String const& filepath, enumint_t flags)
51 {
52 12 gate_controlstream_t* ret = NULL;
53
1/2
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
12 result_t res = gate_file_openstream(filepath.c_impl(), flags, &ret);
54
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
12 GATEXX_CHECK_EXCEPTION(res);
55 12 return ret;
56 }
57
58 12 FileStream::FileStream(String const& filepath, enumint_t flags)
59 12 : ControlStream(open_filestream(filepath, flags))
60 {
61 12 }
62 1 FileStream::FileStream(FileStream const& src)
63 1 : ControlStream(src)
64 {
65
66 1 }
67 1 FileStream& FileStream::operator=(FileStream const& src)
68 {
69 1 Stream::operator=(src);
70 1 return *this;
71 }
72 13 FileStream::~FileStream()
73 {
74
75 13 }
76
77
78
79
80
81
82 10 String File::buildPath(String const& parent, String const& subitem)
83 {
84 gate_string_t tmp;
85
2/4
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
10 if (NULL == gate_file_build_path_string(&tmp, parent.c_impl(), subitem.c_impl()))
86 {
87 GATEXX_RAISE_ERROR(results::OutOfMemory);
88 }
89 10 String ret(String::duplicate(tmp));
90
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 gate_string_release(&tmp);
91 20 return ret;
92 }
93 3 bool File::splitPath(String const& path, String& parent, String& subitem)
94 {
95 3 gate_string_t parent_str = GATE_STRING_INIT_EMPTY;
96 3 gate_string_t subitem_str = GATE_STRING_INIT_EMPTY;
97
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 gate_result_t result = gate_file_split_path(path.c_impl(), &parent_str, &subitem_str);
98
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (GATE_SUCCEEDED(result))
99 {
100 2 parent = String::createFrom(parent_str);
101 2 subitem = String::createFrom(subitem_str);
102 2 return true;
103 }
104 else
105 {
106 1 parent = String::createFrom(parent_str);
107
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 subitem = path.clone();
108 1 return false;
109 }
110 }
111 1 void File::extractPath(String const& path, char* name, size_t nameCapacity, char* parent, size_t parentCapacity)
112 {
113 1 result_t result = gate_file_extract_path(path.c_str(), path.length(), name, nameCapacity, parent, parentCapacity);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
115 1 }
116 1 void File::extractPath(String const& path, String* name, String* parent)
117 {
118 gate_string_t str_name;
119 gate_string_t str_parent;
120
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 result_t result = gate_file_extract_path_string(
121 path.c_impl(),
122 name ? &str_name : NULL,
123 parent ? &str_parent : NULL);
124
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
125
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (name)
126 {
127 1 *name = String::createFrom(str_name);
128 }
129
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (parent)
130 {
131 1 *parent = String::createFrom(str_parent);
132 }
133 1 }
134
135
136 2 bool_t File::getParent(String const& path, String& parent)
137 {
138 2 gate_string_t parent_str = GATE_STRING_INIT_EMPTY;
139
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 gate_result_t result = gate_file_split_path(path.c_impl(), &parent_str, NULL);
140
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_SUCCEEDED(result))
141 {
142 2 parent = String::createFrom(parent_str);
143 2 return true;
144 }
145 return false;
146 }
147 3 bool_t File::getName(String const& path, String& name)
148 {
149 3 gate_string_t name_str = GATE_STRING_INIT_EMPTY;
150
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 gate_result_t result = gate_file_split_path(path.c_impl(), NULL, &name_str);
151
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (GATE_SUCCEEDED(result))
152 {
153 3 name = String::createFrom(name_str);
154 3 return true;
155 }
156 return false;
157 }
158
159 2 String File::baseName(String const& path)
160 {
161 2 String ret;
162
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 File::getName(path, ret);
163 2 return ret;
164 }
165
166 1 String File::dirName(String const& path)
167 {
168 1 String ret;
169
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 File::getParent(path, ret);
170 1 return ret;
171 }
172
173 1 String File::realPath(String const& path)
174 {
175 1 gate_string_t ret = GATE_STRING_INIT_EMPTY;
176
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_real_path(path.c_impl(), &ret);
177
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
178 2 return String::createFrom(ret);
179 }
180
181 1 ControlStream File::open(String const& path, enumint_t openFlags)
182 {
183 1 gate_controlstream_t* stream = NULL;
184
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_openstream(path.c_impl(), openFlags, &stream);
185
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
186
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return ControlStream(stream);
187 }
188
189
190 23 bool File::exists(String const& filepath)
191 {
192 23 return GATE_SUCCEEDED(gate_file_exists(filepath.c_impl()));
193 }
194 1 void File::copy(String const& srcfilepath, String const& dstfilepath, CopyEnum flags)
195 {
196 1 result_t const res = gate_file_copy(srcfilepath.c_impl(), dstfilepath.c_impl(), (gate_uint32_t)flags);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
198 1 }
199
200 1 static gate_bool_t File_copyList_callback(char const* source_path, char const* dest_path, gate_result_t result, void* user_param)
201 {
202 1 File::CopyListCallback const* const ptrCb = static_cast<File::CopyListCallback const*>(user_param);
203
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 String const source(source_path);
204
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 String const dest(dest_path);
205
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ptrCb->invoke(source, dest, result);
206 2 return true;
207 }
208
209 1 void File::copyList(Map<String, String> const& fileMapping, CopyListCallback const& callback, enumint_t flags)
210 {
211 1 result_t const result = gate_file_copy_list(
212 fileMapping.c_impl(), flags,
213 &File_copyList_callback, const_cast<File::CopyListCallback*>(&callback));
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
215 1 }
216
217
218 1 void File::move(String const& srcfilepath, String const& dstfilepath)
219 {
220 1 result_t res = gate_file_move(srcfilepath.c_impl(), dstfilepath.c_impl());
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
222 1 }
223 4 void File::removeFile(String const& filepath)
224 {
225 4 result_t res = gate_file_delete(filepath.c_impl());
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATEXX_CHECK_EXCEPTION(res);
227 4 }
228 1 void File::createLink(String const& targetpath, String const& linkfile)
229 {
230 1 result_t res = gate_file_create_link(targetpath.c_impl(), linkfile.c_impl());
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
232 1 }
233 1 void File::createHardLink(String const& targetpath, String const& newlinkpath)
234 {
235 1 result_t res = gate_file_create_hardlink(targetpath.c_impl(), newlinkpath.c_impl());
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
237 1 }
238 1 String File::readLink(String const& linkfilepath)
239 {
240 gate_string_t realpath;
241
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t res = gate_file_read_link(linkfilepath.c_impl(), &realpath);
242
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(res);
243 2 return String::createFrom(realpath);
244 }
245
246 struct GATE_API_LOCAL gate_directory_list_entries_struct
247 {
248 File::callback_t delegate_cb;
249 void* user_param;
250
251 2 gate_directory_list_entries_struct(File::callback_t const& cb, void* param)
252 2 : delegate_cb(cb), user_param(param)
253 {
254 2 }
255 };
256
257 20 File::Entry::Entry()
258 {
259 20 gate_mem_clear(static_cast<gate_file_entry_t*>(this), sizeof(gate_file_entry_t));
260 20 }
261
262 30 File::Entry::Entry(gate_file_entry_t const& entry)
263 {
264 30 gate_mem_copy(static_cast<gate_file_entry_t*>(this), &entry, sizeof(gate_file_entry_t));
265 30 }
266 1 File::Entry::Entry(Entry const& src)
267 {
268 1 gate_mem_copy(static_cast<gate_file_entry_t*>(this),
269 static_cast<gate_file_entry_t const*>(&src),
270 sizeof(gate_file_entry_t));
271 1 }
272 1 File::Entry& File::Entry::operator=(Entry const& src)
273 {
274
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (this != &src)
275 {
276 1 gate_mem_copy(static_cast<gate_file_entry_t*>(this),
277 static_cast<gate_file_entry_t const*>(&src),
278 sizeof(gate_file_entry_t));
279 }
280 1 return *this;
281 }
282
283 51 File::Entry::~Entry() noexcept
284 {
285 51 }
286
287 19 String File::Entry::getName() const
288 {
289 19 return String(this->name, gate_str_length(this->name));
290 }
291 25 String File::Entry::getPath() const
292 {
293 25 return String(this->path, gate_str_length(this->path));
294 }
295 2 uint64_t File::Entry::getSize() const
296 {
297 2 return this->props.size;
298 }
299 1 enumint_t File::Entry::getAttributes() const
300 {
301 1 return this->props.attribs;
302 }
303 1 enumint_t File::Entry::getAccessBits() const
304 {
305 1 return this->props.access;
306 }
307
308 1 DateTime File::Entry::getTimeCreated(bool_t addSystemBias) const
309 {
310 1 return DateTime(this->props.time_created, addSystemBias);
311 }
312 1 DateTime File::Entry::getTimeModified(bool_t addSystemBias) const
313 {
314 1 return DateTime(this->props.time_modified, addSystemBias);
315 }
316 1 DateTime File::Entry::getTimeAccessed(bool_t addSystemBias) const
317 {
318 1 return DateTime(this->props.time_accessed, addSystemBias);
319 }
320
321
322 2 bool_t File::Entry::isDirectory() const
323 {
324 2 return GATE_FLAG_ENABLED(this->props.attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY);
325 }
326 2 bool_t File::Entry::isVolume() const
327 {
328 2 return GATE_FLAG_ENABLED(this->props.attribs, GATE_FILEENTRY_ATTRIB_VOLUME);
329 }
330 2 bool_t File::Entry::isExecutable() const
331 {
332 2 return 0 != (this->props.access & (GATE_FILEENTRY_ACCESS_OWNEREXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_ALLEXECUTE));
333 }
334
335
336 19 File::Properties::Properties(gate_file_properties_t const& props)
337 {
338 19 gate_mem_copy(static_cast<gate_file_properties_t*>(this),
339 static_cast<gate_file_properties_t const*>(&props),
340 sizeof(gate_file_properties_t));
341 19 }
342 18 File::Properties::Properties(Properties const& src)
343 {
344 18 gate_mem_copy(static_cast<gate_file_properties_t*>(this),
345 static_cast<gate_file_properties_t const*>(&src),
346 sizeof(gate_file_properties_t));
347
348 18 }
349 2 File::Properties& File::Properties::operator=(Properties const& src)
350 {
351 2 gate_mem_copy(static_cast<gate_file_properties_t*>(this),
352 static_cast<gate_file_properties_t const*>(&src),
353 sizeof(gate_file_properties_t));
354 2 return *this;
355 }
356 37 File::Properties::~Properties()
357 {
358
359 37 }
360
361 3 uint64_t File::Properties::getSize() const
362 {
363 3 return this->size;
364 }
365 1 enumint_t File::Properties::getAttributes() const
366 {
367 1 return this->attribs;
368 }
369 1 enumint_t File::Properties::getAccessBits() const
370 {
371 1 return this->access;
372 }
373 1 DateTime File::Properties::getTimeCreated(bool_t addSystemBias) const
374 {
375 1 return DateTime(this->time_created, addSystemBias);
376 }
377 1 DateTime File::Properties::getTimeModified(bool_t addSystemBias) const
378 {
379 1 return DateTime(this->time_modified, addSystemBias);
380 }
381 1 DateTime File::Properties::getTimeAccessed(bool_t addSystemBias) const
382 {
383 1 return DateTime(this->time_accessed, addSystemBias);
384 }
385
386 1 bool_t File::Properties::isDirectory() const
387 {
388 1 return GATE_FLAG_ENABLED(this->attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY);
389 }
390 1 bool_t File::Properties::isVolume() const
391 {
392 1 return GATE_FLAG_ENABLED(this->attribs, GATE_FILEENTRY_ATTRIB_VOLUME);
393 }
394 1 bool_t File::Properties::isExecutable() const
395 {
396 1 return 0 != (this->access &
397 1 (GATE_FILEENTRY_ACCESS_ALLEXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_OWNEREXECUTE));
398 }
399
400
401
402
403
404 2 static bool_t gate_file_list_entries(gate_file_entry_t const* ptr, void* userparam)
405 {
406 2 gate_directory_list_entries_struct* param = (gate_directory_list_entries_struct*)userparam;
407
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 File::Entry entry(*ptr);
408 2 bool_t continue_listing = true;
409
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 param->delegate_cb(&entry, param->user_param, &continue_listing);
410 4 return continue_listing;
411 }
412
413 1 void File::listEntries(String const& dirpath, callback_t const& callback, void* userparam)
414 {
415 2 gate_directory_list_entries_struct param(callback, userparam);
416
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 gate_result_t result = gate_file_list(dirpath.c_impl(), &gate_file_list_entries, &param);
417
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
418 1 }
419 2 static bool_t gate_file_list_entries_in_map(gate_file_entry_t const* ptr, void* userparam)
420 {
421 2 File::pathmap_t* mapping = static_cast<File::pathmap_t*>(userparam);
422 try
423 {
424
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 String path(ptr->path);
425
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 String name(ptr->name);
426
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 mapping->add(path, name);
427 }
428 catch (...) {}
429 2 return true;
430 }
431
432 1 File::pathmap_t File::listEntryPaths(String const& dirpath)
433 {
434 1 File::pathmap_t paths;
435
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_list(dirpath.c_impl(), &gate_file_list_entries_in_map, &paths);
436
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
437 1 return paths;
438 }
439
440 1 void File::listRootEntries(callback_t const& callback, void* userparam)
441 {
442 2 gate_directory_list_entries_struct param(callback, userparam);
443
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_result_t result = gate_file_root_list(&gate_file_list_entries, &param);
444
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
445 1 }
446
447
448 1 File::pathmap_t File::listRootEntryPaths()
449 {
450 1 File::pathmap_t paths;
451
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 result_t result = gate_file_root_list(&gate_file_list_entries_in_map, &paths);
452
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
453 1 return paths;
454 }
455
456
457 1 File::Entry File::getEntry(String const& filepath)
458 {
459 gate_file_entry_t direntry;
460
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 gate_result_t res = gate_file_get_entry(filepath.c_impl(), &direntry);
461
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(res);
462
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return File::Entry(direntry);
463 }
464 3 void File::createDirectory(String const& dirpath, CreateDirEnum mode)
465 {
466 3 result_t res = gate_file_dir_create(dirpath.c_impl(), static_cast<gate_enumint_t>(mode));
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATEXX_CHECK_EXCEPTION(res);
468 3 }
469 1 void File::createAllDirectories(String const& dirpath, CreateDirEnum mode)
470 {
471 1 result_t res = gate_file_dir_create_all(dirpath.c_impl(), static_cast<gate_enumint_t>(mode));
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
473 1 }
474
475 1 void File::removeDirectory(String const& dirpath)
476 {
477 1 result_t res = gate_file_dir_delete(dirpath.c_impl());
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
479 1 }
480 2 void File::removeDirectoryRecursive(String const& dirpath)
481 {
482 2 result_t res = gate_file_dir_delete_recursive(dirpath.c_impl());
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(res);
484 2 }
485
486
487 2 bool_t File::isDirectory(String const& dirpath)
488 {
489 2 result_t res = gate_file_dir_exists(dirpath.c_impl());
490 2 return GATE_SUCCEEDED(res);
491 }
492 1 bool_t File::isHidden(String const& path)
493 {
494 1 bool_t ret = false;
495 1 gate_enumint_t attrib = 0;
496 1 gate_enumint_t accessbits = 0;
497
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_get_attributes(path.c_impl(), &attrib, &accessbits);
498
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
499 {
500 1 ret = GATE_FLAG_ENABLED(attrib, GATE_FILEENTRY_ATTRIB_HIDDEN);
501 }
502 1 return ret;
503 }
504 1 bool_t File::isExecutable(String const& filepath)
505 {
506 1 bool_t ret = false;
507 1 gate_enumint_t attrib = 0;
508 1 gate_enumint_t accessbits = 0;
509
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_get_attributes(filepath.c_impl(), &attrib, &accessbits);
510
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
511 {
512
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!GATE_FLAG_ENABLED(attrib, GATE_FILEENTRY_ATTRIB_DIRECTORY))
513 {
514 1 ret = (0 != (accessbits &
515 (GATE_FILEENTRY_ACCESS_OWNEREXECUTE
516 | GATE_FILEENTRY_ACCESS_GROUPEXECUTE
517 | GATE_FILEENTRY_ACCESS_ALLEXECUTE))
518 );
519 }
520 }
521 1 return ret;
522 }
523
524
525 2 String File::getContent(String const& filePath)
526 {
527 2 gate_string_t buffer = GATE_STRING_INIT_EMPTY;
528
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 result_t result = gate_file_get_content(filePath.c_impl(), &buffer);
529
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2 GATEXX_CHECK_EXCEPTION(result);
530 4 return String::createFrom(buffer);
531 }
532 1 size_t File::getContent(String const& filePath, char* buffer, size_t buffer_len)
533 {
534 1 result_t result = gate_file_get_content_buffer(filePath.c_impl(), buffer, &buffer_len);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
536 1 return buffer_len;
537 }
538 1 ArrayList<String> File::getContentLines(String const& filePath)
539 {
540
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ArrayList<String> ret;
541
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_arraylist_t list = gate_util_stringarray_create();
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!list)
543 {
544 GATEXX_RAISE_ERROR(results::OutOfMemory);
545 }
546
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_get_content_lines(filePath.c_impl(), list);
547
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
548 {
549 try
550 {
551
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ret = util::convertStringArray(list);
552 }
553 catch (...)
554 {
555 gate_arraylist_release(list);
556 throw;
557 }
558 }
559
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_arraylist_release(list);
560 2 return ret;
561 }
562 1 void File::setContent(String const& filePath, String const& content)
563 {
564 1 result_t result = gate_file_set_content(filePath.c_impl(), content.c_impl());
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
566 1 }
567 1 void File::setContent(String const& filePath, char const* buffer, size_t buffer_len)
568 {
569 1 result_t result = gate_file_set_content_buffer(filePath.c_impl(), buffer, buffer_len);
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
571 1 }
572 1 void File::setContentLines(String const& filePath, ArrayList<String> const& lines)
573 {
574 1 gate_arraylist_t list = util::convertStringArray(lines);
575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!list)
576 {
577 GATEXX_RAISE_ERROR(results::OutOfMemory);
578 }
579 1 result_t result = gate_file_set_content_lines(filePath.c_impl(), list);
580 1 gate_arraylist_release(list);
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
582 1 }
583
584 1 File::SearchFilter::SearchFilter(String const& pattern, bool_t recursive,
585 uint64_t fromSize, uint64_t toSize,
586 1 timestamp_t fromModified, timestamp_t toModified)
587 {
588 1 gate_string_duplicate(&this->pattern, pattern.c_impl());
589 1 this->recursive = recursive;
590 1 this->from_size = fromSize;
591 1 this->to_size = toSize;
592 1 this->from_modified = fromModified;
593 1 this->to_modified = toModified;
594 1 }
595 2 File::SearchFilter::~SearchFilter() noexcept
596 {
597 1 gate_string_release(&this->pattern);
598 1 }
599
600 void File::SearchFilter::setPattern(String const& pattern)
601 {
602 String::assign(this->pattern, pattern);
603 }
604 String File::SearchFilter::getPattern() const
605 {
606 return String::clone(this->pattern);
607 }
608
609 struct GATE_API_LOCAL File_find_dispatcher_param
610 {
611 File::callback_t const* ptr_cb;
612 void* user_param;
613 Exception user_error;
614
615 1 File_find_dispatcher_param()
616 1 : user_error(results::Ok)
617 {
618 1 }
619 };
620
621 27 static gate_bool_t File_find_dispatcher(gate_file_entry_t const* ptr_entry, void* userparam)
622 {
623 27 File_find_dispatcher_param* ptr_param = static_cast<File_find_dispatcher_param*>(userparam);
624 27 bool_t continueSearch = true;
625
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (ptr_entry)
626 {
627
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
27 File::Entry entry(*ptr_entry);
628 try
629 {
630
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
27 ptr_param->ptr_cb->invoke(&entry, ptr_param->user_param, &continueSearch);
631 }
632 catch(Throwable const& xcpt)
633 {
634 ptr_param->user_error = Exception(xcpt.getResult(), xcpt.getMessage(), xcpt.getSource(), xcpt.getErrorCode());
635 return false;
636 }
637 catch(...)
638 {
639 ptr_param->user_error = Exception(results::UnknownException, NULL, "File::find_dispatcher");
640 return false;
641 }
642 }
643 27 return continueSearch;
644 }
645
646 1 void File::find(String const& path, SearchFilter const& filter, callback_t const& callback, void* userparam)
647 {
648 2 File_find_dispatcher_param param;
649 1 param.ptr_cb = &callback;
650 1 param.user_param = userparam;
651
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_find(path.c_impl(), &filter, &File_find_dispatcher, &param);
652
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
653
654 1 Exception const& xcpt = param.user_error;
655
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (GATE_FAILED(xcpt.getResult()))
656 {
657 gate::raiseException(
658 xcpt.getResult(), xcpt.getMessage(),
659 xcpt.getSource(), xcpt.getErrorCode());
660 }
661 1 }
662
663
664
665 String const FilePath::Separator = String::createStatic(gate_file_path_separator);
666
667 FilePath::FilePath()
668 {
669 }
670 2 FilePath::FilePath(FilePath const& src)
671 2 : value(src.value)
672 {
673 2 }
674 15 FilePath::FilePath(String const& path)
675 15 : value(path)
676 {
677 15 }
678 FilePath& FilePath::operator=(FilePath const& src)
679 {
680 this->value = src.value;
681 return *this;
682 }
683 17 FilePath::~FilePath()
684 {
685 17 }
686
687 7 FilePath FilePath::build(String const& parent, String const& child)
688 {
689 7 return FilePath(File::buildPath(parent, child));
690 }
691
692 1 ArrayList<String> FilePath::split(String const& path, char pathSeparator)
693 {
694 gate_string_t components[512];
695
696
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_mem_clear(&components[0], sizeof(components));
697
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (pathSeparator == 0)
698 {
699 1 pathSeparator = gate_file_path_separator_char;
700 }
701
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 String basePath = path.clone();
702
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 size_t used = gate_file_split_path_components(basePath.c_impl(), pathSeparator, components, sizeof(components) / sizeof(components[0]));
703
704
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ArrayList<String> ret(used);
705
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (size_t ndx = 0; ndx != used; ++ndx)
706 {
707
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 ret.add(String::createFrom(components[ndx]));
708 }
709
710 2 return ret;
711 }
712
713 1 ArrayList<String> FilePath::split(String const& path)
714 {
715 static char const defaultPathSeparator = 0;
716 1 return FilePath::split(path, defaultPathSeparator);
717 }
718
719
720
721 1 FilePath FilePath::getParent() const
722 {
723 2 String parent;
724
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 if (File::getParent(this->value, parent))
725 {
726 1 return FilePath(parent);
727 }
728 else
729 {
730 return FilePath();
731 }
732 }
733 1 FilePath FilePath::getName() const
734 {
735 2 String name;
736
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 if (File::getName(this->value, name))
737 {
738 1 return FilePath(name);
739 }
740 else
741 {
742 return FilePath();
743 }
744 }
745 14 String FilePath::toString() const
746 {
747 14 return this->value;
748 }
749
750 2 FilePath& FilePath::append(FilePath const& path)
751 {
752
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 String newPath = File::buildPath(this->value, path.toString());
753 2 this->value = newPath;
754 4 return *this;
755 }
756
757 1 FilePath& FilePath::operator/=(FilePath const& path)
758 {
759 1 return this->append(path);
760 }
761 1 FilePath& FilePath::operator/=(char const* path)
762 {
763
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 return this->append(FilePath(String::createStatic(path)));
764 }
765
766 2 FilePath FilePath::operator/(FilePath const& path) const
767 {
768 2 return FilePath::build(this->value, path.value);
769 }
770 5 FilePath FilePath::operator/(char const* path) const
771 {
772
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 return FilePath::build(this->value, String::createStatic(path));
773 }
774
775
776 FilePath::operator String() const
777 {
778 return this->toString();
779 }
780
781
782 2 FileDirReader::FileDirReader(String const& dirpath)
783 {
784 2 result_t result = gate_file_dir_open(dirpath.c_impl(), &this->impl);
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(result);
786 2 }
787
788 2 String FileDirReader::nextFilename()
789 {
790 char pathBuffer[GATE_MAX_FILEPATH_LENGTH];
791
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 size_t pathLength = gate_file_dir_read(&this->impl, pathBuffer, sizeof(pathBuffer), true);
792
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pathLength)
793 {
794
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return String(pathBuffer, pathLength);
795 }
796 1 return String();
797 }
798 2 String FileDirReader::nextPath()
799 {
800 char pathBuffer[GATE_MAX_FILEPATH_LENGTH];
801
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 size_t pathLength = gate_file_dir_read(&this->impl, pathBuffer, sizeof(pathBuffer), false);
802
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pathLength)
803 {
804
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return String(pathBuffer, pathLength);
805 }
806 1 return String();
807 }
808
809 4 FileDirReader::~FileDirReader()
810 {
811 2 gate_file_dir_close(&this->impl);
812 2 }
813
814
815
816 } // end of namespace gate
817