From f54ac8d50e9ab6e80ef19500f56239e6ee5c4fc4 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Fri, 11 Aug 2023 23:24:12 +0200 Subject: [PATCH] add fragment_chunkable --- CMakeLists.txt | 1 + include/dwarfs/fragment_chunkable.h | 56 +++++++++++++++++++++++++ include/dwarfs/inode_fragments.h | 8 ++++ src/dwarfs/fragment_chunkable.cpp | 65 +++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 include/dwarfs/fragment_chunkable.h create mode 100644 src/dwarfs/fragment_chunkable.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 81afec12..87564129 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -373,6 +373,7 @@ list( src/dwarfs/filesystem_extractor.cpp src/dwarfs/filesystem_v2.cpp src/dwarfs/filesystem_writer.cpp + src/dwarfs/fragment_chunkable.cpp src/dwarfs/fragment_order_parser.cpp src/dwarfs/fstypes.cpp src/dwarfs/fs_section.cpp diff --git a/include/dwarfs/fragment_chunkable.h b/include/dwarfs/fragment_chunkable.h new file mode 100644 index 00000000..0c28984e --- /dev/null +++ b/include/dwarfs/fragment_chunkable.h @@ -0,0 +1,56 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#pragma once + +#include + +#include "dwarfs/chunkable.h" + +namespace dwarfs { + +class categorizer_manager; +class inode; +class mmif; +class single_inode_fragment; + +class fragment_chunkable : public chunkable { + public: + fragment_chunkable(inode const& ino, single_inode_fragment& frag, + file_off_t offset, mmif& mm, + categorizer_manager const& catmgr); + ~fragment_chunkable(); + + size_t size() const override; + std::string description() const override; + std::span span() const override; + void add_chunk(size_t block, size_t offset, size_t size) override; + void release_until(size_t offset) override; + + private: + inode const& ino_; + single_inode_fragment& frag_; + file_off_t offset_; + mmif& mm_; + categorizer_manager const& catmgr_; +}; + +} // namespace dwarfs diff --git a/include/dwarfs/inode_fragments.h b/include/dwarfs/inode_fragments.h index c00a473f..a11565a1 100644 --- a/include/dwarfs/inode_fragments.h +++ b/include/dwarfs/inode_fragments.h @@ -45,6 +45,14 @@ class single_inode_fragment { file_off_t length() const { return length_; } file_off_t size() const { return length_; } + void add_chunk(size_t block, size_t offset, size_t size) { + thrift::metadata::chunk c; + c.block() = block; + c.offset() = offset; + c.size() = size; + chunks_.push_back(c); + } + std::span chunks() const { return chunks_; } void extend(file_off_t length) { length_ += length; } diff --git a/src/dwarfs/fragment_chunkable.cpp b/src/dwarfs/fragment_chunkable.cpp new file mode 100644 index 00000000..5735cbf0 --- /dev/null +++ b/src/dwarfs/fragment_chunkable.cpp @@ -0,0 +1,65 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#include + +#include "dwarfs/categorizer.h" +#include "dwarfs/entry.h" +#include "dwarfs/fragment_chunkable.h" +#include "dwarfs/inode.h" +#include "dwarfs/inode_fragments.h" +#include "dwarfs/mmif.h" + +namespace dwarfs { + +fragment_chunkable::fragment_chunkable(inode const& ino, + single_inode_fragment& frag, + file_off_t offset, mmif& mm, + categorizer_manager const& catmgr) + : ino_{ino} + , frag_{frag} + , offset_{offset} + , mm_{mm} + , catmgr_{catmgr} {} + +fragment_chunkable::~fragment_chunkable() = default; + +size_t fragment_chunkable::size() const { return frag_.size(); } + +std::string fragment_chunkable::description() const { + return fmt::format("{} fragment at offset {} of inode {} [{}] - size: {}", + catmgr_.category_name(frag_.category().value()), offset_, + ino_.num(), ino_.any()->name(), size()); +} + +std::span fragment_chunkable::span() const { + return mm_.span(offset_, frag_.size()); +} + +void fragment_chunkable::add_chunk(size_t block, size_t offset, size_t size) { + frag_.add_chunk(block, offset, size); +} + +void fragment_chunkable::release_until(size_t offset) { + mm_.release_until(offset_ + offset); +} + +} // namespace dwarfs