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 c2d144a commit e028dc5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
39 changes: 30 additions & 9 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,43 @@ class Grouping
GroupId maxGroupId_;
};

class BadSizedFile: public std::exception {};

} //unnamed namespace

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) {
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(BadSizedFile& 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 of bad size or corrupted.");
}
}
}

// 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 (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 e028dc5

Please sign in to comment.