X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Ffilesys.cpp;h=f61b39b9465b191badf70befdb27da41e27b13f5;hb=09f9e465e760cb8fd791222405a9e5e68a676ba0;hp=bd8b94aff98ff86ebf2ca6a0a5383c4625fd8717;hpb=f522e7351a1eaffcd4b0f1f06fab65a44281f972;p=oweals%2Fminetest.git diff --git a/src/filesys.cpp b/src/filesys.cpp index bd8b94aff..f61b39b94 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -20,13 +20,16 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "util/string.h" #include -#include -#include -#include +#include +#include +#include #include #include "log.h" #include "config.h" #include "porting.h" +#ifdef __ANDROID__ +#include "settings.h" // For g_settings +#endif namespace fs { @@ -122,46 +125,33 @@ bool IsDirDelimiter(char c) bool RecursiveDelete(const std::string &path) { - infostream<<"Recursively deleting \""< content = GetDirListing(path); - for(size_t i=0; i content = GetDirListing(path); + for (const DirListNode &n: content) { + std::string fullpath = path + DIR_DELIM + n.name; + if (!RecursiveDelete(fullpath)) { + errorstream << "RecursiveDelete: Failed to recurse to " + << fullpath << std::endl; return false; } } + infostream << "RecursiveDelete: Deleting directory " << path << std::endl; + if (!RemoveDirectory(path.c_str())) { + errorstream << "Failed to recursively delete directory " + << path << std::endl; + return false; + } return true; } @@ -246,7 +236,7 @@ std::vector GetDirListing(const std::string &pathstring) If so, try stat(). */ if(isdir == -1) { - struct stat statbuf; + struct stat statbuf{}; if (stat((pathstring + "/" + node.name).c_str(), &statbuf)) continue; isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR); @@ -262,22 +252,20 @@ std::vector GetDirListing(const std::string &pathstring) bool CreateDir(const std::string &path) { int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if(r == 0) - { + if (r == 0) { return true; } - else - { - // If already exists, return true - if(errno == EEXIST) - return true; - return false; - } + + // If already exists, return true + if (errno == EEXIST) + return true; + return false; + } bool PathExists(const std::string &path) { - struct stat st; + struct stat st{}; return (stat(path.c_str(),&st) == 0); } @@ -288,7 +276,7 @@ bool IsPathAbsolute(const std::string &path) bool IsDir(const std::string &path) { - struct stat statbuf; + struct stat statbuf{}; if(stat(path.c_str(), &statbuf)) return false; // Actually error; but certainly not a directory return ((statbuf.st_mode & S_IFDIR) == S_IFDIR); @@ -315,9 +303,13 @@ bool RecursiveDelete(const std::string &path) { // Child char argv_data[3][10000]; +#ifdef __ANDROID__ + strcpy(argv_data[0], "/system/bin/rm"); +#else strcpy(argv_data[0], "/bin/rm"); +#endif strcpy(argv_data[1], "-rf"); - strncpy(argv_data[2], path.c_str(), 10000); + strncpy(argv_data[2], path.c_str(), sizeof(argv_data[2]) - 1); char *argv[4]; argv[0] = argv_data[0]; argv[1] = argv_data[1]; @@ -347,19 +339,19 @@ bool RecursiveDelete(const std::string &path) bool DeleteSingleFileOrEmptyDirectory(const std::string &path) { - if(IsDir(path)){ + if (IsDir(path)) { bool did = (rmdir(path.c_str()) == 0); - if(!did) - errorstream<<"rmdir errno: "<get("TMPFolder"); #else return DIR_DELIM "tmp"; #endif @@ -382,16 +374,36 @@ std::string TempPath() #endif -void GetRecursiveSubPaths(const std::string &path, std::vector &dst) +void GetRecursiveDirs(std::vector &dirs, const std::string &dir) +{ + static const std::set chars_to_ignore = { '_', '.' }; + if (dir.empty() || !IsDir(dir)) + return; + dirs.push_back(dir); + fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore); +} + +std::vector GetRecursiveDirs(const std::string &dir) +{ + std::vector result; + GetRecursiveDirs(result, dir); + return result; +} + +void GetRecursiveSubPaths(const std::string &path, + std::vector &dst, + bool list_files, + const std::set &ignore) { std::vector content = GetDirListing(path); - for(unsigned int i=0; i list = GetDirListing(path); - for(unsigned int i=0; i content = fs::GetDirListing(source); - for(unsigned int i=0; i < content.size(); i++){ - std::string sourcechild = source + DIR_DELIM + content[i].name; - std::string targetchild = target + DIR_DELIM + content[i].name; - if(content[i].dir){ + for (const auto &dln : content) { + std::string sourcechild = source + DIR_DELIM + dln.name; + std::string targetchild = target + DIR_DELIM + dln.name; + if(dln.dir){ if(!fs::CopyDir(sourcechild, targetchild)){ retval = false; } @@ -526,9 +536,8 @@ bool CopyDir(const std::string &source, const std::string &target) } return retval; } - else { - return false; - } + + return false; } bool PathStartsWith(const std::string &path, const std::string &prefix)