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(); } size_t length() const { return impl_->length(); }
compression_type compression() const { return impl_->compression(); } compression_type compression() const { return impl_->compression(); }
section_type type() const { return impl_->type(); } section_type type() const { return impl_->type(); }
std::string name() const { return impl_->name(); }
std::string description() const { return impl_->description(); } std::string description() const { return impl_->description(); }
bool check_fast(mmif& mm) const { return impl_->check_fast(mm); } bool check_fast(mmif& mm) const { return impl_->check_fast(mm); }
bool verify(mmif& mm) const { return impl_->verify(mm); } bool verify(mmif& mm) const { return impl_->verify(mm); }
@ -52,6 +53,7 @@ class fs_section {
virtual size_t length() const = 0; virtual size_t length() const = 0;
virtual compression_type compression() const = 0; virtual compression_type compression() const = 0;
virtual section_type type() const = 0; virtual section_type type() const = 0;
virtual std::string name() const = 0;
virtual std::string description() const = 0; virtual std::string description() const = 0;
virtual bool check_fast(mmif& mm) const = 0; virtual bool check_fast(mmif& mm) const = 0;
virtual bool verify(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); cache.insert(*s);
} else { } else {
if (!s->check_fast(*mm_)) { if (!s->check_fast(*mm_)) {
DWARFS_THROW(runtime_error, "checksum error in section: " + DWARFS_THROW(runtime_error, "checksum error in section: " + s->name());
get_section_name(s->type()));
} }
if (!sections.emplace(s->type(), *s).second) { if (!sections.emplace(s->type(), *s).second) {
DWARFS_THROW(runtime_error, DWARFS_THROW(runtime_error, "duplicate section: " + s->name());
"duplicate section: " + get_section_name(s->type()));
} }
} }
} }
@ -394,19 +392,17 @@ void filesystem_v2::rewrite(logger& lgr, progress& prog,
while (auto s = parser.next_section()) { while (auto s = parser.next_section()) {
if (!s->check_fast(*mm)) { if (!s->check_fast(*mm)) {
DWARFS_THROW(runtime_error, DWARFS_THROW(runtime_error, "checksum error in section: " + s->name());
"checksum error in section: " + get_section_name(s->type()));
} }
if (!s->verify(*mm)) { if (!s->verify(*mm)) {
DWARFS_THROW(runtime_error, "integrity check error in section: " + DWARFS_THROW(runtime_error,
get_section_name(s->type())); "integrity check error in section: " + s->name());
} }
if (s->type() == section_type::BLOCK) { if (s->type() == section_type::BLOCK) {
++prog.block_count; ++prog.block_count;
} else { } else {
if (!sections.emplace(s->type(), *s).second) { if (!sections.emplace(s->type(), *s).second) {
DWARFS_THROW(runtime_error, DWARFS_THROW(runtime_error, "duplicate section: " + s->name());
"duplicate section: " + get_section_name(s->type()));
} }
} }
} }
@ -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 // TODO: don't throw if we're just checking the file system
if (!s->check_fast(*mm)) { if (!s->check_fast(*mm)) {
DWARFS_THROW(runtime_error, DWARFS_THROW(runtime_error, "checksum error in section: " + s->name());
"checksum error in section: " + get_section_name(s->type()));
} }
if (!s->verify(*mm)) { if (!s->verify(*mm)) {
DWARFS_THROW(runtime_error, "integrity check error in section: " + DWARFS_THROW(runtime_error,
get_section_name(s->type())); "integrity check error in section: " + s->name());
} }
if (s->type() != section_type::BLOCK) { if (s->type() != section_type::BLOCK) {
if (!sections.emplace(s->type(), *s).second) { if (!sections.emplace(s->type(), *s).second) {
DWARFS_THROW(runtime_error, DWARFS_THROW(runtime_error, "duplicate section: " + s->name());
"duplicate section: " + get_section_name(s->type()));
} }
} }
} }

View File

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