diff options
-rwxr-xr-x | configure.ac | 1 | ||||
-rw-r--r-- | data/test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | data/test/Makefile.am | 3 | ||||
-rw-r--r-- | data/test/dir1/CMakeLists.txt | 5 | ||||
-rw-r--r-- | data/test/dir1/Makefile.am | 8 | ||||
-rw-r--r-- | data/test/dir1/file1.txt | 2 | ||||
l--------- | data/test/test2.txt | 1 | ||||
-rw-r--r-- | src/utils/virtfs.cpp | 12 | ||||
-rw-r--r-- | src/utils/virtfs.h | 3 | ||||
-rw-r--r-- | src/utils/virtfs_unittest.cc | 160 | ||||
-rw-r--r-- | src/utils/virtfstools.cpp | 18 |
11 files changed, 202 insertions, 14 deletions
diff --git a/configure.ac b/configure.ac index 89df3c276..ddad878d7 100755 --- a/configure.ac +++ b/configure.ac @@ -558,6 +558,7 @@ data/graphics/sprites/Makefile data/sfx/Makefile data/sfx/system/Makefile data/test/Makefile +data/test/dir1/Makefile data/themes/Makefile data/themes/blacknblack/Makefile data/themes/blackwood/Makefile diff --git a/data/test/CMakeLists.txt b/data/test/CMakeLists.txt index 73802bc56..11d0b5b61 100644 --- a/data/test/CMakeLists.txt +++ b/data/test/CMakeLists.txt @@ -13,9 +13,12 @@ SET(FILES serverlistplus.xml simplefile.txt test.txt + test2.txt test.zip testintmap.xml units.xml ) INSTALL(FILES ${FILES} DESTINATION ${DATA_DIR}/test) + +ADD_SUBDIRECTORY(dir1) diff --git a/data/test/Makefile.am b/data/test/Makefile.am index de8aeacc8..2f7b7f0dd 100644 --- a/data/test/Makefile.am +++ b/data/test/Makefile.am @@ -1,3 +1,5 @@ +SUBDIRS = dir1 + testdir = $(pkgdatadir)/data/test test_DATA = \ @@ -15,6 +17,7 @@ test_DATA = \ serverlistplus.xml \ simplefile.txt \ test.txt \ + test2.txt \ test.zip \ testintmap.xml \ units.xml diff --git a/data/test/dir1/CMakeLists.txt b/data/test/dir1/CMakeLists.txt new file mode 100644 index 000000000..1c032280b --- /dev/null +++ b/data/test/dir1/CMakeLists.txt @@ -0,0 +1,5 @@ +SET(FILES + file1.txt + ) + +INSTALL(FILES ${FILES} DESTINATION ${DATA_DIR}/test/dir1) diff --git a/data/test/dir1/Makefile.am b/data/test/dir1/Makefile.am new file mode 100644 index 000000000..e731f75f2 --- /dev/null +++ b/data/test/dir1/Makefile.am @@ -0,0 +1,8 @@ +dir1dir = $(pkgdatadir)/data/test/dir1 + +dir1_DATA = \ + file1.txt + +EXTRA_DIST = \ + $(dir1_DATA) \ + CMakeLists.txt diff --git a/data/test/dir1/file1.txt b/data/test/dir1/file1.txt new file mode 100644 index 000000000..6a14a1b0f --- /dev/null +++ b/data/test/dir1/file1.txt @@ -0,0 +1,2 @@ +test line 1 +test line 2
\ No newline at end of file diff --git a/data/test/test2.txt b/data/test/test2.txt new file mode 120000 index 000000000..541cb64f9 --- /dev/null +++ b/data/test/test2.txt @@ -0,0 +1 @@ +test.txt
\ No newline at end of file diff --git a/src/utils/virtfs.cpp b/src/utils/virtfs.cpp index 5fcb568d3..7c67deebc 100644 --- a/src/utils/virtfs.cpp +++ b/src/utils/virtfs.cpp @@ -197,9 +197,12 @@ namespace VirtFs return PHYSFS_removeFromSearchPath(oldDir.c_str()); } - const char *getRealDir(const std::string &restrict filename) + std::string getRealDir(const std::string &restrict filename) { - return PHYSFS_getRealDir(filename.c_str()); + const char *const str = PHYSFS_getRealDir(filename.c_str()); + if (str == nullptr) + return std::string(); + return str; } bool mkdir(const std::string &restrict dirname) @@ -207,6 +210,11 @@ namespace VirtFs 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) diff --git a/src/utils/virtfs.h b/src/utils/virtfs.h index 4f4471ad0..2b2550826 100644 --- a/src/utils/virtfs.h +++ b/src/utils/virtfs.h @@ -51,8 +51,9 @@ namespace VirtFs bool addZipToSearchPath(const std::string &restrict newDir, const Append append); bool removeZipFromSearchPath(const std::string &restrict oldDir); - const char *getRealDir(const std::string &restrict filename); + 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(); diff --git a/src/utils/virtfs_unittest.cc b/src/utils/virtfs_unittest.cc index 56f800e00..3ea6ebd9f 100644 --- a/src/utils/virtfs_unittest.cc +++ b/src/utils/virtfs_unittest.cc @@ -20,8 +20,12 @@ #include "catch.hpp" +#include "configuration.h" + #include "utils/checkutils.h" #include "utils/virtfs.h" +#include "utils/virtfstools.h" +#include "utils/virtlist.h" #include "debug.h" @@ -77,7 +81,35 @@ TEST_CASE("VirtFs exists") TEST_CASE("VirtFs enumerateFiles") { - // +++ need implement + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + ::remove("data/test/serverlistplus.xml.part"); + ::remove("../data/test/serverlistplus.xml.part"); +// VirtFs::remove("test/serverlistplus.xml.part"); + + VirtList *list = nullptr; + + VirtFs::permitLinks(false); + list = VirtFs::enumerateFiles("test"); + const size_t sz = list->names.size(); + REQUIRE(sz == 22); + VirtFs::freeList(list); + + VirtFs::permitLinks(true); + list = VirtFs::enumerateFiles("test"); + REQUIRE(list->names.size() > sz); + REQUIRE(list->names.size() - sz == 1); + REQUIRE(list->names.size() == 23); + VirtFs::freeList(list); + + VirtFs::permitLinks(false); + list = VirtFs::enumerateFiles("test"); + REQUIRE(list->names.size() == sz); + REQUIRE(list->names.size() == 22); + VirtFs::freeList(list); + + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); } TEST_CASE("VirtFs isDirectory") @@ -87,21 +119,38 @@ TEST_CASE("VirtFs isDirectory") VirtFs::addDirToSearchPath("../data", Append_false); REQUIRE(VirtFs::isDirectory("test/units.xml") == false); + REQUIRE(VirtFs::isDirectory("test/units.xml/") == false); + REQUIRE(VirtFs::isDirectory("test//units.xml") == false); REQUIRE(VirtFs::isDirectory("test/units123.xml") == false); + REQUIRE(VirtFs::isDirectory("test//units123.xml") == false); REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false); + REQUIRE(VirtFs::isDirectory("tesQ//units.xml") == false); REQUIRE(VirtFs::isDirectory("units.xml") == false); REQUIRE(VirtFs::isDirectory("test") == true); + REQUIRE(VirtFs::isDirectory("test/") == true); + REQUIRE(VirtFs::isDirectory("test//") == true); + REQUIRE(VirtFs::isDirectory("test/dir1") == true); + REQUIRE(VirtFs::isDirectory("test//dir1") == true); + REQUIRE(VirtFs::isDirectory("test//dir1/") == true); + REQUIRE(VirtFs::isDirectory("test//dir1//") == true); + REQUIRE(VirtFs::isDirectory("test/dir1/") == true); + REQUIRE(VirtFs::isDirectory("test/dir1//") == true); REQUIRE(VirtFs::isDirectory("testQ") == false); + REQUIRE(VirtFs::isDirectory("testQ/") == false); + REQUIRE(VirtFs::isDirectory("testQ//") == false); VirtFs::addDirToSearchPath("data/test", Append_false); VirtFs::addDirToSearchPath("../data/test", Append_false); REQUIRE(VirtFs::isDirectory("test/units.xml") == false); + REQUIRE(VirtFs::isDirectory("test/units.xml/") == false); + REQUIRE(VirtFs::isDirectory("test//units.xml") == false); REQUIRE(VirtFs::isDirectory("test/units123.xml") == false); REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false); REQUIRE(VirtFs::isDirectory("units.xml") == false); REQUIRE(VirtFs::isDirectory("test") == true); REQUIRE(VirtFs::isDirectory("testQ") == false); + REQUIRE(VirtFs::isDirectory("test/dir1") == true); VirtFs::removeDirFromSearchPath("data/test"); VirtFs::removeDirFromSearchPath("../data/test"); @@ -110,8 +159,11 @@ TEST_CASE("VirtFs isDirectory") REQUIRE(VirtFs::isDirectory("test/units123.xml") == false); REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false); REQUIRE(VirtFs::isDirectory("units.xml") == false); + REQUIRE(VirtFs::isDirectory("units.xml/") == false); REQUIRE(VirtFs::isDirectory("test") == true); + REQUIRE(VirtFs::isDirectory("test/") == true); REQUIRE(VirtFs::isDirectory("testQ") == false); + REQUIRE(VirtFs::isDirectory("test/dir1") == true); VirtFs::removeDirFromSearchPath("data"); VirtFs::removeDirFromSearchPath("../data"); @@ -178,6 +230,109 @@ TEST_CASE("VirtFs openRead") VirtFs::removeDirFromSearchPath("../data"); } +TEST_CASE("VirtFs addZipToSearchPath") +{ + // +++ need implement +} + +TEST_CASE("VirtFs removeZipFromSearchPath") +{ + // +++ need implement +} + +TEST_CASE("VirtFs getRealDir") +{ + REQUIRE(VirtFs::getRealDir(".") == ""); + REQUIRE(VirtFs::getRealDir("..") == ""); + const bool dir1 = VirtFs::addDirToSearchPath("data", Append_false); + REQUIRE((dir1 || VirtFs::addDirToSearchPath("../data", Append_false)) == true); + if (dir1 == true) + { + REQUIRE(VirtFs::getRealDir("test") == "data"); + REQUIRE(VirtFs::getRealDir("test/test.txt") == + "data"); + } + else + { + REQUIRE(VirtFs::getRealDir("test") == "../data"); + REQUIRE(VirtFs::getRealDir("test/test.txt") == + "../data"); + } + REQUIRE(VirtFs::getRealDir("zzz") == ""); + + VirtFs::addDirToSearchPath("data/test", Append_false); + VirtFs::addDirToSearchPath("../data/test", Append_false); + if (dir1 == true) + { + REQUIRE(VirtFs::getRealDir("test") == "data"); + REQUIRE(VirtFs::getRealDir("test/test.txt") == + "data"); + REQUIRE(VirtFs::getRealDir("test.txt") == + "data/test"); + } + else + { + REQUIRE(VirtFs::getRealDir("test") == "../data"); + REQUIRE(VirtFs::getRealDir("test/test.txt") == + "../data"); + REQUIRE(VirtFs::getRealDir("test.txt") == + "../data/test"); + } + REQUIRE(VirtFs::getRealDir("zzz") == ""); + + VirtFs::removeDirFromSearchPath("data/test"); + VirtFs::removeDirFromSearchPath("../data/test"); + + if (dir1 == true) + { + REQUIRE(VirtFs::getRealDir("test") == "data"); + REQUIRE(VirtFs::getRealDir("test/test.txt") == + "data"); + } + else + { + REQUIRE(VirtFs::getRealDir("test") == "../data"); + REQUIRE(VirtFs::getRealDir("test/test.txt") == + "../data"); + } + REQUIRE(VirtFs::getRealDir("zzz") == ""); + + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); +} + +TEST_CASE("VirtFs permitLinks") +{ + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + ::remove("data/test/serverlistplus.xml.part"); + ::remove("../data/test/serverlistplus.xml.part"); +// VirtFs::remove("test/serverlistplus.xml.part"); + + StringVect list; + VirtFs::permitLinks(false); + VirtFs::getFiles("test", list); + const size_t sz = list.size(); + REQUIRE(sz == 21); + + list.clear(); + VirtFs::permitLinks(true); + VirtFs::getFiles("test", list); + REQUIRE(list.size() > sz); + REQUIRE(list.size() - sz == 1); + REQUIRE(list.size() == 22); + + list.clear(); + VirtFs::permitLinks(false); + VirtFs::getFiles("test", list); + REQUIRE(list.size() == sz); + REQUIRE(list.size() == 21); + + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); +} + TEST_CASE("VirtFs read") { VirtFs::addDirToSearchPath("data", Append_false); @@ -193,14 +348,17 @@ TEST_CASE("VirtFs read") REQUIRE(strcmp(static_cast<char*>(buffer), "test line 1\ntest line 2") == 0); REQUIRE(VirtFs::tell(file) == fileSize); + REQUIRE(VirtFs::eof(file) == true); free(buffer); buffer = calloc(fileSize + 1, 1); REQUIRE(VirtFs::seek(file, 12) != 0); + REQUIRE(VirtFs::eof(file) == false); REQUIRE(VirtFs::tell(file) == 12); REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11); REQUIRE(strcmp(static_cast<char*>(buffer), "test line 2") == 0); + REQUIRE(VirtFs::eof(file) == true); VirtFs::close(file); free(buffer); diff --git a/src/utils/virtfstools.cpp b/src/utils/virtfstools.cpp index e4b4d02cf..adc1c9772 100644 --- a/src/utils/virtfstools.cpp +++ b/src/utils/virtfstools.cpp @@ -49,7 +49,7 @@ namespace VirtFs } logger->log("Loaded %s/%s", - VirtFs::getRealDir(fileName), + VirtFs::getRealDir(fileName).c_str(), fileName.c_str()); fileSize = CAST_S32(VirtFs::fileLength(file)); @@ -75,8 +75,7 @@ namespace VirtFs !ext.compare(str.substr(len - ext.length()))) { const std::string file = path + str; - const std::string realPath = std::string( - VirtFs::getRealDir(file)); + const std::string realPath = VirtFs::getRealDir(file); VirtFs::addZipToSearchPath(std::string(realPath).append( dirSeparator).append(file), append); } @@ -96,8 +95,7 @@ namespace VirtFs !ext.compare(str.substr(len - ext.length()))) { const std::string file = path + str; - const std::string realPath = std::string( - VirtFs::getRealDir(file)); + const std::string realPath = VirtFs::getRealDir(file); VirtFs::removeZipFromSearchPath(std::string( realPath).append( dirSeparator).append( @@ -141,7 +139,7 @@ namespace VirtFs VirtList *const fonts = VirtFs::enumerateFiles(path); FOR_EACH (StringVectCIter, i, fonts->names) { - if (!VirtFs::isDirectory(path + *i)) + if (!VirtFs::isDirectory(path + dirSeparator + *i)) list.push_back(*i); } VirtFs::freeList(fonts); @@ -152,7 +150,7 @@ namespace VirtFs VirtList *const fonts = VirtFs::enumerateFiles(path); FOR_EACH (StringVectCIter, i, fonts->names) { - if (VirtFs::isDirectory(path + *i)) + if (VirtFs::isDirectory(path + dirSeparator + *i)) list.push_back(*i); } VirtFs::freeList(fonts); @@ -161,11 +159,11 @@ namespace VirtFs std::string getPath(const std::string &file) { // get the real path to the file - const char *const tmp = VirtFs::getRealDir(file); + const std::string tmp = VirtFs::getRealDir(file); std::string path; - // if the file is not in the search path, then its nullptr - if (tmp) + // if the file is not in the search path, then its empty + if (!tmp.empty()) { path = std::string(tmp).append(dirSeparator).append(file); #if defined __native_client__ |