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