From cfb75b015a4e70843e571ce599e11bf2990abc1b Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 16 May 2017 02:41:29 +0300 Subject: Add subdir support into VirtFs::getRealDir. --- src/fs/virtfs/fsdir.cpp | 6 ++--- src/fs/virtfs/fsdir.h | 4 ++-- src/fs/virtfs/fsfuncs.h | 4 ++-- src/fs/virtfs/fszip.cpp | 12 ++++++---- src/fs/virtfs/fszip.h | 4 ++-- src/fs/virtfs/virtfs1_unittest.cc | 50 +++++++++++++++++++++++++++++++++++++++ src/fs/virtfs/virtfs_unittest.cc | 31 ++++++++++++++++++++++++ 7 files changed, 98 insertions(+), 13 deletions(-) diff --git a/src/fs/virtfs/fsdir.cpp b/src/fs/virtfs/fsdir.cpp index f7642c72c..39c26b1b0 100644 --- a/src/fs/virtfs/fsdir.cpp +++ b/src/fs/virtfs/fsdir.cpp @@ -168,12 +168,12 @@ namespace FsDir } bool getRealDir(FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName A_UNUSED, + std::string filename, + std::string dirName A_UNUSED, std::string &realDir) { DirEntry *const dirEntry = static_cast(entry); - if (Files::existsLocal(dirEntry->root + filename)) + if (Files::existsLocal(dirEntry->root + entry->subDir + filename)) { realDir = dirEntry->userDir; return true; diff --git a/src/fs/virtfs/fsdir.h b/src/fs/virtfs/fsdir.h index bba9eaf59..ca3ecf1a4 100644 --- a/src/fs/virtfs/fsdir.h +++ b/src/fs/virtfs/fsdir.h @@ -74,8 +74,8 @@ namespace FsDir void freeList(List *restrict const handle); bool setWriteDir(std::string newDir); bool getRealDir(FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName, + std::string filename, + std::string dirName, std::string &realDir); bool mkdir(std::string dirName); bool remove(std::string filename); diff --git a/src/fs/virtfs/fsfuncs.h b/src/fs/virtfs/fsfuncs.h index eabf799b0..36df93934 100644 --- a/src/fs/virtfs/fsfuncs.h +++ b/src/fs/virtfs/fsfuncs.h @@ -85,8 +85,8 @@ struct FsFuncs final std::string filename, std::string dirName); bool (*getRealDir) (FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName, + std::string filename, + std::string dirName, std::string &realDir); void (*enumerate) (FsEntry *restrict const entry, const std::string &dirName, diff --git a/src/fs/virtfs/fszip.cpp b/src/fs/virtfs/fszip.cpp index 562e0bc80..30f3fbd8e 100644 --- a/src/fs/virtfs/fszip.cpp +++ b/src/fs/virtfs/fszip.cpp @@ -89,11 +89,17 @@ namespace FsZip } bool getRealDir(FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName, + std::string filename, + std::string dirName, std::string &realDir) { ZipEntry *const zipEntry = static_cast(entry); + std::string subDir = zipEntry->subDir; + if (subDir != dirSeparator) + { + filename = pathJoin(subDir, filename); + dirName = pathJoin(subDir, dirName); + } FOR_EACH (std::vector::const_iterator, it2, zipEntry->mHeaders) @@ -128,8 +134,6 @@ namespace FsZip filename = pathJoin(subDir, filename); dirName = pathJoin(subDir, dirName); } - if (subDir == dirSeparator) - subDir.clear(); FOR_EACH (std::vector::const_iterator, it2, zipEntry->mHeaders) diff --git a/src/fs/virtfs/fszip.h b/src/fs/virtfs/fszip.h index 7935702cc..2749ec60f 100644 --- a/src/fs/virtfs/fszip.h +++ b/src/fs/virtfs/fszip.h @@ -66,8 +66,8 @@ namespace FsZip const std::string &filename); File *openReadInternal(const std::string &filename); bool getRealDir(FsEntry *restrict const entry, - const std::string &filename, - const std::string &dirName, + std::string filename, + std::string dirName, std::string &realDir); int64_t read(File *restrict const handle, void *restrict const buffer, diff --git a/src/fs/virtfs/virtfs1_unittest.cc b/src/fs/virtfs/virtfs1_unittest.cc index d34af34aa..264f92079 100644 --- a/src/fs/virtfs/virtfs1_unittest.cc +++ b/src/fs/virtfs/virtfs1_unittest.cc @@ -1404,6 +1404,56 @@ TEST_CASE("VirtFs1 getRealDir2") delete2(logger); } +TEST_CASE("VirtFs1 getRealDir3") +{ + VirtFs::init("."); + logger = new Logger(); + const std::string sep = dirSeparator; + REQUIRE(VirtFs::getRealDir(".") == ""); + REQUIRE(VirtFs::getRealDir("..") == ""); + const bool dir1 = VirtFs::mountDirSilent2("data", + "test", + Append_false); + REQUIRE((dir1 || VirtFs::mountDirSilent2("../data", + "test", + Append_false)) == true); + REQUIRE(VirtFs::getRealDir("file1.txt") == ""); + if (dir1 == true) + { + REQUIRE(VirtFs::getRealDir("dir1") == "data"); + REQUIRE(VirtFs::getRealDir("simplefile.txt") == "data"); + } + else + { + REQUIRE(VirtFs::getRealDir("dir1") == ".." + sep + "data"); + REQUIRE(VirtFs::getRealDir("simplefile.txt") == ".." + sep + "data"); + } + REQUIRE(VirtFs::getRealDir("zzz") == ""); + + VirtFs::mountDirSilent2("data/test", + "dir2", + Append_false); + VirtFs::mountDirSilent2("../data/test", + "dir2", + Append_false); + REQUIRE(VirtFs::getRealDir("dir") == ""); + if (dir1 == true) + { + REQUIRE(VirtFs::getRealDir("file1.txt") == "data"); + REQUIRE(VirtFs::getRealDir("simplefile.txt") == "data" + sep + "test"); + } + else + { + REQUIRE(VirtFs::getRealDir("file1.txt") == + ".." + sep + "data" + sep + "test"); + REQUIRE(VirtFs::getRealDir("simplefile.txt") == ".." + sep + "data"); + } + REQUIRE(VirtFs::getRealDir("zzz") == ""); + + VirtFs::deinit(); + delete2(logger); +} + static bool inList(const VirtFs::List *const list, const std::string &name) { diff --git a/src/fs/virtfs/virtfs_unittest.cc b/src/fs/virtfs/virtfs_unittest.cc index 3718fddda..d6ab42ff4 100644 --- a/src/fs/virtfs/virtfs_unittest.cc +++ b/src/fs/virtfs/virtfs_unittest.cc @@ -747,6 +747,37 @@ TEST_CASE("VirtFs getrealDir2") delete2(logger); } +TEST_CASE("VirtFs getrealDir3") +{ + logger = new Logger(); + const std::string sep = dirSeparator; + std::string name("data/test/test.zip"); + std::string prefix; + if (Files::existsLocal(name) == false) + prefix = "../" + prefix; + + VirtFs::mountZip2(prefix + "data/test/test2.zip", + "dir", + Append_false); + VirtFs::mountDir(prefix + "data/test", Append_false); + + REQUIRE(VirtFs::getRealDir("zzz") == ""); + + REQUIRE(VirtFs::getRealDir("dir1/file1.txt") == + prefix + "data" + sep + "test"); + REQUIRE(VirtFs::getRealDir("hide.png") == + prefix + "data" + sep + "test"); + REQUIRE(VirtFs::getRealDir("hide.png") == + prefix + "data" + sep + "test"); + REQUIRE(VirtFs::getRealDir("1//test.txt") == + prefix + "data" + sep + "test" + sep + "test2.zip"); + + VirtFs::unmountZip2(prefix + "data/test/test2.zip", + "dir"); + VirtFs::unmountDir(prefix + "data/test"); + delete2(logger); +} + TEST_CASE("VirtFs permitLinks") { logger = new Logger(); -- cgit v1.2.3-70-g09d2