From 13aaf6737cecf18cd29e91c87e67a721f1447bd8 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Tue, 1 Dec 2020 17:50:38 +0100 Subject: [PATCH] Deprecate (chunk|link)_inode_offset These can be determined at run-time. This also magically fixes links on file systems created with pre 0.2.3 releases. --- src/dwarfs/metadata_v2.cpp | 29 ++++++++++++++++++++++++----- src/dwarfs/scanner.cpp | 2 -- thrift/metadata.thrift | 19 ++++++++++++------- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/dwarfs/metadata_v2.cpp b/src/dwarfs/metadata_v2.cpp index 717d0438..edc8ddbc 100644 --- a/src/dwarfs/metadata_v2.cpp +++ b/src/dwarfs/metadata_v2.cpp @@ -101,9 +101,14 @@ class metadata_ : public metadata_v2::impl { : data_(data) , meta_(map_frozen(schema, data_)) , root_(meta_.entries()[meta_.entry_index()[0]], &meta_) + , log_(lgr) , inode_offset_(inode_offset) - , chunk_index_offset_(meta_.chunk_index_offset()) - , log_(lgr) {} + , chunk_index_offset_( + find_index_offset(meta_.entry_index().size(), + [](uint16_t mode) { return S_ISREG(mode); })) + , link_index_offset_(find_index_offset( + chunk_index_offset_, [](uint16_t mode) { return S_ISLNK(mode); })) { + } void dump(std::ostream& os, int detail_level, std::function const& icb) @@ -154,6 +159,19 @@ class metadata_ : public metadata_v2::impl { return make_entry_view(meta_.entry_index()[inode]); } + template + size_t find_index_offset(size_t last, Func&& func) const { + auto range = boost::irange(size_t(0), last); + + auto it = + std::upper_bound(range.begin(), range.end(), 0, [&](int, auto inode) { + auto e = make_entry_view_from_inode(inode); + return bool(func(e.mode())); + }); + + return *it; + } + directory_view make_directory_view(entry_view entry) const { return directory_view(entry, &meta_); } @@ -205,15 +223,16 @@ class metadata_ : public metadata_v2::impl { std::string_view link_value(entry_view entry) const { return meta_ - .links()[meta_.link_index()[entry.inode() - meta_.link_index_offset()]]; + .links()[meta_.link_index()[entry.inode() - link_index_offset_]]; } folly::ByteRange data_; MappedFrozen meta_; entry_view root_; + log_proxy log_; const int inode_offset_; const int chunk_index_offset_; - log_proxy log_; + const int link_index_offset_; }; template @@ -534,7 +553,7 @@ template std::optional metadata_::get_chunks(int inode) const { std::optional rv; - inode -= inode_offset_ + meta_.chunk_index_offset(); + inode -= inode_offset_ + chunk_index_offset_; if (inode >= 0 && inode < (static_cast(meta_.chunk_index().size()) - 1)) { uint32_t begin = meta_.chunk_index()[inode]; diff --git a/src/dwarfs/scanner.cpp b/src/dwarfs/scanner.cpp index 9aa5bdd6..203181bb 100644 --- a/src/dwarfs/scanner.cpp +++ b/src/dwarfs/scanner.cpp @@ -532,8 +532,6 @@ void scanner_::scan(filesystem_writer& fsw, mv2.names = ge_data.get_names(); mv2.links = ge_data.get_links(); mv2.timestamp_base = ge_data.get_timestamp_base(); - mv2.chunk_index_offset = first_file_inode; - mv2.link_index_offset = first_link_inode; mv2.block_size = UINT32_C(1) << cfg_.block_size_bits; mv2.total_fs_size = prog.original_size; diff --git a/thrift/metadata.thrift b/thrift/metadata.thrift index 41641186..cc072c43 100644 --- a/thrift/metadata.thrift +++ b/thrift/metadata.thrift @@ -63,9 +63,9 @@ struct entry { * * - For directories, the inode can be used as an index into * metadata.directories. - * - For links, (inode - metadata.link_index_offset) can be + * - For links, (inode - link_index_offset) can be * used as an index into metadata.links. - * - For files, (inode - metadata.chunk_index_offset) can be + * - For files, (inode - chunk_index_offset) can be * used as in index into metadata.chunk_index. */ 3: required UInt32 inode, @@ -138,11 +138,16 @@ struct metadata { // timestamp base for all entry timestamps 12: required UInt64 timestamp_base, - // inode offset for lookups into chunk_index - 13: required UInt32 chunk_index_offset; - - // inode offset for lookups into link_index - 14: required UInt32 link_index_offset; + /************************ DEPRECATED ********************** + * + * These are redundant and can be determined at run-time + * with a simple binary search. Compatibility is not + * affected. + * + * 13: required UInt32 chunk_index_offset; + * 14: required UInt32 link_index_offset; + * + *********************************************************/ // block size 15: required UInt32 block_size;