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