3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 #include "util/string.h"
34 #ifdef _WIN32 // WINDOWS
36 #define _WIN32_WINNT 0x0501
40 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
42 std::vector<DirListNode> listing;
44 WIN32_FIND_DATA FindFileData;
45 HANDLE hFind = INVALID_HANDLE_VALUE;
48 std::string dirSpec = pathstring + "\\*";
50 // Find the first file in the directory.
51 hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
53 if (hFind == INVALID_HANDLE_VALUE) {
54 dwError = GetLastError();
55 if (dwError != ERROR_FILE_NOT_FOUND && dwError != ERROR_PATH_NOT_FOUND) {
56 errorstream << "GetDirListing: FindFirstFile error."
57 << " Error is " << dwError << std::endl;
61 // Be very sure to not include '..' in the results, it will
62 // result in an epic failure when deleting stuff.
65 node.name = FindFileData.cFileName;
66 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
67 if (node.name != "." && node.name != "..")
68 listing.push_back(node);
70 // List all the other files in the directory.
71 while (FindNextFile(hFind, &FindFileData) != 0) {
73 node.name = FindFileData.cFileName;
74 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
75 if(node.name != "." && node.name != "..")
76 listing.push_back(node);
79 dwError = GetLastError();
81 if (dwError != ERROR_NO_MORE_FILES) {
82 errorstream << "GetDirListing: FindNextFile error."
83 << " Error is " << dwError << std::endl;
91 bool CreateDir(const std::string &path)
93 bool r = CreateDirectory(path.c_str(), NULL);
96 if(GetLastError() == ERROR_ALREADY_EXISTS)
101 bool PathExists(const std::string &path)
103 return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
106 bool IsPathAbsolute(const std::string &path)
108 return !PathIsRelative(path.c_str());
111 bool IsDir(const std::string &path)
113 DWORD attr = GetFileAttributes(path.c_str());
114 return (attr != INVALID_FILE_ATTRIBUTES &&
115 (attr & FILE_ATTRIBUTE_DIRECTORY));
118 bool IsDirDelimiter(char c)
120 return c == '/' || c == '\\';
123 bool RecursiveDelete(const std::string &path)
125 infostream << "Recursively deleting \"" << path << "\"" << std::endl;
127 infostream << "RecursiveDelete: Deleting file " << path << std::endl;
128 if (!DeleteFile(path.c_str())) {
129 errorstream << "RecursiveDelete: Failed to delete file "
130 << path << std::endl;
135 infostream << "RecursiveDelete: Deleting content of directory "
136 << path << std::endl;
137 std::vector<DirListNode> content = GetDirListing(path);
138 for (const DirListNode &n: content) {
139 std::string fullpath = path + DIR_DELIM + n.name;
140 if (!RecursiveDelete(fullpath)) {
141 errorstream << "RecursiveDelete: Failed to recurse to "
142 << fullpath << std::endl;
146 infostream << "RecursiveDelete: Deleting directory " << path << std::endl;
147 if (!RemoveDirectory(path.c_str())) {
148 errorstream << "Failed to recursively delete directory "
149 << path << std::endl;
155 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
157 DWORD attr = GetFileAttributes(path.c_str());
158 bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
159 (attr & FILE_ATTRIBUTE_DIRECTORY));
162 bool did = DeleteFile(path.c_str());
167 bool did = RemoveDirectory(path.c_str());
172 std::string TempPath()
174 DWORD bufsize = GetTempPath(0, NULL);
176 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
179 std::vector<char> buf(bufsize);
180 DWORD len = GetTempPath(bufsize, &buf[0]);
181 if(len == 0 || len > bufsize){
182 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
185 return std::string(buf.begin(), buf.begin() + len);
190 #include <sys/types.h>
192 #include <sys/stat.h>
193 #include <sys/wait.h>
196 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
198 std::vector<DirListNode> listing;
202 if((dp = opendir(pathstring.c_str())) == NULL) {
203 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
207 while ((dirp = readdir(dp)) != NULL) {
209 // Be very sure to not include '..' in the results, it will
210 // result in an epic failure when deleting stuff.
211 if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
215 node.name = dirp->d_name;
217 int isdir = -1; // -1 means unknown
220 POSIX doesn't define d_type member of struct dirent and
221 certain filesystems on glibc/Linux will only return
222 DT_UNKNOWN for the d_type member.
224 Also we don't know whether symlinks are directories or not.
226 #ifdef _DIRENT_HAVE_D_TYPE
227 if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
228 isdir = (dirp->d_type == DT_DIR);
229 #endif /* _DIRENT_HAVE_D_TYPE */
232 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
236 struct stat statbuf{};
237 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
239 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
242 listing.push_back(node);
249 bool CreateDir(const std::string &path)
251 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
256 // If already exists, return true
263 bool PathExists(const std::string &path)
266 return (stat(path.c_str(),&st) == 0);
269 bool IsPathAbsolute(const std::string &path)
271 return path[0] == '/';
274 bool IsDir(const std::string &path)
276 struct stat statbuf{};
277 if(stat(path.c_str(), &statbuf))
278 return false; // Actually error; but certainly not a directory
279 return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
282 bool IsDirDelimiter(char c)
287 bool RecursiveDelete(const std::string &path)
290 Execute the 'rm' command directly, by fork() and execve()
293 infostream<<"Removing \""<<path<<"\""<<std::endl;
295 pid_t child_pid = fork();
300 const char *argv[4] = {
311 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
312 <<argv[2]<<"'"<<std::endl;
314 execv(argv[0], const_cast<char**>(argv));
316 // Execv shouldn't return. Failed.
325 tpid = wait(&child_status);
326 }while(tpid != child_pid);
327 return (child_status == 0);
331 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
334 bool did = (rmdir(path.c_str()) == 0);
336 errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
341 bool did = (unlink(path.c_str()) == 0);
343 errorstream << "unlink errno: " << errno << ": " << strerror(errno)
348 std::string TempPath()
351 Should the environment variables TMPDIR, TMP and TEMP
352 and the macro P_tmpdir (if defined by stdio.h) be checked
353 before falling back on /tmp?
355 Probably not, because this function is intended to be
356 compatible with lua's os.tmpname which under the default
357 configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
361 return porting::path_cache;
363 return DIR_DELIM "tmp";
369 void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
371 static const std::set<char> chars_to_ignore = { '_', '.' };
372 if (dir.empty() || !IsDir(dir))
375 fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore);
378 std::vector<std::string> GetRecursiveDirs(const std::string &dir)
380 std::vector<std::string> result;
381 GetRecursiveDirs(result, dir);
385 void GetRecursiveSubPaths(const std::string &path,
386 std::vector<std::string> &dst,
388 const std::set<char> &ignore)
390 std::vector<DirListNode> content = GetDirListing(path);
391 for (const auto &n : content) {
392 std::string fullpath = path + DIR_DELIM + n.name;
393 if (ignore.count(n.name[0]))
395 if (list_files || n.dir)
396 dst.push_back(fullpath);
398 GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
402 bool RecursiveDeleteContent(const std::string &path)
404 infostream<<"Removing content of \""<<path<<"\""<<std::endl;
405 std::vector<DirListNode> list = GetDirListing(path);
406 for (const DirListNode &dln : list) {
407 if(trim(dln.name) == "." || trim(dln.name) == "..")
409 std::string childpath = path + DIR_DELIM + dln.name;
410 bool r = RecursiveDelete(childpath);
412 errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
419 bool CreateAllDirs(const std::string &path)
422 std::vector<std::string> tocreate;
423 std::string basepath = path;
424 while(!PathExists(basepath))
426 tocreate.push_back(basepath);
427 basepath = RemoveLastPathComponent(basepath);
431 for(int i=tocreate.size()-1;i>=0;i--)
432 if(!CreateDir(tocreate[i]))
437 bool CopyFileContents(const std::string &source, const std::string &target)
439 FILE *sourcefile = fopen(source.c_str(), "rb");
440 if(sourcefile == NULL){
441 errorstream<<source<<": can't open for reading: "
442 <<strerror(errno)<<std::endl;
446 FILE *targetfile = fopen(target.c_str(), "wb");
447 if(targetfile == NULL){
448 errorstream<<target<<": can't open for writing: "
449 <<strerror(errno)<<std::endl;
457 char readbuffer[BUFSIZ];
459 size_t readbytes = fread(readbuffer, 1,
460 sizeof(readbuffer), sourcefile);
462 if(ferror(sourcefile)){
463 errorstream<<source<<": IO error: "
464 <<strerror(errno)<<std::endl;
469 fwrite(readbuffer, 1, readbytes, targetfile);
471 if(feof(sourcefile) || ferror(sourcefile)){
472 // flush destination file to catch write errors
477 if(ferror(targetfile)){
478 errorstream<<target<<": IO error: "
479 <<strerror(errno)<<std::endl;
484 infostream<<"copied "<<total<<" bytes from "
485 <<source<<" to "<<target<<std::endl;
491 bool CopyDir(const std::string &source, const std::string &target)
493 if(PathExists(source)){
494 if(!PathExists(target)){
495 fs::CreateAllDirs(target);
498 std::vector<DirListNode> content = fs::GetDirListing(source);
500 for (const auto &dln : content) {
501 std::string sourcechild = source + DIR_DELIM + dln.name;
502 std::string targetchild = target + DIR_DELIM + dln.name;
504 if(!fs::CopyDir(sourcechild, targetchild)){
509 if(!fs::CopyFileContents(sourcechild, targetchild)){
520 bool PathStartsWith(const std::string &path, const std::string &prefix)
522 size_t pathsize = path.size();
524 size_t prefixsize = prefix.size();
525 size_t prefixpos = 0;
527 bool delim1 = pathpos == pathsize
528 || IsDirDelimiter(path[pathpos]);
529 bool delim2 = prefixpos == prefixsize
530 || IsDirDelimiter(prefix[prefixpos]);
536 while(pathpos < pathsize &&
537 IsDirDelimiter(path[pathpos]))
539 while(prefixpos < prefixsize &&
540 IsDirDelimiter(prefix[prefixpos]))
542 if(prefixpos == prefixsize)
544 if(pathpos == pathsize)
550 char pathchar = path[pathpos+len];
551 char prefixchar = prefix[prefixpos+len];
552 if(FILESYS_CASE_INSENSITIVE){
553 pathchar = tolower(pathchar);
554 prefixchar = tolower(prefixchar);
556 if(pathchar != prefixchar)
559 } while(pathpos+len < pathsize
560 && !IsDirDelimiter(path[pathpos+len])
561 && prefixpos+len < prefixsize
563 prefix[prefixpos+len]));
570 std::string RemoveLastPathComponent(const std::string &path,
571 std::string *removed, int count)
576 size_t remaining = path.size();
578 for(int i = 0; i < count; ++i){
579 // strip a dir delimiter
580 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
582 // strip a path component
583 size_t component_end = remaining;
584 while(remaining != 0 && !IsDirDelimiter(path[remaining-1]))
586 size_t component_start = remaining;
587 // strip a dir delimiter
588 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
591 std::string component = path.substr(component_start,
592 component_end - component_start);
594 *removed = component + DIR_DELIM + *removed;
596 *removed = component;
599 return path.substr(0, remaining);
602 std::string RemoveRelativePathComponents(std::string path)
604 size_t pos = path.size();
605 size_t dotdot_count = 0;
607 size_t component_with_delim_end = pos;
608 // skip a dir delimiter
609 while (pos != 0 && IsDirDelimiter(path[pos-1]))
611 // strip a path component
612 size_t component_end = pos;
613 while (pos != 0 && !IsDirDelimiter(path[pos-1]))
615 size_t component_start = pos;
617 std::string component = path.substr(component_start,
618 component_end - component_start);
619 bool remove_this_component = false;
620 if (component == ".") {
621 remove_this_component = true;
622 } else if (component == "..") {
623 remove_this_component = true;
625 } else if (dotdot_count != 0) {
626 remove_this_component = true;
630 if (remove_this_component) {
631 while (pos != 0 && IsDirDelimiter(path[pos-1]))
633 if (component_start == 0) {
634 // We need to remove the delemiter too
635 path = path.substr(component_with_delim_end, std::string::npos);
637 path = path.substr(0, pos) + DIR_DELIM +
638 path.substr(component_with_delim_end, std::string::npos);
645 if (dotdot_count > 0)
648 // remove trailing dir delimiters
650 while (pos != 0 && IsDirDelimiter(path[pos-1]))
652 return path.substr(0, pos);
655 std::string AbsolutePath(const std::string &path)
658 char *abs_path = _fullpath(NULL, path.c_str(), MAX_PATH);
660 char *abs_path = realpath(path.c_str(), NULL);
662 if (!abs_path) return "";
663 std::string abs_path_str(abs_path);
668 const char *GetFilenameFromPath(const char *path)
670 const char *filename = strrchr(path, DIR_DELIM_CHAR);
671 // Consistent with IsDirDelimiter this function handles '/' too
672 if (DIR_DELIM_CHAR != '/') {
673 const char *tmp = strrchr(path, '/');
674 if (tmp && tmp > filename)
677 return filename ? filename + 1 : path;
680 bool safeWriteToFile(const std::string &path, const std::string &content)
682 std::string tmp_file = path + ".~mt";
684 // Write to a tmp file
685 std::ofstream os(tmp_file.c_str(), std::ios::binary);
692 // Remove the temporary file because writing it failed and it's useless.
693 remove(tmp_file.c_str());
697 bool rename_success = false;
699 // Move the finished temporary file over the real file
701 // When creating the file, it can cause Windows Search indexer, virus scanners and other apps
702 // to query the file. This can make the move file call below fail.
703 // We retry up to 5 times, with a 1ms sleep between, before we consider the whole operation failed
704 int number_attempts = 0;
705 while (number_attempts < 5) {
706 rename_success = MoveFileEx(tmp_file.c_str(), path.c_str(),
707 MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
714 // On POSIX compliant systems rename() is specified to be able to swap the
715 // file in place of the destination file, making this a truly error-proof
717 rename_success = rename(tmp_file.c_str(), path.c_str()) == 0;
719 if (!rename_success) {
720 warningstream << "Failed to write to file: " << path.c_str() << std::endl;
721 // Remove the temporary file because moving it over the target file
723 remove(tmp_file.c_str());
730 bool ReadFile(const std::string &path, std::string &out)
732 std::ifstream is(path, std::ios::binary | std::ios::ate);
737 auto size = is.tellg();
740 is.read(&out[0], size);
745 bool Rename(const std::string &from, const std::string &to)
747 return rename(from.c_str(), to.c_str()) == 0;