Skip to content

Commit

Permalink
Catch internal TooSmallFile exception in BuildFileCompound.
Browse files Browse the repository at this point in the history
And try with multi parts compound only if file is not found or file
is too small.

We can now fix the tests as we have a clear (new) message thrown.
  • Loading branch information
mgautierfr committed Apr 26, 2024
1 parent ca0887d commit 452bb07
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
36 changes: 28 additions & 8 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,35 @@ class TooSmallFile: public std::exception {};
//////////////////////////////////////////////////////////////////////
// FileImpl
//
std::shared_ptr<FileImpl> FileImpl::BuildFileImpl(const std::string& filename) {
std::shared_ptr<FileImpl> FileImpl::BuildFileImpl(std::string fname) {
bool found = false;
std::shared_ptr<FileCompound> fileCompound;
try {
return std::shared_ptr<FileImpl>(
new FileImpl(std::make_shared<FileCompound>(filename))
);
} catch (...) {
return std::shared_ptr<FileImpl>(
new FileImpl(std::make_shared<FileCompound>(filename, FileCompound::MultiPartToken::Multi))
);
fileCompound = std::make_shared<FileCompound>(fname);
found = true;
} catch(...) { }
if (found) {
try {
return std::shared_ptr<FileImpl>(new FileImpl(fileCompound));
} catch(TooSmallFile& e) {
if (fname.size() > 6 && fname.substr(fname.size()-6) == ".zimaa") {
// We have tried to open a split file. Let's continue with "aa" postfix removed.
fname.resize(fname.size()-2);
} else {
// We have found a (not split) file but it is too small.
throw ZimFileFormatError("Zim file(s) is too small.");
}
}
}

// Either we haven't succeed to open fname (.zim) or the file (.zimaa) is too small.
// Let's try to open as split files.
fileCompound = std::make_shared<FileCompound>(fname, FileCompound::MultiPartToken::Multi);
try {
return std::shared_ptr<FileImpl>(new FileImpl(fileCompound));
} catch (TooSmallFile& e) {
// TooSmallFile is internal error.
throw ZimFileFormatError("Zim file(s) is too small.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/fileimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace zim
using FindxResult = std::pair<bool, entry_index_t>;
using FindxTitleResult = std::pair<bool, title_index_t>;

static std::shared_ptr<FileImpl> BuildFileImpl(const std::string& fname);
static std::shared_ptr<FileImpl> BuildFileImpl(std::string fname);
#ifndef _WIN32
explicit FileImpl(int fd);
explicit FileImpl(FdInput fd);
Expand Down
29 changes: 27 additions & 2 deletions test/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,31 @@ TEST(ZimArchive, openRealZimArchive)
}
}

TEST(ZimArchive, openSplitZimArchive)
{
const char* fname = "wikibooks_be_all_nopic_2017-02_splitted.zim";

for (auto& testfile: getDataFilePath(fname)) {
const TestContext ctx{ {"path", testfile.path+"aa" } };
std::unique_ptr<zim::Archive> archive;
EXPECT_NO_THROW( archive.reset(new zim::Archive(testfile.path+"aa")) ) << ctx;
if ( archive ) {
EXPECT_TRUE( archive->check() ) << ctx;
}
}
}

TEST(ZimArchive, openDontFallbackOnNonSplitZimArchive)
{
const char* fname = "wikibooks_be_all_nopic_2017-02.zim";

for (auto& testfile: getDataFilePath(fname)) {
const TestContext ctx{ {"path", testfile.path+"aa" } };
std::unique_ptr<zim::Archive> archive;
EXPECT_THROW( archive.reset(new zim::Archive(testfile.path+"aa")), zim::EntryNotFound) << ctx;
}
}

TEST(ZimArchive, randomEntry)
{
const char* const zimfiles[] = {
Expand Down Expand Up @@ -404,7 +429,7 @@ TEST(ZimArchive, validate)

TEST_BROKEN_ZIM_NAME(
"invalid.smaller_than_header.zim",
"zim-file is too small to contain a header\n"
"Zim file(s) is too small.\n"
);

TEST_BROKEN_ZIM_NAME(
Expand Down Expand Up @@ -434,7 +459,7 @@ TEST(ZimArchive, validate)

TEST_BROKEN_ZIM_NAME(
"invalid.invalid_checksumpos.zim",
"Checksum position is not valid\n"
"Zim file(s) is too small.\n"
);

TEST_BROKEN_ZIM_NAME(
Expand Down

0 comments on commit 452bb07

Please sign in to comment.