metadata_v2: factor out global_entry_data

This commit is contained in:
Marcus Holland-Moritz 2020-11-28 02:18:06 +01:00
parent c767b0b93e
commit 2340720730
6 changed files with 179 additions and 133 deletions

View File

@ -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

View File

@ -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<uint16_t, uint16_t>& 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<std::string, uint32_t>& 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<uint16_t> get_uids() const;
std::vector<uint16_t> get_gids() const;
std::vector<uint16_t> get_modes() const;
std::vector<std::string> get_names() const;
std::vector<std::string> get_links() const;
// TODO: make private
template <typename T, typename U>
std::vector<T> get_vector(std::unordered_map<T, U> const& map) const;
std::unordered_map<uint16_t, uint16_t> uids;
std::unordered_map<uint16_t, uint16_t> gids;
std::unordered_map<uint16_t, uint16_t> modes;
std::unordered_map<std::string, uint32_t> names;
std::unordered_map<std::string, uint32_t> 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<uint64_t>::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<entry> 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<entry> parent_;
struct ::stat stat_;
uint32_t name_offset_;
};
class file : public entry {
@ -204,7 +124,6 @@ class dir : public entry {
void walk(std::function<void(const entry*)> 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<entry>;
std::vector<std::shared_ptr<entry>> 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;
};

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
#include <limits>
#include <unordered_map>
#include <vector>
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<uint16_t, uint16_t>& 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<std::string, uint32_t>& 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<uint16_t> get_uids() const;
std::vector<uint16_t> get_gids() const;
std::vector<uint16_t> get_modes() const;
std::vector<std::string> get_names() const;
std::vector<std::string> get_links() const;
// TODO: make private
template <typename T, typename U>
std::vector<T> get_vector(std::unordered_map<T, U> const& map) const;
std::unordered_map<uint16_t, uint16_t> uids;
std::unordered_map<uint16_t, uint16_t> gids;
std::unordered_map<uint16_t, uint16_t> modes;
std::unordered_map<std::string, uint32_t> names;
std::unordered_map<std::string, uint32_t> 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<uint64_t>::max()};
bool no_time_;
};
} // namespace dwarfs

View File

@ -27,12 +27,10 @@
#include <sys/types.h>
#include <unistd.h>
#include <folly/Conv.h>
#include <folly/gen/Base.h>
#include <openssl/sha.h>
#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 <typename T, typename U>
std::vector<T>
global_entry_data::get_vector(std::unordered_map<T, U> const& map) const {
using namespace folly::gen;
std::vector<std::pair<T, U>> pairs(map.begin(), map.end());
return from(pairs) | orderBy([](auto const& p) { return p.second; }) |
get<0>() | as<std::vector>();
}
std::vector<uint16_t> global_entry_data::get_uids() const {
return get_vector(uids);
}
std::vector<uint16_t> global_entry_data::get_gids() const {
return get_vector(gids);
}
std::vector<uint16_t> global_entry_data::get_modes() const {
return get_vector(modes);
}
std::vector<std::string> global_entry_data::get_names() const {
return get_vector(names);
}
std::vector<std::string> global_entry_data::get_links() const {
return get_vector(links);
}
void global_entry_data::index(std::unordered_map<std::string, uint32_t>& 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<entry> 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> 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<uint32_t>(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<uint32_t>(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<uint32_t>(offset); }
void link::set_inode(uint32_t inode) { inode_ = inode; }
void link::accept(entry_visitor& v, bool) { v.visit(this); }

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <folly/gen/Base.h>
#include "dwarfs/global_entry_data.h"
namespace dwarfs {
template <typename T, typename U>
std::vector<T>
global_entry_data::get_vector(std::unordered_map<T, U> const& map) const {
using namespace folly::gen;
std::vector<std::pair<T, U>> pairs(map.begin(), map.end());
return from(pairs) | orderBy([](auto const& p) { return p.second; }) |
get<0>() | as<std::vector>();
}
std::vector<uint16_t> global_entry_data::get_uids() const {
return get_vector(uids);
}
std::vector<uint16_t> global_entry_data::get_gids() const {
return get_vector(gids);
}
std::vector<uint16_t> global_entry_data::get_modes() const {
return get_vector(modes);
}
std::vector<std::string> global_entry_data::get_names() const {
return get_vector(names);
}
std::vector<std::string> global_entry_data::get_links() const {
return get_vector(links);
}
void global_entry_data::index(std::unordered_map<std::string, uint32_t>& map) {
using namespace folly::gen;
uint32_t ix = 0;
from(map) | get<0>() | order | [&](std::string const& s) { map[s] = ix++; };
}
} // namespace dwarfs

View File

@ -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"