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"
45 #define _WIN32_WINNT 0x0501
52 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
54 std::vector<DirListNode> listing;
56 WIN32_FIND_DATA FindFileData;
57 HANDLE hFind = INVALID_HANDLE_VALUE;
60 std::string dirSpec = pathstring + "\\*";
62 // Find the first file in the directory.
63 hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
65 if (hFind == INVALID_HANDLE_VALUE) {
66 dwError = GetLastError();
67 if (dwError != ERROR_FILE_NOT_FOUND && dwError != ERROR_PATH_NOT_FOUND) {
68 errorstream << "GetDirListing: FindFirstFile error."
69 << " Error is " << dwError << std::endl;
73 // Be very sure to not include '..' in the results, it will
74 // result in an epic failure when deleting stuff.
77 node.name = FindFileData.cFileName;
78 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
79 if (node.name != "." && node.name != "..")
80 listing.push_back(node);
82 // List all the other files in the directory.
83 while (FindNextFile(hFind, &FindFileData) != 0) {
85 node.name = FindFileData.cFileName;
86 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
87 if(node.name != "." && node.name != "..")
88 listing.push_back(node);
91 dwError = GetLastError();
93 if (dwError != ERROR_NO_MORE_FILES) {
94 errorstream << "GetDirListing: FindNextFile error."
95 << " Error is " << dwError << std::endl;
103 bool CreateDir(const std::string &path)
105 bool r = CreateDirectory(path.c_str(), NULL);
108 if(GetLastError() == ERROR_ALREADY_EXISTS)
113 bool PathExists(const std::string &path)
115 return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
118 bool IsPathAbsolute(const std::string &path)
120 return !PathIsRelative(path.c_str());
123 bool IsDir(const std::string &path)
125 DWORD attr = GetFileAttributes(path.c_str());
126 return (attr != INVALID_FILE_ATTRIBUTES &&
127 (attr & FILE_ATTRIBUTE_DIRECTORY));
130 bool IsExecutable(const std::string &path)
133 return GetBinaryType(path.c_str(), &type) != 0;
136 bool IsDirDelimiter(char c)
138 return c == '/' || c == '\\';
141 bool RecursiveDelete(const std::string &path)
143 infostream << "Recursively deleting \"" << path << "\"" << std::endl;
145 infostream << "RecursiveDelete: Deleting file " << path << std::endl;
146 if (!DeleteFile(path.c_str())) {
147 errorstream << "RecursiveDelete: Failed to delete file "
148 << path << std::endl;
153 infostream << "RecursiveDelete: Deleting content of directory "
154 << path << std::endl;
155 std::vector<DirListNode> content = GetDirListing(path);
156 for (const DirListNode &n: content) {
157 std::string fullpath = path + DIR_DELIM + n.name;
158 if (!RecursiveDelete(fullpath)) {
159 errorstream << "RecursiveDelete: Failed to recurse to "
160 << fullpath << std::endl;
164 infostream << "RecursiveDelete: Deleting directory " << path << std::endl;
165 if (!RemoveDirectory(path.c_str())) {
166 errorstream << "Failed to recursively delete directory "
167 << path << std::endl;
173 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
175 DWORD attr = GetFileAttributes(path.c_str());
176 bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
177 (attr & FILE_ATTRIBUTE_DIRECTORY));
180 bool did = DeleteFile(path.c_str());
185 bool did = RemoveDirectory(path.c_str());
190 std::string TempPath()
192 DWORD bufsize = GetTempPath(0, NULL);
194 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
199 DWORD len = GetTempPath(bufsize, &buf[0]);
200 if(len == 0 || len > bufsize){
201 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
208 std::string CreateTempFile()
210 std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
211 _mktemp_s(&path[0], path.size() + 1); // modifies path
212 HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr,
213 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
214 if (file == INVALID_HANDLE_VALUE)
226 #include <sys/types.h>
228 #include <sys/stat.h>
229 #include <sys/wait.h>
232 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
234 std::vector<DirListNode> listing;
238 if((dp = opendir(pathstring.c_str())) == NULL) {
239 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
243 while ((dirp = readdir(dp)) != NULL) {
245 // Be very sure to not include '..' in the results, it will
246 // result in an epic failure when deleting stuff.
247 if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
251 node.name = dirp->d_name;
253 int isdir = -1; // -1 means unknown
256 POSIX doesn't define d_type member of struct dirent and
257 certain filesystems on glibc/Linux will only return
258 DT_UNKNOWN for the d_type member.
260 Also we don't know whether symlinks are directories or not.
262 #ifdef _DIRENT_HAVE_D_TYPE
263 if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
264 isdir = (dirp->d_type == DT_DIR);
265 #endif /* _DIRENT_HAVE_D_TYPE */
268 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
272 struct stat statbuf{};
273 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
275 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
278 listing.push_back(node);
285 bool CreateDir(const std::string &path)
287 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
292 // If already exists, return true
299 bool PathExists(const std::string &path)
302 return (stat(path.c_str(),&st) == 0);
305 bool IsPathAbsolute(const std::string &path)
307 return path[0] == '/';
310 bool IsDir(const std::string &path)
312 struct stat statbuf{};
313 if(stat(path.c_str(), &statbuf))
314 return false; // Actually error; but certainly not a directory
315 return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
318 bool IsExecutable(const std::string &path)
320 return access(path.c_str(), X_OK) == 0;
323 bool IsDirDelimiter(char c)
328 bool RecursiveDelete(const std::string &path)
331 Execute the 'rm' command directly, by fork() and execve()
334 infostream<<"Removing \""<<path<<"\""<<std::endl;
336 pid_t child_pid = fork();
341 const char *argv[4] = {
352 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
353 <<argv[2]<<"'"<<std::endl;
355 execv(argv[0], const_cast<char**>(argv));
357 // Execv shouldn't return. Failed.
366 tpid = wait(&child_status);
367 }while(tpid != child_pid);
368 return (child_status == 0);
372 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
375 bool did = (rmdir(path.c_str()) == 0);
377 errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
382 bool did = (unlink(path.c_str()) == 0);
384 errorstream << "unlink errno: " << errno << ": " << strerror(errno)
389 std::string TempPath()
392 Should the environment variables TMPDIR, TMP and TEMP
393 and the macro P_tmpdir (if defined by stdio.h) be checked
394 before falling back on /tmp?
396 Probably not, because this function is intended to be
397 compatible with lua's os.tmpname which under the default
398 configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
402 return porting::path_cache;
404 return DIR_DELIM "tmp";
408 std::string CreateTempFile()
410 std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
411 int fd = mkstemp(&path[0]); // modifies path
420 /****************************
421 * portable implementations *
422 ****************************/
424 void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
426 static const std::set<char> chars_to_ignore = { '_', '.' };
427 if (dir.empty() || !IsDir(dir))
430 fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore);
433 std::vector<std::string> GetRecursiveDirs(const std::string &dir)
435 std::vector<std::string> result;
436 GetRecursiveDirs(result, dir);
440 void GetRecursiveSubPaths(const std::string &path,
441 std::vector<std::string> &dst,
443 const std::set<char> &ignore)
445 std::vector<DirListNode> content = GetDirListing(path);
446 for (const auto &n : content) {
447 std::string fullpath = path + DIR_DELIM + n.name;
448 if (ignore.count(n.name[0]))
450 if (list_files || n.dir)
451 dst.push_back(fullpath);
453 GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
457 bool RecursiveDeleteContent(const std::string &path)
459 infostream<<"Removing content of \""<<path<<"\""<<std::endl;
460 std::vector<DirListNode> list = GetDirListing(path);
461 for (const DirListNode &dln : list) {
462 if(trim(dln.name) == "." || trim(dln.name) == "..")
464 std::string childpath = path + DIR_DELIM + dln.name;
465 bool r = RecursiveDelete(childpath);
467 errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
474 bool CreateAllDirs(const std::string &path)
477 std::vector<std::string> tocreate;
478 std::string basepath = path;
479 while(!PathExists(basepath))
481 tocreate.push_back(basepath);
482 basepath = RemoveLastPathComponent(basepath);
486 for(int i=tocreate.size()-1;i>=0;i--)
487 if(!CreateDir(tocreate[i]))
492 bool CopyFileContents(const std::string &source, const std::string &target)
494 FILE *sourcefile = fopen(source.c_str(), "rb");
495 if(sourcefile == NULL){
496 errorstream<<source<<": can't open for reading: "
497 <<strerror(errno)<<std::endl;
501 FILE *targetfile = fopen(target.c_str(), "wb");
502 if(targetfile == NULL){
503 errorstream<<target<<": can't open for writing: "
504 <<strerror(errno)<<std::endl;
512 char readbuffer[BUFSIZ];
514 size_t readbytes = fread(readbuffer, 1,
515 sizeof(readbuffer), sourcefile);
517 if(ferror(sourcefile)){
518 errorstream<<source<<": IO error: "
519 <<strerror(errno)<<std::endl;
524 fwrite(readbuffer, 1, readbytes, targetfile);
526 if(feof(sourcefile) || ferror(sourcefile)){
527 // flush destination file to catch write errors
532 if(ferror(targetfile)){
533 errorstream<<target<<": IO error: "
534 <<strerror(errno)<<std::endl;
539 infostream<<"copied "<<total<<" bytes from "
540 <<source<<" to "<<target<<std::endl;
546 bool CopyDir(const std::string &source, const std::string &target)
548 if(PathExists(source)){
549 if(!PathExists(target)){
550 fs::CreateAllDirs(target);
553 std::vector<DirListNode> content = fs::GetDirListing(source);
555 for (const auto &dln : content) {
556 std::string sourcechild = source + DIR_DELIM + dln.name;
557 std::string targetchild = target + DIR_DELIM + dln.name;
559 if(!fs::CopyDir(sourcechild, targetchild)){
564 if(!fs::CopyFileContents(sourcechild, targetchild)){
575 bool MoveDir(const std::string &source, const std::string &target)
577 infostream << "Moving \"" << source << "\" to \"" << target << "\"" << std::endl;
579 // If target exists as empty folder delete, otherwise error
580 if (fs::PathExists(target)) {
581 if (rmdir(target.c_str()) != 0) {
582 errorstream << "MoveDir: target \"" << target
583 << "\" exists as file or non-empty folder" << std::endl;
588 // Try renaming first which is instant
589 if (fs::Rename(source, target))
592 infostream << "MoveDir: rename not possible, will copy instead" << std::endl;
593 bool retval = fs::CopyDir(source, target);
595 retval &= fs::RecursiveDelete(source);
599 bool PathStartsWith(const std::string &path, const std::string &prefix)
601 size_t pathsize = path.size();
603 size_t prefixsize = prefix.size();
604 size_t prefixpos = 0;
606 bool delim1 = pathpos == pathsize
607 || IsDirDelimiter(path[pathpos]);
608 bool delim2 = prefixpos == prefixsize
609 || IsDirDelimiter(prefix[prefixpos]);
615 while(pathpos < pathsize &&
616 IsDirDelimiter(path[pathpos]))
618 while(prefixpos < prefixsize &&
619 IsDirDelimiter(prefix[prefixpos]))
621 if(prefixpos == prefixsize)
623 if(pathpos == pathsize)
629 char pathchar = path[pathpos+len];
630 char prefixchar = prefix[prefixpos+len];
631 if(FILESYS_CASE_INSENSITIVE){
632 pathchar = tolower(pathchar);
633 prefixchar = tolower(prefixchar);
635 if(pathchar != prefixchar)
638 } while(pathpos+len < pathsize
639 && !IsDirDelimiter(path[pathpos+len])
640 && prefixpos+len < prefixsize
642 prefix[prefixpos+len]));
649 std::string RemoveLastPathComponent(const std::string &path,
650 std::string *removed, int count)
655 size_t remaining = path.size();
657 for(int i = 0; i < count; ++i){
658 // strip a dir delimiter
659 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
661 // strip a path component
662 size_t component_end = remaining;
663 while(remaining != 0 && !IsDirDelimiter(path[remaining-1]))
665 size_t component_start = remaining;
666 // strip a dir delimiter
667 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
670 std::string component = path.substr(component_start,
671 component_end - component_start);
673 *removed = component + DIR_DELIM + *removed;
675 *removed = component;
678 return path.substr(0, remaining);
681 std::string RemoveRelativePathComponents(std::string path)
683 size_t pos = path.size();
684 size_t dotdot_count = 0;
686 size_t component_with_delim_end = pos;
687 // skip a dir delimiter
688 while (pos != 0 && IsDirDelimiter(path[pos-1]))
690 // strip a path component
691 size_t component_end = pos;
692 while (pos != 0 && !IsDirDelimiter(path[pos-1]))
694 size_t component_start = pos;
696 std::string component = path.substr(component_start,
697 component_end - component_start);
698 bool remove_this_component = false;
699 if (component == ".") {
700 remove_this_component = true;
701 } else if (component == "..") {
702 remove_this_component = true;
704 } else if (dotdot_count != 0) {
705 remove_this_component = true;
709 if (remove_this_component) {
710 while (pos != 0 && IsDirDelimiter(path[pos-1]))
712 if (component_start == 0) {
713 // We need to remove the delemiter too
714 path = path.substr(component_with_delim_end, std::string::npos);
716 path = path.substr(0, pos) + DIR_DELIM +
717 path.substr(component_with_delim_end, std::string::npos);
724 if (dotdot_count > 0)
727 // remove trailing dir delimiters
729 while (pos != 0 && IsDirDelimiter(path[pos-1]))
731 return path.substr(0, pos);
734 std::string AbsolutePath(const std::string &path)
737 char *abs_path = _fullpath(NULL, path.c_str(), MAX_PATH);
739 char *abs_path = realpath(path.c_str(), NULL);
741 if (!abs_path) return "";
742 std::string abs_path_str(abs_path);
747 const char *GetFilenameFromPath(const char *path)
749 const char *filename = strrchr(path, DIR_DELIM_CHAR);
750 // Consistent with IsDirDelimiter this function handles '/' too
751 if (DIR_DELIM_CHAR != '/') {
752 const char *tmp = strrchr(path, '/');
753 if (tmp && tmp > filename)
756 return filename ? filename + 1 : path;
759 bool safeWriteToFile(const std::string &path, const std::string &content)
761 std::string tmp_file = path + ".~mt";
763 // Write to a tmp file
764 std::ofstream os(tmp_file.c_str(), std::ios::binary);
771 // Remove the temporary file because writing it failed and it's useless.
772 remove(tmp_file.c_str());
776 bool rename_success = false;
778 // Move the finished temporary file over the real file
780 // When creating the file, it can cause Windows Search indexer, virus scanners and other apps
781 // to query the file. This can make the move file call below fail.
782 // We retry up to 5 times, with a 1ms sleep between, before we consider the whole operation failed
783 int number_attempts = 0;
784 while (number_attempts < 5) {
785 rename_success = MoveFileEx(tmp_file.c_str(), path.c_str(),
786 MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
793 // On POSIX compliant systems rename() is specified to be able to swap the
794 // file in place of the destination file, making this a truly error-proof
796 rename_success = rename(tmp_file.c_str(), path.c_str()) == 0;
798 if (!rename_success) {
799 warningstream << "Failed to write to file: " << path.c_str() << std::endl;
800 // Remove the temporary file because moving it over the target file
802 remove(tmp_file.c_str());
810 bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination)
812 // Be careful here not to touch the global file hierarchy in Irrlicht
813 // since this function needs to be thread-safe!
815 io::IArchiveLoader *zip_loader = nullptr;
816 for (u32 i = 0; i < fs->getArchiveLoaderCount(); i++) {
817 if (fs->getArchiveLoader(i)->isALoadableFileFormat(io::EFAT_ZIP)) {
818 zip_loader = fs->getArchiveLoader(i);
823 warningstream << "fs::extractZipFile(): Irrlicht said it doesn't support ZIPs." << std::endl;
827 irr_ptr<io::IFileArchive> opened_zip(zip_loader->createArchive(filename, false, false));
828 const io::IFileList* files_in_zip = opened_zip->getFileList();
830 for (u32 i = 0; i < files_in_zip->getFileCount(); i++) {
831 std::string fullpath = destination + DIR_DELIM;
832 fullpath += files_in_zip->getFullFileName(i).c_str();
833 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
835 if (files_in_zip->isDirectory(i))
836 continue; // ignore, we create dirs as necessary
838 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir))
841 irr_ptr<io::IReadFile> toread(opened_zip->createAndOpenFile(i));
843 std::ofstream os(fullpath.c_str(), std::ios::binary);
850 while (total_read < toread->getSize()) {
851 long bytes_read = toread->read(buffer, sizeof(buffer));
853 if (bytes_read != 0) {
854 os.write(buffer, bytes_read);
859 remove(fullpath.c_str());
862 total_read += bytes_read;
870 bool ReadFile(const std::string &path, std::string &out)
872 std::ifstream is(path, std::ios::binary | std::ios::ate);
877 auto size = is.tellg();
880 is.read(&out[0], size);
885 bool Rename(const std::string &from, const std::string &to)
887 return rename(from.c_str(), to.c_str()) == 0;