]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/filesys.cpp
GUIScene: Clear depth buffer + replace deprecated clearZBuffer calls
[dragonfireclient.git] / src / filesys.cpp
index d965384a298669a6289d547d84c40297a2dc9a51..5ffb4506ea3c8bb50dc774b3e9db1e930f1748ae 100644 (file)
@@ -122,46 +122,33 @@ bool IsDirDelimiter(char c)
 
 bool RecursiveDelete(const std::string &path)
 {
-       infostream<<"Recursively deleting \""<<path<<"\""<<std::endl;
-
-       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;
+       infostream << "Recursively deleting \"" << path << "\"" << std::endl;
+       if (!IsDir(path)) {
+               infostream << "RecursiveDelete: Deleting file  " << path << std::endl;
+               if (!DeleteFile(path.c_str())) {
+                       errorstream << "RecursiveDelete: Failed to delete file "
+                                       << path << std::endl;
                        return false;
                }
+               return true;
        }
-       else
-       {
-               infostream<<"RecursiveDelete: Deleting content of directory "
-                               <<path<<std::endl;
-               std::vector<DirListNode> content = GetDirListing(path);
-               for(size_t 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;
+       infostream << "RecursiveDelete: Deleting content of directory "
+                       << path << std::endl;
+       std::vector<DirListNode> content = GetDirListing(path);
+       for (const DirListNode &n: content) {
+               std::string fullpath = path + DIR_DELIM + n.name;
+               if (!RecursiveDelete(fullpath)) {
+                       errorstream << "RecursiveDelete: Failed to recurse to "
+                                       << fullpath << std::endl;
                        return false;
                }
        }
+       infostream << "RecursiveDelete: Deleting directory " << path << std::endl;
+       if (!RemoveDirectory(path.c_str())) {
+               errorstream << "Failed to recursively delete directory "
+                               << path << std::endl;
+               return false;
+       }
        return true;
 }
 
@@ -305,27 +292,26 @@ bool RecursiveDelete(const std::string &path)
 
        infostream<<"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;
+               const char *argv[4] = {
+#ifdef __ANDROID__
+                       "/system/bin/rm",
+#else
+                       "/bin/rm",
+#endif
+                       "-rf",
+                       path.c_str(),
+                       NULL
+               };
 
                verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
                                <<argv[2]<<"'"<<std::endl;
 
-               execv(argv[0], argv);
+               execv(argv[0], const_cast<char**>(argv));
 
                // Execv shouldn't return. Failed.
                _exit(1);
@@ -337,7 +323,6 @@ bool RecursiveDelete(const std::string &path)
                pid_t tpid;
                do{
                        tpid = wait(&child_status);
-                       //if(tpid != child_pid) process_terminated(tpid);
                }while(tpid != child_pid);
                return (child_status == 0);
        }
@@ -371,8 +356,9 @@ std::string TempPath()
                compatible with lua's os.tmpname which under the default
                configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
        */
+
 #ifdef __ANDROID__
-       return DIR_DELIM "sdcard" DIR_DELIM PROJECT_NAME DIR_DELIM "tmp";
+       return porting::path_cache;
 #else
        return DIR_DELIM "tmp";
 #endif
@@ -380,31 +366,37 @@ std::string TempPath()
 
 #endif
 
-void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst)
+void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir)
 {
-       std::vector<DirListNode> content = GetDirListing(path);
-       for (const auto &n : content) {
-               std::string fullpath = path + DIR_DELIM + n.name;
-               dst.push_back(fullpath);
-               if (n.dir) {
-                       GetRecursiveSubPaths(fullpath, dst);
-               }
-       }
+       static const std::set<char> chars_to_ignore = { '_', '.' };
+       if (dir.empty() || !IsDir(dir))
+               return;
+       dirs.push_back(dir);
+       fs::GetRecursiveSubPaths(dir, dirs, false, chars_to_ignore);
 }
 
-bool DeletePaths(const std::vector<std::string> &paths)
+std::vector<std::string> GetRecursiveDirs(const std::string &dir)
 {
-       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;
-               }
+       std::vector<std::string> result;
+       GetRecursiveDirs(result, dir);
+       return result;
+}
+
+void GetRecursiveSubPaths(const std::string &path,
+                 std::vector<std::string> &dst,
+                 bool list_files,
+                 const std::set<char> &ignore)
+{
+       std::vector<DirListNode> content = GetDirListing(path);
+       for (const auto &n : content) {
+               std::string fullpath = path + DIR_DELIM + n.name;
+               if (ignore.count(n.name[0]))
+                       continue;
+               if (list_files || n.dir)
+                       dst.push_back(fullpath);
+               if (n.dir)
+                       GetRecursiveSubPaths(fullpath, dst, list_files, ignore);
        }
-       return success;
 }
 
 bool RecursiveDeleteContent(const std::string &path)
@@ -676,6 +668,12 @@ std::string AbsolutePath(const std::string &path)
 const char *GetFilenameFromPath(const char *path)
 {
        const char *filename = strrchr(path, DIR_DELIM_CHAR);
+       // Consistent with IsDirDelimiter this function handles '/' too
+       if (DIR_DELIM_CHAR != '/') {
+               const char *tmp = strrchr(path, '/');
+               if (tmp && tmp > filename)
+                       filename = tmp;
+       }
        return filename ? filename + 1 : path;
 }
 
@@ -729,6 +727,21 @@ bool safeWriteToFile(const std::string &path, const std::string &content)
        return true;
 }
 
+bool ReadFile(const std::string &path, std::string &out)
+{
+       std::ifstream is(path, std::ios::binary | std::ios::ate);
+       if (!is.good()) {
+               return false;
+       }
+
+       auto size = is.tellg();
+       out.resize(size);
+       is.seekg(0);
+       is.read(&out[0], size);
+
+       return true;
+}
+
 bool Rename(const std::string &from, const std::string &to)
 {
        return rename(from.c_str(), to.c_str()) == 0;