xref: /aosp_15_r20/external/leveldb/db/leveldbutil.cc (revision 9507f98c5f32dee4b5f9e4a38cd499f3ff5c4490)
1 // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 
5 #include <cstdio>
6 
7 #include "leveldb/dumpfile.h"
8 #include "leveldb/env.h"
9 #include "leveldb/status.h"
10 
11 namespace leveldb {
12 namespace {
13 
14 class StdoutPrinter : public WritableFile {
15  public:
Append(const Slice & data)16   Status Append(const Slice& data) override {
17     fwrite(data.data(), 1, data.size(), stdout);
18     return Status::OK();
19   }
Close()20   Status Close() override { return Status::OK(); }
Flush()21   Status Flush() override { return Status::OK(); }
Sync()22   Status Sync() override { return Status::OK(); }
23 };
24 
HandleDumpCommand(Env * env,char ** files,int num)25 bool HandleDumpCommand(Env* env, char** files, int num) {
26   StdoutPrinter printer;
27   bool ok = true;
28   for (int i = 0; i < num; i++) {
29     Status s = DumpFile(env, files[i], &printer);
30     if (!s.ok()) {
31       std::fprintf(stderr, "%s\n", s.ToString().c_str());
32       ok = false;
33     }
34   }
35   return ok;
36 }
37 
38 }  // namespace
39 }  // namespace leveldb
40 
Usage()41 static void Usage() {
42   std::fprintf(
43       stderr,
44       "Usage: leveldbutil command...\n"
45       "   dump files...         -- dump contents of specified files\n");
46 }
47 
main(int argc,char ** argv)48 int main(int argc, char** argv) {
49   leveldb::Env* env = leveldb::Env::Default();
50   bool ok = true;
51   if (argc < 2) {
52     Usage();
53     ok = false;
54   } else {
55     std::string command = argv[1];
56     if (command == "dump") {
57       ok = leveldb::HandleDumpCommand(env, argv + 2, argc - 2);
58     } else {
59       Usage();
60       ok = false;
61     }
62   }
63   return (ok ? 0 : 1);
64 }
65