mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-09 20:41:04 -04:00
Add metadata export to dwarfsck
This commit is contained in:
parent
cb246a8e6f
commit
04f2f1b976
@ -332,6 +332,7 @@ add_library(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/fbthrift/thrift/lib/cpp2/protocol/BinaryProtocol.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/fbthrift/thrift/lib/cpp2/protocol/DebugProtocol.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/fbthrift/thrift/lib/cpp2/protocol/JSONProtocolCommon.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/fbthrift/thrift/lib/cpp2/protocol/JSONProtocol.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/fbthrift/thrift/lib/cpp/protocol/TProtocolException.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/fbthrift/thrift/lib/cpp/util/VarintUtils.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/fbthrift/thrift/lib/cpp2/gen/module_types_cpp.cpp
|
||||
|
@ -74,6 +74,10 @@ class filesystem_v2 {
|
||||
return impl_->metadata_as_dynamic();
|
||||
}
|
||||
|
||||
std::string serialize_metadata_as_json(bool simple) const {
|
||||
return impl_->serialize_metadata_as_json(simple);
|
||||
}
|
||||
|
||||
void walk(std::function<void(entry_view)> const& func) const {
|
||||
impl_->walk(func);
|
||||
}
|
||||
@ -134,6 +138,7 @@ class filesystem_v2 {
|
||||
|
||||
virtual void dump(std::ostream& os, int detail_level) const = 0;
|
||||
virtual folly::dynamic metadata_as_dynamic() const = 0;
|
||||
virtual std::string serialize_metadata_as_json(bool simple) const = 0;
|
||||
virtual void walk(std::function<void(entry_view)> const& func) const = 0;
|
||||
virtual std::optional<entry_view> find(const char* path) const = 0;
|
||||
virtual std::optional<entry_view> find(int inode) const = 0;
|
||||
|
@ -71,6 +71,10 @@ class metadata_v2 {
|
||||
|
||||
folly::dynamic as_dynamic() const { return impl_->as_dynamic(); }
|
||||
|
||||
std::string serialize_as_json(bool simple) const {
|
||||
return impl_->serialize_as_json(simple);
|
||||
}
|
||||
|
||||
static void get_stat_defaults(struct ::stat* defaults);
|
||||
|
||||
// TODO: check if this is needed
|
||||
@ -142,6 +146,7 @@ class metadata_v2 {
|
||||
std::function<void(const std::string&, uint32_t)> const& icb) const = 0;
|
||||
|
||||
virtual folly::dynamic as_dynamic() const = 0;
|
||||
virtual std::string serialize_as_json(bool simple) const = 0;
|
||||
|
||||
virtual size_t size() const = 0;
|
||||
virtual bool empty() const = 0;
|
||||
|
@ -195,6 +195,7 @@ class filesystem_ : public filesystem_v2::impl {
|
||||
|
||||
void dump(std::ostream& os, int detail_level) const override;
|
||||
folly::dynamic metadata_as_dynamic() const override;
|
||||
std::string serialize_metadata_as_json(bool simple) const override;
|
||||
void walk(std::function<void(entry_view)> const& func) const override;
|
||||
std::optional<entry_view> find(const char* path) const override;
|
||||
std::optional<entry_view> find(int inode) const override;
|
||||
@ -278,6 +279,12 @@ folly::dynamic filesystem_<LoggerPolicy>::metadata_as_dynamic() const {
|
||||
return meta_.as_dynamic();
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
std::string
|
||||
filesystem_<LoggerPolicy>::serialize_metadata_as_json(bool simple) const {
|
||||
return meta_.serialize_as_json(simple);
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
void filesystem_<LoggerPolicy>::walk(
|
||||
std::function<void(entry_view)> const& func) const {
|
||||
|
@ -34,6 +34,7 @@
|
||||
|
||||
#include <thrift/lib/cpp2/frozen/FrozenUtil.h>
|
||||
#include <thrift/lib/cpp2/protocol/DebugProtocol.h>
|
||||
#include <thrift/lib/cpp2/protocol/Serializer.h>
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
@ -170,6 +171,7 @@ class metadata_ : public metadata_v2::impl {
|
||||
const override;
|
||||
|
||||
folly::dynamic as_dynamic() const override;
|
||||
std::string serialize_as_json(bool simple) const override;
|
||||
|
||||
size_t size() const override { return data_.size(); }
|
||||
|
||||
@ -525,6 +527,19 @@ folly::dynamic metadata_<LoggerPolicy>::as_dynamic() const {
|
||||
return obj;
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
std::string metadata_<LoggerPolicy>::serialize_as_json(bool simple) const {
|
||||
std::string json;
|
||||
if (simple) {
|
||||
apache::thrift::SimpleJSONSerializer serializer;
|
||||
serializer.serialize(meta_.thaw(), &json);
|
||||
} else {
|
||||
apache::thrift::JSONSerializer serializer;
|
||||
serializer.serialize(meta_.thaw(), &json);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
template <typename LoggerPolicy>
|
||||
std::string metadata_<LoggerPolicy>::modestring(uint16_t mode) const {
|
||||
std::ostringstream oss;
|
||||
|
@ -24,6 +24,8 @@
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
#include <folly/File.h>
|
||||
#include <folly/FileUtil.h>
|
||||
#include <folly/String.h>
|
||||
#include <folly/json.h>
|
||||
|
||||
@ -38,7 +40,7 @@ namespace dwarfs {
|
||||
namespace po = boost::program_options;
|
||||
|
||||
int dwarfsck(int argc, char** argv) {
|
||||
std::string log_level, input;
|
||||
std::string log_level, input, export_metadata;
|
||||
int detail;
|
||||
bool json = false;
|
||||
|
||||
@ -54,6 +56,9 @@ int dwarfsck(int argc, char** argv) {
|
||||
("json",
|
||||
po::value<bool>(&json)->zero_tokens(),
|
||||
"print metadata in JSON format")
|
||||
("export-metadata",
|
||||
po::value<std::string>(&export_metadata),
|
||||
"export raw metadata as JSON to file")
|
||||
("log-level",
|
||||
po::value<std::string>(&log_level)->default_value("info"),
|
||||
"log level (error, warn, info, debug, trace)")
|
||||
@ -87,7 +92,15 @@ int dwarfsck(int argc, char** argv) {
|
||||
try {
|
||||
auto mm = std::make_shared<mmap>(input);
|
||||
|
||||
if (json) {
|
||||
if (!export_metadata.empty()) {
|
||||
auto of = folly::File(export_metadata, O_RDWR | O_CREAT);
|
||||
filesystem_v2 fs(lgr, mm);
|
||||
auto json = fs.serialize_metadata_as_json(true);
|
||||
if (folly::writeFull(of.fd(), json.data(), json.size()) < 0) {
|
||||
LOG_ERROR << "failed to export metadata";
|
||||
}
|
||||
of.close();
|
||||
} else if (json) {
|
||||
filesystem_v2 fs(lgr, mm);
|
||||
std::cout << folly::toPrettyJson(fs.metadata_as_dynamic()) << std::endl;
|
||||
} else {
|
||||
@ -99,6 +112,9 @@ int dwarfsck(int argc, char** argv) {
|
||||
} catch (runtime_error const& e) {
|
||||
LOG_ERROR << folly::exceptionStr(e);
|
||||
return 1;
|
||||
} catch (std::system_error const& e) {
|
||||
LOG_ERROR << folly::exceptionStr(e);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user