From 5b31d32da8b1dc8b5e60b495bdfc20a563487cc7 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 26 Mar 2012 00:21:38 +0300 Subject: [PATCH] Better file/directory removal platform code and utilities --- src/filesys.cpp | 134 ++++++++++++++++++++++++++++++++++++++++-------- src/filesys.h | 19 +++++-- 2 files changed, 129 insertions(+), 24 deletions(-) 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 -- 2.25.1