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"
31 #include "settings.h" // For g_settings
37 #ifdef _WIN32 // WINDOWS
39 #define _WIN32_WINNT 0x0501
43 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
45 std::vector<DirListNode> listing;
47 WIN32_FIND_DATA FindFileData;
48 HANDLE hFind = INVALID_HANDLE_VALUE;
51 std::string dirSpec = pathstring + "\\*";
53 // Find the first file in the directory.
54 hFind = FindFirstFile(dirSpec.c_str(), &FindFileData);
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;
64 // Be very sure to not include '..' in the results, it will
65 // result in an epic failure when deleting stuff.
68 node.name = FindFileData.cFileName;
69 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
70 if (node.name != "." && node.name != "..")
71 listing.push_back(node);
73 // List all the other files in the directory.
74 while (FindNextFile(hFind, &FindFileData) != 0) {
76 node.name = FindFileData.cFileName;
77 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
78 if(node.name != "." && node.name != "..")
79 listing.push_back(node);
82 dwError = GetLastError();
84 if (dwError != ERROR_NO_MORE_FILES) {
85 errorstream << "GetDirListing: FindNextFile error."
86 << " Error is " << dwError << std::endl;
94 bool CreateDir(const std::string &path)
96 bool r = CreateDirectory(path.c_str(), NULL);
99 if(GetLastError() == ERROR_ALREADY_EXISTS)
104 bool PathExists(const std::string &path)
106 return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
109 bool IsPathAbsolute(const std::string &path)
111 return !PathIsRelative(path.c_str());
114 bool IsDir(const std::string &path)
116 DWORD attr = GetFileAttributes(path.c_str());
117 return (attr != INVALID_FILE_ATTRIBUTES &&
118 (attr & FILE_ATTRIBUTE_DIRECTORY));
121 bool IsDirDelimiter(char c)
123 return c == '/' || c == '\\';
126 bool RecursiveDelete(const std::string &path)
128 infostream << "Recursively deleting \"" << path << "\"" << std::endl;
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;
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;
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;
158 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
160 DWORD attr = GetFileAttributes(path.c_str());
161 bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
162 (attr & FILE_ATTRIBUTE_DIRECTORY));
165 bool did = DeleteFile(path.c_str());
170 bool did = RemoveDirectory(path.c_str());
175 std::string TempPath()
177 DWORD bufsize = GetTempPath(0, NULL);
179 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
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;
188 return std::string(buf.begin(), buf.begin() + len);
193 #include <sys/types.h>
195 #include <sys/stat.h>
196 #include <sys/wait.h>
199 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
201 std::vector<DirListNode> listing;
205 if((dp = opendir(pathstring.c_str())) == NULL) {
206 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
210 while ((dirp = readdir(dp)) != NULL) {
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)
218 node.name = dirp->d_name;
220 int isdir = -1; // -1 means unknown
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.
227 Also we don't know whether symlinks are directories or not.
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 */
235 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
239 struct stat statbuf{};
240 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
242 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
245 listing.push_back(node);
252 bool CreateDir(const std::string &path)
254 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
259 // If already exists, return true
266 bool PathExists(const std::string &path)
269 return (stat(path.c_str(),&st) == 0);
272 bool IsPathAbsolute(const std::string &path)
274 return path[0] == '/';
277 bool IsDir(const std::string &path)
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);
285 bool IsDirDelimiter(char c)
290 bool RecursiveDelete(const std::string &path)
293 Execute the 'rm' command directly, by fork() and execve()
296 infostream<<"Removing \""<<path<<"\""<<std::endl;
300 pid_t child_pid = fork();
305 char argv_data[3][10000];
307 strcpy(argv_data[0], "/system/bin/rm");
309 strcpy(argv_data[0], "/bin/rm");
311 strcpy(argv_data[1], "-rf");
312 strncpy(argv_data[2], path.c_str(), sizeof(argv_data[2]) - 1);
314 argv[0] = argv_data[0];
315 argv[1] = argv_data[1];
316 argv[2] = argv_data[2];
319 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
320 <<argv[2]<<"'"<<std::endl;
322 execv(argv[0], argv);
324 // Execv shouldn't return. Failed.
333 tpid = wait(&child_status);
334 //if(tpid != child_pid) process_terminated(tpid);
335 }while(tpid != child_pid);
336 return (child_status == 0);
340 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
343 bool did = (rmdir(path.c_str()) == 0);
345 errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
350 bool did = (unlink(path.c_str()) == 0);
352 errorstream << "unlink errno: " << errno << ": " << strerror(errno)
357 std::string TempPath()
360 Should the environment variables TMPDIR, TMP and TEMP
361 and the macro P_tmpdir (if defined by stdio.h) be checked
362 before falling back on /tmp?
364 Probably not, because this function is intended to be
365 compatible with lua's os.tmpname which under the default
366 configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
369 return g_settings->get("TMPFolder");
371 return DIR_DELIM "tmp";
377 void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
379 static const std::set<char> chars_to_ignore = { '_', '.' };
380 if (dir.empty() || !IsDir(dir))
383 fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore);
386 std::vector<std::string> GetRecursiveDirs(const std::string &dir)
388 std::vector<std::string> result;
389 GetRecursiveDirs(result, dir);
393 void GetRecursiveSubPaths(const std::string &path,
394 std::vector<std::string> &dst,
396 const std::set<char> &ignore)
398 std::vector<DirListNode> content = GetDirListing(path);
399 for (const auto &n : content) {
400 std::string fullpath = path + DIR_DELIM + n.name;
401 if (ignore.count(n.name[0]))
403 if (list_files || n.dir)
404 dst.push_back(fullpath);
406 GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
410 bool DeletePaths(const std::vector<std::string> &paths)
413 // Go backwards to succesfully delete the output of GetRecursiveSubPaths
414 for(int i=paths.size()-1; i>=0; i--){
415 const std::string &path = paths[i];
416 bool did = DeleteSingleFileOrEmptyDirectory(path);
418 errorstream<<"Failed to delete "<<path<<std::endl;
425 bool RecursiveDeleteContent(const std::string &path)
427 infostream<<"Removing content of \""<<path<<"\""<<std::endl;
428 std::vector<DirListNode> list = GetDirListing(path);
429 for (const DirListNode &dln : list) {
430 if(trim(dln.name) == "." || trim(dln.name) == "..")
432 std::string childpath = path + DIR_DELIM + dln.name;
433 bool r = RecursiveDelete(childpath);
435 errorstream << "Removing \"" << childpath << "\" failed" << std::endl;
442 bool CreateAllDirs(const std::string &path)
445 std::vector<std::string> tocreate;
446 std::string basepath = path;
447 while(!PathExists(basepath))
449 tocreate.push_back(basepath);
450 basepath = RemoveLastPathComponent(basepath);
454 for(int i=tocreate.size()-1;i>=0;i--)
455 if(!CreateDir(tocreate[i]))
460 bool CopyFileContents(const std::string &source, const std::string &target)
462 FILE *sourcefile = fopen(source.c_str(), "rb");
463 if(sourcefile == NULL){
464 errorstream<<source<<": can't open for reading: "
465 <<strerror(errno)<<std::endl;
469 FILE *targetfile = fopen(target.c_str(), "wb");
470 if(targetfile == NULL){
471 errorstream<<target<<": can't open for writing: "
472 <<strerror(errno)<<std::endl;
480 char readbuffer[BUFSIZ];
482 size_t readbytes = fread(readbuffer, 1,
483 sizeof(readbuffer), sourcefile);
485 if(ferror(sourcefile)){
486 errorstream<<source<<": IO error: "
487 <<strerror(errno)<<std::endl;
492 fwrite(readbuffer, 1, readbytes, targetfile);
494 if(feof(sourcefile) || ferror(sourcefile)){
495 // flush destination file to catch write errors
500 if(ferror(targetfile)){
501 errorstream<<target<<": IO error: "
502 <<strerror(errno)<<std::endl;
507 infostream<<"copied "<<total<<" bytes from "
508 <<source<<" to "<<target<<std::endl;
514 bool CopyDir(const std::string &source, const std::string &target)
516 if(PathExists(source)){
517 if(!PathExists(target)){
518 fs::CreateAllDirs(target);
521 std::vector<DirListNode> content = fs::GetDirListing(source);
523 for (const auto &dln : content) {
524 std::string sourcechild = source + DIR_DELIM + dln.name;
525 std::string targetchild = target + DIR_DELIM + dln.name;
527 if(!fs::CopyDir(sourcechild, targetchild)){
532 if(!fs::CopyFileContents(sourcechild, targetchild)){
543 bool PathStartsWith(const std::string &path, const std::string &prefix)
545 size_t pathsize = path.size();
547 size_t prefixsize = prefix.size();
548 size_t prefixpos = 0;
550 bool delim1 = pathpos == pathsize
551 || IsDirDelimiter(path[pathpos]);
552 bool delim2 = prefixpos == prefixsize
553 || IsDirDelimiter(prefix[prefixpos]);
559 while(pathpos < pathsize &&
560 IsDirDelimiter(path[pathpos]))
562 while(prefixpos < prefixsize &&
563 IsDirDelimiter(prefix[prefixpos]))
565 if(prefixpos == prefixsize)
567 if(pathpos == pathsize)
573 char pathchar = path[pathpos+len];
574 char prefixchar = prefix[prefixpos+len];
575 if(FILESYS_CASE_INSENSITIVE){
576 pathchar = tolower(pathchar);
577 prefixchar = tolower(prefixchar);
579 if(pathchar != prefixchar)
582 } while(pathpos+len < pathsize
583 && !IsDirDelimiter(path[pathpos+len])
584 && prefixpos+len < prefixsize
586 prefix[prefixpos+len]));
593 std::string RemoveLastPathComponent(const std::string &path,
594 std::string *removed, int count)
599 size_t remaining = path.size();
601 for(int i = 0; i < count; ++i){
602 // strip a dir delimiter
603 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
605 // strip a path component
606 size_t component_end = remaining;
607 while(remaining != 0 && !IsDirDelimiter(path[remaining-1]))
609 size_t component_start = remaining;
610 // strip a dir delimiter
611 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
614 std::string component = path.substr(component_start,
615 component_end - component_start);
617 *removed = component + DIR_DELIM + *removed;
619 *removed = component;
622 return path.substr(0, remaining);
625 std::string RemoveRelativePathComponents(std::string path)
627 size_t pos = path.size();
628 size_t dotdot_count = 0;
630 size_t component_with_delim_end = pos;
631 // skip a dir delimiter
632 while (pos != 0 && IsDirDelimiter(path[pos-1]))
634 // strip a path component
635 size_t component_end = pos;
636 while (pos != 0 && !IsDirDelimiter(path[pos-1]))
638 size_t component_start = pos;
640 std::string component = path.substr(component_start,
641 component_end - component_start);
642 bool remove_this_component = false;
643 if (component == ".") {
644 remove_this_component = true;
645 } else if (component == "..") {
646 remove_this_component = true;
648 } else if (dotdot_count != 0) {
649 remove_this_component = true;
653 if (remove_this_component) {
654 while (pos != 0 && IsDirDelimiter(path[pos-1]))
656 if (component_start == 0) {
657 // We need to remove the delemiter too
658 path = path.substr(component_with_delim_end, std::string::npos);
660 path = path.substr(0, pos) + DIR_DELIM +
661 path.substr(component_with_delim_end, std::string::npos);
668 if (dotdot_count > 0)
671 // remove trailing dir delimiters
673 while (pos != 0 && IsDirDelimiter(path[pos-1]))
675 return path.substr(0, pos);
678 std::string AbsolutePath(const std::string &path)
681 char *abs_path = _fullpath(NULL, path.c_str(), MAX_PATH);
683 char *abs_path = realpath(path.c_str(), NULL);
685 if (!abs_path) return "";
686 std::string abs_path_str(abs_path);
691 const char *GetFilenameFromPath(const char *path)
693 const char *filename = strrchr(path, DIR_DELIM_CHAR);
694 return filename ? filename + 1 : path;
697 bool safeWriteToFile(const std::string &path, const std::string &content)
699 std::string tmp_file = path + ".~mt";
701 // Write to a tmp file
702 std::ofstream os(tmp_file.c_str(), std::ios::binary);
709 // Remove the temporary file because writing it failed and it's useless.
710 remove(tmp_file.c_str());
714 bool rename_success = false;
716 // Move the finished temporary file over the real file
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);
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
734 rename_success = rename(tmp_file.c_str(), path.c_str()) == 0;
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
740 remove(tmp_file.c_str());
747 bool Rename(const std::string &from, const std::string &to)
749 return rename(from.c_str(), to.c_str()) == 0;