leveldbutil.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <cstdio>
  5. #include "leveldb/dumpfile.h"
  6. #include "leveldb/env.h"
  7. #include "leveldb/status.h"
  8. namespace leveldb {
  9. namespace {
  10. class StdoutPrinter : public WritableFile {
  11. public:
  12. Status Append(const Slice& data) override {
  13. fwrite(data.data(), 1, data.size(), stdout);
  14. return Status::OK();
  15. }
  16. Status Close() override { return Status::OK(); }
  17. Status Flush() override { return Status::OK(); }
  18. Status Sync() override { return Status::OK(); }
  19. };
  20. bool HandleDumpCommand(Env* env, char** files, int num) {
  21. StdoutPrinter printer;
  22. bool ok = true;
  23. for (int i = 0; i < num; i++) {
  24. Status s = DumpFile(env, files[i], &printer);
  25. if (!s.ok()) {
  26. std::fprintf(stderr, "%s\n", s.ToString().c_str());
  27. ok = false;
  28. }
  29. }
  30. return ok;
  31. }
  32. } // namespace
  33. } // namespace leveldb
  34. static void Usage() {
  35. std::fprintf(
  36. stderr,
  37. "Usage: leveldbutil command...\n"
  38. " dump files... -- dump contents of specified files\n");
  39. }
  40. int main(int argc, char** argv) {
  41. leveldb::Env* env = leveldb::Env::Default();
  42. bool ok = true;
  43. if (argc < 2) {
  44. Usage();
  45. ok = false;
  46. } else {
  47. std::string command = argv[1];
  48. if (command == "dump") {
  49. ok = leveldb::HandleDumpCommand(env, argv + 2, argc - 2);
  50. } else {
  51. Usage();
  52. ok = false;
  53. }
  54. }
  55. return (ok ? 0 : 1);
  56. }