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 <stdio.h>
  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. virtual Status Append(const Slice& data) {
  13. fwrite(data.data(), 1, data.size(), stdout);
  14. return Status::OK();
  15. }
  16. virtual Status Close() { return Status::OK(); }
  17. virtual Status Flush() { return Status::OK(); }
  18. virtual Status Sync() { 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. 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. fprintf(
  36. stderr,
  37. "Usage: leveldbutil command...\n"
  38. " dump files... -- dump contents of specified files\n"
  39. );
  40. }
  41. int main(int argc, char** argv) {
  42. leveldb::Env* env = leveldb::Env::Default();
  43. bool ok = true;
  44. if (argc < 2) {
  45. Usage();
  46. ok = false;
  47. } else {
  48. std::string command = argv[1];
  49. if (command == "dump") {
  50. ok = leveldb::HandleDumpCommand(env, argv+2, argc-2);
  51. } else {
  52. Usage();
  53. ok = false;
  54. }
  55. }
  56. return (ok ? 0 : 1);
  57. }