From b56a0cfc3d2f0361c47030ff02a8298edd70a00c Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 24 Dec 2015 22:53:48 -0500 Subject: [PATCH] Avoid hard crashes when calling love.filesystem functions if love.filesystem.init was never called. This only applies when liblove is used directly, as love.filesystem.init is called automatically when the love executable starts up. --- src/modules/filesystem/physfs/File.cpp | 3 ++ src/modules/filesystem/physfs/Filesystem.cpp | 38 ++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/modules/filesystem/physfs/File.cpp b/src/modules/filesystem/physfs/File.cpp index 12be9e5ed..2eb3ca113 100644 --- a/src/modules/filesystem/physfs/File.cpp +++ b/src/modules/filesystem/physfs/File.cpp @@ -57,6 +57,9 @@ bool File::open(Mode mode) if (mode == MODE_CLOSED) return true; + if (!PHYSFS_isInit()) + throw love::Exception("PhysFS is not initialized."); + // File must exist if read mode. if ((mode == MODE_READ) && !PHYSFS_exists(filename.c_str())) throw love::Exception("Could not open file %s. Does not exist.", filename.c_str()); diff --git a/src/modules/filesystem/physfs/Filesystem.cpp b/src/modules/filesystem/physfs/Filesystem.cpp index ee0498cc7..ff9ee7b59 100644 --- a/src/modules/filesystem/physfs/Filesystem.cpp +++ b/src/modules/filesystem/physfs/Filesystem.cpp @@ -571,6 +571,9 @@ std::string Filesystem::getSourceBaseDirectory() const std::string Filesystem::getRealDirectory(const char *filename) const { + if (!PHYSFS_isInit()) + throw love::Exception("PhysFS is not initialized."); + const char *dir = PHYSFS_getRealDir(filename); if (dir == nullptr) @@ -581,11 +584,17 @@ std::string Filesystem::getRealDirectory(const char *filename) const bool Filesystem::exists(const char *path) const { + if (!PHYSFS_isInit()) + return false; + return PHYSFS_exists(path) != 0; } bool Filesystem::isDirectory(const char *dir) const { + if (!PHYSFS_isInit()) + return false; + #ifdef LOVE_USE_PHYSFS_2_1 PHYSFS_Stat stat = {}; if (PHYSFS_stat(dir, &stat)) @@ -599,11 +608,17 @@ bool Filesystem::isDirectory(const char *dir) const bool Filesystem::isFile(const char *file) const { + if (!PHYSFS_isInit()) + return false; + return PHYSFS_exists(file) && !isDirectory(file); } bool Filesystem::isSymlink(const char *filename) const { + if (!PHYSFS_isInit()) + return false; + #ifdef LOVE_USE_PHYSFS_2_1 PHYSFS_Stat stat = {}; if (PHYSFS_stat(filename, &stat)) @@ -617,21 +632,29 @@ bool Filesystem::isSymlink(const char *filename) const bool Filesystem::createDirectory(const char *dir) { + if (!PHYSFS_isInit()) + return false; + if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory()) return false; if (!PHYSFS_mkdir(dir)) return false; + return true; } bool Filesystem::remove(const char *file) { + if (!PHYSFS_isInit()) + return false; + if (PHYSFS_getWriteDir() == 0 && !setupWriteDirectory()) return false; if (!PHYSFS_delete(file)) return false; + return true; } @@ -669,8 +692,14 @@ void Filesystem::append(const char *filename, const void *data, int64 size) cons void Filesystem::getDirectoryItems(const char *dir, std::vector &items) { + if (!PHYSFS_isInit()) + return; + char **rc = PHYSFS_enumerateFiles(dir); + if (rc == nullptr) + return; + for (char **i = rc; *i != 0; i++) items.push_back(*i); @@ -681,6 +710,9 @@ int64 Filesystem::getLastModified(const char *filename) const { PHYSFS_sint64 time = -1; + if (!PHYSFS_isInit()) + return -1; + #ifdef LOVE_USE_PHYSFS_2_1 PHYSFS_Stat stat = {}; if (PHYSFS_stat(filename, &stat)) @@ -704,6 +736,9 @@ int64 Filesystem::getSize(const char *filename) const void Filesystem::setSymlinksEnabled(bool enable) { + if (!PHYSFS_isInit()) + return; + if (!enable) { PHYSFS_Version version = {}; @@ -720,6 +755,9 @@ void Filesystem::setSymlinksEnabled(bool enable) bool Filesystem::areSymlinksEnabled() const { + if (!PHYSFS_isInit()) + return false; + return PHYSFS_symbolicLinksPermitted() != 0; }