leveldbutil.cc raw

   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 <stdio.h>
   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:
  16    Status Append(const Slice& data) override {
  17      fwrite(data.data(), 1, data.size(), stdout);
  18      return Status::OK();
  19    }
  20    Status Close() override { return Status::OK(); }
  21    Status Flush() override { return Status::OK(); }
  22    Status Sync() override { return Status::OK(); }
  23    std::string GetName() const override { return "[stdout]"; }
  24  };
  25  
  26  bool HandleDumpCommand(Env* env, char** files, int num) {
  27    StdoutPrinter printer;
  28    bool ok = true;
  29    for (int i = 0; i < num; i++) {
  30      Status s = DumpFile(env, files[i], &printer);
  31      if (!s.ok()) {
  32        fprintf(stderr, "%s\n", s.ToString().c_str());
  33        ok = false;
  34      }
  35    }
  36    return ok;
  37  }
  38  
  39  }  // namespace
  40  }  // namespace leveldb
  41  
  42  static void Usage() {
  43    fprintf(stderr,
  44            "Usage: leveldbutil command...\n"
  45            "   dump files...         -- dump contents of specified files\n");
  46  }
  47  
  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