You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

leveldbutil.cc 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. virtual std::string GetName() const { return "[stdout]"; }
  20. };
  21. bool HandleDumpCommand(Env* env, char** files, int num) {
  22. StdoutPrinter printer;
  23. bool ok = true;
  24. for (int i = 0; i < num; i++) {
  25. Status s = DumpFile(env, files[i], &printer);
  26. if (!s.ok()) {
  27. fprintf(stderr, "%s\n", s.ToString().c_str());
  28. ok = false;
  29. }
  30. }
  31. return ok;
  32. }
  33. } // namespace
  34. } // namespace leveldb
  35. static void Usage() {
  36. fprintf(
  37. stderr,
  38. "Usage: leveldbutil command...\n"
  39. " dump files... -- dump contents of specified files\n"
  40. );
  41. }
  42. int main(int argc, char** argv) {
  43. leveldb::Env* env = leveldb::Env::Default();
  44. bool ok = true;
  45. if (argc < 2) {
  46. Usage();
  47. ok = false;
  48. } else {
  49. std::string command = argv[1];
  50. if (command == "dump") {
  51. ok = leveldb::HandleDumpCommand(env, argv+2, argc-2);
  52. } else {
  53. Usage();
  54. ok = false;
  55. }
  56. }
  57. return (ok ? 0 : 1);
  58. }