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 &
78 FILE_ATTRIBUTE_DIRECTORY;
79 if (node.name != "." && node.name != "..")
80 listing.push_back(node);
83 dwError = GetLastError();
85 if (dwError != ERROR_NO_MORE_FILES) {
86 errorstream << "GetDirListing: FindNextFile error."
87 << " Error is " << dwError << std::endl;
95 bool CreateDir(const std::string &path)
97 bool r = CreateDirectory(path.c_str(), NULL);
100 if (GetLastError() == ERROR_ALREADY_EXISTS)
105 bool PathExists(const std::string &path)
107 return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
110 bool IsPathAbsolute(const std::string &path)
112 return !PathIsRelative(path.c_str());
115 bool IsDir(const std::string &path)
117 DWORD attr = GetFileAttributes(path.c_str());
118 return (attr != INVALID_FILE_ATTRIBUTES && (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 " << path
138 infostream << "RecursiveDelete: Deleting content of directory " << path
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 " << path
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));
164 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()
180 std::vector<char> buf(bufsize);
181 DWORD len = GetTempPath(bufsize, &buf[0]);
182 if (len == 0 || len > bufsize) {
183 errorstream << "GetTempPath failed, error = " << GetLastError()
187 return std::string(buf.begin(), buf.begin() + len);
192 #include <sys/types.h>
194 #include <sys/stat.h>
195 #include <sys/wait.h>
198 std::vector<DirListNode> GetDirListing(const std::string &pathstring)
200 std::vector<DirListNode> listing;
204 if ((dp = opendir(pathstring.c_str())) == NULL) {
205 // infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
209 while ((dirp = readdir(dp)) != NULL) {
211 // Be very sure to not include '..' in the results, it will
212 // result in an epic failure when deleting stuff.
213 if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
217 node.name = dirp->d_name;
219 int isdir = -1; // -1 means unknown
222 POSIX doesn't define d_type member of struct dirent and
223 certain filesystems on glibc/Linux will only return
224 DT_UNKNOWN for the d_type member.
226 Also we don't know whether symlinks are directories or not.
228 #ifdef _DIRENT_HAVE_D_TYPE
229 if (dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
230 isdir = (dirp->d_type == DT_DIR);
231 #endif /* _DIRENT_HAVE_D_TYPE */
234 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
241 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
243 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
246 listing.push_back(node);
253 bool CreateDir(const std::string &path)
255 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
260 // If already exists, return true
266 bool PathExists(const std::string &path)
271 return (stat(path.c_str(), &st) == 0);
274 bool IsPathAbsolute(const std::string &path)
276 return path[0] == '/';
279 bool IsDir(const std::string &path)
284 if (stat(path.c_str(), &statbuf))
285 return false; // Actually error; but certainly not a directory
286 return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
289 bool IsDirDelimiter(char c)
294 bool RecursiveDelete(const std::string &path)
297 Execute the 'rm' command directly, by fork() and execve()
300 infostream << "Removing \"" << path << "\"" << std::endl;
304 pid_t child_pid = fork();
306 if (child_pid == 0) {
308 char argv_data[3][10000];
310 strcpy(argv_data[0], "/system/bin/rm");
312 strcpy(argv_data[0], "/bin/rm");
314 strcpy(argv_data[1], "-rf");
315 strncpy(argv_data[2], path.c_str(), sizeof(argv_data[2]) - 1);
317 argv[0] = argv_data[0];
318 argv[1] = argv_data[1];
319 argv[2] = argv_data[2];
322 verbosestream << "Executing '" << argv[0] << "' '" << argv[1] << "' '"
323 << argv[2] << "'" << std::endl;
325 execv(argv[0], argv);
327 // Execv shouldn't return. Failed.
334 tpid = wait(&child_status);
335 // if(tpid != child_pid) process_terminated(tpid);
336 } while (tpid != child_pid);
337 return (child_status == 0);
341 bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
344 bool did = (rmdir(path.c_str()) == 0);
346 errorstream << "rmdir errno: " << errno << ": " << strerror(errno)
351 bool did = (unlink(path.c_str()) == 0);
353 errorstream << "unlink errno: " << errno << ": " << strerror(errno)
358 std::string TempPath()
361 Should the environment variables TMPDIR, TMP and TEMP
362 and the macro P_tmpdir (if defined by stdio.h) be checked
363 before falling back on /tmp?
365 Probably not, because this function is intended to be
366 compatible with lua's os.tmpname which under the default
367 configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
370 return g_settings->get("TMPFolder");
372 return DIR_DELIM "tmp";
378 void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
380 static const std::set<char> chars_to_ignore = {'_', '.'};
381 if (dir.empty() || !IsDir(dir))
384 fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore);
387 std::vector<std::string> GetRecursiveDirs(const std::string &dir)
389 std::vector<std::string> result;
390 GetRecursiveDirs(result, dir);
394 void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst,
395 bool list_files, const std::set<char> &ignore)
397 std::vector<DirListNode> content = GetDirListing(path);
398 for (const auto &n : content) {
399 std::string fullpath = path + DIR_DELIM + n.name;
400 if (ignore.count(n.name[0]))
402 if (list_files || n.dir)
403 dst.push_back(fullpath);
405 GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
409 bool DeletePaths(const std::vector<std::string> &paths)
412 // Go backwards to succesfully delete the output of GetRecursiveSubPaths
413 for (int i = paths.size() - 1; i >= 0; i--) {
414 const std::string &path = paths[i];
415 bool did = DeleteSingleFileOrEmptyDirectory(path);
417 errorstream << "Failed to delete " << path << std::endl;
424 bool RecursiveDeleteContent(const std::string &path)
426 infostream << "Removing content of \"" << path << "\"" << std::endl;
427 std::vector<DirListNode> list = GetDirListing(path);
428 for (const DirListNode &dln : list) {
429 if (trim(dln.name) == "." || trim(dln.name) == "..")
431 std::string childpath = path + DIR_DELIM + dln.name;
432 bool r = RecursiveDelete(childpath);
434 errorstream << "Removing \"" << childpath << "\" failed"
442 bool CreateAllDirs(const std::string &path)
445 std::vector<std::string> tocreate;
446 std::string basepath = path;
447 while (!PathExists(basepath)) {
448 tocreate.push_back(basepath);
449 basepath = RemoveLastPathComponent(basepath);
450 if (basepath.empty())
453 for (int i = tocreate.size() - 1; i >= 0; i--)
454 if (!CreateDir(tocreate[i]))
459 bool CopyFileContents(const std::string &source, const std::string &target)
461 FILE *sourcefile = fopen(source.c_str(), "rb");
462 if (sourcefile == NULL) {
463 errorstream << source << ": can't open for reading: " << strerror(errno)
468 FILE *targetfile = fopen(target.c_str(), "wb");
469 if (targetfile == NULL) {
470 errorstream << target << ": can't open for writing: " << strerror(errno)
479 char readbuffer[BUFSIZ];
481 size_t readbytes = fread(readbuffer, 1, sizeof(readbuffer), sourcefile);
483 if (ferror(sourcefile)) {
484 errorstream << source << ": IO error: " << strerror(errno)
490 fwrite(readbuffer, 1, readbytes, targetfile);
492 if (feof(sourcefile) || ferror(sourcefile)) {
493 // flush destination file to catch write errors
498 if (ferror(targetfile)) {
499 errorstream << target << ": IO error: " << strerror(errno)
505 infostream << "copied " << total << " bytes from " << source << " to " << target
512 bool CopyDir(const std::string &source, const std::string &target)
514 if (PathExists(source)) {
515 if (!PathExists(target)) {
516 fs::CreateAllDirs(target);
519 std::vector<DirListNode> content = fs::GetDirListing(source);
521 for (const auto &dln : content) {
522 std::string sourcechild = source + DIR_DELIM + dln.name;
523 std::string targetchild = target + DIR_DELIM + dln.name;
525 if (!fs::CopyDir(sourcechild, targetchild)) {
529 if (!fs::CopyFileContents(sourcechild, targetchild)) {
540 bool PathStartsWith(const std::string &path, const std::string &prefix)
542 size_t pathsize = path.size();
544 size_t prefixsize = prefix.size();
545 size_t prefixpos = 0;
547 bool delim1 = pathpos == pathsize || IsDirDelimiter(path[pathpos]);
548 bool delim2 = prefixpos == prefixsize ||
549 IsDirDelimiter(prefix[prefixpos]);
551 if (delim1 != delim2)
555 while (pathpos < pathsize && IsDirDelimiter(path[pathpos]))
557 while (prefixpos < prefixsize &&
558 IsDirDelimiter(prefix[prefixpos]))
560 if (prefixpos == prefixsize)
562 if (pathpos == pathsize)
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);
573 if (pathchar != prefixchar)
576 } while (pathpos + len < pathsize &&
577 !IsDirDelimiter(path[pathpos + len]) &&
578 prefixpos + len < prefixsize &&
579 !IsDirDelimiter(prefix[prefixpos + len]));
586 std::string RemoveLastPathComponent(
587 const std::string &path, std::string *removed, int count)
592 size_t remaining = path.size();
594 for (int i = 0; i < count; ++i) {
595 // strip a dir delimiter
596 while (remaining != 0 && IsDirDelimiter(path[remaining - 1]))
598 // strip a path component
599 size_t component_end = remaining;
600 while (remaining != 0 && !IsDirDelimiter(path[remaining - 1]))
602 size_t component_start = remaining;
603 // strip a dir delimiter
604 while (remaining != 0 && IsDirDelimiter(path[remaining - 1]))
607 std::string component = path.substr(
608 component_start, component_end - component_start);
610 *removed = component + DIR_DELIM + *removed;
612 *removed = component;
615 return path.substr(0, remaining);
618 std::string RemoveRelativePathComponents(std::string path)
620 size_t pos = path.size();
621 size_t dotdot_count = 0;
623 size_t component_with_delim_end = pos;
624 // skip a dir delimiter
625 while (pos != 0 && IsDirDelimiter(path[pos - 1]))
627 // strip a path component
628 size_t component_end = pos;
629 while (pos != 0 && !IsDirDelimiter(path[pos - 1]))
631 size_t component_start = pos;
633 std::string component = path.substr(
634 component_start, component_end - component_start);
635 bool remove_this_component = false;
636 if (component == ".") {
637 remove_this_component = true;
638 } else if (component == "..") {
639 remove_this_component = true;
641 } else if (dotdot_count != 0) {
642 remove_this_component = true;
646 if (remove_this_component) {
647 while (pos != 0 && IsDirDelimiter(path[pos - 1]))
649 if (component_start == 0) {
650 // We need to remove the delemiter too
651 path = path.substr(component_with_delim_end,
654 path = path.substr(0, pos) + DIR_DELIM +
655 path.substr(component_with_delim_end,
663 if (dotdot_count > 0)
666 // remove trailing dir delimiters
668 while (pos != 0 && IsDirDelimiter(path[pos - 1]))
670 return path.substr(0, pos);
673 std::string AbsolutePath(const std::string &path)
676 char *abs_path = _fullpath(NULL, path.c_str(), MAX_PATH);
678 char *abs_path = realpath(path.c_str(), NULL);
682 std::string abs_path_str(abs_path);
687 const char *GetFilenameFromPath(const char *path)
689 const char *filename = strrchr(path, DIR_DELIM_CHAR);
690 // Consistent with IsDirDelimiter this function handles '/' too
691 if (DIR_DELIM_CHAR != '/') {
692 const char *tmp = strrchr(path, '/');
693 if (tmp && tmp > filename)
696 return filename ? filename + 1 : path;
699 bool safeWriteToFile(const std::string &path, const std::string &content)
701 std::string tmp_file = path + ".~mt";
703 // Write to a tmp file
704 std::ofstream os(tmp_file.c_str(), std::ios::binary);
711 // Remove the temporary file because writing it failed and it's useless.
712 remove(tmp_file.c_str());
716 bool rename_success = false;
718 // Move the finished temporary file over the real file
720 // When creating the file, it can cause Windows Search indexer, virus scanners and
721 // other apps to query the file. This can make the move file call below fail. We
722 // retry up to 5 times, with a 1ms sleep between, before we consider the whole
724 int number_attempts = 0;
725 while (number_attempts < 5) {
726 rename_success = MoveFileEx(tmp_file.c_str(), path.c_str(),
727 MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH);
734 // On POSIX compliant systems rename() is specified to be able to swap the
735 // file in place of the destination file, making this a truly error-proof
737 rename_success = rename(tmp_file.c_str(), path.c_str()) == 0;
739 if (!rename_success) {
740 warningstream << "Failed to write to file: " << path.c_str() << std::endl;
741 // Remove the temporary file because moving it over the target file
743 remove(tmp_file.c_str());
750 bool Rename(const std::string &from, const std::string &to)
752 return rename(from.c_str(), to.c_str()) == 0;