2012-12-28 02:38:48 +08:00
|
|
|
// Copyright (c) 2012 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2020-04-30 03:59:39 +08:00
|
|
|
#include <cstdio>
|
2019-05-03 02:01:00 +08:00
|
|
|
|
2014-09-17 05:19:52 +08:00
|
|
|
#include "leveldb/dumpfile.h"
|
2012-12-28 02:38:48 +08:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/status.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
namespace {
|
|
|
|
|
2014-09-17 05:19:52 +08:00
|
|
|
class StdoutPrinter : public WritableFile {
|
2012-12-28 02:38:48 +08:00
|
|
|
public:
|
2019-05-10 05:00:07 +08:00
|
|
|
Status Append(const Slice& data) override {
|
2014-09-17 05:19:52 +08:00
|
|
|
fwrite(data.data(), 1, data.size(), stdout);
|
|
|
|
return Status::OK();
|
2012-12-28 02:38:48 +08:00
|
|
|
}
|
2019-05-10 05:00:07 +08:00
|
|
|
Status Close() override { return Status::OK(); }
|
|
|
|
Status Flush() override { return Status::OK(); }
|
|
|
|
Status Sync() override { return Status::OK(); }
|
2012-12-28 02:38:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool HandleDumpCommand(Env* env, char** files, int num) {
|
2014-09-17 05:19:52 +08:00
|
|
|
StdoutPrinter printer;
|
2012-12-28 02:38:48 +08:00
|
|
|
bool ok = true;
|
|
|
|
for (int i = 0; i < num; i++) {
|
2014-09-17 05:19:52 +08:00
|
|
|
Status s = DumpFile(env, files[i], &printer);
|
|
|
|
if (!s.ok()) {
|
2020-04-30 06:31:41 +08:00
|
|
|
std::fprintf(stderr, "%s\n", s.ToString().c_str());
|
2014-09-17 05:19:52 +08:00
|
|
|
ok = false;
|
|
|
|
}
|
2012-12-28 02:38:48 +08:00
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2014-09-17 05:19:52 +08:00
|
|
|
} // namespace
|
2012-12-28 02:38:48 +08:00
|
|
|
} // namespace leveldb
|
|
|
|
|
|
|
|
static void Usage() {
|
2020-04-30 06:31:41 +08:00
|
|
|
std::fprintf(
|
|
|
|
stderr,
|
|
|
|
"Usage: leveldbutil command...\n"
|
|
|
|
" dump files... -- dump contents of specified files\n");
|
2012-12-28 02:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
leveldb::Env* env = leveldb::Env::Default();
|
|
|
|
bool ok = true;
|
|
|
|
if (argc < 2) {
|
|
|
|
Usage();
|
|
|
|
ok = false;
|
|
|
|
} else {
|
|
|
|
std::string command = argv[1];
|
|
|
|
if (command == "dump") {
|
2019-05-03 02:01:00 +08:00
|
|
|
ok = leveldb::HandleDumpCommand(env, argv + 2, argc - 2);
|
2012-12-28 02:38:48 +08:00
|
|
|
} else {
|
|
|
|
Usage();
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (ok ? 0 : 1);
|
|
|
|
}
|