]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/filesys.cpp
fix to the former
[dragonfireclient.git] / src / filesys.cpp
index a2d3f9d145c2388da2db27ca7aa2c298f704c15f..8248a13d46f3703c48061faa68f3b2e150289f8e 100644 (file)
@@ -18,7 +18,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "filesys.h"
+#include "strfnd.h"
 #include <iostream>
+#include <string.h>
 
 namespace fs
 {
@@ -77,10 +79,15 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
        } 
        else 
        {
+               // NOTE:
+               // Be very sure to not include '..' in the results, it will
+               // result in an epic failure when deleting stuff.
+
                DirListNode node;
                node.name = FindFileData.cFileName;
                node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
-               listing.push_back(node);
+               if(node.name != "." && node.name != "..")
+                       listing.push_back(node);
 
                // List all the other files in the directory.
                while (FindNextFile(hFind, &FindFileData) != 0) 
@@ -88,7 +95,8 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
                        DirListNode node;
                        node.name = FindFileData.cFileName;
                        node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
-                       listing.push_back(node);
+                       if(node.name != "." && node.name != "..")
+                               listing.push_back(node);
                }
 
                dwError = GetLastError();
@@ -130,12 +138,39 @@ bool PathExists(std::string path)
        return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
 }
 
+bool RecursiveDelete(std::string path)
+{
+       std::cerr<<"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);
+
+       if(r != 0)
+               std::cerr<<"SHFileOperation returned "<<r<<std::endl;
+
+       //return (r == 0);
+       return true;
+}
+
 #else // POSIX
 
 #include <sys/types.h>
 #include <dirent.h>
 #include <errno.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 
 std::vector<DirListNode> GetDirListing(std::string pathstring)
 {
@@ -149,12 +184,16 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
     }
 
     while ((dirp = readdir(dp)) != NULL) {
+               // NOTE:
+               // Be very sure to not include '..' in the results, it will
+               // result in an epic failure when deleting stuff.
                if(dirp->d_name[0]!='.'){
                        DirListNode node;
                        node.name = dirp->d_name;
                        if(dirp->d_type == DT_DIR) node.dir = true;
                        else node.dir = false;
-                       listing.push_back(node);
+                       if(node.name != "." && node.name != "..")
+                               listing.push_back(node);
                }
     }
     closedir(dp);
@@ -184,7 +223,91 @@ bool PathExists(std::string path)
        return (stat(path.c_str(),&st) == 0);
 }
 
+bool RecursiveDelete(std::string path)
+{
+       /*
+               Execute the 'rm' command directly, by fork() and execve()
+       */
+       
+       std::cerr<<"Removing \""<<path<<"\""<<std::endl;
+
+       //return false;
+       
+       pid_t child_pid = fork();
+
+       if(child_pid == 0)
+       {
+               // Child
+               char argv_data[3][10000];
+               strcpy(argv_data[0], "/bin/rm");
+               strcpy(argv_data[1], "-rf");
+               strncpy(argv_data[2], path.c_str(), 10000);
+               char *argv[4];
+               argv[0] = argv_data[0];
+               argv[1] = argv_data[1];
+               argv[2] = argv_data[2];
+               argv[3] = NULL;
+
+               std::cerr<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
+                               <<argv[2]<<"'"<<std::endl;
+               
+               execv(argv[0], argv);
+               
+               // Execv shouldn't return. Failed.
+               return false;
+       }
+       else
+       {
+               // Parent
+               int child_status;
+               pid_t tpid;
+               do{
+                       tpid = wait(&child_status);
+                       //if(tpid != child_pid) process_terminated(tpid);
+               }while(tpid != child_pid);
+               return (child_status == 0);
+       }
+}
+
 #endif
 
+bool RecursiveDeleteContent(std::string path)
+{
+       std::cerr<<"Removing content of \""<<path<<"\""<<std::endl;
+       std::vector<DirListNode> list = GetDirListing(path);
+       for(unsigned int i=0; i<list.size(); i++)
+       {
+               if(trim(list[i].name) == "." || trim(list[i].name) == "..")
+                       continue;
+               std::string childpath = path + "/" + list[i].name;
+               bool r = RecursiveDelete(childpath);
+               if(r == false)
+               {
+                       std::cerr<<"Removing \""<<childpath<<"\" failed"<<std::endl;
+                       return false;
+               }
+       }
+       return true;
+}
+
+bool CreateAllDirs(std::string path)
+{
+
+       size_t pos;
+       std::vector<std::string> tocreate;
+       std::string basepath = path;
+       while(!PathExists(basepath))
+       {
+               tocreate.push_back(basepath);
+               pos = basepath.rfind('/');
+               if(pos == std::string::npos)
+                       return false;
+               basepath = basepath.substr(0,pos);
+       }
+       for(int i=tocreate.size()-1;i>=0;i--)
+               CreateDir(tocreate[i]);
+       return true;
+}
+
 } // namespace fs