diff options
author | Andrei Karas <akaras@inbox.ru> | 2017-02-27 16:17:39 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2017-02-27 16:17:39 +0300 |
commit | 757069bdedabbd904e56d153e2e858db2a2cf2f1 (patch) | |
tree | a89504b98b5df8973fbb8697e25cbccfb0ccff33 | |
parent | 87ed78002ec488b2983dcf47b8b01d49f432e13b (diff) | |
download | plus-757069bdedabbd904e56d153e2e858db2a2cf2f1.tar.gz plus-757069bdedabbd904e56d153e2e858db2a2cf2f1.tar.bz2 plus-757069bdedabbd904e56d153e2e858db2a2cf2f1.tar.xz plus-757069bdedabbd904e56d153e2e858db2a2cf2f1.zip |
Move physfs related functions into virtfsphys file.
-rw-r--r-- | src/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/fs/virtfs.cpp | 155 | ||||
-rw-r--r-- | src/fs/virtfsphys.cpp | 307 | ||||
-rw-r--r-- | src/fs/virtfsphys.h | 77 | ||||
-rw-r--r-- | src/fs/virtfsphys_unittest.cc | 450 | ||||
-rw-r--r-- | src/fs/virtfstools.cpp | 17 | ||||
-rw-r--r-- | src/fs/virtfstools.h | 7 |
8 files changed, 894 insertions, 126 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d0c0f1a22..868ef3422 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -889,6 +889,8 @@ SET(SRCS utils/debugmemoryobject.h fs/virtfsdir.cpp fs/virtfsdir.h + fs/virtfsphys.cpp + fs/virtfsphys.h fs/virtfsrwops.cpp fs/virtfsrwops.h fs/virtfstools.cpp @@ -1787,6 +1789,8 @@ SET(DYE_CMD_SRCS utils/perfomance.h fs/virtfsdir.cpp fs/virtfsdir.h + fs/virtfsphys.cpp + fs/virtfsphys.h fs/virtfsrwops.cpp fs/virtfsrwops.h fs/virtfstools.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 0a0250412..7ee85b638 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -553,6 +553,8 @@ BASE_SRC += events/actionevent.h \ utils/debugmemoryobject.h \ fs/virtfsdir.cpp \ fs/virtfsdir.h \ + fs/virtfsphys.cpp \ + fs/virtfsphys.h \ fs/virtfsrwops.cpp \ fs/virtfsrwops.h \ fs/virtfstools.cpp \ @@ -1932,6 +1934,7 @@ manaplustests_SOURCES = ${SRC} \ utils/dumplibs_unittest.cc \ utils/checkutils_unittest.cc \ fs/virtfs_unittest.cc \ + fs/virtfsphys_unittest.cc \ fs/zip_unittest.cc \ fs/virtfsdir_unittest.cc \ fs/virtfszip_unittest.cc \ diff --git a/src/fs/virtfs.cpp b/src/fs/virtfs.cpp index 9b19890eb..fa9df5ac2 100644 --- a/src/fs/virtfs.cpp +++ b/src/fs/virtfs.cpp @@ -20,51 +20,25 @@ #include "fs/virtfs.h" +#include "fs/virtfsphys.h" #include "fs/virtfile.h" -#include "fs/virtfileprivate.h" #include "fs/virtlist.h" -#include "utils/checkutils.h" - -#include <iostream> -#include <unistd.h> - -#ifdef ANDROID -#include "fs/paths.h" -#endif // ANDROID - #include "debug.h" const char *dirSeparator = nullptr; namespace VirtFs { -#if defined(__native_client__) - void init(const std::string &restrict name A_UNUSED) - { - if (!PHYSFS_init("/fakebinary")) -#elif defined(ANDROID) - void init(const std::string &restrict name A_UNUSED) - { - if (!PHYSFS_init((getRealPath(".").append("/fakebinary")).c_str())) -#else // defined(__native_client__) - void init(const std::string &restrict name) { - if (!PHYSFS_init(name.c_str())) -#endif // defined(__native_client__) - { - std::cout << "Error while initializing PhysFS: " - << VirtFs::getLastError() << std::endl; - _exit(1); - } + VirtFsPhys::init(name); updateDirSeparator(); - atexit(reinterpret_cast<void(*)()>(PHYSFS_deinit)); } void updateDirSeparator() { - dirSeparator = PHYSFS_getDirSeparator(); + dirSeparator = VirtFsPhys::getDirSeparator(); } const char *getDirSeparator() @@ -74,42 +48,32 @@ namespace VirtFs const char *getBaseDir() { - return PHYSFS_getBaseDir(); + return VirtFsPhys::getBaseDir(); } const char *getUserDir() { - return PHYSFS_getUserDir(); + return VirtFsPhys::getUserDir(); } bool exists(const std::string &restrict name) { - return PHYSFS_exists(name.c_str()); + return VirtFsPhys::exists(name); } VirtList *enumerateFiles(const std::string &restrict dir) { - char ** handle = PHYSFS_enumerateFiles(dir.c_str()); - VirtList *const files = new VirtList; - if (handle == nullptr) - return files; - for (const char *const *i = handle; *i; i++) - { - std::string str = *i; - files->names.push_back(str); - } - PHYSFS_freeList(handle); - return files; + return VirtFsPhys::enumerateFiles(dir); } bool isDirectory(const std::string &restrict name) { - return PHYSFS_isDirectory(name.c_str()); + return VirtFsPhys::isDirectory(name); } bool isSymbolicLink(const std::string &restrict name) { - return PHYSFS_isSymbolicLink(name.c_str()); + return VirtFsPhys::isSymbolicLink(name); } void freeList(VirtList *restrict const handle) @@ -119,127 +83,74 @@ namespace VirtFs VirtFile *openRead(const std::string &restrict filename) { - PHYSFS_file *restrict const handle = PHYSFS_openRead( - filename.c_str()); - if (!handle) - return nullptr; - VirtFile *restrict const file = new VirtFile; - file->mPrivate = new VirtFilePrivate(handle); - return file; + return VirtFsPhys::openRead(filename); } VirtFile *openWrite(const std::string &restrict filename) { - PHYSFS_file *restrict const handle = PHYSFS_openWrite( - filename.c_str()); - if (!handle) - return nullptr; - VirtFile *restrict const file = new VirtFile; - file->mPrivate = new VirtFilePrivate(handle); - return file; + return VirtFsPhys::openWrite(filename); } VirtFile *openAppend(const std::string &restrict filename) { - PHYSFS_file *restrict const handle = PHYSFS_openAppend( - filename.c_str()); - if (!handle) - return nullptr; - VirtFile *restrict const file = new VirtFile; - file->mPrivate = new VirtFilePrivate(handle); - return file; + return VirtFsPhys::openAppend(filename); } bool setWriteDir(const std::string &restrict newDir) { - return PHYSFS_setWriteDir(newDir.c_str()); + return VirtFsPhys::setWriteDir(newDir); } bool addDirToSearchPath(const std::string &restrict newDir, const Append append) { - logger->log("Add virtual directory: " + newDir); - if (newDir.find(".zip") != std::string::npos) - { - reportAlways("Called addDirToSearchPath with zip archive"); - return false; - } - return PHYSFS_addToSearchPath(newDir.c_str(), - append == Append_true ? 1 : 0); + return VirtFsPhys::addDirToSearchPath(newDir, append); } bool removeDirFromSearchPath(const std::string &restrict oldDir) { - logger->log("Remove virtual directory: " + oldDir); - if (oldDir.find(".zip") != std::string::npos) - { - reportAlways("Called removeDirFromSearchPath with zip archive"); - return false; - } - return PHYSFS_removeFromSearchPath(oldDir.c_str()); + return VirtFsPhys::removeDirFromSearchPath(oldDir); } bool addZipToSearchPath(const std::string &restrict newDir, const Append append) { - logger->log("Add virtual zip: " + newDir); - if (newDir.find(".zip") == std::string::npos) - { - reportAlways("Called addZipToSearchPath without zip archive"); - return false; - } - return PHYSFS_addToSearchPath(newDir.c_str(), - append == Append_true ? 1 : 0); + return VirtFsPhys::addZipToSearchPath(newDir, append); } bool removeZipFromSearchPath(const std::string &restrict oldDir) { - logger->log("Remove virtual zip: " + oldDir); - if (oldDir.find(".zip") == std::string::npos) - { - reportAlways("Called removeZipFromSearchPath without zip archive"); - return false; - } - return PHYSFS_removeFromSearchPath(oldDir.c_str()); + return VirtFsPhys::removeZipFromSearchPath(oldDir); } std::string getRealDir(const std::string &restrict filename) { - const char *const str = PHYSFS_getRealDir(filename.c_str()); - if (str == nullptr) - return std::string(); - return str; + return VirtFsPhys::getRealDir(filename); } bool mkdir(const std::string &restrict dirname) { - return PHYSFS_mkdir(dirname.c_str()); + return VirtFsPhys::mkdir(dirname); } bool remove(const std::string &restrict filename) { - return PHYSFS_delete(filename.c_str()); + return VirtFsPhys::remove(filename); } bool deinit() { - if (PHYSFS_deinit() != 0) - { - logger->log("Physfs deinit error: %s", - VirtFs::getLastError()); - return false; - } - return true; + return VirtFsPhys::deinit(); } void permitLinks(const bool val) { - PHYSFS_permitSymbolicLinks(val ? 1 : 0); + VirtFsPhys::permitLinks(val); } const char *getLastError() { - return PHYSFS_getLastError(); + return VirtFsPhys::getLastError(); } int close(VirtFile *restrict const file) @@ -255,9 +166,7 @@ namespace VirtFs const uint32_t objSize, const uint32_t objCount) { - if (file == nullptr) - return 0; - return PHYSFS_read(file->mPrivate->mFile, + return VirtFsPhys::read(file, buffer, objSize, objCount); @@ -268,9 +177,7 @@ namespace VirtFs const uint32_t objSize, const uint32_t objCount) { - if (file == nullptr) - return 0; - return PHYSFS_write(file->mPrivate->mFile, + return VirtFsPhys::write(file, buffer, objSize, objCount); @@ -278,27 +185,23 @@ namespace VirtFs int64_t fileLength(VirtFile *restrict const file) { - if (file == nullptr) - return -1; - return PHYSFS_fileLength(file->mPrivate->mFile); + return VirtFsPhys::fileLength(file); } int64_t tell(VirtFile *restrict const file) { - if (file == nullptr) - return -1; - return PHYSFS_tell(file->mPrivate->mFile); + return VirtFsPhys::tell(file); } int seek(VirtFile *restrict const file, const uint64_t pos) { - return PHYSFS_seek(file->mPrivate->mFile, + return VirtFsPhys::seek(file, pos); } int eof(VirtFile *restrict const file) { - return PHYSFS_eof(file->mPrivate->mFile); + return VirtFsPhys::eof(file); } } // namespace VirtFs diff --git a/src/fs/virtfsphys.cpp b/src/fs/virtfsphys.cpp new file mode 100644 index 000000000..16b2c40df --- /dev/null +++ b/src/fs/virtfsphys.cpp @@ -0,0 +1,307 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "fs/virtfsphys.h" + +#include "fs/virtfile.h" +#include "fs/virtfileprivate.h" +#include "fs/virtlist.h" + +#include "utils/checkutils.h" + +#include <iostream> +#include <unistd.h> + +#ifdef ANDROID +#include "fs/paths.h" +#endif // ANDROID + +#include "debug.h" + +namespace +{ + const char *dirSeparator = nullptr; +} // namespace + +namespace VirtFsPhys +{ +#if defined(__native_client__) + void init(const std::string &restrict name A_UNUSED) + { + if (!PHYSFS_init("/fakebinary")) +#elif defined(ANDROID) + void init(const std::string &restrict name A_UNUSED) + { + if (!PHYSFS_init((getRealPath(".").append("/fakebinary")).c_str())) +#else // defined(__native_client__) + + void init(const std::string &restrict name) + { + if (!PHYSFS_init(name.c_str())) +#endif // defined(__native_client__) + { + std::cout << "Error while initializing PhysFS: " + << VirtFsPhys::getLastError() << std::endl; + _exit(1); + } + updateDirSeparator(); + atexit(reinterpret_cast<void(*)()>(PHYSFS_deinit)); + } + + void updateDirSeparator() + { + dirSeparator = PHYSFS_getDirSeparator(); + } + + const char *getDirSeparator() + { + return dirSeparator; + } + + const char *getBaseDir() + { + return PHYSFS_getBaseDir(); + } + + const char *getUserDir() + { + return PHYSFS_getUserDir(); + } + + bool exists(const std::string &restrict name) + { + return PHYSFS_exists(name.c_str()); + } + + VirtList *enumerateFiles(const std::string &restrict dir) + { + char ** handle = PHYSFS_enumerateFiles(dir.c_str()); + VirtList *const files = new VirtList; + if (handle == nullptr) + return files; + for (const char *const *i = handle; *i; i++) + { + std::string str = *i; + files->names.push_back(str); + } + PHYSFS_freeList(handle); + return files; + } + + bool isDirectory(const std::string &restrict name) + { + return PHYSFS_isDirectory(name.c_str()); + } + + bool isSymbolicLink(const std::string &restrict name) + { + return PHYSFS_isSymbolicLink(name.c_str()); + } + + void freeList(VirtList *restrict const handle) + { + delete handle; + } + + VirtFile *openRead(const std::string &restrict filename) + { + PHYSFS_file *restrict const handle = PHYSFS_openRead( + filename.c_str()); + if (!handle) + return nullptr; + VirtFile *restrict const file = new VirtFile; + file->mPrivate = new VirtFilePrivate(handle); + return file; + } + + VirtFile *openWrite(const std::string &restrict filename) + { + PHYSFS_file *restrict const handle = PHYSFS_openWrite( + filename.c_str()); + if (!handle) + return nullptr; + VirtFile *restrict const file = new VirtFile; + file->mPrivate = new VirtFilePrivate(handle); + return file; + } + + VirtFile *openAppend(const std::string &restrict filename) + { + PHYSFS_file *restrict const handle = PHYSFS_openAppend( + filename.c_str()); + if (!handle) + return nullptr; + VirtFile *restrict const file = new VirtFile; + file->mPrivate = new VirtFilePrivate(handle); + return file; + } + + bool setWriteDir(const std::string &restrict newDir) + { + return PHYSFS_setWriteDir(newDir.c_str()); + } + + bool addDirToSearchPath(const std::string &restrict newDir, + const Append append) + { + logger->log("Add virtual directory: " + newDir); + if (newDir.find(".zip") != std::string::npos) + { + reportAlways("Called addDirToSearchPath with zip archive"); + return false; + } + return PHYSFS_addToSearchPath(newDir.c_str(), + append == Append_true ? 1 : 0); + } + + bool removeDirFromSearchPath(const std::string &restrict oldDir) + { + logger->log("Remove virtual directory: " + oldDir); + if (oldDir.find(".zip") != std::string::npos) + { + reportAlways("Called removeDirFromSearchPath with zip archive"); + return false; + } + return PHYSFS_removeFromSearchPath(oldDir.c_str()); + } + + bool addZipToSearchPath(const std::string &restrict newDir, + const Append append) + { + logger->log("Add virtual zip: " + newDir); + if (newDir.find(".zip") == std::string::npos) + { + reportAlways("Called addZipToSearchPath without zip archive"); + return false; + } + return PHYSFS_addToSearchPath(newDir.c_str(), + append == Append_true ? 1 : 0); + } + + bool removeZipFromSearchPath(const std::string &restrict oldDir) + { + logger->log("Remove virtual zip: " + oldDir); + if (oldDir.find(".zip") == std::string::npos) + { + reportAlways("Called removeZipFromSearchPath without zip archive"); + return false; + } + return PHYSFS_removeFromSearchPath(oldDir.c_str()); + } + + std::string getRealDir(const std::string &restrict filename) + { + const char *const str = PHYSFS_getRealDir(filename.c_str()); + if (str == nullptr) + return std::string(); + return str; + } + + bool mkdir(const std::string &restrict dirname) + { + return PHYSFS_mkdir(dirname.c_str()); + } + + bool remove(const std::string &restrict filename) + { + return PHYSFS_delete(filename.c_str()); + } + + bool deinit() + { + if (PHYSFS_deinit() != 0) + { + logger->log("Physfs deinit error: %s", + VirtFsPhys::getLastError()); + return false; + } + return true; + } + + void permitLinks(const bool val) + { + PHYSFS_permitSymbolicLinks(val ? 1 : 0); + } + + const char *getLastError() + { + return PHYSFS_getLastError(); + } + + int close(VirtFile *restrict const file) + { + if (file == nullptr) + return 0; + delete file; + return 1; + } + + int64_t read(VirtFile *restrict const file, + void *restrict const buffer, + const uint32_t objSize, + const uint32_t objCount) + { + if (file == nullptr) + return 0; + return PHYSFS_read(file->mPrivate->mFile, + buffer, + objSize, + objCount); + } + + int64_t write(VirtFile *restrict const file, + const void *restrict const buffer, + const uint32_t objSize, + const uint32_t objCount) + { + if (file == nullptr) + return 0; + return PHYSFS_write(file->mPrivate->mFile, + buffer, + objSize, + objCount); + } + + int64_t fileLength(VirtFile *restrict const file) + { + if (file == nullptr) + return -1; + return PHYSFS_fileLength(file->mPrivate->mFile); + } + + int64_t tell(VirtFile *restrict const file) + { + if (file == nullptr) + return -1; + return PHYSFS_tell(file->mPrivate->mFile); + } + + int seek(VirtFile *restrict const file, + const uint64_t pos) + { + return PHYSFS_seek(file->mPrivate->mFile, + pos); + } + + int eof(VirtFile *restrict const file) + { + return PHYSFS_eof(file->mPrivate->mFile); + } +} // namespace VirtFsPhys diff --git a/src/fs/virtfsphys.h b/src/fs/virtfsphys.h new file mode 100644 index 000000000..99c3e3e0c --- /dev/null +++ b/src/fs/virtfsphys.h @@ -0,0 +1,77 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef UTILS_VIRTFSPHYS_H +#define UTILS_VIRTFSPHYS_H + +#include "enums/simpletypes/append.h" + +#include "localconsts.h" + +#include <string> + +struct VirtFile; +struct VirtList; + +namespace VirtFsPhys +{ + void init(const std::string &restrict name); + void updateDirSeparator(); + const char *getDirSeparator(); + const char *getBaseDir(); + const char *getUserDir(); + bool exists(const std::string &restrict name); + VirtList *enumerateFiles(const std::string &restrict dir) RETURNS_NONNULL; + bool isDirectory(const std::string &restrict name); + bool isSymbolicLink(const std::string &restrict name); + void freeList(VirtList *restrict const handle); + VirtFile *openRead(const std::string &restrict filename); + VirtFile *openWrite(const std::string &restrict filename); + VirtFile *openAppend(const std::string &restrict filename); + bool setWriteDir(const std::string &restrict newDir); + bool addDirToSearchPath(const std::string &restrict newDir, + const Append append); + bool removeDirFromSearchPath(const std::string &restrict oldDir); + bool addZipToSearchPath(const std::string &restrict newDir, + const Append append); + bool removeZipFromSearchPath(const std::string &restrict oldDir); + std::string getRealDir(const std::string &restrict filename); + bool mkdir(const std::string &restrict dirName); + bool remove(const std::string &restrict filename); + bool deinit(); + void permitLinks(const bool val); + const char *getLastError(); + int64_t read(VirtFile *restrict const handle, + void *restrict const buffer, + const uint32_t objSize, + const uint32_t objCount); + int64_t write(VirtFile *restrict const file, + const void *restrict const buffer, + const uint32_t objSize, + const uint32_t objCount); + int close(VirtFile *restrict const file); + int64_t fileLength(VirtFile *restrict const file); + int64_t tell(VirtFile *restrict const file); + int seek(VirtFile *restrict const file, + const uint64_t pos); + int eof(VirtFile *restrict const file); +} // namespace VirtFsPhys + +#endif // UTILS_VIRTFSPHYS_H diff --git a/src/fs/virtfsphys_unittest.cc b/src/fs/virtfsphys_unittest.cc new file mode 100644 index 000000000..efb37e2d7 --- /dev/null +++ b/src/fs/virtfsphys_unittest.cc @@ -0,0 +1,450 @@ +/* + * The ManaPlus Client + * Copyright (C) 2016-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "catch.hpp" + +#include "fs/virtfs.h" +#include "fs/virtfsphys.h" +#include "fs/virtfstools.h" +#include "fs/virtlist.h" + +#include "utils/checkutils.h" +#include "utils/delete2.h" + +#include "debug.h" + +TEST_CASE("VirtFsPhys dirSeparator") +{ + REQUIRE(VirtFs::getDirSeparator() != nullptr); + REQUIRE(VirtFsPhys::getDirSeparator() == + std::string(VirtFs::getDirSeparator())); + VirtFsPhys::updateDirSeparator(); + REQUIRE(VirtFs::getDirSeparator() != nullptr); + REQUIRE(VirtFsPhys::getDirSeparator() == + std::string(VirtFs::getDirSeparator())); +} + +TEST_CASE("VirtFsPhys getBaseDir") +{ + REQUIRE(VirtFsPhys::getBaseDir() != nullptr); +} + +TEST_CASE("VirtFsPhys getUserDir") +{ + REQUIRE(VirtFsPhys::getUserDir() != nullptr); +} + +TEST_CASE("VirtFsPhys exists") +{ + logger = new Logger(); + VirtFsPhys::addDirToSearchPath("data", Append_false); + VirtFsPhys::addDirToSearchPath("../data", Append_false); + + REQUIRE(VirtFsPhys::exists("test/units.xml") == true); + REQUIRE(VirtFsPhys::exists("test/units123.xml") == false); + REQUIRE(VirtFsPhys::exists("tesQ/units.xml") == false); + REQUIRE(VirtFsPhys::exists("units.xml") == false); + + VirtFsPhys::addDirToSearchPath("data/test", Append_false); + VirtFsPhys::addDirToSearchPath("../data/test", Append_false); + + REQUIRE(VirtFsPhys::exists("test/units.xml") == true); + REQUIRE(VirtFsPhys::exists("test/units123.xml") == false); + REQUIRE(VirtFsPhys::exists("tesQ/units.xml") == false); + REQUIRE(VirtFsPhys::exists("units.xml") == true); + + VirtFsPhys::removeDirFromSearchPath("data/test"); + VirtFsPhys::removeDirFromSearchPath("../data/test"); + + REQUIRE(VirtFsPhys::exists("test/units.xml") == true); + REQUIRE(VirtFsPhys::exists("test/units123.xml") == false); + REQUIRE(VirtFsPhys::exists("tesQ/units.xml") == false); + REQUIRE(VirtFsPhys::exists("units.xml") == false); + + VirtFsPhys::removeDirFromSearchPath("data"); + VirtFsPhys::removeDirFromSearchPath("../data"); + delete2(logger); +} + +static void removeTemp(StringVect &restrict list) +{ + int cnt = 0; + std::sort(list.begin(), list.end()); + + FOR_EACH (StringVectIter, it, list) + { + if (*it != "serverlistplus.xml.part") + { + logger->log("file: %d %s", + cnt, + (*it).c_str()); + cnt ++; + } + } + + FOR_EACH (StringVectIter, it, list) + { + if (*it == "serverlistplus.xml.part") + { + list.erase(it); + return; + } + } +} + +TEST_CASE("VirtFsPhys enumerateFiles1") +{ + logger = new Logger; + + VirtFsPhys::addDirToSearchPath("data", Append_false); + VirtFsPhys::addDirToSearchPath("../data", Append_false); + + VirtList *list = nullptr; + + const int cnt1 = VirtFsPhys::exists("test/test2.txt") ? 27 : 26; + const int cnt2 = 27; + + VirtFsPhys::permitLinks(false); + list = VirtFsPhys::enumerateFiles("test"); + removeTemp(list->names); + const size_t sz = list->names.size(); + REQUIRE(sz == cnt1); + VirtFsPhys::freeList(list); + + VirtFsPhys::permitLinks(true); + list = VirtFsPhys::enumerateFiles("test"); + removeTemp(list->names); + REQUIRE(list->names.size() == cnt2); + VirtFsPhys::freeList(list); + + VirtFsPhys::permitLinks(false); + list = VirtFsPhys::enumerateFiles("test"); + removeTemp(list->names); + REQUIRE(list->names.size() == cnt1); + VirtFsPhys::freeList(list); + + VirtFsPhys::removeDirFromSearchPath("data"); + VirtFsPhys::removeDirFromSearchPath("../data"); + delete2(logger); +} + +TEST_CASE("VirtFsPhys enumerateFiles2") +{ + logger = new Logger; + + VirtFsPhys::addDirToSearchPath("data/test/dir1", + Append_false); + VirtFsPhys::addDirToSearchPath("../data/test/dir1", + Append_false); + + VirtList *list = nullptr; + + list = VirtFsPhys::enumerateFiles("/"); + const size_t sz = list->names.size(); + REQUIRE(list->names.size() == 5); + VirtFsPhys::freeList(list); + + VirtFsPhys::removeDirFromSearchPath("data/test/dir1"); + VirtFsPhys::removeDirFromSearchPath("../data/test/dir1"); + delete2(logger); +} + +TEST_CASE("VirtFsPhys isDirectory") +{ + logger = new Logger(); + VirtFsPhys::addDirToSearchPath("data", Append_false); + VirtFsPhys::addDirToSearchPath("../data", Append_false); + + REQUIRE(VirtFsPhys::isDirectory("test/units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("test/units.xml/") == false); + REQUIRE(VirtFsPhys::isDirectory("test//units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("test/units123.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("test//units123.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("tesQ/units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("tesQ//units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("test") == true); + REQUIRE(VirtFsPhys::isDirectory("test/") == true); + REQUIRE(VirtFsPhys::isDirectory("test//") == true); + REQUIRE(VirtFsPhys::isDirectory("test/dir1") == true); + REQUIRE(VirtFsPhys::isDirectory("test//dir1") == true); + REQUIRE(VirtFsPhys::isDirectory("test//dir1/") == true); + REQUIRE(VirtFsPhys::isDirectory("test//dir1//") == true); + REQUIRE(VirtFsPhys::isDirectory("test/dir1/") == true); + REQUIRE(VirtFsPhys::isDirectory("test/dir1//") == true); + REQUIRE(VirtFsPhys::isDirectory("testQ") == false); + REQUIRE(VirtFsPhys::isDirectory("testQ/") == false); + REQUIRE(VirtFsPhys::isDirectory("testQ//") == false); + + VirtFsPhys::addDirToSearchPath("data/test", Append_false); + VirtFsPhys::addDirToSearchPath("../data/test", Append_false); + + REQUIRE(VirtFsPhys::isDirectory("test/units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("test/units.xml/") == false); + REQUIRE(VirtFsPhys::isDirectory("test//units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("test/units123.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("tesQ/units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("test") == true); + REQUIRE(VirtFsPhys::isDirectory("testQ") == false); + REQUIRE(VirtFsPhys::isDirectory("test/dir1") == true); + + VirtFsPhys::removeDirFromSearchPath("data/test"); + VirtFsPhys::removeDirFromSearchPath("../data/test"); + + REQUIRE(VirtFsPhys::isDirectory("test/units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("test/units123.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("tesQ/units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("units.xml") == false); + REQUIRE(VirtFsPhys::isDirectory("units.xml/") == false); + REQUIRE(VirtFsPhys::isDirectory("test") == true); + REQUIRE(VirtFsPhys::isDirectory("test/") == true); + REQUIRE(VirtFsPhys::isDirectory("testQ") == false); + REQUIRE(VirtFsPhys::isDirectory("test/dir1") == true); + + VirtFsPhys::removeDirFromSearchPath("data"); + VirtFsPhys::removeDirFromSearchPath("../data"); + delete2(logger); +} + +TEST_CASE("VirtFsPhys openRead") +{ + logger = new Logger(); + VirtFsPhys::addDirToSearchPath("data", Append_false); + VirtFsPhys::addDirToSearchPath("../data", Append_false); + + VirtFile *file = nullptr; + + file = VirtFsPhys::openRead("test/units.xml"); + REQUIRE(file != nullptr); + VirtFsPhys::close(file); + file = VirtFsPhys::openRead("test/units123.xml"); + REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("tesQ/units.xml"); + REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("units.xml"); + REQUIRE(file == nullptr); +// file = VirtFsPhys::openRead("test"); +// REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("testQ"); + REQUIRE(file == nullptr); + + VirtFsPhys::addDirToSearchPath("data/test", Append_false); + VirtFsPhys::addDirToSearchPath("../data/test", Append_false); + + file = VirtFsPhys::openRead("test/units.xml"); + REQUIRE(file != nullptr); + VirtFsPhys::close(file); + file = VirtFsPhys::openRead("test/units123.xml"); + REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("tesQ/units.xml"); + REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("units.xml"); + REQUIRE(file != nullptr); + VirtFsPhys::close(file); +// file = VirtFsPhys::openRead("test"); +// REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("testQ"); + REQUIRE(file == nullptr); + + VirtFsPhys::removeDirFromSearchPath("data/test"); + VirtFsPhys::removeDirFromSearchPath("../data/test"); + + file = VirtFsPhys::openRead("test/units.xml"); + REQUIRE(file != nullptr); + VirtFsPhys::close(file); + file = VirtFsPhys::openRead("test/units123.xml"); + REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("tesQ/units.xml"); + REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("units.xml"); + REQUIRE(file == nullptr); +// file = VirtFsPhys::openRead("test"); +// REQUIRE(file == nullptr); + file = VirtFsPhys::openRead("testQ"); + REQUIRE(file == nullptr); + + VirtFsPhys::removeDirFromSearchPath("data"); + VirtFsPhys::removeDirFromSearchPath("../data"); + delete2(logger); +} + +TEST_CASE("VirtFsPhys addZipToSearchPath") +{ + // +++ need implement +} + +TEST_CASE("VirtFsPhys removeZipFromSearchPath") +{ + // +++ need implement +} + +TEST_CASE("VirtFsPhys getRealDir") +{ + logger = new Logger(); + REQUIRE(VirtFsPhys::getRealDir(".") == ""); + REQUIRE(VirtFsPhys::getRealDir("..") == ""); + const bool dir1 = VirtFsPhys::addDirToSearchPath("data", Append_false); + REQUIRE((dir1 || VirtFsPhys::addDirToSearchPath( + "../data", Append_false)) == true); + if (dir1 == true) + { + REQUIRE(VirtFsPhys::getRealDir("test") == "data"); + REQUIRE(VirtFsPhys::getRealDir("test/test.txt") == + "data"); + } + else + { + REQUIRE(VirtFsPhys::getRealDir("test") == "../data"); + REQUIRE(VirtFsPhys::getRealDir("test/test.txt") == + "../data"); + } + REQUIRE(VirtFsPhys::getRealDir("zzz") == ""); + + VirtFsPhys::addDirToSearchPath("data/test", Append_false); + VirtFsPhys::addDirToSearchPath("../data/test", Append_false); + if (dir1 == true) + { + REQUIRE(VirtFsPhys::getRealDir("test") == "data"); + REQUIRE(VirtFsPhys::getRealDir("test/test.txt") == + "data"); + REQUIRE(VirtFsPhys::getRealDir("test.txt") == + "data/test"); + } + else + { + REQUIRE(VirtFsPhys::getRealDir("test") == "../data"); + REQUIRE(VirtFsPhys::getRealDir("test/test.txt") == + "../data"); + REQUIRE(VirtFsPhys::getRealDir("test.txt") == + "../data/test"); + } + REQUIRE(VirtFsPhys::getRealDir("zzz") == ""); + + if (dir1 == true) + { + VirtFsPhys::addZipToSearchPath("data/test/test.zip", Append_false); + REQUIRE(VirtFsPhys::getRealDir("dir/brimmedhat.png") == + "data/test/test.zip"); + REQUIRE(VirtFsPhys::getRealDir("hide.png") == "data/test"); + } + else + { + VirtFsPhys::addZipToSearchPath("../data/test/test.zip", Append_false); + REQUIRE(VirtFsPhys::getRealDir("dir/brimmedhat.png") == + "../data/test/test.zip"); + REQUIRE(VirtFsPhys::getRealDir("hide.png") == "../data/test"); + } + + VirtFsPhys::removeDirFromSearchPath("data/test"); + VirtFsPhys::removeDirFromSearchPath("../data/test"); + + if (dir1 == true) + { + REQUIRE(VirtFsPhys::getRealDir("test") == "data"); + REQUIRE(VirtFsPhys::getRealDir("test/test.txt") == + "data"); + REQUIRE(VirtFsPhys::getRealDir("dir/hide.png") == "data/test/test.zip"); + } + else + { + REQUIRE(VirtFsPhys::getRealDir("test") == "../data"); + REQUIRE(VirtFsPhys::getRealDir("test/test.txt") == + "../data"); + REQUIRE(VirtFsPhys::getRealDir("dir/hide.png") == "../data/test/test.zip"); + } + REQUIRE(VirtFsPhys::exists("dir/hide.png")); + REQUIRE(VirtFsPhys::getRealDir("zzz") == ""); + + VirtFsPhys::removeDirFromSearchPath("data"); + VirtFsPhys::removeDirFromSearchPath("../data"); + VirtFsPhys::removeZipFromSearchPath("data/test/test.zip"); + VirtFsPhys::removeZipFromSearchPath("../data/test/test.zip"); + delete2(logger); +} + +TEST_CASE("VirtFsPhys permitLinks") +{ + logger = new Logger(); + VirtFsPhys::addDirToSearchPath("data", Append_false); + VirtFsPhys::addDirToSearchPath("../data", Append_false); + + const int cnt1 = VirtFsPhys::exists("test/test2.txt") ? 25 : 24; + const int cnt2 = 25; + + StringVect list; + VirtFsPhys::permitLinks(false); + VirtFsPhys::getFiles("test", list); + removeTemp(list); + const size_t sz = list.size(); + REQUIRE(sz == cnt1); + + list.clear(); + VirtFsPhys::permitLinks(true); + VirtFsPhys::getFiles("test", list); + removeTemp(list); + REQUIRE(list.size() == cnt2); + + list.clear(); + VirtFsPhys::permitLinks(false); + VirtFsPhys::getFiles("test", list); + removeTemp(list); + REQUIRE(list.size() == cnt1); + + VirtFsPhys::removeDirFromSearchPath("data"); + VirtFsPhys::removeDirFromSearchPath("../data"); + delete2(logger); +} + +TEST_CASE("VirtFsPhys read") +{ + logger = new Logger(); + VirtFsPhys::addDirToSearchPath("data", Append_false); + VirtFsPhys::addDirToSearchPath("../data", Append_false); + + VirtFile *file = VirtFsPhys::openRead("test/test.txt"); + REQUIRE(file != nullptr); + REQUIRE(VirtFsPhys::fileLength(file) == 23); + const int fileSize = VirtFsPhys::fileLength(file); + + void *restrict buffer = calloc(fileSize + 1, 1); + REQUIRE(VirtFsPhys::read(file, buffer, 1, fileSize) == fileSize); + REQUIRE(strcmp(static_cast<char*>(buffer), + "test line 1\ntest line 2") == 0); + REQUIRE(VirtFsPhys::tell(file) == fileSize); + REQUIRE(VirtFsPhys::eof(file) == true); + + free(buffer); + buffer = calloc(fileSize + 1, 1); + REQUIRE(VirtFsPhys::seek(file, 12) != 0); + REQUIRE(VirtFsPhys::eof(file) == false); + REQUIRE(VirtFsPhys::tell(file) == 12); + REQUIRE(VirtFsPhys::read(file, buffer, 1, 11) == 11); + REQUIRE(strcmp(static_cast<char*>(buffer), + "test line 2") == 0); + REQUIRE(VirtFsPhys::eof(file) == true); + + VirtFsPhys::close(file); + free(buffer); + + VirtFsPhys::removeDirFromSearchPath("data"); + VirtFsPhys::removeDirFromSearchPath("../data"); + delete2(logger); +} diff --git a/src/fs/virtfstools.cpp b/src/fs/virtfstools.cpp index d227f5f07..c9e976864 100644 --- a/src/fs/virtfstools.cpp +++ b/src/fs/virtfstools.cpp @@ -25,6 +25,7 @@ #include "fs/paths.h" #include "fs/virtfs.h" #include "fs/virtfsdir.h" +#include "fs/virtfsphys.h" #include "fs/virtlist.h" #include "utils/stringutils.h" @@ -238,3 +239,19 @@ namespace VirtFsDir VirtFsDir::freeList(fonts); } } // namespace VirtFs + +// +++ temporary add it here +namespace VirtFsPhys +{ + void getFiles(const std::string &path, + StringVect &list) + { + VirtList *const fonts = VirtFsPhys::enumerateFiles(path); + FOR_EACH (StringVectCIter, i, fonts->names) + { + if (!VirtFsPhys::isDirectory(path + dirSeparator + *i)) + list.push_back(*i); + } + VirtFsPhys::freeList(fonts); + } +} // namespace VirtFs diff --git a/src/fs/virtfstools.h b/src/fs/virtfstools.h index e29e3e427..3271fee6f 100644 --- a/src/fs/virtfstools.h +++ b/src/fs/virtfstools.h @@ -58,4 +58,11 @@ namespace VirtFsDir StringVect &list); } // namespace VirtFs +// +++ temporary add it here +namespace VirtFsPhys +{ + void getFiles(const std::string &path, + StringVect &list); +} // namespace VirtFs + #endif // UTILS_VIRTFSTOOLS_H |