Skip to content

Commit

Permalink
Move creation of FileCompound into two different constructors.
Browse files Browse the repository at this point in the history
One is for simple file part only, the other is for multi part only.

We also introduce a static helper function `openSinglePieceOrSplitFile`
to build the FileCompound as before.
This helper function is put in FileImpl which can seem a bit odd but will
simplify next commits.
  • Loading branch information
mgautierfr committed Apr 30, 2024
1 parent 51b8a4d commit 926d862
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
45 changes: 28 additions & 17 deletions src/file_compound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "file_compound.h"

#include <errno.h>
#include <stdexcept>
#include <string.h>
#include <sys/stat.h>
#include <zim/tools.h>
Expand All @@ -40,30 +41,40 @@ void FileCompound::addPart(FilePart* fpart)
_fsize += fpart->size();
}

std::shared_ptr<FileCompound> FileCompound::openSinglePieceOrSplitZimFile(const std::string& filename) {
try {
return std::make_shared<FileCompound>(filename);
} catch (...) {
return std::make_shared<FileCompound>(filename, FileCompound::MultiPartToken::Multi);
}
}

FileCompound::FileCompound(const std::string& filename):
_filename(filename),
_fsize(0)
{
addPart(new FilePart(filename));
}

FileCompound::FileCompound(const std::string& base_filename, MultiPartToken _token):
_filename(base_filename),
_fsize(0)
{
try {
addPart(new FilePart(filename));
} catch(...) {
int errnoSave = errno;
_fsize = zsize_t(0);
try {
for (char ch0 = 'a'; ch0 <= 'z'; ++ch0)
for (char ch0 = 'a'; ch0 <= 'z'; ++ch0)
{
const std::string fname0 = base_filename + ch0;
for (char ch1 = 'a'; ch1 <= 'z'; ++ch1)
{
const std::string fname0 = filename + ch0;
for (char ch1 = 'a'; ch1 <= 'z'; ++ch1)
{
addPart(new FilePart(fname0 + ch1));
}
addPart(new FilePart(fname0 + ch1));
}
} catch (...) { }

if (empty())
throw std::runtime_error(Formatter()
<< "error " << errnoSave << " opening file \""
<< filename << "\"");
}
} catch (std::runtime_error& e) {
// This catch acts as a break for the double loop.
}
if (empty()) {
// We haven't found any part
throw std::runtime_error(Formatter() << "Error opening as a split file: " << base_filename);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/file_compound.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "zim_types.h"
#include "debug.h"
#include <map>
#include <memory>
#include <vector>

namespace zim {
Expand Down Expand Up @@ -53,9 +54,12 @@ class FileCompound : private std::map<Range, FilePart*, less_range> {
public: // types
typedef const_iterator PartIterator;
typedef std::pair<PartIterator, PartIterator> PartRange;
enum class MultiPartToken { Multi };

public: // functions
static std::shared_ptr<FileCompound> openSinglePieceOrSplitZimFile(const std::string& filename);
explicit FileCompound(const std::string& filename);
explicit FileCompound(const std::string& filename, MultiPartToken token);

#ifndef _WIN32
explicit FileCompound(int fd);
Expand Down
2 changes: 1 addition & 1 deletion src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class Grouping
// FileImpl
//
FileImpl::FileImpl(const std::string& fname)
: FileImpl(std::make_shared<FileCompound>(fname))
: FileImpl(FileCompound::openSinglePieceOrSplitZimFile(fname))
{}

#ifndef _WIN32
Expand Down

0 comments on commit 926d862

Please sign in to comment.