Simplify filesystem code

This commit is contained in:
Marcus Holland-Moritz 2020-12-18 00:01:13 +01:00
parent a05f9e4bce
commit 8328ccd048
3 changed files with 16 additions and 16 deletions

View File

@ -38,6 +38,7 @@ class fs_section {
size_t length() const { return impl_->length(); }
compression_type compression() const { return impl_->compression(); }
section_type type() const { return impl_->type(); }
std::string name() const { return impl_->name(); }
std::string description() const { return impl_->description(); }
bool check_fast(mmif& mm) const { return impl_->check_fast(mm); }
bool verify(mmif& mm) const { return impl_->verify(mm); }
@ -52,6 +53,7 @@ class fs_section {
virtual size_t length() const = 0;
virtual compression_type compression() const = 0;
virtual section_type type() const = 0;
virtual std::string name() const = 0;
virtual std::string description() const = 0;
virtual bool check_fast(mmif& mm) const = 0;
virtual bool verify(mmif& mm) const = 0;

View File

@ -229,13 +229,11 @@ filesystem_<LoggerPolicy>::filesystem_(logger& lgr, std::shared_ptr<mmif> mm,
cache.insert(*s);
} else {
if (!s->check_fast(*mm_)) {
DWARFS_THROW(runtime_error, "checksum error in section: " +
get_section_name(s->type()));
DWARFS_THROW(runtime_error, "checksum error in section: " + s->name());
}
if (!sections.emplace(s->type(), *s).second) {
DWARFS_THROW(runtime_error,
"duplicate section: " + get_section_name(s->type()));
DWARFS_THROW(runtime_error, "duplicate section: " + s->name());
}
}
}
@ -394,19 +392,17 @@ void filesystem_v2::rewrite(logger& lgr, progress& prog,
while (auto s = parser.next_section()) {
if (!s->check_fast(*mm)) {
DWARFS_THROW(runtime_error,
"checksum error in section: " + get_section_name(s->type()));
DWARFS_THROW(runtime_error, "checksum error in section: " + s->name());
}
if (!s->verify(*mm)) {
DWARFS_THROW(runtime_error, "integrity check error in section: " +
get_section_name(s->type()));
DWARFS_THROW(runtime_error,
"integrity check error in section: " + s->name());
}
if (s->type() == section_type::BLOCK) {
++prog.block_count;
} else {
if (!sections.emplace(s->type(), *s).second) {
DWARFS_THROW(runtime_error,
"duplicate section: " + get_section_name(s->type()));
DWARFS_THROW(runtime_error, "duplicate section: " + s->name());
}
}
}
@ -462,17 +458,15 @@ void filesystem_v2::identify(logger& lgr, std::shared_ptr<mmif> mm,
// TODO: don't throw if we're just checking the file system
if (!s->check_fast(*mm)) {
DWARFS_THROW(runtime_error,
"checksum error in section: " + get_section_name(s->type()));
DWARFS_THROW(runtime_error, "checksum error in section: " + s->name());
}
if (!s->verify(*mm)) {
DWARFS_THROW(runtime_error, "integrity check error in section: " +
get_section_name(s->type()));
DWARFS_THROW(runtime_error,
"integrity check error in section: " + s->name());
}
if (s->type() != section_type::BLOCK) {
if (!sections.emplace(s->type(), *s).second) {
DWARFS_THROW(runtime_error,
"duplicate section: " + get_section_name(s->type()));
DWARFS_THROW(runtime_error, "duplicate section: " + s->name());
}
}
}

View File

@ -38,6 +38,7 @@ class fs_section_v1 : public fs_section::impl {
size_t length() const override { return hdr_.length; }
compression_type compression() const override { return hdr_.compression; }
section_type type() const override { return hdr_.type; }
std::string name() const override { return get_section_name(hdr_.type); }
std::string description() const override { return hdr_.to_string(); }
bool check_fast(mmif&) const override { return true; }
bool verify(mmif&) const override { return true; }
@ -59,6 +60,9 @@ class fs_section_v2 : public fs_section::impl {
section_type type() const override {
return static_cast<section_type>(hdr_.type);
}
std::string name() const override {
return get_section_name(static_cast<section_type>(hdr_.type));
}
std::string description() const override { return hdr_.to_string(); }
bool check_fast(mmif& mm) const override {
auto hdr_cs_len =