1// Copyright (c) 2011 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 "leveldb/env.h"
6
7namespace leveldb {
8
9Env::~Env() {
10}
11
12SequentialFile::~SequentialFile() {
13}
14
15RandomAccessFile::~RandomAccessFile() {
16}
17
18WritableFile::~WritableFile() {
19}
20
21Logger::~Logger() {
22}
23
24FileLock::~FileLock() {
25}
26
27void Log(Logger* info_log, const char* format, ...) {
28  if (info_log != NULL) {
29    va_list ap;
30    va_start(ap, format);
31    info_log->Logv(format, ap);
32    va_end(ap);
33  }
34}
35
36static Status DoWriteStringToFile(Env* env, const Slice& data,
37                                  const std::string& fname,
38                                  bool should_sync) {
39  WritableFile* file;
40  Status s = env->NewWritableFile(fname, &file);
41  if (!s.ok()) {
42    return s;
43  }
44  s = file->Append(data);
45  if (s.ok() && should_sync) {
46    s = file->Sync();
47  }
48  if (s.ok()) {
49    s = file->Close();
50  }
51  delete file;  // Will auto-close if we did not close above
52  if (!s.ok()) {
53    env->DeleteFile(fname);
54  }
55  return s;
56}
57
58Status WriteStringToFile(Env* env, const Slice& data,
59                         const std::string& fname) {
60  return DoWriteStringToFile(env, data, fname, false);
61}
62
63Status WriteStringToFileSync(Env* env, const Slice& data,
64                             const std::string& fname) {
65  return DoWriteStringToFile(env, data, fname, true);
66}
67
68Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
69  data->clear();
70  SequentialFile* file;
71  Status s = env->NewSequentialFile(fname, &file);
72  if (!s.ok()) {
73    return s;
74  }
75  static const int kBufferSize = 8192;
76  char* space = new char[kBufferSize];
77  while (true) {
78    Slice fragment;
79    s = file->Read(kBufferSize, &fragment, space);
80    if (!s.ok()) {
81      break;
82    }
83    data->append(fragment.data(), fragment.size());
84    if (fragment.empty()) {
85      break;
86    }
87  }
88  delete[] space;
89  delete file;
90  return s;
91}
92
93EnvWrapper::~EnvWrapper() {
94}
95
96}  // namespace leveldb
97