From 5b31d32da8b1dc8b5e60b495bdfc20a563487cc7 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 26 Mar 2012 00:21:38 +0300 Subject: Better file/directory removal platform code and utilities --- src/filesys.cpp | 134 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 114 insertions(+), 20 deletions(-) (limited to 'src/filesys.cpp') 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 "<