Translated using Weblate (Italian)
[oweals/minetest.git] / src / filesys.cpp
index 694169d2071a7ad24acffaed6f069ef615ed15be..0bc351669d0e744f5255648c0dd0e37ec94fcb31 100644 (file)
@@ -27,6 +27,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "config.h"
 #include "porting.h"
+#ifdef __ANDROID__
+#include "settings.h" // For g_settings
+#endif
 
 namespace fs
 {
@@ -122,46 +125,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;
 }
 
@@ -313,9 +303,13 @@ bool RecursiveDelete(const std::string &path)
        {
                // Child
                char argv_data[3][10000];
+#ifdef __ANDROID__
+               strcpy(argv_data[0], "/system/bin/rm");
+#else
                strcpy(argv_data[0], "/bin/rm");
+#endif
                strcpy(argv_data[1], "-rf");
-               strncpy(argv_data[2], path.c_str(), 10000);
+               strncpy(argv_data[2], path.c_str(), sizeof(argv_data[2]) - 1);
                char *argv[4];
                argv[0] = argv_data[0];
                argv[1] = argv_data[1];
@@ -372,7 +366,7 @@ std::string TempPath()
                configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
        */
 #ifdef __ANDROID__
-       return DIR_DELIM "sdcard" DIR_DELIM PROJECT_NAME DIR_DELIM "tmp";
+       return g_settings->get("TMPFolder");
 #else
        return DIR_DELIM "tmp";
 #endif
@@ -697,6 +691,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;
 }