]> git.lizzy.rs Git - minetest.git/blobdiff - src/filesys.cpp
Fix mod paths
[minetest.git] / src / filesys.cpp
index 8248a13d46f3703c48061faa68f3b2e150289f8e..1070b31e593a4f70755cb66c3de3ad21a0936a00 100644 (file)
@@ -28,7 +28,7 @@ namespace fs
 #ifdef _WIN32 // WINDOWS
 
 #define _WIN32_WINNT 0x0501
-#include <Windows.h>
+#include <windows.h>
 #include <stdio.h>
 #include <malloc.h>
 #include <tchar.h> 
@@ -190,10 +190,36 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
                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;
-                       if(node.name != "." && node.name != "..")
-                               listing.push_back(node);
+                       if(node.name == "." || node.name == "..")
+                               continue;
+
+                       int isdir = -1; // -1 means unknown
+
+                       /*
+                               POSIX doesn't define d_type member of struct dirent and
+                               certain filesystems on glibc/Linux will only return
+                               DT_UNKNOWN for the d_type member.
+
+                               Also we don't know whether symlinks are directories or not.
+                       */
+#ifdef _DIRENT_HAVE_D_TYPE
+                       if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
+                               isdir = (dirp->d_type == DT_DIR);
+#endif /* _DIRENT_HAVE_D_TYPE */
+
+                       /*
+                               Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
+                               If so, try stat().
+                       */
+                       if(isdir == -1)
+                       {
+                               struct stat statbuf;
+                               if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
+                                       continue;
+                               isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
+                       }
+                       node.dir = isdir;
+                       listing.push_back(node);
                }
     }
     closedir(dp);
@@ -254,7 +280,7 @@ bool RecursiveDelete(std::string path)
                execv(argv[0], argv);
                
                // Execv shouldn't return. Failed.
-               return false;
+               _exit(1);
        }
        else
        {
@@ -279,7 +305,7 @@ bool RecursiveDeleteContent(std::string path)
        {
                if(trim(list[i].name) == "." || trim(list[i].name) == "..")
                        continue;
-               std::string childpath = path + "/" + list[i].name;
+               std::string childpath = path + DIR_DELIM + list[i].name;
                bool r = RecursiveDelete(childpath);
                if(r == false)
                {
@@ -299,13 +325,14 @@ bool CreateAllDirs(std::string path)
        while(!PathExists(basepath))
        {
                tocreate.push_back(basepath);
-               pos = basepath.rfind('/');
+               pos = basepath.rfind(DIR_DELIM_C);
                if(pos == std::string::npos)
-                       return false;
+                       break;
                basepath = basepath.substr(0,pos);
        }
        for(int i=tocreate.size()-1;i>=0;i--)
-               CreateDir(tocreate[i]);
+               if(!CreateDir(tocreate[i]))
+                       return false;
        return true;
 }