Browse Source

Merge pull request #1659 from slime73/filesystem

Expand love.filesystem APIs

- Add love.filesystem.mountFullPath and love.filesystem.unmountFullPath
- Add Add love.filesystem.mountCommonPath, love.filesystem.unmountCommonPath, and love.filesystem.getFullCommonPath
- Add readonly boolean field to the table returned by love.filesystem.getInfo
Alex Szpakowski 4 years ago
parent
commit
79d0a7a228

+ 8 - 0
platform/xcode/liblove.xcodeproj/project.pbxproj

@@ -819,6 +819,8 @@
 		FAAA3FDA1F64B3AD00F89E99 /* lstrlib.h in Headers */ = {isa = PBXBuildFile; fileRef = FAAA3FD51F64B3AD00F89E99 /* lstrlib.h */; };
 		FAAA3FDA1F64B3AD00F89E99 /* lstrlib.h in Headers */ = {isa = PBXBuildFile; fileRef = FAAA3FD51F64B3AD00F89E99 /* lstrlib.h */; };
 		FAAA3FDB1F64B3AD00F89E99 /* lutf8lib.c in Sources */ = {isa = PBXBuildFile; fileRef = FAAA3FD61F64B3AD00F89E99 /* lutf8lib.c */; };
 		FAAA3FDB1F64B3AD00F89E99 /* lutf8lib.c in Sources */ = {isa = PBXBuildFile; fileRef = FAAA3FD61F64B3AD00F89E99 /* lutf8lib.c */; };
 		FAAA3FDC1F64B3AD00F89E99 /* lutf8lib.h in Headers */ = {isa = PBXBuildFile; fileRef = FAAA3FD71F64B3AD00F89E99 /* lutf8lib.h */; };
 		FAAA3FDC1F64B3AD00F89E99 /* lutf8lib.h in Headers */ = {isa = PBXBuildFile; fileRef = FAAA3FD71F64B3AD00F89E99 /* lutf8lib.h */; };
+		FAAC2F79251A9D2200BCB81B /* apple.mm in Sources */ = {isa = PBXBuildFile; fileRef = FAAC2F78251A9D2200BCB81B /* apple.mm */; };
+		FAAC2F7A251A9D2200BCB81B /* apple.mm in Sources */ = {isa = PBXBuildFile; fileRef = FAAC2F78251A9D2200BCB81B /* apple.mm */; };
 		FAAFF04416CB11C700CCDE45 /* OpenAL-Soft.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FAAFF04316CB11C700CCDE45 /* OpenAL-Soft.framework */; };
 		FAAFF04416CB11C700CCDE45 /* OpenAL-Soft.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FAAFF04316CB11C700CCDE45 /* OpenAL-Soft.framework */; };
 		FAB17BE61ABFAA9000F9BA27 /* lz4.c in Sources */ = {isa = PBXBuildFile; fileRef = FAB17BE41ABFAA9000F9BA27 /* lz4.c */; };
 		FAB17BE61ABFAA9000F9BA27 /* lz4.c in Sources */ = {isa = PBXBuildFile; fileRef = FAB17BE41ABFAA9000F9BA27 /* lz4.c */; };
 		FAB17BE71ABFAA9000F9BA27 /* lz4.c in Sources */ = {isa = PBXBuildFile; fileRef = FAB17BE41ABFAA9000F9BA27 /* lz4.c */; };
 		FAB17BE71ABFAA9000F9BA27 /* lz4.c in Sources */ = {isa = PBXBuildFile; fileRef = FAB17BE41ABFAA9000F9BA27 /* lz4.c */; };
@@ -1822,6 +1824,8 @@
 		FAAA3FD51F64B3AD00F89E99 /* lstrlib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lstrlib.h; sourceTree = "<group>"; };
 		FAAA3FD51F64B3AD00F89E99 /* lstrlib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lstrlib.h; sourceTree = "<group>"; };
 		FAAA3FD61F64B3AD00F89E99 /* lutf8lib.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lutf8lib.c; sourceTree = "<group>"; };
 		FAAA3FD61F64B3AD00F89E99 /* lutf8lib.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lutf8lib.c; sourceTree = "<group>"; };
 		FAAA3FD71F64B3AD00F89E99 /* lutf8lib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lutf8lib.h; sourceTree = "<group>"; };
 		FAAA3FD71F64B3AD00F89E99 /* lutf8lib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lutf8lib.h; sourceTree = "<group>"; };
+		FAAC2F78251A9D2200BCB81B /* apple.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = apple.mm; sourceTree = "<group>"; };
+		FAAC2F7F251A9D3E00BCB81B /* apple.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = apple.h; sourceTree = "<group>"; };
 		FAAFF04316CB11C700CCDE45 /* OpenAL-Soft.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = "OpenAL-Soft.framework"; path = "/Library/Frameworks/OpenAL-Soft.framework"; sourceTree = "<absolute>"; };
 		FAAFF04316CB11C700CCDE45 /* OpenAL-Soft.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = "OpenAL-Soft.framework"; path = "/Library/Frameworks/OpenAL-Soft.framework"; sourceTree = "<absolute>"; };
 		FAB17BE41ABFAA9000F9BA27 /* lz4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lz4.c; sourceTree = "<group>"; };
 		FAB17BE41ABFAA9000F9BA27 /* lz4.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lz4.c; sourceTree = "<group>"; };
 		FAB17BE51ABFAA9000F9BA27 /* lz4.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lz4.h; sourceTree = "<group>"; };
 		FAB17BE51ABFAA9000F9BA27 /* lz4.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lz4.h; sourceTree = "<group>"; };
@@ -2193,6 +2197,8 @@
 			children = (
 			children = (
 				FAA3A9AC1B7D465A00CED060 /* android.cpp */,
 				FAA3A9AC1B7D465A00CED060 /* android.cpp */,
 				FAA3A9AD1B7D465A00CED060 /* android.h */,
 				FAA3A9AD1B7D465A00CED060 /* android.h */,
+				FAAC2F7F251A9D3E00BCB81B /* apple.h */,
+				FAAC2F78251A9D2200BCB81B /* apple.mm */,
 				FA0B78F71A958E3B000E1D17 /* b64.cpp */,
 				FA0B78F71A958E3B000E1D17 /* b64.cpp */,
 				FA0B78F81A958E3B000E1D17 /* b64.h */,
 				FA0B78F81A958E3B000E1D17 /* b64.h */,
 				FA6BDE5B1F31725300786805 /* Color.h */,
 				FA6BDE5B1F31725300786805 /* Color.h */,
@@ -4632,6 +4638,7 @@
 				FA0B7E1F1A95902C000E1D17 /* Physics.cpp in Sources */,
 				FA0B7E1F1A95902C000E1D17 /* Physics.cpp in Sources */,
 				FA6A2B7B1F60B8250074C308 /* wrap_ByteData.cpp in Sources */,
 				FA6A2B7B1F60B8250074C308 /* wrap_ByteData.cpp in Sources */,
 				FA0B7E821A95902C000E1D17 /* Shape.cpp in Sources */,
 				FA0B7E821A95902C000E1D17 /* Shape.cpp in Sources */,
+				FAAC2F7A251A9D2200BCB81B /* apple.mm in Sources */,
 				FA0B7ACE1A958EA3000E1D17 /* packet.c in Sources */,
 				FA0B7ACE1A958EA3000E1D17 /* packet.c in Sources */,
 				FABDA9872552448200B5C523 /* b2_body.cpp in Sources */,
 				FABDA9872552448200B5C523 /* b2_body.cpp in Sources */,
 				FAF140891E20934C00F898D2 /* PoolAlloc.cpp in Sources */,
 				FAF140891E20934C00F898D2 /* PoolAlloc.cpp in Sources */,
@@ -5025,6 +5032,7 @@
 				217DFC0B1D9F6D490055D849 /* unixtcp.c in Sources */,
 				217DFC0B1D9F6D490055D849 /* unixtcp.c in Sources */,
 				FAF140881E20934C00F898D2 /* PoolAlloc.cpp in Sources */,
 				FAF140881E20934C00F898D2 /* PoolAlloc.cpp in Sources */,
 				FA0B7DEE1A95902C000E1D17 /* Mouse.cpp in Sources */,
 				FA0B7DEE1A95902C000E1D17 /* Mouse.cpp in Sources */,
+				FAAC2F79251A9D2200BCB81B /* apple.mm in Sources */,
 				FAA54ACC1F91660400A8FA7B /* TheoraVideoStream.cpp in Sources */,
 				FAA54ACC1F91660400A8FA7B /* TheoraVideoStream.cpp in Sources */,
 				FA1E887E1DF363CD00E808AA /* Filter.cpp in Sources */,
 				FA1E887E1DF363CD00E808AA /* Filter.cpp in Sources */,
 				FABDA9862552448200B5C523 /* b2_body.cpp in Sources */,
 				FABDA9862552448200B5C523 /* b2_body.cpp in Sources */,

+ 15 - 0
src/common/StringMap.h

@@ -197,6 +197,21 @@ bool getConstant(const char *in, type &out) { return name##s.find(in, out); } \
 bool getConstant(type in, const char *&out) { return name##s.find(in, out); } \
 bool getConstant(type in, const char *&out) { return name##s.find(in, out); } \
 std::vector<std::string> getConstants(type) { return name##s.getNames(); }
 std::vector<std::string> getConstants(type) { return name##s.getNames(); }
 
 
+#define STRINGMAP_CLASS_DECLARE(type) \
+static bool getConstant(const char *in, type &out); \
+static bool getConstant(type in, const char *&out); \
+static std::vector<std::string> getConstants(type); \
+
+#define STRINGMAP_CLASS_BEGIN(classname, type, count, name) \
+static StringMap<type, count>::Entry name##Entries[] =
+
+#define STRINGMAP_CLASS_END(classname, type, count, name) \
+; \
+static StringMap<type, count> name##s(name##Entries, sizeof(name##Entries)); \
+bool classname::getConstant(const char *in, type &out) { return name##s.find(in, out); } \
+bool classname::getConstant(type in, const char *&out) { return name##s.find(in, out); } \
+std::vector<std::string> classname::getConstants(type) { return name##s.getNames(); }
+
 } // love
 } // love
 
 
 #endif // LOVE_STRING_MAP_H
 #endif // LOVE_STRING_MAP_H

+ 50 - 0
src/common/apple.h

@@ -0,0 +1,50 @@
+/**
+ * Copyright (c) 2006-2020 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#pragma once
+
+#include "config.h"
+
+#if defined(LOVE_IOS) || defined(LOVE_MACOS)
+
+#include <string>
+
+namespace love
+{
+namespace apple
+{
+
+enum UserDirectory
+{
+	USER_DIRECTORY_HOME,
+	USER_DIRECTORY_APPSUPPORT,
+	USER_DIRECTORY_DOCUMENTS,
+	USER_DIRECTORY_DESKTOP,
+	USER_DIRECTORY_CACHES,
+	USER_DIRECTORY_TEMP,
+};
+
+std::string getUserDirectory(UserDirectory dir);
+std::string getExecutablePath();
+
+} // apple
+} // love
+
+#endif // defined(LOVE_IOS) || defined(LOVE_MACOS)

+ 79 - 0
src/common/apple.mm

@@ -0,0 +1,79 @@
+/**
+ * Copyright (c) 2006-2020 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#include "apple.h"
+
+#if defined(LOVE_IOS) || defined(LOVE_MACOS)
+
+#import <Foundation/Foundation.h>
+
+namespace love
+{
+namespace apple
+{
+
+std::string getUserDirectory(UserDirectory dir)
+{
+	std::string path;
+	NSSearchPathDirectory nsdir = NSTrashDirectory;
+
+	@autoreleasepool
+	{
+		switch (dir)
+		{
+		case USER_DIRECTORY_HOME:
+			return NSHomeDirectory().UTF8String;
+		case USER_DIRECTORY_APPSUPPORT:
+			nsdir = NSApplicationSupportDirectory;
+			break;
+		case USER_DIRECTORY_DOCUMENTS:
+			nsdir = NSDocumentDirectory;
+			break;
+		case USER_DIRECTORY_DESKTOP:
+			nsdir = NSDesktopDirectory;
+			break;
+		case USER_DIRECTORY_CACHES:
+			nsdir = NSCachesDirectory;
+			break;
+		case USER_DIRECTORY_TEMP:
+			nsdir = NSItemReplacementDirectory;
+			break;
+		}
+
+		NSArray<NSURL *> *dirs = [[NSFileManager defaultManager] URLsForDirectory:nsdir inDomains:NSUserDomainMask];
+		if (dirs.count > 0)
+			path = [dirs[0].path UTF8String];
+	}
+
+	return path;
+}
+
+std::string getExecutablePath()
+{
+	@autoreleasepool
+	{
+		return std::string([NSBundle mainBundle].executablePath.UTF8String);
+	}
+}
+
+} // apple
+} // love
+
+#endif // defined(LOVE_IOS) || defined(LOVE_MACOS)

+ 0 - 15
src/common/ios.h

@@ -42,27 +42,12 @@ namespace ios
  **/
  **/
 std::string getLoveInResources(bool &fused);
 std::string getLoveInResources(bool &fused);
 
 
-/**
- * Gets the directory path where files should be stored.
- **/
-std::string getAppdataDirectory();
-
-/**
- * Get the home directory (on iOS, this really means the app's sandbox dir.)
- **/
-std::string getHomeDirectory();
-
 /**
 /**
  * Opens the specified URL with the default program associated with the URL's
  * Opens the specified URL with the default program associated with the URL's
  * scheme.
  * scheme.
  **/
  **/
 bool openURL(const std::string &url);
 bool openURL(const std::string &url);
 
 
-/**
- * Returns the full path to the executable.
- **/
-std::string getExecutablePath();
-
 /**
 /**
  * Causes devices with vibration support to vibrate for about 0.5 seconds.
  * Causes devices with vibration support to vibrate for about 0.5 seconds.
  **/
  **/

+ 1 - 46
src/common/ios.mm

@@ -19,6 +19,7 @@
  **/
  **/
 
 
 #include "ios.h"
 #include "ios.h"
+#include "apple.h"
 
 
 #ifdef LOVE_IOS
 #ifdef LOVE_IOS
 
 
@@ -127,12 +128,6 @@ static bool deleteFileInDocuments(NSString *filename);
 
 
 @end
 @end
 
 
-static NSString *getDocumentsDirectory()
-{
-	NSArray *docdirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-	return docdirs[0];
-}
-
 static NSArray *getLovesInDocuments()
 static NSArray *getLovesInDocuments()
 {
 {
 	NSMutableArray *paths = [NSMutableArray new];
 	NSMutableArray *paths = [NSMutableArray new];
@@ -339,38 +334,6 @@ std::string getLoveInResources(bool &fused)
 	return path;
 	return path;
 }
 }
 
 
-static std::string getUserDirectory(NSSearchPathDirectory dir)
-{
-	std::string path;
-
-	@autoreleasepool
-	{
-		NSArray<NSURL *> *dirs = [[NSFileManager defaultManager] URLsForDirectory:dir inDomains:NSUserDomainMask];
-
-		if (dirs.count > 0)
-			path = [dirs[0].path UTF8String];
-	}
-
-	return path;
-}
-
-std::string getAppdataDirectory()
-{
-	return getUserDirectory(NSApplicationSupportDirectory);
-}
-
-std::string getHomeDirectory()
-{
-	std::string path;
-
-	@autoreleasepool
-	{
-		path = [NSHomeDirectory() UTF8String];
-	}
-
-	return path;
-}
-
 bool openURL(const std::string &url)
 bool openURL(const std::string &url)
 {
 {
 	bool success = false;
 	bool success = false;
@@ -387,14 +350,6 @@ bool openURL(const std::string &url)
 	return success;
 	return success;
 }
 }
 
 
-std::string getExecutablePath()
-{
-	@autoreleasepool
-	{
-		return std::string([NSBundle mainBundle].executablePath.UTF8String);
-	}
-}
-
 void vibrate()
 void vibrate()
 {
 {
 	@autoreleasepool
 	@autoreleasepool

+ 0 - 7
src/common/macos.h

@@ -31,8 +31,6 @@ namespace love
 namespace macos
 namespace macos
 {
 {
 
 
-std::string getAppdataDirectory();
-
 /**
 /**
  * Returns the filepath of the first detected love file in the Resources folder
  * Returns the filepath of the first detected love file in the Resources folder
  * in the main bundle (love.app.)
  * in the main bundle (love.app.)
@@ -46,11 +44,6 @@ std::string getLoveInResources();
  **/
  **/
 std::string checkDropEvents();
 std::string checkDropEvents();
 
 
-/**
- * Returns the full path to the executable.
- **/
-std::string getExecutablePath();
-
 /**
 /**
  * Bounce the dock icon, if the app isn't in the foreground.
  * Bounce the dock icon, if the app isn't in the foreground.
  **/
  **/

+ 0 - 28
src/common/macos.mm

@@ -36,26 +36,6 @@ namespace love
 namespace macos
 namespace macos
 {
 {
 
 
-static std::string getUserDirectory(NSSearchPathDirectory dir)
-{
-	std::string path;
-
-	@autoreleasepool
-	{
-		NSArray<NSURL *> *dirs = [[NSFileManager defaultManager] URLsForDirectory:dir inDomains:NSUserDomainMask];
-
-		if (dirs.count > 0)
-			path = [dirs[0].path UTF8String];
-	}
-
-	return path;
-}
-
-std::string getAppdataDirectory()
-{
-	return getUserDirectory(NSApplicationSupportDirectory);
-}
-
 std::string getLoveInResources()
 std::string getLoveInResources()
 {
 {
 	std::string path;
 	std::string path;
@@ -94,14 +74,6 @@ std::string checkDropEvents()
 	return dropstr;
 	return dropstr;
 }
 }
 
 
-std::string getExecutablePath()
-{
-	@autoreleasepool
-	{
-		return std::string([NSBundle mainBundle].executablePath.UTF8String);
-	}
-}
-
 void requestAttention(bool continuous)
 void requestAttention(bool continuous)
 {
 {
 	@autoreleasepool
 	@autoreleasepool

+ 137 - 18
src/libraries/physfs/physfs.c

@@ -49,6 +49,7 @@ typedef struct __PHYSFS_DIRHANDLE__
     char *root;  /* subdirectory of archiver to use as root of archive (NULL for actual root) */
     char *root;  /* subdirectory of archiver to use as root of archive (NULL for actual root) */
     size_t rootlen;  /* subdirectory of archiver to use as root of archive (NULL for actual root) */
     size_t rootlen;  /* subdirectory of archiver to use as root of archive (NULL for actual root) */
     const PHYSFS_Archiver *funcs;  /* Ptr to archiver info for this handle. */
     const PHYSFS_Archiver *funcs;  /* Ptr to archiver info for this handle. */
+    int forWriting; /* Whether this was opened for writing or not. */
     struct __PHYSFS_DIRHANDLE__ *next;  /* linked list stuff. */
     struct __PHYSFS_DIRHANDLE__ *next;  /* linked list stuff. */
 } DirHandle;
 } DirHandle;
 
 
@@ -862,6 +863,7 @@ static DirHandle *tryOpenDir(PHYSFS_Io *io, const PHYSFS_Archiver *funcs,
             retval->mountPoint = NULL;
             retval->mountPoint = NULL;
             retval->funcs = funcs;
             retval->funcs = funcs;
             retval->opaque = opaque;
             retval->opaque = opaque;
+            retval->forWriting = forWriting;
         } /* else */
         } /* else */
     } /* if */
     } /* if */
 
 
@@ -1102,6 +1104,23 @@ static int freeDirHandle(DirHandle *dh, FileHandle *openList)
 } /* freeDirHandle */
 } /* freeDirHandle */
 
 
 
 
+static int dirHandleFilesOpen(DirHandle *dh, FileHandle *openList)
+{
+    FileHandle *i;
+
+    if (dh == NULL)
+        return 0;
+
+    for (i = openList; i != NULL; i = i->next)
+    {
+        if (i->dirHandle == dh)
+            return 1;
+    }
+
+    return 0;
+} /* dirHandleFilesOpen */
+
+
 static char *calculateBaseDir(const char *argv0)
 static char *calculateBaseDir(const char *argv0)
 {
 {
     const char dirsep = __PHYSFS_platformDirSeparator;
     const char dirsep = __PHYSFS_platformDirSeparator;
@@ -1749,7 +1768,7 @@ int PHYSFS_setRoot(const char *archive, const char *subdir)
 
 
 
 
 static int doMount(PHYSFS_Io *io, const char *fname,
 static int doMount(PHYSFS_Io *io, const char *fname,
-                   const char *mountPoint, int appendToPath)
+                   const char *mountPoint, int appendToPath, int forWriting)
 {
 {
     DirHandle *dh;
     DirHandle *dh;
     DirHandle *prev = NULL;
     DirHandle *prev = NULL;
@@ -1770,7 +1789,7 @@ static int doMount(PHYSFS_Io *io, const char *fname,
         prev = i;
         prev = i;
     } /* for */
     } /* for */
 
 
-    dh = createDirHandle(io, fname, mountPoint, 0);
+    dh = createDirHandle(io, fname, mountPoint, forWriting);
     BAIL_IF_MUTEX_ERRPASS(!dh, stateLock, 0);
     BAIL_IF_MUTEX_ERRPASS(!dh, stateLock, 0);
 
 
     if (appendToPath)
     if (appendToPath)
@@ -1797,7 +1816,7 @@ int PHYSFS_mountIo(PHYSFS_Io *io, const char *fname,
     BAIL_IF(!io, PHYSFS_ERR_INVALID_ARGUMENT, 0);
     BAIL_IF(!io, PHYSFS_ERR_INVALID_ARGUMENT, 0);
     BAIL_IF(!fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
     BAIL_IF(!fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
     BAIL_IF(io->version != 0, PHYSFS_ERR_UNSUPPORTED, 0);
     BAIL_IF(io->version != 0, PHYSFS_ERR_UNSUPPORTED, 0);
-    return doMount(io, fname, mountPoint, appendToPath);
+    return doMount(io, fname, mountPoint, appendToPath, 0);
 } /* PHYSFS_mountIo */
 } /* PHYSFS_mountIo */
 
 
 
 
@@ -1813,7 +1832,7 @@ int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),
 
 
     io = __PHYSFS_createMemoryIo(buf, len, del);
     io = __PHYSFS_createMemoryIo(buf, len, del);
     BAIL_IF_ERRPASS(!io, 0);
     BAIL_IF_ERRPASS(!io, 0);
-    retval = doMount(io, fname, mountPoint, appendToPath);
+    retval = doMount(io, fname, mountPoint, appendToPath, 0);
     if (!retval)
     if (!retval)
     {
     {
         /* docs say not to call (del) in case of failure, so cheat. */
         /* docs say not to call (del) in case of failure, so cheat. */
@@ -1837,7 +1856,7 @@ int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
 
 
     io = __PHYSFS_createHandleIo(file);
     io = __PHYSFS_createHandleIo(file);
     BAIL_IF_ERRPASS(!io, 0);
     BAIL_IF_ERRPASS(!io, 0);
-    retval = doMount(io, fname, mountPoint, appendToPath);
+    retval = doMount(io, fname, mountPoint, appendToPath, 0);
     if (!retval)
     if (!retval)
     {
     {
         /* docs say not to destruct in case of failure, so cheat. */
         /* docs say not to destruct in case of failure, so cheat. */
@@ -1852,7 +1871,14 @@ int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
 int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
 int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
 {
 {
     BAIL_IF(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
     BAIL_IF(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
-    return doMount(NULL, newDir, mountPoint, appendToPath);
+    return doMount(NULL, newDir, mountPoint, appendToPath, 0);
+} /* PHYSFS_mount */
+
+
+int PHYSFS_mountRW(const char *newDir, const char *mountPoint, int appendToPath)
+{
+    BAIL_IF(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
+    return doMount(NULL, newDir, mountPoint, appendToPath, 1);
 } /* PHYSFS_mount */
 } /* PHYSFS_mount */
 
 
 
 
@@ -1882,6 +1908,8 @@ int PHYSFS_unmount(const char *oldDir)
         if (strcmp(i->dirName, oldDir) == 0)
         if (strcmp(i->dirName, oldDir) == 0)
         {
         {
             next = i->next;
             next = i->next;
+            if (i->forWriting && dirHandleFilesOpen(i, openWriteList))
+                BAIL_MUTEX(PHYSFS_ERR_FILES_STILL_OPEN, stateLock, 0);
             BAIL_IF_MUTEX_ERRPASS(!freeDirHandle(i, openReadList),
             BAIL_IF_MUTEX_ERRPASS(!freeDirHandle(i, openReadList),
                                 stateLock, 0);
                                 stateLock, 0);
 
 
@@ -1899,6 +1927,28 @@ int PHYSFS_unmount(const char *oldDir)
 } /* PHYSFS_unmount */
 } /* PHYSFS_unmount */
 
 
 
 
+int PHYSFS_canUnmount(const char *oldDir)
+{
+    DirHandle *i;
+
+    BAIL_IF(oldDir == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
+
+    __PHYSFS_platformGrabMutex(stateLock);
+    for (i = searchPath; i != NULL; i = i->next)
+    {
+        if (strcmp(i->dirName, oldDir) == 0)
+        {
+            if (i->forWriting && dirHandleFilesOpen(i, openWriteList))
+                BAIL_MUTEX(PHYSFS_ERR_OK, stateLock, 0);
+            if (dirHandleFilesOpen(i, openReadList))
+                BAIL_MUTEX(PHYSFS_ERR_OK, stateLock, 0);
+            BAIL_MUTEX(PHYSFS_ERR_OK, stateLock, 1);
+        }
+    }
+
+    BAIL_MUTEX(PHYSFS_ERR_NOT_MOUNTED, stateLock, 0);
+} /* PHYSFS_canUnmount */
+
 char **PHYSFS_getSearchPath(void)
 char **PHYSFS_getSearchPath(void)
 {
 {
     return doEnumStringList(PHYSFS_getSearchPathCallback);
     return doEnumStringList(PHYSFS_getSearchPathCallback);
@@ -2159,10 +2209,76 @@ static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
 } /* verifyPath */
 } /* verifyPath */
 
 
 
 
+static int countPathComponents(const char *path)
+{
+    int components = 0;
+    const char *start;
+    const char *end;
+
+    if (path == NULL)
+        return 0;
+
+    if (*path != '/')
+        components = 1;
+
+    start = path;
+    while (1)
+    {
+        end = strchr(start, '/');
+
+        if (end == NULL)
+            break;
+
+        components++;
+        start = end + 1;
+    }
+
+    return components;
+} /* countPathComponents */
+
+
+static DirHandle *findWriteHandle(const char *_fname)
+{
+    DirHandle *i = NULL;
+    int deepest_path_components = -1;
+    DirHandle *deepest_dirhandle = NULL;
+    char *allocated_fname;
+    char *fname;
+    size_t len;
+
+    len = strlen(_fname) + longest_root + 1;
+    allocated_fname = (char *) __PHYSFS_smallAlloc(len);
+    BAIL_IF(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
+    fname = allocated_fname + longest_root;
+
+    if (sanitizePlatformIndependentPath(_fname, fname))
+    {
+        for (i = searchPath; i != NULL; i = i->next)
+        {
+            char *arcfname = fname;
+            if (i->forWriting && verifyPath(i, &arcfname, 0))
+            {
+                int path_components = 0;
+                if (i->mountPoint != NULL)
+                    path_components = countPathComponents(i->mountPoint);
+                if (path_components > deepest_path_components)
+                {
+                    deepest_path_components = path_components;
+                    deepest_dirhandle = i;
+                } /* if */
+            } /* if */
+        } /* for */
+    } /* if */
+
+    __PHYSFS_smallFree(allocated_fname);
+
+    return deepest_dirhandle != NULL ? deepest_dirhandle : writeDir;
+} /* findWriteHandle */
+
+
 /* This must hold the stateLock before calling. */
 /* This must hold the stateLock before calling. */
-static int doMkdir(const char *_dname, char *dname)
+static int doMkdir(const char *_dname, char *dname, DirHandle *h)
 {
 {
-    DirHandle *h = writeDir;
     char *start;
     char *start;
     char *end;
     char *end;
     int retval = 0;
     int retval = 0;
@@ -2212,15 +2328,17 @@ int PHYSFS_mkdir(const char *_dname)
     int retval = 0;
     int retval = 0;
     char *dname;
     char *dname;
     size_t len;
     size_t len;
+    DirHandle *h = NULL;
 
 
     BAIL_IF(!_dname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
     BAIL_IF(!_dname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
 
 
     __PHYSFS_platformGrabMutex(stateLock);
     __PHYSFS_platformGrabMutex(stateLock);
-    BAIL_IF_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
-    len = strlen(_dname) + dirHandleRootLen(writeDir) + 1;
+    h = findWriteHandle(_dname);
+    BAIL_IF_MUTEX(!h, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
+    len = strlen(_dname) + dirHandleRootLen(h) + 1;
     dname = (char *) __PHYSFS_smallAlloc(len);
     dname = (char *) __PHYSFS_smallAlloc(len);
     BAIL_IF_MUTEX(!dname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
     BAIL_IF_MUTEX(!dname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
-    retval = doMkdir(_dname, dname);
+    retval = doMkdir(_dname, dname, h);
     __PHYSFS_platformReleaseMutex(stateLock);
     __PHYSFS_platformReleaseMutex(stateLock);
     __PHYSFS_smallFree(dname);
     __PHYSFS_smallFree(dname);
     return retval;
     return retval;
@@ -2228,9 +2346,8 @@ int PHYSFS_mkdir(const char *_dname)
 
 
 
 
 /* This must hold the stateLock before calling. */
 /* This must hold the stateLock before calling. */
-static int doDelete(const char *_fname, char *fname)
+static int doDelete(const char *_fname, char *fname, DirHandle *h)
 {
 {
-    DirHandle *h = writeDir;
     BAIL_IF_ERRPASS(!sanitizePlatformIndependentPathWithRoot(h, _fname, fname), 0);
     BAIL_IF_ERRPASS(!sanitizePlatformIndependentPathWithRoot(h, _fname, fname), 0);
     BAIL_IF_ERRPASS(!verifyPath(h, &fname, 0), 0);
     BAIL_IF_ERRPASS(!verifyPath(h, &fname, 0), 0);
     return h->funcs->remove(h->opaque, fname);
     return h->funcs->remove(h->opaque, fname);
@@ -2242,13 +2359,15 @@ int PHYSFS_delete(const char *_fname)
     int retval;
     int retval;
     char *fname;
     char *fname;
     size_t len;
     size_t len;
+    DirHandle *h = NULL;
 
 
     __PHYSFS_platformGrabMutex(stateLock);
     __PHYSFS_platformGrabMutex(stateLock);
-    BAIL_IF_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
-    len = strlen(_fname) + dirHandleRootLen(writeDir) + 1;
+    h = findWriteHandle(_fname);
+    BAIL_IF_MUTEX(!h, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
+    len = strlen(_fname) + dirHandleRootLen(h) + 1;
     fname = (char *) __PHYSFS_smallAlloc(len);
     fname = (char *) __PHYSFS_smallAlloc(len);
     BAIL_IF_MUTEX(!fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
     BAIL_IF_MUTEX(!fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
-    retval = doDelete(_fname, fname);
+    retval = doDelete(_fname, fname, h);
     __PHYSFS_platformReleaseMutex(stateLock);
     __PHYSFS_platformReleaseMutex(stateLock);
     __PHYSFS_smallFree(fname);
     __PHYSFS_smallFree(fname);
     return retval;
     return retval;
@@ -2631,7 +2750,7 @@ static PHYSFS_File *doOpenWrite(const char *_fname, const int appending)
 
 
     __PHYSFS_platformGrabMutex(stateLock);
     __PHYSFS_platformGrabMutex(stateLock);
 
 
-    h = writeDir;
+    h = findWriteHandle(_fname);
     BAIL_IF_MUTEX(!h, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
     BAIL_IF_MUTEX(!h, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
 
 
     len = strlen(_fname) + dirHandleRootLen(h) + 1;
     len = strlen(_fname) + dirHandleRootLen(h) + 1;
@@ -3115,7 +3234,7 @@ int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
                 if (exists)
                 if (exists)
                 {
                 {
                     stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
                     stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
-                    stat->readonly = 1;
+                    stat->readonly = !i->forWriting;
                     retval = 1;
                     retval = 1;
                 } /* if */
                 } /* if */
                 else if (verifyPath(i, &arcfname, 0))
                 else if (verifyPath(i, &arcfname, 0))

+ 14 - 0
src/libraries/physfs/physfs.h

@@ -2204,6 +2204,14 @@ PHYSFS_DECL int PHYSFS_mount(const char *newDir,
                              const char *mountPoint,
                              const char *mountPoint,
                              int appendToPath);
                              int appendToPath);
 
 
+/**
+ * Just like mount() but attempts to open the directory for writing as well as
+ * reading.
+ */
+PHYSFS_DECL int PHYSFS_mountRW(const char *newDir,
+                               const char *mountPoint,
+                               int appendToPath);
+
 /**
 /**
  * \fn int PHYSFS_getMountPoint(const char *dir)
  * \fn int PHYSFS_getMountPoint(const char *dir)
  * \brief Determine a mounted archive's mountpoint.
  * \brief Determine a mounted archive's mountpoint.
@@ -2779,6 +2787,12 @@ PHYSFS_DECL int PHYSFS_enumerate(const char *dir, PHYSFS_EnumerateCallback c,
  */
  */
 PHYSFS_DECL int PHYSFS_unmount(const char *oldDir);
 PHYSFS_DECL int PHYSFS_unmount(const char *oldDir);
 
 
+/**
+ * \fn int PHYSFS_canUnmount(const char *oldDir)
+ * \brief Check whether a directory or archive can be unmounted.
+ */
+PHYSFS_DECL int PHYSFS_canUnmount(const char *oldDir);
+
 
 
 /**
 /**
  * \fn const PHYSFS_Allocator *PHYSFS_getAllocator(void)
  * \fn const PHYSFS_Allocator *PHYSFS_getAllocator(void)

+ 102 - 26
src/modules/filesystem/Filesystem.cpp

@@ -26,12 +26,12 @@
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
 
 
-#if defined(LOVE_MACOS)
-#include "common/macos.h"
-#elif defined(LOVE_IOS)
-#include "common/ios.h"
+#if defined(LOVE_MACOS) || defined(LOVE_IOS)
+#include "common/apple.h"
+#include <unistd.h>
 #elif defined(LOVE_WINDOWS)
 #elif defined(LOVE_WINDOWS)
 #include <windows.h>
 #include <windows.h>
+#include <fileapi.h>
 #include "common/utf8.h"
 #include "common/utf8.h"
 #elif defined(LOVE_LINUX)
 #elif defined(LOVE_LINUX)
 #include <unistd.h>
 #include <unistd.h>
@@ -70,6 +70,14 @@ FileData *Filesystem::newFileData(const void *data, size_t size, const char *fil
 }
 }
 
 
 bool Filesystem::isRealDirectory(const std::string &path) const
 bool Filesystem::isRealDirectory(const std::string &path) const
+{
+	FileType ftype = FILETYPE_MAX_ENUM;
+	if (!getRealPathType(path, ftype))
+		return false;
+	return ftype == FILETYPE_DIRECTORY;
+}
+
+bool Filesystem::getRealPathType(const std::string &path, FileType &ftype) const
 {
 {
 #ifdef LOVE_WINDOWS
 #ifdef LOVE_WINDOWS
 	// make sure non-ASCII paths work.
 	// make sure non-ASCII paths work.
@@ -79,23 +87,89 @@ bool Filesystem::isRealDirectory(const std::string &path) const
 	if (_wstat(wpath.c_str(), &buf) != 0)
 	if (_wstat(wpath.c_str(), &buf) != 0)
 		return false;
 		return false;
 
 
-	return (buf.st_mode & _S_IFDIR) == _S_IFDIR;
+	if ((buf.st_mode & _S_IFREG) == _S_IFREG)
+		ftype = FILETYPE_FILE;
+	else if ((buf.st_mode & _S_IFDIR) == _S_IFDIR)
+		ftype = FILETYPE_DIRECTORY;
+	else
+		ftype = FILETYPE_OTHER;
 #else
 #else
 	// Assume POSIX support...
 	// Assume POSIX support...
 	struct stat buf;
 	struct stat buf;
 	if (stat(path.c_str(), &buf) != 0)
 	if (stat(path.c_str(), &buf) != 0)
 		return false;
 		return false;
 
 
-	return S_ISDIR(buf.st_mode) != 0;
+	if (S_ISREG(buf.st_mode))
+		ftype = FILETYPE_FILE;
+	else if (S_ISDIR(buf.st_mode))
+		ftype = FILETYPE_DIRECTORY;
+	else if (S_ISLNK(buf.st_mode))
+		ftype = FILETYPE_SYMLINK;
+	else
+		ftype = FILETYPE_OTHER;
+#endif
+
+	return true;
+}
+
+static bool getContainingDirectory(const std::string &path, std::string &newpath)
+{
+	size_t index = path.find_last_of("/\\");
+
+	if (index == std::string::npos)
+		return false;
+
+	newpath = path.substr(0, index);
+
+	// Bail if the root has been stripped out.
+	return newpath.find("/\\") != std::string::npos;
+}
+
+static bool createDirectoryRaw(const std::string &path)
+{
+#ifdef LOVE_WINDOWS
+	std::wstring wpath = to_widestr(path);
+	return CreateDirectoryW(wpath.c_str(), nullptr) != 0;
+#else
+	return mkdir(path.c_str(), S_IRWXU) == 0;
 #endif
 #endif
 }
 }
 
 
+bool Filesystem::createRealDirectory(const std::string &path)
+{
+	FileType ftype = FILETYPE_MAX_ENUM;
+	if (getRealPathType(path, ftype))
+		return ftype == FILETYPE_DIRECTORY;
+
+	std::vector<std::string> createpaths = {path};
+
+	// Find the deepest subdirectory in the given path that actually exists.
+	while (true)
+	{
+		std::string subpath;
+		if (!getContainingDirectory(createpaths[0], subpath))
+			break;
+
+		if (isRealDirectory(subpath))
+			break;
+
+		createpaths.insert(createpaths.begin(), subpath);
+	}
+
+	// Try to create missing subdirectories starting from that existing one.
+	for (const std::string &p : createpaths)
+	{
+		if (!createDirectoryRaw(p))
+			return false;
+	}
+
+	return true;
+}
+
 std::string Filesystem::getExecutablePath() const
 std::string Filesystem::getExecutablePath() const
 {
 {
-#if defined(LOVE_MACOS)
-	return love::macos::getExecutablePath();
-#elif defined(LOVE_IOS)
-	return love::ios::getExecutablePath();
+#if defined(LOVE_MACOS) || defined(LOVE_IOS)
+	return love::apple::getExecutablePath();
 #elif defined(LOVE_WINDOWS)
 #elif defined(LOVE_WINDOWS)
 
 
 	wchar_t buffer[MAX_PATH + 1] = {0};
 	wchar_t buffer[MAX_PATH + 1] = {0};
@@ -120,30 +194,32 @@ std::string Filesystem::getExecutablePath() const
 #endif
 #endif
 }
 }
 
 
-bool Filesystem::getConstant(const char *in, FileType &out)
+STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::FileType, Filesystem::FILETYPE_MAX_ENUM, fileType)
 {
 {
-	return fileTypes.find(in, out);
+	{ "file",      Filesystem::FILETYPE_FILE      },
+	{ "directory", Filesystem::FILETYPE_DIRECTORY },
+	{ "symlink",   Filesystem::FILETYPE_SYMLINK   },
+	{ "other",     Filesystem::FILETYPE_OTHER     },
 }
 }
+STRINGMAP_CLASS_END(Filesystem, Filesystem::FileType, Filesystem::FILETYPE_MAX_ENUM, fileType)
 
 
-bool Filesystem::getConstant(FileType in, const char *&out)
+STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::CommonPath, Filesystem::COMMONPATH_MAX_ENUM, commonPath)
 {
 {
-	return fileTypes.find(in, out);
+	{ "appsavedir",    Filesystem::COMMONPATH_APP_SAVEDIR    },
+	{ "appdocuments",  Filesystem::COMMONPATH_APP_DOCUMENTS  },
+	{ "userhome",      Filesystem::COMMONPATH_USER_HOME      },
+	{ "userappdata",   Filesystem::COMMONPATH_USER_APPDATA   },
+	{ "userdesktop",   Filesystem::COMMONPATH_USER_DESKTOP   },
+	{ "userdocuments", Filesystem::COMMONPATH_USER_DOCUMENTS },
 }
 }
+STRINGMAP_CLASS_END(Filesystem, Filesystem::CommonPath, Filesystem::COMMONPATH_MAX_ENUM, commonPath)
 
 
-std::vector<std::string> Filesystem::getConstants(FileType)
+STRINGMAP_CLASS_BEGIN(Filesystem, Filesystem::MountPermissions, Filesystem::MOUNT_PERMISSIONS_MAX_ENUM, mountPermissions)
 {
 {
-	return fileTypes.getNames();
+	{ "read",      Filesystem::MOUNT_PERMISSIONS_READ      },
+	{ "readwrite", Filesystem::MOUNT_PERMISSIONS_READWRITE },
 }
 }
-
-StringMap<Filesystem::FileType, Filesystem::FILETYPE_MAX_ENUM>::Entry Filesystem::fileTypeEntries[] =
-{
-	{ "file",      FILETYPE_FILE      },
-	{ "directory", FILETYPE_DIRECTORY },
-	{ "symlink",   FILETYPE_SYMLINK   },
-	{ "other",     FILETYPE_OTHER     },
-};
-
-StringMap<Filesystem::FileType, Filesystem::FILETYPE_MAX_ENUM> Filesystem::fileTypes(Filesystem::fileTypeEntries, sizeof(Filesystem::fileTypeEntries));
+STRINGMAP_CLASS_END(Filesystem, Filesystem::MountPermissions, Filesystem::MOUNT_PERMISSIONS_MAX_ENUM, mountPermissions)
 
 
 } // filesystem
 } // filesystem
 } // love
 } // love

+ 41 - 7
src/modules/filesystem/Filesystem.h

@@ -71,12 +71,31 @@ public:
 		FILETYPE_MAX_ENUM
 		FILETYPE_MAX_ENUM
 	};
 	};
 
 
+	enum CommonPath
+	{
+		COMMONPATH_APP_SAVEDIR,
+		COMMONPATH_APP_DOCUMENTS,
+		COMMONPATH_USER_HOME,
+		COMMONPATH_USER_APPDATA,
+		COMMONPATH_USER_DESKTOP,
+		COMMONPATH_USER_DOCUMENTS,
+		COMMONPATH_MAX_ENUM
+	};
+
+	enum MountPermissions
+	{
+		MOUNT_PERMISSIONS_READ,
+		MOUNT_PERMISSIONS_READWRITE,
+		MOUNT_PERMISSIONS_MAX_ENUM
+	};
+
 	struct Info
 	struct Info
 	{
 	{
 		// Numbers will be -1 if they cannot be determined.
 		// Numbers will be -1 if they cannot be determined.
 		int64 size;
 		int64 size;
 		int64 modtime;
 		int64 modtime;
 		FileType type;
 		FileType type;
+		bool readonly;
 	};
 	};
 
 
 	static love::Type type;
 	static love::Type type;
@@ -136,8 +155,14 @@ public:
 
 
 	virtual bool mount(const char *archive, const char *mountpoint, bool appendToPath = false) = 0;
 	virtual bool mount(const char *archive, const char *mountpoint, bool appendToPath = false) = 0;
 	virtual bool mount(Data *data, const char *archivename, const char *mountpoint, bool appendToPath = false) = 0;
 	virtual bool mount(Data *data, const char *archivename, const char *mountpoint, bool appendToPath = false) = 0;
+
+	virtual bool mountFullPath(const char *archive, const char *mountpoint, MountPermissions permissions, bool appendToPath = false) = 0;
+	virtual bool mountCommonPath(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath = false) = 0;
+
 	virtual bool unmount(const char *archive) = 0;
 	virtual bool unmount(const char *archive) = 0;
 	virtual bool unmount(Data *data) = 0;
 	virtual bool unmount(Data *data) = 0;
+	virtual bool unmount(CommonPath path) = 0;
+	virtual bool unmountFullPath(const char *fullpath) = 0;
 
 
 	/**
 	/**
 	 * Creates a new file.
 	 * Creates a new file.
@@ -152,6 +177,11 @@ public:
 	 **/
 	 **/
 	virtual FileData *newFileData(const void *data, size_t size, const char *filename) const;
 	virtual FileData *newFileData(const void *data, size_t size, const char *filename) const;
 
 
+	/**
+	 * Gets the full path for the given common path.
+	 */
+	virtual std::string getFullCommonPath(CommonPath path) = 0;
+
 	/**
 	/**
 	 * Gets the current working directory.
 	 * Gets the current working directory.
 	 **/
 	 **/
@@ -172,7 +202,7 @@ public:
 	/**
 	/**
 	 * Gets the full path of the save folder.
 	 * Gets the full path of the save folder.
 	 **/
 	 **/
-	virtual const char *getSaveDirectory() = 0;
+	virtual std::string getSaveDirectory() = 0;
 
 
 	/**
 	/**
 	 * Gets the full path to the directory containing the game source.
 	 * Gets the full path to the directory containing the game source.
@@ -258,23 +288,27 @@ public:
 	 **/
 	 **/
 	virtual bool isRealDirectory(const std::string &path) const;
 	virtual bool isRealDirectory(const std::string &path) const;
 
 
+	/**
+	 * Recursively creates a directory at the given full OS-dependent path.
+	 **/
+	virtual bool createRealDirectory(const std::string &path);
+
 	/**
 	/**
 	 * Gets the full platform-dependent path to the executable.
 	 * Gets the full platform-dependent path to the executable.
 	 **/
 	 **/
 	virtual std::string getExecutablePath() const;
 	virtual std::string getExecutablePath() const;
 
 
-	static bool getConstant(const char *in, FileType &out);
-	static bool getConstant(FileType in, const char *&out);
-	static std::vector<std::string> getConstants(FileType);
+	STRINGMAP_CLASS_DECLARE(FileType);
+	STRINGMAP_CLASS_DECLARE(CommonPath);
+	STRINGMAP_CLASS_DECLARE(MountPermissions);
 
 
 private:
 private:
 
 
+	bool getRealPathType(const std::string &path, FileType &ftype) const;
+
 	// Should we save external or internal for Android
 	// Should we save external or internal for Android
 	bool useExternal;
 	bool useExternal;
 
 
-	static StringMap<FileType, FILETYPE_MAX_ENUM>::Entry fileTypeEntries[];
-	static StringMap<FileType, FILETYPE_MAX_ENUM> fileTypes;
-
 }; // Filesystem
 }; // Filesystem
 
 
 } // filesystem
 } // filesystem

+ 1 - 2
src/modules/filesystem/physfs/File.cpp

@@ -69,14 +69,13 @@ bool File::open(Mode mode)
 		throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
 		throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
 
 
 	// Check whether the write directory is set.
 	// Check whether the write directory is set.
-	if ((mode == MODE_APPEND || mode == MODE_WRITE) && (PHYSFS_getWriteDir() == nullptr) && !setupWriteDirectory())
+	if ((mode == MODE_APPEND || mode == MODE_WRITE) && !setupWriteDirectory())
 		throw love::Exception("Could not set write directory.");
 		throw love::Exception("Could not set write directory.");
 
 
 	// File already open?
 	// File already open?
 	if (file != nullptr)
 	if (file != nullptr)
 		return false;
 		return false;
 
 
-	PHYSFS_getLastErrorCode();
 	PHYSFS_File *handle = nullptr;
 	PHYSFS_File *handle = nullptr;
 
 
 	switch (mode)
 	switch (mode)

+ 344 - 211
src/modules/filesystem/physfs/Filesystem.cpp

@@ -45,6 +45,10 @@
 #	include <unistd.h>
 #	include <unistd.h>
 #endif
 #endif
 
 
+#if defined(LOVE_IOS) || defined(LOVE_MACOS)
+#	include "common/apple.h"
+#endif
+
 #ifdef LOVE_IOS
 #ifdef LOVE_IOS
 #	include "common/ios.h"
 #	include "common/ios.h"
 #endif
 #endif
@@ -60,54 +64,53 @@
 #include "common/android.h"
 #include "common/android.h"
 #endif
 #endif
 
 
-namespace
+namespace love
+{
+namespace filesystem
+{
+namespace physfs
 {
 {
-	size_t getDriveDelim(const std::string &input)
-	{
-		for (size_t i = 0; i < input.size(); ++i)
-			if (input[i] == '/' || input[i] == '\\')
-				return i;
-		// Something's horribly wrong
-		return 0;
-	}
-
-	std::string getDriveRoot(const std::string &input)
-	{
-		return input.substr(0, getDriveDelim(input)+1);
-	}
-
-	std::string skipDriveRoot(const std::string &input)
-	{
-		return input.substr(getDriveDelim(input)+1);
-	}
 
 
-	std::string normalize(const std::string &input)
+static std::string normalize(const std::string &input)
+{
+	std::stringstream out;
+	bool seenSep = false, isSep = false;
+	for (size_t i = 0; i < input.size(); ++i)
 	{
 	{
-		std::stringstream out;
-		bool seenSep = false, isSep = false;
-		for (size_t i = 0; i < input.size(); ++i)
-		{
-			isSep = (input[i] == LOVE_PATH_SEPARATOR[0]);
-			if (!isSep || !seenSep)
-				out << input[i];
-			seenSep = isSep;
-		}
-
-		return out.str();
+		isSep = (input[i] == LOVE_PATH_SEPARATOR[0]);
+		if (!isSep || !seenSep)
+			out << input[i];
+		seenSep = isSep;
 	}
 	}
 
 
+	return out.str();
 }
 }
 
 
-namespace love
-{
-namespace filesystem
+static const Filesystem::CommonPath appCommonPaths[] =
 {
 {
-namespace physfs
+	Filesystem::COMMONPATH_APP_SAVEDIR,
+	Filesystem::COMMONPATH_APP_DOCUMENTS
+};
+
+static bool isAppCommonPath(Filesystem::CommonPath path)
 {
 {
+	switch (path)
+	{
+	case Filesystem::COMMONPATH_APP_SAVEDIR:
+	case Filesystem::COMMONPATH_APP_DOCUMENTS:
+		return true;
+	default:
+		return false;
+	}
+}
 
 
 Filesystem::Filesystem()
 Filesystem::Filesystem()
-	: fused(false)
+	: appendIdentityToPath(false)
+	, fused(false)
 	, fusedSet(false)
 	, fusedSet(false)
+	, fullPaths()
+	, commonPathMountInfo()
+	, saveDirectoryNeedsMounting(false)
 {
 {
 	requirePath = {"?.lua", "?/init.lua"};
 	requirePath = {"?.lua", "?/init.lua"};
 	cRequirePath = {"??"};
 	cRequirePath = {"??"};
@@ -153,67 +156,65 @@ bool Filesystem::setIdentity(const char *ident, bool appendToPath)
 	if (!PHYSFS_isInit())
 	if (!PHYSFS_isInit())
 		return false;
 		return false;
 
 
-	std::string old_save_path = save_path_full;
-
-	// Store the save directory.
-	save_identity = std::string(ident);
-
-	// Generate the relative path to the game save folder.
-	save_path_relative = std::string(LOVE_APPDATA_PREFIX LOVE_APPDATA_FOLDER LOVE_PATH_SEPARATOR) + save_identity;
-
-	// Generate the full path to the game save folder.
-	save_path_full = std::string(getAppdataDirectory()) + std::string(LOVE_PATH_SEPARATOR);
-	if (fused)
-		save_path_full += std::string(LOVE_APPDATA_PREFIX) + save_identity;
-	else
-		save_path_full += save_path_relative;
-
-	save_path_full = normalize(save_path_full);	
-	
-#ifdef LOVE_ANDROID
-	if (save_identity == "")
-		save_identity = "unnamed";
-
-	std::string storage_path;
-	if (isAndroidSaveExternal())
-		storage_path = SDL_AndroidGetExternalStoragePath();
-	else
-		storage_path = SDL_AndroidGetInternalStoragePath();
-
-	std::string save_directory = storage_path + "/save";
-
-	save_path_full = storage_path + std::string("/save/") + save_identity;
+	if (ident == nullptr || strlen(ident) == 0)
+		return false;
 
 
-	if (!love::android::directoryExists(save_path_full.c_str()) &&
-			!love::android::mkdir(save_path_full.c_str()))
-		SDL_Log("Error: Could not create save directory %s!", save_path_full.c_str());
-#endif
+	// Validate whether re-mounting will work.
+	for (CommonPath p : appCommonPaths)
+	{
+		if (!commonPathMountInfo[p].mounted)
+			continue;
 
 
-	// We now have something like:
-	// save_identity: game
-	// save_path_relative: ./LOVE/game
-	// save_path_full: C:\Documents and Settings\user\Application Data/LOVE/game
+		// If a file is still open, unmount will fail.
+		std::string fullPath = getFullCommonPath(p);
+		if (!fullPath.empty() && !PHYSFS_canUnmount(fullPath.c_str()))
+			return false;
+	}
 
 
-	// We don't want old read-only save paths to accumulate when we set a new
-	// identity.
-	if (!old_save_path.empty())
-		PHYSFS_unmount(old_save_path.c_str());
+	bool oldMountedCommonPaths[COMMONPATH_MAX_ENUM] = {false};
 
 
-	// Try to add the save directory to the search path.
-	// (No error on fail, it means that the path doesn't exist).
-	PHYSFS_mount(save_path_full.c_str(), nullptr, appendToPath);
+	// We don't want old save paths to accumulate when we set a new identity.
+	for (CommonPath p : appCommonPaths)
+	{
+		oldMountedCommonPaths[p] = commonPathMountInfo[p].mounted;
+		if (commonPathMountInfo[p].mounted)
+			unmount(p);
+	}
 
 
-	// HACK: This forces setupWriteDirectory to be called the next time a file
-	// is opened for writing - otherwise it won't be called at all if it was
-	// already called at least once before.
-	PHYSFS_setWriteDir(nullptr);
+	// These will be re-populated by getFullCommonPath.
+	for (CommonPath p : appCommonPaths)
+		fullPaths[p].clear();
+
+	// Store the save directory. getFullCommonPath(COMMONPATH_APP_*) uses this.
+	saveIdentity = std::string(ident);
+	appendIdentityToPath = appendToPath;
+
+	// Try to mount as readwrite without creating missing directories in the
+	// path hierarchy. If this fails, setupWriteDirectory will attempt to create
+	// them and try again.
+	// This is done so the save directory is only created on-demand.
+	if (!mountCommonPathInternal(COMMONPATH_APP_SAVEDIR, nullptr, MOUNT_PERMISSIONS_READWRITE, appendToPath, false))
+		saveDirectoryNeedsMounting = true;
+
+	// Mount any other app common paths with directory creation immediately
+	// instead of on-demand, since to get to this point they would have to be
+	// explicitly mounted already beforehand.
+	for (CommonPath p : appCommonPaths)
+	{
+		if (oldMountedCommonPaths[p] && p != COMMONPATH_APP_SAVEDIR)
+		{
+			// TODO: error handling?
+			auto info = commonPathMountInfo[p];
+			mountCommonPathInternal(p, info.mountPoint.c_str(), info.permissions, appendToPath, true);
+		}
+	}
 
 
 	return true;
 	return true;
 }
 }
 
 
 const char *Filesystem::getIdentity() const
 const char *Filesystem::getIdentity() const
 {
 {
-	return save_identity.c_str();
+	return saveIdentity.c_str();
 }
 }
 
 
 bool Filesystem::setSource(const char *source)
 bool Filesystem::setSource(const char *source)
@@ -222,7 +223,7 @@ bool Filesystem::setSource(const char *source)
 		return false;
 		return false;
 
 
 	// Check whether directory is already set.
 	// Check whether directory is already set.
-	if (!game_source.empty())
+	if (!gameSource.empty())
 		return false;
 		return false;
 
 
 	std::string new_search_path = source;
 	std::string new_search_path = source;
@@ -264,14 +265,14 @@ bool Filesystem::setSource(const char *source)
 #endif
 #endif
 
 
 	// Save the game source.
 	// Save the game source.
-	game_source = new_search_path;
+	gameSource = new_search_path;
 
 
 	return true;
 	return true;
 }
 }
 
 
 const char *Filesystem::getSource() const
 const char *Filesystem::getSource() const
 {
 {
-	return game_source.c_str();
+	return gameSource.c_str();
 }
 }
 
 
 bool Filesystem::setupWriteDirectory()
 bool Filesystem::setupWriteDirectory()
@@ -279,54 +280,19 @@ bool Filesystem::setupWriteDirectory()
 	if (!PHYSFS_isInit())
 	if (!PHYSFS_isInit())
 		return false;
 		return false;
 
 
-	// These must all be set.
-	if (save_identity.empty() || save_path_full.empty() || save_path_relative.empty())
-		return false;
-
-	// We need to make sure the write directory is created. To do that, we also
-	// need to make sure all its parent directories are also created.
-	std::string temp_writedir = getDriveRoot(save_path_full);
-	std::string temp_createdir = skipDriveRoot(save_path_full);
-
-	// On some sandboxed platforms, physfs will break when its write directory
-	// is the root of the drive and it tries to create a folder (even if the
-	// folder's path is in a writable location.) If the user's home folder is
-	// in the save path, we'll try starting from there instead.
-	if (save_path_full.find(getUserDirectory()) == 0)
-	{
-		temp_writedir = getUserDirectory();
-		temp_createdir = save_path_full.substr(getUserDirectory().length());
-
-		// Strip leading '/' characters from the path we want to create.
-		size_t startpos = temp_createdir.find_first_not_of('/');
-		if (startpos != std::string::npos)
-			temp_createdir = temp_createdir.substr(startpos);
-	}
-
-	// Set either '/' or the user's home as a writable directory.
-	// (We must create the save folder before mounting it).
-	if (!PHYSFS_setWriteDir(temp_writedir.c_str()))
-		return false;
-
-	// Create the save folder. (We're now "at" either '/' or the user's home).
-	if (!createDirectory(temp_createdir.c_str()))
-	{
-		// Clear the write directory in case of error.
-		PHYSFS_setWriteDir(nullptr);
-		return false;
-	}
+	if (!saveDirectoryNeedsMounting)
+		return true;
 
 
-	// Set the final write directory.
-	if (!PHYSFS_setWriteDir(save_path_full.c_str()))
+	if (saveIdentity.empty())
 		return false;
 		return false;
 
 
-	// Add the directory. (Will not be readded if already present).
-	if (!PHYSFS_mount(save_path_full.c_str(), nullptr, 0))
-	{
-		PHYSFS_setWriteDir(nullptr); // Clear the write directory in case of error.
+	// Only the save directory is mounted on-demand if it doesn't exist yet.
+	// Other app common paths are immediately re-mounted in setIdentity.
+	bool createdir = true;
+	if (!mountCommonPathInternal(COMMONPATH_APP_SAVEDIR, nullptr, MOUNT_PERMISSIONS_READWRITE, appendIdentityToPath, createdir))
 		return false;
 		return false;
-	}
 
 
+	saveDirectoryNeedsMounting = false;
 	return true;
 	return true;
 }
 }
 
 
@@ -363,17 +329,52 @@ bool Filesystem::mount(const char *archive, const char *mountpoint, bool appendT
 
 
 		// Always disallow mounting of files inside the game source, since it
 		// Always disallow mounting of files inside the game source, since it
 		// won't work anyway if the game source is a zipped .love file.
 		// won't work anyway if the game source is a zipped .love file.
-		if (realPath.find(game_source) == 0)
+		if (realPath.find(gameSource) == 0)
 			return false;
 			return false;
 
 
 		realPath += LOVE_PATH_SEPARATOR;
 		realPath += LOVE_PATH_SEPARATOR;
 		realPath += archive;
 		realPath += archive;
 	}
 	}
 
 
-	if (realPath.length() == 0)
+	return mountFullPath(realPath.c_str(), mountpoint, MOUNT_PERMISSIONS_READ, appendToPath);
+}
+
+bool Filesystem::mountFullPath(const char *archive, const char *mountpoint, MountPermissions permissions, bool appendToPath)
+{
+	if (!PHYSFS_isInit() || !archive)
+		return false;
+
+	if (permissions == MOUNT_PERMISSIONS_READWRITE)
+		return PHYSFS_mountRW(archive, mountpoint, appendToPath) != 0;
+
+	return PHYSFS_mount(archive, mountpoint, appendToPath) != 0;
+}
+
+bool Filesystem::mountCommonPathInternal(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath, bool createDir)
+{
+	std::string fullpath = getFullCommonPath(path);
+	if (fullpath.empty())
 		return false;
 		return false;
 
 
-	return PHYSFS_mount(realPath.c_str(), mountpoint, appendToPath) != 0;
+	if (createDir && isAppCommonPath(path) && !isRealDirectory(fullpath))
+	{
+		if (!createRealDirectory(fullpath))
+			return false;
+	}
+
+	if (mountFullPath(fullpath.c_str(), mountpoint, permissions, appendToPath))
+	{
+		std::string mp = mountpoint != nullptr ? mountpoint : "/";
+		commonPathMountInfo[path] = {true, mp, permissions};
+		return true;
+	}
+
+	return false;
+}
+
+bool Filesystem::mountCommonPath(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath)
+{
+	return mountCommonPathInternal(path, mountpoint, permissions, appendToPath, true);
 }
 }
 
 
 bool Filesystem::mount(Data *data, const char *archivename, const char *mountpoint, bool appendToPath)
 bool Filesystem::mount(Data *data, const char *archivename, const char *mountpoint, bool appendToPath)
@@ -403,42 +404,52 @@ bool Filesystem::unmount(const char *archive)
 		return true;
 		return true;
 	}
 	}
 
 
-	std::string realPath;
-	std::string sourceBase = getSourceBaseDirectory();
-
-	// Check whether the given archive path is in the list of allowed full paths.
 	auto it = std::find(allowedMountPaths.begin(), allowedMountPaths.end(), archive);
 	auto it = std::find(allowedMountPaths.begin(), allowedMountPaths.end(), archive);
-
 	if (it != allowedMountPaths.end())
 	if (it != allowedMountPaths.end())
-		realPath = *it;
-	else if (isFused() && sourceBase.compare(archive) == 0)
-	{
-		// Special case: if the game is fused and the archive is the source's
-		// base directory, unmount it even though it's outside of the save dir.
-		realPath = sourceBase;
-	}
-	else
-	{
-		// Not allowed for safety reasons.
-		if (strlen(archive) == 0 || strstr(archive, "..") || strcmp(archive, "/") == 0)
-			return false;
+		return unmountFullPath(archive);
 
 
-		const char *realDir = PHYSFS_getRealDir(archive);
-		if (!realDir)
-			return false;
+	std::string sourceBase = getSourceBaseDirectory();
+	if (isFused() && sourceBase.compare(archive) == 0)
+		return unmountFullPath(archive);
 
 
-		realPath = realDir;
-		realPath += LOVE_PATH_SEPARATOR;
-		realPath += archive;
-	}
+	if (strlen(archive) == 0 || strstr(archive, "..") || strcmp(archive, "/") == 0)
+		return false;
+
+	const char *realDir = PHYSFS_getRealDir(archive);
+	if (!realDir)
+		return false;
+
+	std::string realPath = realDir;
+	realPath += LOVE_PATH_SEPARATOR;
+	realPath += archive;
 
 
-	const char *mountPoint = PHYSFS_getMountPoint(realPath.c_str());
-	if (!mountPoint)
+	if (PHYSFS_getMountPoint(realPath.c_str()) == nullptr)
 		return false;
 		return false;
 
 
 	return PHYSFS_unmount(realPath.c_str()) != 0;
 	return PHYSFS_unmount(realPath.c_str()) != 0;
 }
 }
 
 
+bool Filesystem::unmountFullPath(const char *fullpath)
+{
+	if (!PHYSFS_isInit() || !fullpath)
+		return false;
+
+	return PHYSFS_unmount(fullpath) != 0;
+}
+
+bool Filesystem::unmount(CommonPath path)
+{
+	std::string fullpath = getFullCommonPath(path);
+
+	if (!fullpath.empty() && unmountFullPath(fullpath.c_str()))
+	{
+		commonPathMountInfo[path].mounted = false;
+		return true;
+	}
+
+	return false;
+}
+
 bool Filesystem::unmount(Data *data)
 bool Filesystem::unmount(Data *data)
 {
 {
 	for (const auto &datapair : mountedData)
 	for (const auto &datapair : mountedData)
@@ -458,6 +469,166 @@ love::filesystem::File *Filesystem::newFile(const char *filename) const
 	return new File(filename);
 	return new File(filename);
 }
 }
 
 
+std::string Filesystem::getFullCommonPath(CommonPath path)
+{
+	if (!fullPaths[path].empty())
+		return fullPaths[path];
+
+	if (isAppCommonPath(path))
+	{
+		if (saveIdentity.empty())
+			return fullPaths[path];
+
+		std::string rootpath;
+		switch (path)
+		{
+		case COMMONPATH_APP_SAVEDIR:
+			rootpath = getFullCommonPath(COMMONPATH_USER_APPDATA);
+			break;
+		case COMMONPATH_APP_DOCUMENTS:
+			rootpath = getFullCommonPath(COMMONPATH_USER_DOCUMENTS);
+			break;
+		default:
+			break;
+		}
+
+		if (rootpath.empty())
+			return fullPaths[path];
+
+		std::string suffix;
+		if (isFused())
+			suffix = std::string(LOVE_PATH_SEPARATOR) + saveIdentity;
+		else
+			suffix = std::string(LOVE_PATH_SEPARATOR LOVE_APPDATA_FOLDER LOVE_PATH_SEPARATOR) + saveIdentity;
+
+		fullPaths[path] = normalize(rootpath + suffix);
+
+		return fullPaths[path];
+	}
+
+#if defined(LOVE_MACOS) || defined(LOVE_IOS)
+
+	switch (path)
+	{
+	case COMMONPATH_APP_SAVEDIR:
+	case COMMONPATH_APP_DOCUMENTS:
+		// Handled above.
+		break;
+	case COMMONPATH_USER_HOME:
+		fullPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_HOME);
+		break;
+	case COMMONPATH_USER_APPDATA:
+		fullPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_APPSUPPORT);
+		break;
+	case COMMONPATH_USER_DESKTOP:
+		fullPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_DESKTOP);
+		break;
+	case COMMONPATH_USER_DOCUMENTS:
+		fullPaths[path] = apple::getUserDirectory(apple::USER_DIRECTORY_DOCUMENTS);
+		break;
+	case COMMONPATH_MAX_ENUM:
+		break;
+	}
+
+#elif defined(LOVE_WINDOWS)
+
+	PWSTR winpath = nullptr;
+	HRESULT hr = E_FAIL;
+
+	switch (path)
+	{
+	case COMMONPATH_APP_SAVEDIR:
+	case COMMONPATH_APP_DOCUMENTS:
+		// Handled above.
+		break;
+	case COMMONPATH_USER_HOME:
+		hr = SHGetKnownFolderPath(FOLDERID_Profile, 0, nullptr, &winpath);
+		break;
+	case COMMONPATH_USER_APPDATA:
+		hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &winpath);
+		break;
+	case COMMONPATH_USER_DESKTOP:
+		hr = SHGetKnownFolderPath(FOLDERID_Desktop, 0, nullptr, &winpath);
+		break;
+	case COMMONPATH_USER_DOCUMENTS:
+		hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, nullptr, &winpath);
+		break;
+	case COMMONPATH_MAX_ENUM:
+		break;
+	}
+
+	if (SUCCEEDED(hr))
+	{
+		fullPaths[path] = to_utf8(winpath);
+		CoTaskMemFree(winpath);
+	}
+
+#elif defined(LOVE_ANDROID)
+
+	std::string storagepath;
+	if (isAndroidSaveExternal())
+		storagepath = SDL_AndroidGetExternalStoragePath();
+	else
+		storagepath = SDL_AndroidGetInternalStoragePath();
+
+	switch (path)
+	{
+	case COMMONPATH_APP_SAVEDIR:
+	case COMMONPATH_APP_DOCUMENTS:
+		// Handled above.
+		break;
+	case COMMONPATH_USER_HOME:
+		fullPaths[path] = normalize(PHYSFS_getUserDir());
+		break;
+	case COMMONPATH_USER_APPDATA:
+		fullPaths[path] = normalize(storagepath + "/save/");
+		break;
+	case COMMONPATH_USER_DESKTOP:
+		// No such thing on Android?
+		break;
+	case COMMONPATH_USER_DOCUMENTS:
+		// TODO: something more idiomatic / useful?
+		fullPaths[path] = normalize(storagepath + "/Documents/");
+		break;
+	case COMMONPATH_MAX_ENUM:
+		break;
+	}
+
+#elif defined(LOVE_LINUX)
+
+	const char *xdgdir = nullptr;
+
+	switch (path)
+	{
+	case COMMONPATH_APP_SAVEDIR:
+	case COMMONPATH_APP_DOCUMENTS:
+		// Handled above.
+		break;
+	case COMMONPATH_USER_HOME:
+		fullPaths[path] = normalize(PHYSFS_getUserDir());
+		break;
+	case COMMONPATH_USER_APPDATA:
+		xdgdir = getenv("XDG_DATA_HOME");
+		if (!xdgdir)
+			fullPaths[path] = normalize(std::string(getUserDirectory()) + "/.local/share/");
+		else
+			fullPaths[path] = xdgdir;
+		break;
+	case COMMONPATH_USER_DESKTOP:
+		fullPaths[path] = normalize(std::string(getUserDirectory()) + "/Desktop/");
+		break;
+	case COMMONPATH_USER_DOCUMENTS:
+		fullPaths[path] = normalize(std::string(getUserDirectory()) + "/Documents/");
+		break;
+	case COMMONPATH_MAX_ENUM:
+		break;
+	}
+
+#endif
+
+	return fullPaths[path];
+}
+
 const char *Filesystem::getWorkingDirectory()
 const char *Filesystem::getWorkingDirectory()
 {
 {
 	if (cwd.empty())
 	if (cwd.empty())
@@ -474,7 +645,7 @@ const char *Filesystem::getWorkingDirectory()
 		if (getcwd(cwd_char, LOVE_MAX_PATH))
 		if (getcwd(cwd_char, LOVE_MAX_PATH))
 			cwd = cwd_char; // if getcwd fails, cwd_char (and thus cwd) will still be empty
 			cwd = cwd_char; // if getcwd fails, cwd_char (and thus cwd) will still be empty
 
 
-		delete [] cwd_char;
+		delete[] cwd_char;
 #endif
 #endif
 	}
 	}
 
 
@@ -483,61 +654,22 @@ const char *Filesystem::getWorkingDirectory()
 
 
 std::string Filesystem::getUserDirectory()
 std::string Filesystem::getUserDirectory()
 {
 {
-#ifdef LOVE_IOS
-	// PHYSFS_getUserDir doesn't give exactly the path we want on iOS.
-	static std::string userDir = normalize(love::ios::getHomeDirectory());
-#else
-	static std::string userDir = normalize(PHYSFS_getUserDir());
-#endif
-
-	return userDir;
+	return getFullCommonPath(COMMONPATH_USER_HOME);
 }
 }
 
 
 std::string Filesystem::getAppdataDirectory()
 std::string Filesystem::getAppdataDirectory()
 {
 {
-	if (appdata.empty())
-	{
-#ifdef LOVE_WINDOWS_UWP
-		appdata = getUserDirectory();
-#elif defined(LOVE_WINDOWS)
-		PWSTR path = nullptr;
-		if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &path)))
-		{
-			appdata = to_utf8(path);
-			CoTaskMemFree(path);
-		}
-		else
-		{
-			wchar_t *w_appdata = _wgetenv(L"APPDATA");
-			appdata = to_utf8(w_appdata);
-		}
-		replace_char(appdata, '\\', '/');
-#elif defined(LOVE_MACOS)
-		appdata = normalize(love::macos::getAppdataDirectory());
-#elif defined(LOVE_IOS)
-		appdata = normalize(love::ios::getAppdataDirectory());
-#elif defined(LOVE_LINUX)
-		char *xdgdatahome = getenv("XDG_DATA_HOME");
-		if (!xdgdatahome)
-			appdata = normalize(std::string(getUserDirectory()) + "/.local/share/");
-		else
-			appdata = xdgdatahome;
-#else
-		appdata = getUserDirectory();
-#endif
-	}
-	return appdata;
+	return getFullCommonPath(COMMONPATH_USER_APPDATA);
 }
 }
 
 
-
-const char *Filesystem::getSaveDirectory()
+std::string Filesystem::getSaveDirectory()
 {
 {
-	return save_path_full.c_str();
+	return getFullCommonPath(COMMONPATH_APP_SAVEDIR);
 }
 }
 
 
 std::string Filesystem::getSourceBaseDirectory() const
 std::string Filesystem::getSourceBaseDirectory() const
 {
 {
-	size_t source_len = game_source.length();
+	size_t source_len = gameSource.length();
 
 
 	if (source_len == 0)
 	if (source_len == 0)
 		return "";
 		return "";
@@ -546,9 +678,9 @@ std::string Filesystem::getSourceBaseDirectory() const
 	// symbols (i.e. '..' and '.')
 	// symbols (i.e. '..' and '.')
 #ifdef LOVE_WINDOWS
 #ifdef LOVE_WINDOWS
 	// In windows, delimiters can be either '/' or '\'.
 	// In windows, delimiters can be either '/' or '\'.
-	size_t base_end_pos = game_source.find_last_of("/\\", source_len - 2);
+	size_t base_end_pos = gameSource.find_last_of("/\\", source_len - 2);
 #else
 #else
-	size_t base_end_pos = game_source.find_last_of('/', source_len - 2);
+	size_t base_end_pos = gameSource.find_last_of('/', source_len - 2);
 #endif
 #endif
 
 
 	if (base_end_pos == std::string::npos)
 	if (base_end_pos == std::string::npos)
@@ -558,7 +690,7 @@ std::string Filesystem::getSourceBaseDirectory() const
 	if (base_end_pos == 0)
 	if (base_end_pos == 0)
 		base_end_pos = 1;
 		base_end_pos = 1;
 
 
-	return game_source.substr(0, base_end_pos);
+	return gameSource.substr(0, base_end_pos);
 }
 }
 
 
 std::string Filesystem::getRealDirectory(const char *filename) const
 std::string Filesystem::getRealDirectory(const char *filename) const
@@ -585,6 +717,7 @@ bool Filesystem::getInfo(const char *filepath, Info &info) const
 
 
 	info.size = (int64) stat.filesize;
 	info.size = (int64) stat.filesize;
 	info.modtime = (int64) stat.modtime;
 	info.modtime = (int64) stat.modtime;
+	info.readonly = stat.readonly != 0;
 
 
 	if (stat.filetype == PHYSFS_FILETYPE_REGULAR)
 	if (stat.filetype == PHYSFS_FILETYPE_REGULAR)
 		info.type = FILETYPE_FILE;
 		info.type = FILETYPE_FILE;
@@ -603,7 +736,7 @@ bool Filesystem::createDirectory(const char *dir)
 	if (!PHYSFS_isInit())
 	if (!PHYSFS_isInit())
 		return false;
 		return false;
 
 
-	if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
+	if (!setupWriteDirectory())
 		return false;
 		return false;
 
 
 	if (!PHYSFS_mkdir(dir))
 	if (!PHYSFS_mkdir(dir))
@@ -617,7 +750,7 @@ bool Filesystem::remove(const char *file)
 	if (!PHYSFS_isInit())
 	if (!PHYSFS_isInit())
 		return false;
 		return false;
 
 
-	if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory())
+	if (!setupWriteDirectory())
 		return false;
 		return false;
 
 
 	if (!PHYSFS_delete(file))
 	if (!PHYSFS_delete(file))

+ 27 - 15
src/modules/filesystem/physfs/Filesystem.h

@@ -63,15 +63,21 @@ public:
 	bool mount(const char *archive, const char *mountpoint, bool appendToPath = false) override;
 	bool mount(const char *archive, const char *mountpoint, bool appendToPath = false) override;
 	bool mount(Data *data, const char *archivename, const char *mountpoint, bool appendToPath = false) override;
 	bool mount(Data *data, const char *archivename, const char *mountpoint, bool appendToPath = false) override;
 
 
+	bool mountFullPath(const char *archive, const char *mountpoint, MountPermissions permissions, bool appendToPath = false) override;
+	bool mountCommonPath(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath = false) override;
+
 	bool unmount(const char *archive) override;
 	bool unmount(const char *archive) override;
 	bool unmount(Data *data) override;
 	bool unmount(Data *data) override;
+	bool unmount(CommonPath path) override;
+	bool unmountFullPath(const char *fullpath) override;
 
 
 	love::filesystem::File *newFile(const char *filename) const override;
 	love::filesystem::File *newFile(const char *filename) const override;
 
 
+	std::string getFullCommonPath(CommonPath path) override;
 	const char *getWorkingDirectory() override;
 	const char *getWorkingDirectory() override;
 	std::string getUserDirectory() override;
 	std::string getUserDirectory() override;
 	std::string getAppdataDirectory() override;
 	std::string getAppdataDirectory() override;
-	const char *getSaveDirectory() override;
+	std::string getSaveDirectory() override;
 	std::string getSourceBaseDirectory() const override;
 	std::string getSourceBaseDirectory() const override;
 
 
 	std::string getRealDirectory(const char *filename) const override;
 	std::string getRealDirectory(const char *filename) const override;
@@ -98,26 +104,26 @@ public:
 
 
 private:
 private:
 
 
-	// Contains the current working directory (UTF8).
-	std::string cwd;
+	struct CommonPathMountInfo
+	{
+		bool mounted;
+		std::string mountPoint;
+		MountPermissions permissions;
+	};
 
 
-	// %APPDATA% on Windows.
-	std::string appdata;
+	bool mountCommonPathInternal(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath, bool createDir);
 
 
-	// This name will be used to create the folder
-	// in the appdata/userdata folder.
-	std::string save_identity;
+	// Contains the current working directory (UTF8).
+	std::string cwd;
 
 
-	// Full and relative paths of the game save folder.
-	// (Relative to the %APPDATA% folder, meaning that the
-	// relative string will look something like: ./LOVE/game)
-	std::string save_path_relative, save_path_full;
+	// This name will be used to create the folder in the appdata folder.
+	std::string saveIdentity;
+	bool appendIdentityToPath;
 
 
 	// The full path to the source of the game.
 	// The full path to the source of the game.
-	std::string game_source;
+	std::string gameSource;
 
 
-	// Allow saving outside of the LOVE_APPDATA_FOLDER
-	// for release 'builds'
+	// Allow saving outside of the LOVE_APPDATA_FOLDER for release 'builds'
 	bool fused;
 	bool fused;
 	bool fusedSet;
 	bool fusedSet;
 
 
@@ -129,6 +135,12 @@ private:
 
 
 	std::map<std::string, StrongRef<Data>> mountedData;
 	std::map<std::string, StrongRef<Data>> mountedData;
 
 
+	std::string fullPaths[COMMONPATH_MAX_ENUM];
+
+	CommonPathMountInfo commonPathMountInfo[COMMONPATH_MAX_ENUM];
+
+	bool saveDirectoryNeedsMounting;
+
 }; // Filesystem
 }; // Filesystem
 
 
 } // physfs
 } // physfs

+ 80 - 1
src/modules/filesystem/wrap_Filesystem.cpp

@@ -148,6 +148,48 @@ int w_mount(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
+int w_mountFullPath(lua_State *L)
+{
+	const char *fullpath = luaL_checkstring(L, 1);
+	const char *mountpoint = luaL_checkstring(L, 2);
+
+	auto permissions = Filesystem::MOUNT_PERMISSIONS_READ;
+	if (!lua_isnoneornil(L, 3))
+	{
+		const char *permissionstr = luaL_checkstring(L, 3);
+		if (!Filesystem::getConstant(permissionstr, permissions))
+			return luax_enumerror(L, "mount permissions", Filesystem::getConstants(permissions), permissionstr);
+	}
+
+	bool append = luax_optboolean(L, 4, false);
+
+	luax_pushboolean(L, instance()->mountFullPath(fullpath, mountpoint, permissions, append));
+	return 1;
+}
+
+int w_mountCommonPath(lua_State *L)
+{
+	const char *commonpathstr = luaL_checkstring(L, 1);
+	Filesystem::CommonPath commonpath;
+	if (!Filesystem::getConstant(commonpathstr, commonpath))
+		return luax_enumerror(L, "common path", Filesystem::getConstants(commonpath), commonpathstr);
+
+	const char *mountpoint = luaL_checkstring(L, 2);
+
+	auto permissions = Filesystem::MOUNT_PERMISSIONS_READ;
+	if (!lua_isnoneornil(L, 3))
+	{
+		const char *permissionstr = luaL_checkstring(L, 3);
+		if (!Filesystem::getConstant(permissionstr, permissions))
+			return luax_enumerror(L, "mount permissions", Filesystem::getConstants(permissions), permissionstr);
+	}
+
+	bool append = luax_optboolean(L, 4, false);
+
+	luax_pushboolean(L, instance()->mountCommonPath(commonpath, mountpoint, permissions, append));
+	return 1;
+}
+
 int w_unmount(lua_State *L)
 int w_unmount(lua_State *L)
 {
 {
 	if (luax_istype(L, 1, Data::type))
 	if (luax_istype(L, 1, Data::type))
@@ -163,6 +205,24 @@ int w_unmount(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
+int w_unmountFullPath(lua_State *L)
+{
+	const char *fullpath = luaL_checkstring(L, 1);
+	luax_pushboolean(L, instance()->unmountFullPath(fullpath));
+	return 1;
+}
+
+int w_unmountCommonPath(lua_State *L)
+{
+	const char *commonpathstr = luaL_checkstring(L, 1);
+	Filesystem::CommonPath commonpath;
+	if (!Filesystem::getConstant(commonpathstr, commonpath))
+		return luax_enumerror(L, "common path", Filesystem::getConstants(commonpath), commonpathstr);
+
+	luax_pushboolean(L, instance()->unmount(commonpath));
+	return 1;
+}
+
 int w_newFile(lua_State *L)
 int w_newFile(lua_State *L)
 {
 {
 	const char *filename = luaL_checkstring(L, 1);
 	const char *filename = luaL_checkstring(L, 1);
@@ -331,6 +391,17 @@ int w_newFileData(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
+int w_getFullCommonPath(lua_State *L)
+{
+	const char *commonpathstr = luaL_checkstring(L, 1);
+	Filesystem::CommonPath commonpath;
+	if (!Filesystem::getConstant(commonpathstr, commonpath))
+		return luax_enumerror(L, "common path", Filesystem::getConstants(commonpath), commonpathstr);
+
+	luax_pushstring(L, instance()->getFullCommonPath(commonpath));
+	return 1;
+}
+
 int w_getWorkingDirectory(lua_State *L)
 int w_getWorkingDirectory(lua_State *L)
 {
 {
 	lua_pushstring(L, instance()->getWorkingDirectory());
 	lua_pushstring(L, instance()->getWorkingDirectory());
@@ -351,7 +422,7 @@ int w_getAppdataDirectory(lua_State *L)
 
 
 int w_getSaveDirectory(lua_State *L)
 int w_getSaveDirectory(lua_State *L)
 {
 {
-	lua_pushstring(L, instance()->getSaveDirectory());
+	luax_pushstring(L, instance()->getSaveDirectory());
 	return 1;
 	return 1;
 }
 }
 
 
@@ -421,6 +492,9 @@ int w_getInfo(lua_State *L)
 		lua_pushstring(L, typestr);
 		lua_pushstring(L, typestr);
 		lua_setfield(L, -2, "type");
 		lua_setfield(L, -2, "type");
 
 
+		luax_pushboolean(L, info.readonly);
+		lua_setfield(L, -2, "readonly");
+
 		// Lua numbers (doubles) can't fit the full range of 64 bit ints.
 		// Lua numbers (doubles) can't fit the full range of 64 bit ints.
 		info.size = std::min<int64>(info.size, 0x20000000000000LL);
 		info.size = std::min<int64>(info.size, 0x20000000000000LL);
 		if (info.size >= 0)
 		if (info.size >= 0)
@@ -839,8 +913,13 @@ static const luaL_Reg functions[] =
 	{ "setSource", w_setSource },
 	{ "setSource", w_setSource },
 	{ "getSource", w_getSource },
 	{ "getSource", w_getSource },
 	{ "mount", w_mount },
 	{ "mount", w_mount },
+	{ "mountFullPath", w_mountFullPath },
+	{ "mountCommonPath", w_mountCommonPath },
 	{ "unmount", w_unmount },
 	{ "unmount", w_unmount },
+	{ "unmountFullPath", w_unmountFullPath },
+	{ "unmountCommonPath", w_unmountCommonPath },
 	{ "newFile", w_newFile },
 	{ "newFile", w_newFile },
+	{ "getFullCommonPath", w_getFullCommonPath },
 	{ "getWorkingDirectory", w_getWorkingDirectory },
 	{ "getWorkingDirectory", w_getWorkingDirectory },
 	{ "getUserDirectory", w_getUserDirectory },
 	{ "getUserDirectory", w_getUserDirectory },
 	{ "getAppdataDirectory", w_getAppdataDirectory },
 	{ "getAppdataDirectory", w_getAppdataDirectory },