diff --git a/CMakeLists.txt b/CMakeLists.txt index 2807d8c7..d7e310a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,7 @@ list( src/dwarfs/filesystem_v2.cpp src/dwarfs/filesystem_writer.cpp src/dwarfs/fstypes.cpp + src/dwarfs/global_entry_data.cpp src/dwarfs/inode_manager.cpp src/dwarfs/inode_reader_v2.cpp src/dwarfs/logger.cpp diff --git a/include/dwarfs/entry.h b/include/dwarfs/entry.h index dd6faea1..6a4ee3ec 100644 --- a/include/dwarfs/entry.h +++ b/include/dwarfs/entry.h @@ -38,91 +38,13 @@ namespace dwarfs { -// TODO: clean up -struct global_entry_data { - global_entry_data(bool no_time) - : no_time_(no_time) {} - - void add_uid(uint16_t uid) { add(uid, uids, next_uid_index); } - - void add_gid(uint16_t gid) { add(gid, gids, next_gid_index); } - - void add_mode(uint16_t mode) { add(mode, modes, next_mode_index); } - - void add(uint16_t val, std::unordered_map& map, - uint16_t& next_index) { - if (map.emplace(val, next_index).second) { - ++next_index; - } - } - - void add_time(uint64_t time) { - if (time < timestamp_base) { - timestamp_base = time; - } - } - - void add_name(std::string const& name) { names.emplace(name, 0); } - - void add_link(std::string const& link) { links.emplace(link, 0); } - - void index() { - index(names); - index(links); - } - - void index(std::unordered_map& map); - - uint16_t get_uid_index(uint16_t uid) const { return uids.at(uid); } - - uint16_t get_gid_index(uint16_t gid) const { return gids.at(gid); } - - uint16_t get_mode_index(uint16_t mode) const { return modes.at(mode); } - - uint32_t get_name_index(std::string const& name) const { - return names.at(name); - } - - uint32_t get_link_index(std::string const& link) const { - return links.at(link); - } - - uint64_t get_time_offset(uint64_t time) const { - return no_time_ ? 0 : time - timestamp_base; - } - - std::vector get_uids() const; - - std::vector get_gids() const; - - std::vector get_modes() const; - - std::vector get_names() const; - - std::vector get_links() const; - - // TODO: make private - template - std::vector get_vector(std::unordered_map const& map) const; - - std::unordered_map uids; - std::unordered_map gids; - std::unordered_map modes; - std::unordered_map names; - std::unordered_map links; - uint16_t next_uid_index{0}; - uint16_t next_gid_index{0}; - uint16_t next_mode_index{0}; - uint64_t timestamp_base{std::numeric_limits::max()}; - bool no_time_; -}; - class file; class link; class dir; class inode; class os_access; class progress; +class global_entry_data; class entry_visitor { public: @@ -143,7 +65,6 @@ class entry : public file_interface { bool has_parent() const; std::shared_ptr parent() const; void set_name(const std::string& name); - void set_name_offset(size_t offset); std::string path() const override; const std::string& name() const override { return name_; } size_t size() const override { return stat_.st_size; } @@ -165,7 +86,6 @@ class entry : public file_interface { std::string name_; std::weak_ptr parent_; struct ::stat stat_; - uint32_t name_offset_; }; class file : public entry { @@ -204,7 +124,6 @@ class dir : public entry { void walk(std::function const& f) const override; void accept(entry_visitor& v, bool preorder) override; void sort(); - void set_offset(size_t offset); void set_inode(uint32_t inode); void pack(thrift::metadata::metadata& mv2, global_entry_data const& data) const; @@ -218,7 +137,6 @@ class dir : public entry { using entry_ptr = std::shared_ptr; std::vector> entries_; - uint32_t offset_ = 0; uint32_t inode_ = 0; }; @@ -228,7 +146,6 @@ class link : public entry { type_t type() const override; const std::string& linkname() const; - void set_offset(size_t offset); void set_inode(uint32_t inode); void accept(entry_visitor& v, bool preorder) override; uint32_t inode_num() const override { return inode_; } @@ -238,7 +155,6 @@ class link : public entry { private: std::string link_; - uint32_t offset_ = 0; uint32_t inode_ = 0; }; diff --git a/include/dwarfs/global_entry_data.h b/include/dwarfs/global_entry_data.h new file mode 100644 index 00000000..d8f53bb9 --- /dev/null +++ b/include/dwarfs/global_entry_data.h @@ -0,0 +1,111 @@ +/* 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 +#include +#include + +namespace dwarfs { + +// TODO: clean up +class global_entry_data { + public: + global_entry_data(bool no_time) + : no_time_(no_time) {} + + void add_uid(uint16_t uid) { add(uid, uids, next_uid_index); } + + void add_gid(uint16_t gid) { add(gid, gids, next_gid_index); } + + void add_mode(uint16_t mode) { add(mode, modes, next_mode_index); } + + void add(uint16_t val, std::unordered_map& map, + uint16_t& next_index) { + if (map.emplace(val, next_index).second) { + ++next_index; + } + } + + void add_time(uint64_t time) { + if (time < timestamp_base) { + timestamp_base = time; + } + } + + void add_name(std::string const& name) { names.emplace(name, 0); } + + void add_link(std::string const& link) { links.emplace(link, 0); } + + void index() { + index(names); + index(links); + } + + void index(std::unordered_map& map); + + uint16_t get_uid_index(uint16_t uid) const { return uids.at(uid); } + + uint16_t get_gid_index(uint16_t gid) const { return gids.at(gid); } + + uint16_t get_mode_index(uint16_t mode) const { return modes.at(mode); } + + uint32_t get_name_index(std::string const& name) const { + return names.at(name); + } + + uint32_t get_link_index(std::string const& link) const { + return links.at(link); + } + + uint64_t get_time_offset(uint64_t time) const { + return no_time_ ? 0 : time - timestamp_base; + } + + std::vector get_uids() const; + + std::vector get_gids() const; + + std::vector get_modes() const; + + std::vector get_names() const; + + std::vector get_links() const; + + // TODO: make private + template + std::vector get_vector(std::unordered_map const& map) const; + + std::unordered_map uids; + std::unordered_map gids; + std::unordered_map modes; + std::unordered_map names; + std::unordered_map links; + uint16_t next_uid_index{0}; + uint16_t next_gid_index{0}; + uint16_t next_mode_index{0}; + uint64_t timestamp_base{std::numeric_limits::max()}; + bool no_time_; +}; + +} // namespace dwarfs diff --git a/src/dwarfs/entry.cpp b/src/dwarfs/entry.cpp index 22d2e0c1..8b5526a5 100644 --- a/src/dwarfs/entry.cpp +++ b/src/dwarfs/entry.cpp @@ -27,12 +27,10 @@ #include #include -#include -#include - #include #include "dwarfs/entry.h" +#include "dwarfs/global_entry_data.h" #include "dwarfs/inode.h" #include "dwarfs/os_access.h" #include "dwarfs/progress.h" @@ -40,47 +38,11 @@ namespace dwarfs { -template -std::vector -global_entry_data::get_vector(std::unordered_map const& map) const { - using namespace folly::gen; - std::vector> pairs(map.begin(), map.end()); - return from(pairs) | orderBy([](auto const& p) { return p.second; }) | - get<0>() | as(); -} - -std::vector global_entry_data::get_uids() const { - return get_vector(uids); -} - -std::vector global_entry_data::get_gids() const { - return get_vector(gids); -} - -std::vector global_entry_data::get_modes() const { - return get_vector(modes); -} - -std::vector global_entry_data::get_names() const { - return get_vector(names); -} - -std::vector global_entry_data::get_links() const { - return get_vector(links); -} - -void global_entry_data::index(std::unordered_map& map) { - using namespace folly::gen; - uint32_t ix = 0; - from(map) | get<0>() | order | [&](std::string const& s) { map[s] = ix++; }; -} - entry::entry(const std::string& name, std::shared_ptr parent, const struct ::stat& st) : name_(name) , parent_(std::move(parent)) - , stat_(st) - , name_offset_(0) {} + , stat_(st) {} void entry::scan(os_access& os, progress& prog) { const std::string& p = path(); @@ -100,10 +62,6 @@ std::shared_ptr entry::parent() const { return parent_.lock(); } void entry::set_name(const std::string& name) { name_ = name; } -void entry::set_name_offset(size_t offset) { - name_offset_ = folly::to(offset); -} - std::string entry::path() const { if (auto parent = parent_.lock()) { return parent->path() + "/" + name_; @@ -243,8 +201,6 @@ void dir::sort() { }); } -void dir::set_offset(size_t offset) { offset_ = folly::to(offset); } - void dir::set_inode(uint32_t inode) { inode_ = inode; } void dir::scan(os_access&, const std::string&, progress&) {} @@ -275,8 +231,6 @@ entry::type_t link::type() const { return E_LINK; } const std::string& link::linkname() const { return link_; } -void link::set_offset(size_t offset) { offset_ = folly::to(offset); } - void link::set_inode(uint32_t inode) { inode_ = inode; } void link::accept(entry_visitor& v, bool) { v.visit(this); } diff --git a/src/dwarfs/global_entry_data.cpp b/src/dwarfs/global_entry_data.cpp new file mode 100644 index 00000000..3bfc283f --- /dev/null +++ b/src/dwarfs/global_entry_data.cpp @@ -0,0 +1,63 @@ +/* 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/global_entry_data.h" + +namespace dwarfs { + +template +std::vector +global_entry_data::get_vector(std::unordered_map const& map) const { + using namespace folly::gen; + std::vector> pairs(map.begin(), map.end()); + return from(pairs) | orderBy([](auto const& p) { return p.second; }) | + get<0>() | as(); +} + +std::vector global_entry_data::get_uids() const { + return get_vector(uids); +} + +std::vector global_entry_data::get_gids() const { + return get_vector(gids); +} + +std::vector global_entry_data::get_modes() const { + return get_vector(modes); +} + +std::vector global_entry_data::get_names() const { + return get_vector(names); +} + +std::vector global_entry_data::get_links() const { + return get_vector(links); +} + +void global_entry_data::index(std::unordered_map& map) { + using namespace folly::gen; + uint32_t ix = 0; + from(map) | get<0>() | order | [&](std::string const& s) { map[s] = ix++; }; +} + +} // namespace dwarfs diff --git a/src/dwarfs/scanner.cpp b/src/dwarfs/scanner.cpp index 0079c70b..85b3deb3 100644 --- a/src/dwarfs/scanner.cpp +++ b/src/dwarfs/scanner.cpp @@ -32,6 +32,7 @@ #include "dwarfs/entry.h" #include "dwarfs/filesystem_writer.h" #include "dwarfs/fstypes.h" +#include "dwarfs/global_entry_data.h" #include "dwarfs/hash_util.h" #include "dwarfs/inode_manager.h" #include "dwarfs/logger.h"