]> git.lizzy.rs Git - dragonfireclient.git/blob - src/filesys.cpp
Fix some minor code issues all over the place
[dragonfireclient.git] / src / filesys.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "filesys.h"
21 #include "util/string.h"
22 #include <iostream>
23 #include <cstdio>
24 #include <cstring>
25 #include <cerrno>
26 #include <fstream>
27 #include "log.h"
28 #include "config.h"
29 #include "porting.h"
30 #ifdef __ANDROID__
31 #include "settings.h" // For g_settings
32 #endif
33
34 namespace fs
35 {
36
37 #ifdef _WIN32 // WINDOWS
38
39 #define _WIN32_WINNT 0x0501
40 #include <windows.h>
41 #include <shlwapi.h>
42
43 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
44 {
45         std::vector<DirListNode> listing;
46
47         WIN32_FIND_DATA FindFileData;
48         HANDLE hFind = INVALID_HANDLE_VALUE;
49         DWORD dwError;
50
51         std::string dirSpec = pathstring + "\\*";
52
53         // Find the first file in the directory.
54         hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
55
56         if (hFind == INVALID_HANDLE_VALUE) {
57                 dwError = GetLastError();
58                 if (dwError != ERROR_FILE_NOT_FOUND && dwError != ERROR_PATH_NOT_FOUND) {
59                         errorstream << "GetDirListing: FindFirstFile error."
60                                         << " Error is " << dwError << std::endl;
61                 }
62         } else {
63                 // NOTE:
64                 // Be very sure to not include '..' in the results, it will
65                 // result in an epic failure when deleting stuff.
66
67                 DirListNode node;
68                 node.name = FindFileData.cFileName;
69                 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
70                 if (node.name != "." && node.name != "..")
71                         listing.push_back(node);
72
73                 // List all the other files in the directory.
74                 while (FindNextFile(hFind, &FindFileData) != 0) {
75                         DirListNode node;
76                         node.name = FindFileData.cFileName;
77                         node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
78                         if(node.name != "." && node.name != "..")
79                                 listing.push_back(node);
80                 }
81
82                 dwError = GetLastError();
83                 FindClose(hFind);
84                 if (dwError != ERROR_NO_MORE_FILES) {
85                         errorstream << "GetDirListing: FindNextFile error."
86                                         << " Error is " << dwError << std::endl;
87                         listing.clear();
88                         return listing;
89                 }
90         }
91         return listing;
92 }
93
94 bool CreateDir(const std::string &path)
95 {
96         bool r = CreateDirectory(path.c_str(), NULL);
97         if(r == true)
98                 return true;
99         if(GetLastError() == ERROR_ALREADY_EXISTS)
100                 return true;
101         return false;
102 }
103
104 bool PathExists(const std::string &path)
105 {
106         return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
107 }
108
109 bool IsPathAbsolute(const std::string &path)
110 {
111         return !PathIsRelative(path.c_str());
112 }
113
114 bool IsDir(const std::string &path)
115 {
116         DWORD attr = GetFileAttributes(path.c_str());
117         return (attr != INVALID_FILE_ATTRIBUTES &&
118                         (attr & FILE_ATTRIBUTE_DIRECTORY));
119 }
120
121 bool IsDirDelimiter(char c)
122 {
123         return c == '/' || c == '\\';
124 }
125
126 bool RecursiveDelete(const std::string &path)
127 {
128         infostream << "Recursively deleting \"" << path << "\"" << std::endl;
129         if (!IsDir(path)) {
130                 infostream << "RecursiveDelete: Deleting file  " << path << std::endl;
131                 if (!DeleteFile(path.c_str())) {
132                         errorstream << "RecursiveDelete: Failed to delete file "
133                                         << path << std::endl;
134                         return false;
135                 }
136                 return true;
137         }
138         infostream << "RecursiveDelete: Deleting content of directory "
139                         << path << std::endl;
140         std::vector<DirListNode> content = GetDirListing(path);
141         for (const DirListNode &n: content) {
142                 std::string fullpath = path + DIR_DELIM + n.name;
143                 if (!RecursiveDelete(fullpath)) {
144                         errorstream << "RecursiveDelete: Failed to recurse to "
145                                         << fullpath << std::endl;
146                         return false;
147                 }
148         }
149         infostream << "RecursiveDelete: Deleting directory " << path << std::endl;
150         if (!RemoveDirectory(path.c_str())) {
151                 errorstream << "Failed to recursively delete directory "
152                                 << path << std::endl;
153                 return false;
154         }
155         return true;
156 }
157
158 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
159 {
160         DWORD attr = GetFileAttributes(path.c_str());
161         bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
162                         (attr & FILE_ATTRIBUTE_DIRECTORY));
163         if(!is_directory)
164         {
165                 bool did = DeleteFile(path.c_str());
166                 return did;
167         }
168         else
169         {
170                 bool did = RemoveDirectory(path.c_str());
171                 return did;
172         }
173 }
174
175 std::string TempPath()
176 {
177         DWORD bufsize = GetTempPath(0, NULL);
178         if(bufsize == 0){
179                 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
180                 return "";
181         }
182         std::vector<char> buf(bufsize);
183         DWORD len = GetTempPath(bufsize, &buf[0]);
184         if(len == 0 || len > bufsize){
185                 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
186                 return "";
187         }
188         return std::string(buf.begin(), buf.begin() + len);
189 }
190
191 #else // POSIX
192
193 #include <sys/types.h>
194 #include <dirent.h>
195 #include <sys/stat.h>
196 #include <sys/wait.h>
197 #include <unistd.h>
198
199 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
200 {
201         std::vector<DirListNode> listing;
202
203         DIR *dp;
204         struct dirent *dirp;
205         if((dp = opendir(pathstring.c_str())) == NULL) {
206                 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
207                 return listing;
208         }
209
210         while ((dirp = readdir(dp)) != NULL) {
211                 // NOTE:
212                 // Be very sure to not include '..' in the results, it will
213                 // result in an epic failure when deleting stuff.
214                 if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
215                         continue;
216
217                 DirListNode node;
218                 node.name = dirp->d_name;
219
220                 int isdir = -1; // -1 means unknown
221
222                 /*
223                         POSIX doesn't define d_type member of struct dirent and
224                         certain filesystems on glibc/Linux will only return
225                         DT_UNKNOWN for the d_type member.
226
227                         Also we don't know whether symlinks are directories or not.
228                 */
229 #ifdef _DIRENT_HAVE_D_TYPE
230                 if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
231                         isdir = (dirp->d_type == DT_DIR);
232 #endif /* _DIRENT_HAVE_D_TYPE */
233
234                 /*
235                         Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
236                         If so, try stat().
237                 */
238                 if(isdir == -1) {
239                         struct stat statbuf{};
240                         if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
241                                 continue;
242                         isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
243                 }
244                 node.dir = isdir;
245                 listing.push_back(node);
246         }
247         closedir(dp);
248
249         return listing;
250 }
251
252 bool CreateDir(const std::string &path)
253 {
254         int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
255         if (r == 0) {
256                 return true;
257         }
258
259         // If already exists, return true
260         if (errno == EEXIST)
261                 return true;
262         return false;
263
264 }
265
266 bool PathExists(const std::string &path)
267 {
268         struct stat st{};
269         return (stat(path.c_str(),&st) == 0);
270 }
271
272 bool IsPathAbsolute(const std::string &path)
273 {
274         return path[0] == '/';
275 }
276
277 bool IsDir(const std::string &path)
278 {
279         struct stat statbuf{};
280         if(stat(path.c_str(), &statbuf))
281                 return false; // Actually error; but certainly not a directory
282         return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
283 }
284
285 bool IsDirDelimiter(char c)
286 {
287         return c == '/';
288 }
289
290 bool RecursiveDelete(const std::string &path)
291 {
292         /*
293                 Execute the 'rm' command directly, by fork() and execve()
294         */
295
296         infostream<<"Removing \""<<path<<"\""<<std::endl;
297
298         pid_t child_pid = fork();
299
300         if(child_pid == 0)
301         {
302                 // Child
303                 const char *argv[4] = {
304 #ifdef __ANDROID__
305                         "/system/bin/rm",
306 #else
307                         "/bin/rm",
308 #endif
309                         "-rf",
310                         path.c_str(),
311                         NULL
312                 };
313
314                 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
315                                 <<argv[2]<<"'"<<std::endl;
316
317                 execv(argv[0], const_cast<char**>(argv));
318
319                 // Execv shouldn't return. Failed.
320                 _exit(1);
321         }
322         else
323         {
324                 // Parent
325                 int child_status;
326                 pid_t tpid;
327                 do{
328                         tpid = wait(&child_status);
329                 }while(tpid != child_pid);
330                 return (child_status == 0);
331         }
332 }
333
334 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
335 {
336         if (IsDir(path)) {
337                 bool did = (rmdir(path.c_str()) == 0);
338                 if (!did)
339                         errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
340                                         << std::endl;
341                 return did;
342         }
343
344         bool did = (unlink(path.c_str()) == 0);
345         if (!did)
346                 errorstream << "unlink errno: " << errno << ": " << strerror(errno)
347                                 << std::endl;
348         return did;
349 }
350
351 std::string TempPath()
352 {
353         /*
354                 Should the environment variables TMPDIR, TMP and TEMP
355                 and the macro P_tmpdir (if defined by stdio.h) be checked
356                 before falling back on /tmp?
357
358                 Probably not, because this function is intended to be
359                 compatible with lua's os.tmpname which under the default
360                 configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
361         */
362 #ifdef __ANDROID__
363         return g_settings->get("TMPFolder");
364 #else
365         return DIR_DELIM "tmp";
366 #endif
367 }
368
369 #endif
370
371 void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
372 {
373         static const std::set<char> chars_to_ignore = { '_', '.' };
374         if (dir.empty() || !IsDir(dir))
375                 return;
376         dirs.push_back(dir);
377         fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore);
378 }
379
380 std::vector<std::string> GetRecursiveDirs(const std::string &dir)
381 {
382         std::vector<std::string> result;
383         GetRecursiveDirs(result, dir);
384         return result;
385 }
386
387 void GetRecursiveSubPaths(const std::string &path,
388                   std::vector<std::string> &dst,
389                   bool list_files,
390                   const std::set<char> &ignore)
391 {
392         std::vector<DirListNode> content = GetDirListing(path);
393         for (const auto &n : content) {
394                 std::string fullpath = path + DIR_DELIM + n.name;
395                 if (ignore.count(n.name[0]))
396                         continue;
397                 if (list_files || n.dir)
398                         dst.push_back(fullpath);
399                 if (n.dir)
400                         GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
401         }
402 }
403
404 bool DeletePaths(const std::vector<std::string> &paths)
405 {
406         bool success = true;
407         // Go backwards to succesfully delete the output of GetRecursiveSubPaths
408         for(int i=paths.size()-1; i>=0; i--){
409                 const std::string &path = paths[i];
410                 bool did = DeleteSingleFileOrEmptyDirectory(path);
411                 if(!did){
412                         errorstream<<"Failed to delete "<<path<<std::endl;
413                         success = false;
414                 }
415         }
416         return success;
417 }
418
419 bool RecursiveDeleteContent(const std::string &path)
420 {
421         infostream<<"Removing content of \""<<path<<"\""<<std::endl;
422         std::vector<DirListNode> list = GetDirListing(path);
423         for (const DirListNode &dln : list) {
424                 if(trim(dln.name) == "." || trim(dln.name) == "..")
425                         continue;
426                 std::string childpath = path + DIR_DELIM + dln.name;
427                 bool r = RecursiveDelete(childpath);
428                 if(!r) {
429                         errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
430                         return false;
431                 }
432         }
433         return true;
434 }
435
436 bool CreateAllDirs(const std::string &path)
437 {
438
439         std::vector<std::string> tocreate;
440         std::string basepath = path;
441         while(!PathExists(basepath))
442         {
443                 tocreate.push_back(basepath);
444                 basepath = RemoveLastPathComponent(basepath);
445                 if(basepath.empty())
446                         break;
447         }
448         for(int i=tocreate.size()-1;i>=0;i--)
449                 if(!CreateDir(tocreate[i]))
450                         return false;
451         return true;
452 }
453
454 bool CopyFileContents(const std::string &source, const std::string &target)
455 {
456         FILE *sourcefile = fopen(source.c_str(), "rb");
457         if(sourcefile == NULL){
458                 errorstream<<source<<": can't open for reading: "
459                         <<strerror(errno)<<std::endl;
460                 return false;
461         }
462
463         FILE *targetfile = fopen(target.c_str(), "wb");
464         if(targetfile == NULL){
465                 errorstream<<target<<": can't open for writing: "
466                         <<strerror(errno)<<std::endl;
467                 fclose(sourcefile);
468                 return false;
469         }
470
471         size_t total = 0;
472         bool retval = true;
473         bool done = false;
474         char readbuffer[BUFSIZ];
475         while(!done){
476                 size_t readbytes = fread(readbuffer, 1,
477                                 sizeof(readbuffer), sourcefile);
478                 total += readbytes;
479                 if(ferror(sourcefile)){
480                         errorstream<<source<<": IO error: "
481                                 <<strerror(errno)<<std::endl;
482                         retval = false;
483                         done = true;
484                 }
485                 if(readbytes > 0){
486                         fwrite(readbuffer, 1, readbytes, targetfile);
487                 }
488                 if(feof(sourcefile) || ferror(sourcefile)){
489                         // flush destination file to catch write errors
490                         // (e.g. disk full)
491                         fflush(targetfile);
492                         done = true;
493                 }
494                 if(ferror(targetfile)){
495                         errorstream<<target<<": IO error: "
496                                         <<strerror(errno)<<std::endl;
497                         retval = false;
498                         done = true;
499                 }
500         }
501         infostream<<"copied "<<total<<" bytes from "
502                 <<source<<" to "<<target<<std::endl;
503         fclose(sourcefile);
504         fclose(targetfile);
505         return retval;
506 }
507
508 bool CopyDir(const std::string &source, const std::string &target)
509 {
510         if(PathExists(source)){
511                 if(!PathExists(target)){
512                         fs::CreateAllDirs(target);
513                 }
514                 bool retval = true;
515                 std::vector<DirListNode> content = fs::GetDirListing(source);
516
517                 for (const auto &dln : content) {
518                         std::string sourcechild = source + DIR_DELIM + dln.name;
519                         std::string targetchild = target + DIR_DELIM + dln.name;
520                         if(dln.dir){
521                                 if(!fs::CopyDir(sourcechild, targetchild)){
522                                         retval = false;
523                                 }
524                         }
525                         else {
526                                 if(!fs::CopyFileContents(sourcechild, targetchild)){
527                                         retval = false;
528                                 }
529                         }
530                 }
531                 return retval;
532         }
533
534         return false;
535 }
536
537 bool PathStartsWith(const std::string &path, const std::string &prefix)
538 {
539         size_t pathsize = path.size();
540         size_t pathpos = 0;
541         size_t prefixsize = prefix.size();
542         size_t prefixpos = 0;
543         for(;;){
544                 bool delim1 = pathpos == pathsize
545                         || IsDirDelimiter(path[pathpos]);
546                 bool delim2 = prefixpos == prefixsize
547                         || IsDirDelimiter(prefix[prefixpos]);
548
549                 if(delim1 != delim2)
550                         return false;
551
552                 if(delim1){
553                         while(pathpos < pathsize &&
554                                         IsDirDelimiter(path[pathpos]))
555                                 ++pathpos;
556                         while(prefixpos < prefixsize &&
557                                         IsDirDelimiter(prefix[prefixpos]))
558                                 ++prefixpos;
559                         if(prefixpos == prefixsize)
560                                 return true;
561                         if(pathpos == pathsize)
562                                 return false;
563                 }
564                 else{
565                         size_t len = 0;
566                         do{
567                                 char pathchar = path[pathpos+len];
568                                 char prefixchar = prefix[prefixpos+len];
569                                 if(FILESYS_CASE_INSENSITIVE){
570                                         pathchar = tolower(pathchar);
571                                         prefixchar = tolower(prefixchar);
572                                 }
573                                 if(pathchar != prefixchar)
574                                         return false;
575                                 ++len;
576                         } while(pathpos+len < pathsize
577                                         && !IsDirDelimiter(path[pathpos+len])
578                                         && prefixpos+len < prefixsize
579                                         && !IsDirDelimiter(
580                                                 prefix[prefixpos+len]));
581                         pathpos += len;
582                         prefixpos += len;
583                 }
584         }
585 }
586
587 std::string RemoveLastPathComponent(const std::string &path,
588                 std::string *removed, int count)
589 {
590         if(removed)
591                 *removed = "";
592
593         size_t remaining = path.size();
594
595         for(int i = 0; i < count; ++i){
596                 // strip a dir delimiter
597                 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
598                         remaining--;
599                 // strip a path component
600                 size_t component_end = remaining;
601                 while(remaining != 0 && !IsDirDelimiter(path[remaining-1]))
602                         remaining--;
603                 size_t component_start = remaining;
604                 // strip a dir delimiter
605                 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
606                         remaining--;
607                 if(removed){
608                         std::string component = path.substr(component_start,
609                                         component_end - component_start);
610                         if(i)
611                                 *removed = component + DIR_DELIM + *removed;
612                         else
613                                 *removed = component;
614                 }
615         }
616         return path.substr(0, remaining);
617 }
618
619 std::string RemoveRelativePathComponents(std::string path)
620 {
621         size_t pos = path.size();
622         size_t dotdot_count = 0;
623         while (pos != 0) {
624                 size_t component_with_delim_end = pos;
625                 // skip a dir delimiter
626                 while (pos != 0 && IsDirDelimiter(path[pos-1]))
627                         pos--;
628                 // strip a path component
629                 size_t component_end = pos;
630                 while (pos != 0 && !IsDirDelimiter(path[pos-1]))
631                         pos--;
632                 size_t component_start = pos;
633
634                 std::string component = path.substr(component_start,
635                                 component_end - component_start);
636                 bool remove_this_component = false;
637                 if (component == ".") {
638                         remove_this_component = true;
639                 } else if (component == "..") {
640                         remove_this_component = true;
641                         dotdot_count += 1;
642                 } else if (dotdot_count != 0) {
643                         remove_this_component = true;
644                         dotdot_count -= 1;
645                 }
646
647                 if (remove_this_component) {
648                         while (pos != 0 && IsDirDelimiter(path[pos-1]))
649                                 pos--;
650                         if (component_start == 0) {
651                                 // We need to remove the delemiter too
652                                 path = path.substr(component_with_delim_end, std::string::npos);
653                         } else {
654                                 path = path.substr(0, pos) + DIR_DELIM +
655                                         path.substr(component_with_delim_end, std::string::npos);
656                         }
657                         if (pos > 0)
658                                 pos++;
659                 }
660         }
661
662         if (dotdot_count > 0)
663                 return "";
664
665         // remove trailing dir delimiters
666         pos = path.size();
667         while (pos != 0 && IsDirDelimiter(path[pos-1]))
668                 pos--;
669         return path.substr(0, pos);
670 }
671
672 std::string AbsolutePath(const std::string &path)
673 {
674 #ifdef _WIN32
675         char *abs_path = _fullpath(NULL, path.c_str(), MAX_PATH);
676 #else
677         char *abs_path = realpath(path.c_str(), NULL);
678 #endif
679         if (!abs_path) return "";
680         std::string abs_path_str(abs_path);
681         free(abs_path);
682         return abs_path_str;
683 }
684
685 const char *GetFilenameFromPath(const char *path)
686 {
687         const char *filename = strrchr(path, DIR_DELIM_CHAR);
688         // Consistent with IsDirDelimiter this function handles '/' too
689         if (DIR_DELIM_CHAR != '/') {
690                 const char *tmp = strrchr(path, '/');
691                 if (tmp && tmp > filename)
692                         filename = tmp;
693         }
694         return filename ? filename + 1 : path;
695 }
696
697 bool safeWriteToFile(const std::string &path, const std::string &content)
698 {
699         std::string tmp_file = path + ".~mt";
700
701         // Write to a tmp file
702         std::ofstream os(tmp_file.c_str(), std::ios::binary);
703         if (!os.good())
704                 return false;
705         os << content;
706         os.flush();
707         os.close();
708         if (os.fail()) {
709                 // Remove the temporary file because writing it failed and it's useless.
710                 remove(tmp_file.c_str());
711                 return false;
712         }
713
714         bool rename_success = false;
715
716         // Move the finished temporary file over the real file
717 #ifdef _WIN32
718         // When creating the file, it can cause Windows Search indexer, virus scanners and other apps
719         // to query the file. This can make the move file call below fail.
720         // We retry up to 5 times, with a 1ms sleep between, before we consider the whole operation failed
721         int number_attempts = 0;
722         while (number_attempts < 5) {
723                 rename_success = MoveFileEx(tmp_file.c_str(), path.c_str(),
724                                 MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
725                 if (rename_success)
726                         break;
727                 sleep_ms(1);
728                 ++number_attempts;
729         }
730 #else
731         // On POSIX compliant systems rename() is specified to be able to swap the
732         // file in place of the destination file, making this a truly error-proof
733         // transaction.
734         rename_success = rename(tmp_file.c_str(), path.c_str()) == 0;
735 #endif
736         if (!rename_success) {
737                 warningstream << "Failed to write to file: " << path.c_str() << std::endl;
738                 // Remove the temporary file because moving it over the target file
739                 // failed.
740                 remove(tmp_file.c_str());
741                 return false;
742         }
743
744         return true;
745 }
746
747 bool ReadFile(const std::string &path, std::string &out)
748 {
749         std::ifstream is(path, std::ios::binary | std::ios::ate);
750         if (!is.good()) {
751                 return false;
752         }
753
754         auto size = is.tellg();
755         out.resize(size);
756         is.seekg(0);
757         is.read(&out[0], size);
758
759         return true;
760 }
761
762 bool Rename(const std::string &from, const std::string &to)
763 {
764         return rename(from.c_str(), to.c_str()) == 0;
765 }
766
767 } // namespace fs
768