From bff2c1b5f1eacb13a2b592c4c85290063123533e Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Sat, 22 Jun 2013 14:58:12 +0200 Subject: [PATCH 1/3] Recreate love.filesystem.mount, adding mount and unmount, for your archive (and folder?) pleasure --HG-- branch : love.filesystem.mount2 --- src/modules/filesystem/physfs/Filesystem.cpp | 30 +++++++++++++++++++ src/modules/filesystem/physfs/Filesystem.h | 2 ++ .../filesystem/physfs/wrap_Filesystem.cpp | 19 ++++++++++++ .../filesystem/physfs/wrap_Filesystem.h | 2 ++ 4 files changed, 53 insertions(+) diff --git a/src/modules/filesystem/physfs/Filesystem.cpp b/src/modules/filesystem/physfs/Filesystem.cpp index e31ca96c0..8eb623a09 100644 --- a/src/modules/filesystem/physfs/Filesystem.cpp +++ b/src/modules/filesystem/physfs/Filesystem.cpp @@ -182,6 +182,36 @@ bool Filesystem::setupWriteDirectory() return true; } +bool Filesystem::mount(const char *archive, const char *mountpoint) +{ + if (!isInited) + return false; + + if (strstr(archive, "..")) // Not allowed for safety reasons + return false; + + std::string realPath = PHYSFS_getRealDir(archive); + realPath += LOVE_PATH_SEPARATOR; + realPath += archive; + + return PHYSFS_mount(realPath.c_str(), mountpoint, 0); +} + +bool Filesystem::unmount(const char *archive) +{ + if (!isInited) + return false; + + if (strstr(archive, "..")) // Not allowed for safety reasons + return false; + + std::string realPath = PHYSFS_getRealDir(archive); + realPath += LOVE_PATH_SEPARATOR; + realPath += archive; + + return PHYSFS_removeFromSearchPath(archive); +} + File *Filesystem::newFile(const char *filename) const { return new File(filename); diff --git a/src/modules/filesystem/physfs/Filesystem.h b/src/modules/filesystem/physfs/Filesystem.h index 49fd6c70a..06d340a5a 100644 --- a/src/modules/filesystem/physfs/Filesystem.h +++ b/src/modules/filesystem/physfs/Filesystem.h @@ -143,6 +143,8 @@ public: * @param source Path to a directory or a .love-file. **/ bool setSource(const char *source); + bool mount(const char *archive, const char *mountpoint); + bool unmount(const char *archive); /** * Creates a new file. diff --git a/src/modules/filesystem/physfs/wrap_Filesystem.cpp b/src/modules/filesystem/physfs/wrap_Filesystem.cpp index b641b34fb..e5387c76f 100644 --- a/src/modules/filesystem/physfs/wrap_Filesystem.cpp +++ b/src/modules/filesystem/physfs/wrap_Filesystem.cpp @@ -99,6 +99,23 @@ int w_setSource(lua_State *L) return 0; } +int w_mount(lua_State *L) +{ + const char *archive = luaL_checkstring(L, 1); + const char *mountpoint = luaL_checkstring(L, 2); + + luax_pushboolean(L, instance->mount(archive, mountpoint)); + return 1; +} + +int w_unmount(lua_State *L) +{ + const char *archive = luaL_checkstring(L, 1); + + luax_pushboolean(L, instance->unmount(archive)); + return 1; +} + int w_newFile(lua_State *L) { const char *filename = luaL_checkstring(L, 1); @@ -544,6 +561,8 @@ static const luaL_Reg functions[] = { "setIdentity", w_setIdentity }, { "getIdentity", w_getIdentity }, { "setSource", w_setSource }, + { "mount", w_mount }, + { "unmount", w_unmount }, { "newFile", w_newFile }, { "getWorkingDirectory", w_getWorkingDirectory }, { "getUserDirectory", w_getUserDirectory }, diff --git a/src/modules/filesystem/physfs/wrap_Filesystem.h b/src/modules/filesystem/physfs/wrap_Filesystem.h index cb21c96fc..456d250ae 100644 --- a/src/modules/filesystem/physfs/wrap_Filesystem.h +++ b/src/modules/filesystem/physfs/wrap_Filesystem.h @@ -42,6 +42,8 @@ int w_setRelease(lua_State *L); int w_setIdentity(lua_State *L); int w_getIdentity(lua_State *L); int w_setSource(lua_State *L); +int w_mount(lua_State *L); +int w_unmount(lua_State *L); int w_newFile(lua_State *L); int w_newFileData(lua_State *L); int w_getWorkingDirectory(lua_State *L); From b4fa9131d2f1b078ee3a97826e6708781dfd8fd4 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 22 Jun 2013 15:54:53 -0300 Subject: [PATCH 2/3] Fixed love.filesystem.[un]mount --HG-- branch : love.filesystem.mount2 --- src/modules/filesystem/physfs/Filesystem.cpp | 33 +++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/modules/filesystem/physfs/Filesystem.cpp b/src/modules/filesystem/physfs/Filesystem.cpp index 8eb623a09..935f93db3 100644 --- a/src/modules/filesystem/physfs/Filesystem.cpp +++ b/src/modules/filesystem/physfs/Filesystem.cpp @@ -184,13 +184,22 @@ bool Filesystem::setupWriteDirectory() bool Filesystem::mount(const char *archive, const char *mountpoint) { - if (!isInited) + if (!isInited || !archive || !mountpoint) return false; - if (strstr(archive, "..")) // Not allowed for safety reasons + // Not allowed for safety reasons. + if (strlen(archive) == 0 || strstr(archive, "..") || strcmp(archive, "/") == 0) return false; - std::string realPath = PHYSFS_getRealDir(archive); + // An empty string or "/" is equivalent to doing PHYSFS_addToSearchPath. + if (strlen(mountpoint) == 0 || strcmp(mountpoint, "/") == 0) + return false; + + const char *realDir = PHYSFS_getRealDir(archive); + if (!realDir) + return false; + + std::string realPath(realDir); realPath += LOVE_PATH_SEPARATOR; realPath += archive; @@ -199,17 +208,27 @@ bool Filesystem::mount(const char *archive, const char *mountpoint) bool Filesystem::unmount(const char *archive) { - if (!isInited) + if (!isInited || !archive) return false; - if (strstr(archive, "..")) // Not allowed for safety reasons + // Not allowed for safety reasons. + if (strlen(archive) == 0 || strstr(archive, "..") || strcmp(archive, "/") == 0) return false; - std::string realPath = PHYSFS_getRealDir(archive); + const char *realDir = PHYSFS_getRealDir(archive); + if (!realDir) + return false; + + std::string realPath(realDir); realPath += LOVE_PATH_SEPARATOR; realPath += archive; - return PHYSFS_removeFromSearchPath(archive); + // Make sure the archive path was previously added with PHYSFS_mount. + const char *mountPoint = PHYSFS_getMountPoint(realPath.c_str()); + if (!mountPoint || strcmp(mountPoint, "/") == 0) + return false; + + return PHYSFS_removeFromSearchPath(realPath.c_str()); } File *Filesystem::newFile(const char *filename) const From 579a3052e61ad6b098831eb0be190fd71fbf8063 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 22 Jun 2013 17:04:51 -0300 Subject: [PATCH 3/3] Removed restriction on mounting a filepath to root --HG-- branch : love.filesystem.mount2 --- src/modules/filesystem/physfs/Filesystem.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/modules/filesystem/physfs/Filesystem.cpp b/src/modules/filesystem/physfs/Filesystem.cpp index 935f93db3..475b7587d 100644 --- a/src/modules/filesystem/physfs/Filesystem.cpp +++ b/src/modules/filesystem/physfs/Filesystem.cpp @@ -184,17 +184,13 @@ bool Filesystem::setupWriteDirectory() bool Filesystem::mount(const char *archive, const char *mountpoint) { - if (!isInited || !archive || !mountpoint) + if (!isInited || !archive) return false; // Not allowed for safety reasons. if (strlen(archive) == 0 || strstr(archive, "..") || strcmp(archive, "/") == 0) return false; - // An empty string or "/" is equivalent to doing PHYSFS_addToSearchPath. - if (strlen(mountpoint) == 0 || strcmp(mountpoint, "/") == 0) - return false; - const char *realDir = PHYSFS_getRealDir(archive); if (!realDir) return false; @@ -223,9 +219,8 @@ bool Filesystem::unmount(const char *archive) realPath += LOVE_PATH_SEPARATOR; realPath += archive; - // Make sure the archive path was previously added with PHYSFS_mount. const char *mountPoint = PHYSFS_getMountPoint(realPath.c_str()); - if (!mountPoint || strcmp(mountPoint, "/") == 0) + if (!mountPoint) return false; return PHYSFS_removeFromSearchPath(realPath.c_str());