Skip to content

Commit 4c4fee9

Browse files
committed
refactor(filesystem_v2): really avoid dependency on history
1 parent 7b78074 commit 4c4fee9

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

include/dwarfs/history.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class history;
5151
class history {
5252
public:
5353
explicit history(history_config const& cfg = {});
54+
history(history&&);
55+
history& operator=(history&&);
5456
~history();
5557

5658
void parse(std::span<uint8_t const> data);
@@ -64,7 +66,7 @@ class history {
6466

6567
private:
6668
std::unique_ptr<thrift::history::history> history_;
67-
history_config const cfg_;
69+
history_config cfg_;
6870
};
6971

7072
} // namespace dwarfs

src/history.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ history::history(history_config const& cfg)
4747
: history_{std::make_unique<thrift::history::history>()}
4848
, cfg_{cfg} {}
4949

50+
history::history(history&&) = default;
5051
history::~history() = default;
52+
history& history::operator=(history&&) = default;
5153

5254
void history::parse(std::span<uint8_t const> data) {
5355
history_->entries()->clear();

src/reader/filesystem_v2.cpp

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ class filesystem_ final {
212212
std::shared_ptr<performance_monitor const> const& perfmon);
213213

214214
int check(filesystem_check_level level, size_t num_threads) const;
215-
void dump(std::ostream& os, fsinfo_options const& opts) const;
216-
std::string dump(fsinfo_options const& opts) const;
217-
nlohmann::json info_as_json(fsinfo_options const& opts) const;
215+
void
216+
dump(std::ostream& os, fsinfo_options const& opts, history const& hist) const;
217+
std::string dump(fsinfo_options const& opts, history const& hist) const;
218+
nlohmann::json
219+
info_as_json(fsinfo_options const& opts, history const& hist) const;
218220
nlohmann::json metadata_as_json() const;
219221
std::string serialize_metadata_as_json(bool simple) const;
220222
void walk(std::function<void(dir_entry_view)> const& func) const;
@@ -280,7 +282,7 @@ class filesystem_ final {
280282
}
281283
size_t num_blocks() const { return ir_.num_blocks(); }
282284
bool has_symlinks() const { return meta_.has_symlinks(); }
283-
history const& get_history() const { return history_; }
285+
history get_history() const;
284286
nlohmann::json get_inode_info(inode_view entry) const {
285287
return meta_.get_inode_info(std::move(entry),
286288
std::numeric_limits<size_t>::max());
@@ -362,7 +364,7 @@ class filesystem_ final {
362364
mutable block_access_level fsinfo_block_access_level_{
363365
block_access_level::no_access};
364366
mutable std::unique_ptr<filesystem_info const> fsinfo_;
365-
history history_;
367+
std::vector<fs_section> history_sections_;
366368
file_off_t const image_offset_;
367369
filesystem_options const options_;
368370
PERFMON_CLS_PROXY_DECL
@@ -458,7 +460,6 @@ filesystem_<LoggerPolicy>::filesystem_(
458460
: LOG_PROXY_INIT(lgr)
459461
, os_{os}
460462
, mm_{std::move(mm)}
461-
, history_({.with_timestamps = true})
462463
, image_offset_{filesystem_parser::find_image_offset(*mm_,
463464
options.image_offset)}
464465
, options_{options} // clang-format off
@@ -542,13 +543,22 @@ filesystem_<LoggerPolicy>::filesystem_(
542543
ir_ = inode_reader_v2(lgr, std::move(cache), options.inode_reader, perfmon);
543544

544545
if (auto it = sections.find(section_type::HISTORY); it != sections.end()) {
545-
for (auto& section : it->second) {
546-
if (section.check_fast(*mm_)) {
547-
auto buffer = get_section_data(mm_, section);
548-
history_.parse_append(buffer.span());
549-
}
546+
history_sections_ = std::move(it->second);
547+
}
548+
}
549+
550+
template <typename LoggerPolicy>
551+
history filesystem_<LoggerPolicy>::get_history() const {
552+
history hist({.with_timestamps = true});
553+
554+
for (auto& section : history_sections_) {
555+
if (section.check_fast(*mm_)) {
556+
auto buffer = get_section_data(mm_, section);
557+
hist.parse_append(buffer.span());
550558
}
551559
}
560+
561+
return hist;
552562
}
553563

554564
template <typename LoggerPolicy>
@@ -618,7 +628,8 @@ int filesystem_<LoggerPolicy>::check(filesystem_check_level level,
618628

619629
template <typename LoggerPolicy>
620630
void filesystem_<LoggerPolicy>::dump(std::ostream& os,
621-
fsinfo_options const& opts) const {
631+
fsinfo_options const& opts,
632+
history const& hist) const {
622633
auto parser = make_fs_parser();
623634

624635
if (opts.features.has(fsinfo_feature::version)) {
@@ -670,7 +681,7 @@ void filesystem_<LoggerPolicy>::dump(std::ostream& os,
670681
}
671682

672683
if (opts.features.has(fsinfo_feature::history)) {
673-
history_.dump(os);
684+
hist.dump(os);
674685
}
675686

676687
metadata_v2_utils(meta_).dump(
@@ -688,15 +699,17 @@ void filesystem_<LoggerPolicy>::dump(std::ostream& os,
688699
}
689700

690701
template <typename LoggerPolicy>
691-
std::string filesystem_<LoggerPolicy>::dump(fsinfo_options const& opts) const {
702+
std::string filesystem_<LoggerPolicy>::dump(fsinfo_options const& opts,
703+
history const& hist) const {
692704
std::ostringstream oss;
693-
dump(oss, opts);
705+
dump(oss, opts, hist);
694706
return oss.str();
695707
}
696708

697709
template <typename LoggerPolicy>
698710
nlohmann::json
699-
filesystem_<LoggerPolicy>::info_as_json(fsinfo_options const& opts) const {
711+
filesystem_<LoggerPolicy>::info_as_json(fsinfo_options const& opts,
712+
history const& hist) const {
700713
auto parser = make_fs_parser();
701714

702715
auto info = nlohmann::json::object();
@@ -711,7 +724,7 @@ filesystem_<LoggerPolicy>::info_as_json(fsinfo_options const& opts) const {
711724
}
712725

713726
if (opts.features.has(fsinfo_feature::history)) {
714-
info["history"] = history_.as_json();
727+
info["history"] = hist.as_json();
715728
}
716729

717730
if (opts.features.has(fsinfo_feature::section_details)) {
@@ -1310,21 +1323,26 @@ template <typename LoggerPolicy>
13101323
class filesystem_full_
13111324
: public filesystem_common_<LoggerPolicy, filesystem_v2::impl> {
13121325
public:
1313-
using filesystem_common_<LoggerPolicy,
1314-
filesystem_v2::impl>::filesystem_common_;
13151326
using filesystem_common_<LoggerPolicy, filesystem_v2::impl>::fs;
13161327

1328+
filesystem_full_(logger& lgr, os_access const& os, std::shared_ptr<mmif> mm,
1329+
filesystem_options const& options,
1330+
std::shared_ptr<performance_monitor const> const& perfmon)
1331+
: filesystem_common_<LoggerPolicy, filesystem_v2::impl>(
1332+
lgr, os, std::move(mm), options, perfmon)
1333+
, history_{fs().get_history()} {}
1334+
13171335
int check(filesystem_check_level level, size_t num_threads) const override {
13181336
return fs().check(level, num_threads);
13191337
}
13201338
void dump(std::ostream& os, fsinfo_options const& opts) const override {
1321-
fs().dump(os, opts);
1339+
fs().dump(os, opts, history_);
13221340
}
13231341
std::string dump(fsinfo_options const& opts) const override {
1324-
return fs().dump(opts);
1342+
return fs().dump(opts, history_);
13251343
}
13261344
nlohmann::json info_as_json(fsinfo_options const& opts) const override {
1327-
return fs().info_as_json(opts);
1345+
return fs().info_as_json(opts, history_);
13281346
}
13291347
nlohmann::json metadata_as_json() const override {
13301348
return fs().metadata_as_json();
@@ -1335,7 +1353,10 @@ class filesystem_full_
13351353
std::optional<std::span<uint8_t const>> header() const override {
13361354
return fs().header();
13371355
}
1338-
history const& get_history() const override { return fs().get_history(); }
1356+
history const& get_history() const override { return history_; }
1357+
1358+
private:
1359+
history history_;
13391360
};
13401361

13411362
} // namespace internal

0 commit comments

Comments
 (0)