mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-09 04:19:10 -04:00
refactor: move mode string implementation to file_stat module
This commit is contained in:
parent
14038dcfe7
commit
064c7c74a6
@ -66,6 +66,9 @@ struct file_stat {
|
||||
return t == posix_file_type::block || t == posix_file_type::character;
|
||||
}
|
||||
|
||||
static std::string perm_string(mode_type mode);
|
||||
static std::string mode_string(mode_type mode);
|
||||
|
||||
dev_type dev;
|
||||
ino_type ino;
|
||||
nlink_type nlink;
|
||||
|
@ -61,6 +61,40 @@ uint64_t time_from_filetime(FILETIME const& ft) {
|
||||
|
||||
#endif
|
||||
|
||||
char get_filetype_label(file_stat::mode_type mode) {
|
||||
switch (posix_file_type::from_mode(mode)) {
|
||||
case posix_file_type::regular:
|
||||
return '-';
|
||||
case posix_file_type::directory:
|
||||
return 'd';
|
||||
case posix_file_type::symlink:
|
||||
return 'l';
|
||||
case posix_file_type::block:
|
||||
return 'b';
|
||||
case posix_file_type::character:
|
||||
return 'c';
|
||||
case posix_file_type::fifo:
|
||||
return 'p';
|
||||
case posix_file_type::socket:
|
||||
return 's';
|
||||
default:
|
||||
DWARFS_THROW(runtime_error,
|
||||
fmt::format("unknown file type: {:#06x}", mode));
|
||||
}
|
||||
}
|
||||
|
||||
void perms_to_stream(std::ostream& os, file_stat::mode_type mode) {
|
||||
os << (mode & file_stat::mode_type(fs::perms::owner_read) ? 'r' : '-');
|
||||
os << (mode & file_stat::mode_type(fs::perms::owner_write) ? 'w' : '-');
|
||||
os << (mode & file_stat::mode_type(fs::perms::owner_exec) ? 'x' : '-');
|
||||
os << (mode & file_stat::mode_type(fs::perms::group_read) ? 'r' : '-');
|
||||
os << (mode & file_stat::mode_type(fs::perms::group_write) ? 'w' : '-');
|
||||
os << (mode & file_stat::mode_type(fs::perms::group_exec) ? 'x' : '-');
|
||||
os << (mode & file_stat::mode_type(fs::perms::others_read) ? 'r' : '-');
|
||||
os << (mode & file_stat::mode_type(fs::perms::others_write) ? 'w' : '-');
|
||||
os << (mode & file_stat::mode_type(fs::perms::others_exec) ? 'x' : '-');
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#ifdef _WIN32
|
||||
@ -174,4 +208,22 @@ file_stat make_file_stat(fs::path const& path) {
|
||||
|
||||
#endif
|
||||
|
||||
std::string file_stat::mode_string(mode_type mode) {
|
||||
std::ostringstream oss;
|
||||
|
||||
oss << (mode & mode_type(fs::perms::set_uid) ? 'U' : '-');
|
||||
oss << (mode & mode_type(fs::perms::set_gid) ? 'G' : '-');
|
||||
oss << (mode & mode_type(fs::perms::sticky_bit) ? 'S' : '-');
|
||||
oss << get_filetype_label(mode);
|
||||
perms_to_stream(oss, mode);
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
std::string file_stat::perm_string(mode_type mode) {
|
||||
std::ostringstream oss;
|
||||
perms_to_stream(oss, mode);
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
} // namespace dwarfs
|
||||
|
@ -583,28 +583,6 @@ class metadata_ final : public metadata_v2::impl {
|
||||
}
|
||||
}
|
||||
|
||||
static char get_filetype_label(uint16_t mode) {
|
||||
switch (posix_file_type::from_mode(mode)) {
|
||||
case posix_file_type::regular:
|
||||
return '-';
|
||||
case posix_file_type::directory:
|
||||
return 'd';
|
||||
case posix_file_type::symlink:
|
||||
return 'l';
|
||||
case posix_file_type::block:
|
||||
return 'b';
|
||||
case posix_file_type::character:
|
||||
return 'c';
|
||||
case posix_file_type::fifo:
|
||||
return 'p';
|
||||
case posix_file_type::socket:
|
||||
return 's';
|
||||
default:
|
||||
DWARFS_THROW(runtime_error,
|
||||
fmt::format("unknown file type: {:#06x}", mode));
|
||||
}
|
||||
}
|
||||
|
||||
size_t find_inode_offset(inode_rank rank) const {
|
||||
if (meta_.dir_entries()) {
|
||||
auto range = boost::irange(size_t(0), meta_.inodes().size());
|
||||
@ -651,8 +629,6 @@ class metadata_ final : public metadata_v2::impl {
|
||||
std::optional<inode_view>
|
||||
find(directory_view dir, std::string_view name) const;
|
||||
|
||||
std::string modestring(uint16_t mode) const;
|
||||
|
||||
uint32_t chunk_table_lookup(uint32_t ino) const {
|
||||
return chunk_table_.empty() ? meta_.chunk_table()[ino] : chunk_table_[ino];
|
||||
}
|
||||
@ -940,7 +916,7 @@ void metadata_<LoggerPolicy>::dump(
|
||||
auto mode = iv.mode();
|
||||
auto inode = iv.inode_num();
|
||||
|
||||
os << indent << "<inode:" << inode << "> " << modestring(mode);
|
||||
os << indent << "<inode:" << inode << "> " << file_stat::mode_string(mode);
|
||||
|
||||
if (inode > 0) {
|
||||
os << " " << entry.name();
|
||||
@ -1267,7 +1243,7 @@ folly::dynamic metadata_<LoggerPolicy>::as_dynamic(dir_entry_view entry) const {
|
||||
auto inode = iv.inode_num();
|
||||
|
||||
obj["mode"] = mode;
|
||||
obj["modestring"] = modestring(mode);
|
||||
obj["modestring"] = file_stat::mode_string(mode);
|
||||
obj["inode"] = inode;
|
||||
|
||||
if (inode > 0) {
|
||||
@ -1370,27 +1346,6 @@ std::string metadata_<LoggerPolicy>::serialize_as_json(bool simple) const {
|
||||
return json;
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
std::string metadata_<LoggerPolicy>::modestring(uint16_t mode) const {
|
||||
std::ostringstream oss;
|
||||
|
||||
oss << (mode & uint16_t(fs::perms::set_uid) ? 'U' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::set_gid) ? 'G' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::sticky_bit) ? 'S' : '-');
|
||||
oss << get_filetype_label(mode);
|
||||
oss << (mode & uint16_t(fs::perms::owner_read) ? 'r' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::owner_write) ? 'w' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::owner_exec) ? 'x' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::group_read) ? 'r' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::group_write) ? 'w' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::group_exec) ? 'x' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::others_read) ? 'r' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::others_write) ? 'w' : '-');
|
||||
oss << (mode & uint16_t(fs::perms::others_exec) ? 'x' : '-');
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
template <typename T>
|
||||
void metadata_<LoggerPolicy>::walk(uint32_t self_index, uint32_t parent_index,
|
||||
@ -1758,7 +1713,7 @@ folly::dynamic metadata_<LoggerPolicy>::get_inode_info(inode_view iv) const {
|
||||
}
|
||||
|
||||
obj["mode"] = iv.mode();
|
||||
obj["modestring"] = modestring(iv.mode());
|
||||
obj["modestring"] = file_stat::mode_string(iv.mode());
|
||||
obj["uid"] = iv.getuid();
|
||||
obj["gid"] = iv.getgid();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user