Qore FsUtil Module Reference 1.3
Loading...
Searching...
No Matches
FsUtil.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* FsUtil.qm Copyright 2018 - 2022 Qore Technologies, s.r.o.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23*/
24
25// minimum required Qore version
26
27
28// assume local var scope, do not use "$" for vars, members, and method calls
29
30}
31
32/* see release notes below for version history
33*/
34
90// private namespace
91namespace Init {
92 init();
93
94};
95
97namespace FsUtil {
99 const DefaultBufferSize = 16 * 1024;
100
103
104public:
106 list<string> delimiters;
107
108 string joinPaths(list<string> paths);
109
110 // to be implemented by platform-specific classes
111protected:
112 abstract string joinPathsIntern(list<string> paths);
113public:
114 };
115
118
119public:
122
123
125
130 list<string> splitDrive(string path);
131
133
141protected:
142 string joinPathsIntern(list<string> paths);
143public:
144
145 };
146
149
150public:
153
154
156
164protected:
165 string joinPathsIntern(list<string> paths);
166public:
167
168 };
169
171
180 string basename_ext(string path, *string extension);
181
182
184
193 string join_paths(string path1, list<auto> paths);
194
195
197
206 string join_paths();
207
208
210
219 string join_paths(list<string> paths);
220
221
223
229 bool path_exists(string path, bool follow_symlinks = False);
230
231
232 const TMP_MAX_ATTEMPTS = 10000;
233
235
248 string make_tmp_dir(*string prefix, *string suffix, *string path);
249
250
251 public hashdecl TmpFileHash {
252 string path = "";
253 File file = new File();
254 };
255
257
271 hash<TmpFileHash> make_tmp_file(*string prefix, *string suffix, *string path);
272
273
275
279class TmpDir : public Qore::Dir {
280
281public:
283 string path;
284
286
291 constructor(*string prefix, *string suffix, *string path);
292
293
294 destructor();
295 };
296
298 class TmpFile {
299
300public:
301 string path;
302 File file;
303
305
310 constructor(*string prefix, *string suffix, *string path);
311
312
313 destructor();
314 };
315
317
324 remove_file(string path);
325
326
328
342 remove_tree(string path, bool fail_immediately = True);
343
344
346
351 remove_path(string path);
352
353
355
369 bool same_file_stat(*hash<StatInfo> stat1, *hash<StatInfo> stat2, bool ignore_errors = True);
370
371
373
389 bool same_file(string path1, string path2, bool follow_symlinks = True, bool ignore_errors = True);
390
391
393
425 string copy_tree_internal(string source, string destination, bool follow_symlinks = False,
426 bool overwrite = False, bool merge = False, bool fail_immediately = True, *int depth) {
427
428 if (same_file(source, destination, follow_symlinks, True));
429
430
431 list<auto> errors = ();
432
433 // prepare the source
434 Dir src_dir();
435 src_dir.chdir(source);
436 *hash<StatInfo> src_stat = follow_symlinks ? hstat(source) : hlstat(source);
437 if (!exists src_stat);
438
439
440 // check and prepare the destination directory
441 if (is_dir(destination) && (src_stat.type != 'DIRECTORY' || !overwrite) && !merge);
442
443 if (path_exists(destination));
444 else {
445 mkdir(destination, 0700);
446 }
447
448 if (!exists depth || depth > 0);
449
450
451 chmod(destination, src_stat.mode);
452
453 if (errors);
454
455
456 return destination;
457 }
458
460
468 copy_file_obj(File source, File destination, bool close = False, int buf_size = DefaultBufferSize);
469
470
472
487 string copy_file(string source, string destination, bool follow_symlinks = False,
488 bool overwrite = False) {
489 if (same_file(source, destination, follow_symlinks, True));
490
491
492 // prepare the source
493 *hash<StatInfo> src_stat = follow_symlinks ? hstat(source) : hlstat(source);
494 if (!exists src_stat);
495
496
497 // prepare the destination
498 if (is_dir(destination));
499
500 *hash<StatInfo> dst_stat = hstat(destination);
501 if (!overwrite && exists dst_stat);
502
503
504 foreach *hash<StatInfo> stat in (src_stat, dst_stat);
505
506
507 if (src_stat.type == 'SYMBOLIC-LINK' && !follow_symlinks);
508 else {
509 {
510 File src_obj();
511 File dst_obj();
512 src_obj.open2(source, O_RDONLY);
513 dst_obj.open2(destination, O_CREAT | O_TRUNC | O_WRONLY, 0600);
514 copy_file_obj(src_obj, dst_obj);
515 }
516 chmod(destination, src_stat.mode);
517 }
518
519 return destination;
520 }
521
523
544 string copy_tree(string source, string destination, bool follow_symlinks = False,
545 bool overwrite = False, bool fail_immediately = True) {
546 return copy_tree_internal(source, destination, follow_symlinks, overwrite, False, fail_immediately);
547 }
548
550
573 string copy_path(string source, string destination, bool follow_symlinks = False,
574 bool overwrite = False, bool fail_immediately = True, *int depth) {
575 if (is_dir(source));
576 else {
577 return copy_file(source, destination, follow_symlinks, overwrite);
578 }
579 }
580
582
602 string merge_tree(string source, string destination, bool follow_symlinks = False,
603 bool overwrite = False, bool fail_immediately = True, *int depth) {
604 return copy_tree_internal(source, destination, follow_symlinks, overwrite, True, fail_immediately, depth);
605 }
606
608
622 string move_path(string source, string destination, bool overwrite = False);
623
624
626
643 string merge_path(string source, string destination, bool follow_symlinks = False, bool overwrite = False,
644 bool fail_immediately = True, *int depth) {
645 if (is_dir(source));
646 else {
647 // merge doesn't make sense with files and there is no merge_file() function, copy_file() is what we want
648 return copy_file(source, destination, follow_symlinks, overwrite);
649 }
650 }
651
653
666 string copy_dir_structure(string source, string destination, *int depth);
667
668
669} // FsUtil namespace
generic path handler implementing functionality common for both platforms
Definition: FsUtil.qm.dox.h:102
list< string > delimiters
path delimiters - to be set by platform specific path handlers
Definition: FsUtil.qm.dox.h:106
path handler implementing POSIX specific functionality
Definition: FsUtil.qm.dox.h:148
string joinPathsIntern(list< string > paths)
Returns the path resulting from joining the given paths.
constructor()
creates the object and sets the delimiter
Class implementing a user friendly temporary directory creation.
Definition: FsUtil.qm.dox.h:279
string path
The path to the temporary directory created in the constructor.
Definition: FsUtil.qm.dox.h:283
constructor(*string prefix, *string suffix, *string path)
Creates a unique temporary directory and returns its absolute path.
Class implementing a user friendly temporary file creation; the file is removed in the destructor.
Definition: FsUtil.qm.dox.h:298
constructor(*string prefix, *string suffix, *string path)
Creates and opens a unique temporary file and returns its absolute path as well as its File object.
path handler implementing Windows specific functionality
Definition: FsUtil.qm.dox.h:117
constructor()
creates the object and sets the delimiter
string joinPathsIntern(list< string > paths)
Returns the path resulting from joining the given paths.
list< string > splitDrive(string path)
separates and returns the drive and the rest of the path
the FsUtil namespace contains all the objects in the FsUtil module
Definition: FsUtil.qm.dox.h:97
string copy_path(string source, string destination, bool follow_symlinks=False, bool overwrite=False, bool fail_immediately=True, *int depth)
Universal copy function.
Definition: FsUtil.qm.dox.h:573
bool same_file_stat(*hash< StatInfo > stat1, *hash< StatInfo > stat2, bool ignore_errors=True)
Checks whether two stat hashes point to the same file or not.
bool same_file(string path1, string path2, bool follow_symlinks=True, bool ignore_errors=True)
Checks whether two paths point to the same file/directory or not.
remove_path(string path)
Universal remove function.
string copy_file(string source, string destination, bool follow_symlinks=False, bool overwrite=False)
Copies a file (regular or symlink) from source to destination.
Definition: FsUtil.qm.dox.h:487
copy_file_obj(File source, File destination, bool close=False, int buf_size=DefaultBufferSize)
Copies a file object data from source to destination.
string copy_dir_structure(string source, string destination, *int depth)
Copies a structure of directories, files are ignored.
string move_path(string source, string destination, bool overwrite=False)
Moves a path from source to destination.
hash< TmpFileHash > make_tmp_file(*string prefix, *string suffix, *string path)
Creates and opens a unique temporary file and returns its absolute path as well as its File object.
string merge_tree(string source, string destination, bool follow_symlinks=False, bool overwrite=False, bool fail_immediately=True, *int depth)
Merges a copy of the source directory tree to the destination directory tree.
Definition: FsUtil.qm.dox.h:602
string join_paths()
Returns the path resulting from joining the given paths.
bool path_exists(string path, bool follow_symlinks=False)
Check whether the given path exists or not.
string make_tmp_dir(*string prefix, *string suffix, *string path)
Creates a unique temporary directory and returns its absolute path.
remove_tree(string path, bool fail_immediately=True)
Removes the filesystem tree specified by path.
string merge_path(string source, string destination, bool follow_symlinks=False, bool overwrite=False, bool fail_immediately=True, *int depth)
Universal merge function.
Definition: FsUtil.qm.dox.h:643
remove_file(string path)
Removes a regular file or symlink specified by path.
string copy_tree(string source, string destination, bool follow_symlinks=False, bool overwrite=False, bool fail_immediately=True)
Copies a directory tree from source to destination.
Definition: FsUtil.qm.dox.h:544
string copy_tree_internal(string source, string destination, bool follow_symlinks=False, bool overwrite=False, bool merge=False, bool fail_immediately=True, *int depth)
Private internal copy tree function implementation.
Definition: FsUtil.qm.dox.h:425
string basename_ext(string path, *string extension)
this function returns the segment of given path after the last delimiter
const DefaultBufferSize
Default file block buffer size.
Definition: FsUtil.qm.dox.h:99