From: Perttu Ahola Date: Sun, 25 Mar 2012 21:21:38 +0000 (+0300) Subject: Better file/directory removal platform code and utilities X-Git-Tag: 0.4.dev-20120326~8 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=5b31d32da8b1dc8b5e60b495bdfc20a563487cc7;p=oweals%2Fminetest.git Better file/directory removal platform code and utilities --- diff --git a/src/filesys.cpp b/src/filesys.cpp index e8a9e88db..ab950d464 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -138,32 +138,75 @@ bool PathExists(std::string path) return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES); } -bool RecursiveDelete(std::string path) +bool IsDir(std::string path) { - infostream<<"Removing \""< content = GetDirListing(path); + for(int i=0; i @@ -249,6 +292,14 @@ bool PathExists(std::string path) return (stat(path.c_str(),&st) == 0); } +bool IsDir(std::string path) +{ + 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); +} + bool RecursiveDelete(std::string path) { /* @@ -295,8 +346,51 @@ bool RecursiveDelete(std::string path) } } +bool DeleteSingleFileOrEmptyDirectory(std::string path) +{ + if(IsDir(path)){ + bool did = (rmdir(path.c_str()) == 0); + if(!did) + errorstream<<"rmdir errno: "< &dst) +{ + std::vector content = GetDirListing(path); + for(unsigned int i=0; i &paths) +{ + bool success = true; + // Go backwards to succesfully delete the output of GetRecursiveSubPaths + for(int i=paths.size()-1; i>=0; i--){ + const std::string &path = paths[i]; + bool did = DeleteSingleFileOrEmptyDirectory(path); + if(!did){ + errorstream<<"Failed to delete "< GetDirListing(std::string path); // Returns true if already exists bool CreateDir(std::string path); -// Create all directories on the given path that don't already exist. -bool CreateAllDirs(std::string path); - bool PathExists(std::string path); +bool IsDir(std::string path); + // Only pass full paths to this one. True on success. // NOTE: The WIN32 version returns always true. bool RecursiveDelete(std::string path); +bool DeleteSingleFileOrEmptyDirectory(std::string path); + +/* Multiplatform */ + +// The path itself not included +void GetRecursiveSubPaths(std::string path, std::vector &dst); + +// Tries to delete all, returns false if any failed +bool DeletePaths(const std::vector &paths); + // Only pass full paths to this one. True on success. bool RecursiveDeleteContent(std::string path); +// Create all directories on the given path that don't already exist. +bool CreateAllDirs(std::string path); + }//fs #endif