Skip to content

Commit

Permalink
Catch internal BadSizedFile 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 30, 2024
1 parent 87623d3 commit 6348ae4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
26 changes: 18 additions & 8 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,25 @@ class BadSizedFile: public std::exception {};
//////////////////////////////////////////////////////////////////////
// FileImpl
//
std::shared_ptr<FileImpl> FileImpl::openSinglePieceOrSplitZimFile(const std::string& filename) {
std::shared_ptr<FileImpl> FileImpl::openSinglePieceOrSplitZimFile(std::string fname) {
std::shared_ptr<FileCompound> fileCompound;
if (fname.size() > 6 && fname.substr(fname.size()-6) == ".zimaa") {
fname.resize(fname.size()-2);
} else {
try {
fileCompound = std::make_shared<FileCompound>(fname);
} catch(...) { }
}

if ( !fileCompound ) {
fileCompound = std::make_shared<FileCompound>(fname, FileCompound::MultiPartToken::Multi);
}

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))
);
return std::shared_ptr<FileImpl>(new FileImpl(fileCompound));
} catch (BadSizedFile& e) {
// BadSizedFile is internal error.
throw ZimFileFormatError("Zim file(s) is of bad size or corrupted.");
}
}

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> openSinglePieceOrSplitZimFile(const std::string& fname);
static std::shared_ptr<FileImpl> openSinglePieceOrSplitZimFile(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")), std::runtime_error) << 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 of bad size or corrupted.\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 of bad size or corrupted.\n"
);

TEST_BROKEN_ZIM_NAME(
Expand Down

0 comments on commit 6348ae4

Please sign in to comment.