Skip to content

Commit

Permalink
Update UIDs/GIDs/mode to 32-bit types (fixes gh #173)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Oct 16, 2023
1 parent 67496b3 commit c0f73ca
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 56 deletions.
12 changes: 6 additions & 6 deletions include/dwarfs/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ class entry : public entry_interface {
virtual std::optional<uint32_t> const& inode_num() const = 0;

// more methods from entry_interface
uint16_t get_permissions() const override;
void set_permissions(uint16_t perm) override;
uint16_t get_uid() const override;
void set_uid(uint16_t uid) override;
uint16_t get_gid() const override;
void set_gid(uint16_t gid) override;
mode_type get_permissions() const override;
void set_permissions(mode_type perm) override;
uid_type get_uid() const override;
void set_uid(uid_type uid) override;
gid_type get_gid() const override;
void set_gid(gid_type gid) override;
uint64_t get_atime() const override;
void set_atime(uint64_t atime) override;
uint64_t get_mtime() const override;
Expand Down
17 changes: 11 additions & 6 deletions include/dwarfs/entry_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@

#include <string>

#include "dwarfs/file_stat.h"
#include "dwarfs/object.h"

namespace dwarfs {

class entry_interface : public object {
public:
using uid_type = file_stat::uid_type;
using gid_type = file_stat::gid_type;
using mode_type = file_stat::mode_type;

virtual std::string path_as_string() const = 0;
virtual std::string dpath() const = 0;
virtual std::string unix_dpath() const = 0;
Expand All @@ -37,12 +42,12 @@ class entry_interface : public object {
virtual size_t size() const = 0;
virtual bool is_directory() const = 0;

virtual uint16_t get_permissions() const = 0;
virtual void set_permissions(uint16_t perm) = 0;
virtual uint16_t get_uid() const = 0;
virtual void set_uid(uint16_t uid) = 0;
virtual uint16_t get_gid() const = 0;
virtual void set_gid(uint16_t gid) = 0;
virtual mode_type get_permissions() const = 0;
virtual void set_permissions(mode_type perm) = 0;
virtual uid_type get_uid() const = 0;
virtual void set_uid(uid_type uid) = 0;
virtual gid_type get_gid() const = 0;
virtual void set_gid(gid_type gid) = 0;
virtual uint64_t get_atime() const = 0;
virtual void set_atime(uint64_t atime) = 0;
virtual uint64_t get_mtime() const = 0;
Expand Down
40 changes: 23 additions & 17 deletions include/dwarfs/global_entry_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,27 @@

#include <folly/container/F14Map.h>

#include "dwarfs/file_stat.h"

namespace dwarfs {

struct scanner_options;

class global_entry_data {
public:
using uid_type = file_stat::uid_type;
using gid_type = file_stat::gid_type;
using mode_type = file_stat::mode_type;

enum class timestamp_type { ATIME, MTIME, CTIME };

global_entry_data(scanner_options const& options)
: options_(options) {}

void add_uid(uint16_t uid);
void add_gid(uint16_t gid);
void add_uid(uid_type uid);
void add_gid(gid_type gid);

void add_mode(uint16_t mode) { add(mode, modes_, next_mode_index_); }
void add_mode(mode_type mode) { add(mode, modes_, next_mode_index_); }

void add_mtime(uint64_t time);
void add_atime(uint64_t time);
Expand All @@ -56,9 +62,9 @@ class global_entry_data {
index(symlinks_);
}

uint16_t get_uid_index(uint16_t uid) const;
uint16_t get_gid_index(uint16_t gid) const;
uint16_t get_mode_index(uint16_t mode) const;
size_t get_uid_index(uid_type uid) const;
size_t get_gid_index(gid_type gid) const;
size_t get_mode_index(mode_type mode) const;

uint32_t get_name_index(std::string const& name) const;
uint32_t get_symlink_table_entry(std::string const& link) const;
Expand All @@ -67,9 +73,9 @@ class global_entry_data {
uint64_t get_atime_offset(uint64_t time) const;
uint64_t get_ctime_offset(uint64_t time) const;

std::vector<uint16_t> get_uids() const;
std::vector<uint16_t> get_gids() const;
std::vector<uint16_t> get_modes() const;
std::vector<uid_type> get_uids() const;
std::vector<gid_type> get_gids() const;
std::vector<mode_type> get_modes() const;

std::vector<std::string> get_names() const;
std::vector<std::string> get_symlinks() const;
Expand All @@ -85,23 +91,23 @@ class global_entry_data {

static void index(map_type<std::string, uint32_t>& map);

void
add(uint16_t val, map_type<uint16_t, uint16_t>& map, uint16_t& next_index) {
template <typename T>
void add(T val, map_type<T, T>& map, T& next_index) {
if (map.emplace(val, next_index).second) {
++next_index;
}
}

uint64_t get_time_offset(uint64_t time) const;

map_type<uint16_t, uint16_t> uids_;
map_type<uint16_t, uint16_t> gids_;
map_type<uint16_t, uint16_t> modes_;
map_type<uid_type, uid_type> uids_;
map_type<gid_type, gid_type> gids_;
map_type<mode_type, mode_type> modes_;
map_type<std::string, uint32_t> names_;
map_type<std::string, uint32_t> symlinks_;
uint16_t next_uid_index_{0};
uint16_t next_gid_index_{0};
uint16_t next_mode_index_{0};
uid_type next_uid_index_{0};
gid_type next_gid_index_{0};
mode_type next_mode_index_{0};
uint64_t timestamp_base_{std::numeric_limits<uint64_t>::max()};
scanner_options const& options_;
};
Expand Down
11 changes: 8 additions & 3 deletions include/dwarfs/metadata_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <thrift/lib/cpp2/frozen/FrozenUtil.h>

#include "dwarfs/file_stat.h"
#include "dwarfs/file_type.h"
#include "dwarfs/string_table.h"

Expand Down Expand Up @@ -90,15 +91,19 @@ class inode_view
friend class dir_entry_view;

public:
uint16_t mode() const;
using uid_type = file_stat::uid_type;
using gid_type = file_stat::gid_type;
using mode_type = file_stat::mode_type;

mode_type mode() const;
posix_file_type::value type() const {
return posix_file_type::from_mode(mode());
}
bool is_regular_file() const { return type() == posix_file_type::regular; }
bool is_directory() const { return type() == posix_file_type::directory; }
bool is_symlink() const { return type() == posix_file_type::symlink; }
uint16_t getuid() const;
uint16_t getgid() const;
uid_type getuid() const;
gid_type getgid() const;
uint32_t inode_num() const { return inode_num_; }

private:
Expand Down
5 changes: 3 additions & 2 deletions include/dwarfs/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <iosfwd>
#include <optional>

#include "dwarfs/file_stat.h"
#include "dwarfs/types.h"

namespace dwarfs {
Expand Down Expand Up @@ -97,8 +98,8 @@ struct file_order_options {
struct scanner_options {
file_order_options file_order;
std::optional<std::string> file_hash_algorithm{"xxh3-128"};
std::optional<uint16_t> uid;
std::optional<uint16_t> gid;
std::optional<file_stat::uid_type> uid;
std::optional<file_stat::gid_type> gid;
std::optional<uint64_t> timestamp;
bool keep_all_times{false};
bool remove_empty_dirs{false};
Expand Down
12 changes: 6 additions & 6 deletions src/dwarfs/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ void entry::pack(thrift::metadata::inode_data& entry_v2,

entry::type_t file::type() const { return E_FILE; }

uint16_t entry::get_permissions() const { return stat_.permissions(); }
auto entry::get_permissions() const -> mode_type { return stat_.permissions(); }

void entry::set_permissions(uint16_t perm) { stat_.set_permissions(perm); }
void entry::set_permissions(mode_type perm) { stat_.set_permissions(perm); }

uint16_t entry::get_uid() const { return stat_.uid; }
auto entry::get_uid() const -> uid_type { return stat_.uid; }

void entry::set_uid(uint16_t uid) { stat_.uid = uid; }
void entry::set_uid(uid_type uid) { stat_.uid = uid; }

uint16_t entry::get_gid() const { return stat_.gid; }
auto entry::get_gid() const -> gid_type { return stat_.gid; }

void entry::set_gid(uint16_t gid) { stat_.gid = gid; }
void entry::set_gid(gid_type gid) { stat_.gid = gid; }

uint64_t entry::get_atime() const { return stat_.atime; }

Expand Down
20 changes: 10 additions & 10 deletions src/dwarfs/global_entry_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ std::vector<T> global_entry_data::get_vector(map_type<T, U> const& map) const {
get<0>() | as<std::vector>();
}

std::vector<uint16_t> global_entry_data::get_uids() const {
auto global_entry_data::get_uids() const -> std::vector<uid_type> {
return get_vector(uids_);
}

std::vector<uint16_t> global_entry_data::get_gids() const {
auto global_entry_data::get_gids() const -> std::vector<gid_type> {
return get_vector(gids_);
}

std::vector<uint16_t> global_entry_data::get_modes() const {
auto global_entry_data::get_modes() const -> std::vector<mode_type> {
return get_vector(modes_);
}

std::vector<std::string> global_entry_data::get_names() const {
auto global_entry_data::get_names() const -> std::vector<std::string> {
return get_vector(names_);
}

std::vector<std::string> global_entry_data::get_symlinks() const {
auto global_entry_data::get_symlinks() const -> std::vector<std::string> {
return get_vector(symlinks_);
}

Expand Down Expand Up @@ -84,15 +84,15 @@ uint64_t global_entry_data::get_timestamp_base() const {
options_.time_resolution_sec;
}

uint16_t global_entry_data::get_uid_index(uint16_t uid) const {
size_t global_entry_data::get_uid_index(uid_type uid) const {
return options_.uid ? *options_.uid : DWARFS_NOTHROW(uids_.at(uid));
}

uint16_t global_entry_data::get_gid_index(uint16_t gid) const {
size_t global_entry_data::get_gid_index(gid_type gid) const {
return options_.gid ? *options_.gid : DWARFS_NOTHROW(gids_.at(gid));
}

uint16_t global_entry_data::get_mode_index(uint16_t mode) const {
size_t global_entry_data::get_mode_index(mode_type mode) const {
return DWARFS_NOTHROW(modes_.at(mode));
}

Expand All @@ -105,13 +105,13 @@ global_entry_data::get_symlink_table_entry(std::string const& link) const {
return DWARFS_NOTHROW(symlinks_.at(link));
}

void global_entry_data::add_uid(uint16_t uid) {
void global_entry_data::add_uid(uid_type uid) {
if (!options_.uid) {
add(uid, uids_, next_uid_index_);
}
}

void global_entry_data::add_gid(uint16_t gid) {
void global_entry_data::add_gid(gid_type gid) {
if (!options_.gid) {
add(gid, gids_, next_gid_index_);
}
Expand Down
12 changes: 9 additions & 3 deletions src/dwarfs/metadata_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,17 @@ uint32_t global_metadata::parent_dir_entry(uint32_t ino) const {
: meta_->directories()[ino].parent_entry();
}

uint16_t inode_view::mode() const { return meta_->modes()[mode_index()]; }
auto inode_view::mode() const -> mode_type {
return meta_->modes()[mode_index()];
}

uint16_t inode_view::getuid() const { return meta_->uids()[owner_index()]; }
auto inode_view::getuid() const -> uid_type {
return meta_->uids()[owner_index()];
}

uint16_t inode_view::getgid() const { return meta_->gids()[group_index()]; }
auto inode_view::getgid() const -> gid_type {
return meta_->gids()[group_index()];
}

// TODO: pretty certain some of this stuff can be simplified

Expand Down
6 changes: 3 additions & 3 deletions thrift/metadata.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ struct metadata {
6: list<UInt32> symlink_table

// user ids, for lookup by `inode.owner_index`
7: list<UInt16> uids
7: list<UInt32> uids

// group ids, for lookup by `inode.group_index`
8: list<UInt16> gids
8: list<UInt32> gids

// inode modes, for lookup by `inode.mode_index`
9: list<UInt16> modes
9: list<UInt32> modes

// directory entry names, for lookup by `dir_entry.name_index`
10: list<string> names
Expand Down

0 comments on commit c0f73ca

Please sign in to comment.