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