]> git.lizzy.rs Git - minetest.git/blobdiff - src/filesys.cpp
Fix EmergeThread hang on exit
[minetest.git] / src / filesys.cpp
index 805aae6aedfe5bf271e6b354bdf32f827eef9e91..256c8f16a6dea94a29c9bd3e437fa5686c1bd4fa 100644 (file)
@@ -1,18 +1,18 @@
 /*
-Minetest-c55
-Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
@@ -74,9 +74,8 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
 
        if (hFind == INVALID_HANDLE_VALUE) 
        {
-         errorstream<<"GetDirListing: Invalid file handle. Error is "
-                       <<GetLastError()<<std::endl;
-         retval = (-1);
+               retval = (-1);
+               goto Cleanup;
        } 
        else 
        {
@@ -139,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 \""<<path<<"\""<<std::endl;
-
-       //return false;
-       
-       // This silly function needs a double-null terminated string...
-       // Well, we'll just make sure it has at least two, then.
-       path += "\0\0";
-
-       SHFILEOPSTRUCT sfo;
-       sfo.hwnd = NULL;
-       sfo.wFunc = FO_DELETE;
-       sfo.pFrom = path.c_str();
-       sfo.pTo = NULL;
-       sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
-       
-       int r = SHFileOperation(&sfo);
+       DWORD attr = GetFileAttributes(path.c_str());
+       return (attr != INVALID_FILE_ATTRIBUTES &&
+                       (attr & FILE_ATTRIBUTE_DIRECTORY));
+}
 
-       if(r != 0)
-               errorstream<<"SHFileOperation returned "<<r<<std::endl;
+bool RecursiveDelete(std::string path)
+{
+       infostream<<"Recursively deleting \""<<path<<"\""<<std::endl;
 
-       //return (r == 0);
+       DWORD attr = GetFileAttributes(path.c_str());
+       bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
+                       (attr & FILE_ATTRIBUTE_DIRECTORY));
+       if(!is_directory)
+       {
+               infostream<<"RecursiveDelete: Deleting file "<<path<<std::endl;
+               //bool did = DeleteFile(path.c_str());
+               bool did = true;
+               if(!did){
+                       errorstream<<"RecursiveDelete: Failed to delete file "
+                                       <<path<<std::endl;
+                       return false;
+               }
+       }
+       else
+       {
+               infostream<<"RecursiveDelete: Deleting content of directory "
+                               <<path<<std::endl;
+               std::vector<DirListNode> content = GetDirListing(path);
+               for(int i=0; i<content.size(); i++){
+                       const DirListNode &n = content[i];
+                       std::string fullpath = path + DIR_DELIM + n.name;
+                       bool did = RecursiveDelete(fullpath);
+                       if(!did){
+                               errorstream<<"RecursiveDelete: Failed to recurse to "
+                                               <<fullpath<<std::endl;
+                               return false;
+                       }
+               }
+               infostream<<"RecursiveDelete: Deleting directory "<<path<<std::endl;
+               //bool did = RemoveDirectory(path.c_str();
+               bool did = true;
+               if(!did){
+                       errorstream<<"Failed to recursively delete directory "
+                                       <<path<<std::endl;
+                       return false;
+               }
+       }
        return true;
 }
 
+bool DeleteSingleFileOrEmptyDirectory(std::string path)
+{
+       DWORD attr = GetFileAttributes(path.c_str());
+       bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
+                       (attr & FILE_ATTRIBUTE_DIRECTORY));
+       if(!is_directory)
+       {
+               bool did = DeleteFile(path.c_str());
+               return did;
+       }
+       else
+       {
+               bool did = RemoveDirectory(path.c_str());
+               return did;
+       }
+}
+
 #else // POSIX
 
 #include <sys/types.h>
@@ -172,6 +214,7 @@ bool RecursiveDelete(std::string path)
 #include <errno.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
 std::vector<DirListNode> GetDirListing(std::string pathstring)
 {
@@ -250,6 +293,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)
 {
        /*
@@ -296,8 +347,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: "<<errno<<": "<<strerror(errno)
+                                       <<std::endl;
+               return did;
+       } else {
+               bool did = (unlink(path.c_str()) == 0);
+               if(!did)
+                       errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
+                                       <<std::endl;
+               return did;
+       }
+}
+
 #endif
 
+void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst)
+{
+       std::vector<DirListNode> content = GetDirListing(path);
+       for(unsigned int  i=0; i<content.size(); i++){
+               const DirListNode &n = content[i];
+               std::string fullpath = path + DIR_DELIM + n.name;
+               dst.push_back(fullpath);
+               GetRecursiveSubPaths(fullpath, dst);
+       }
+}
+
+bool DeletePaths(const std::vector<std::string> &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 "<<path<<std::endl;
+                       success = false;
+               }
+       }
+       return success;
+}
+
 bool RecursiveDeleteContent(std::string path)
 {
        infostream<<"Removing content of \""<<path<<"\""<<std::endl;