/Users/deen/code/yugabyte-db/src/yb/util/env.cc
Line | Count | Source (jump to first uncovered line) |
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 | | // The following only applies to changes made to this file as part of YugaByte development. |
6 | | // |
7 | | // Portions Copyright (c) YugaByte, Inc. |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
10 | | // in compliance with the License. You may obtain a copy of the License at |
11 | | // |
12 | | // http://www.apache.org/licenses/LICENSE-2.0 |
13 | | // |
14 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
15 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
16 | | // or implied. See the License for the specific language governing permissions and limitations |
17 | | // under the License. |
18 | | // |
19 | | |
20 | | #include "yb/util/env.h" |
21 | | |
22 | | #include "yb/util/faststring.h" |
23 | | #include "yb/util/path_util.h" |
24 | | #include "yb/util/result.h" |
25 | | #include "yb/util/status_format.h" |
26 | | #include "yb/util/status_log.h" |
27 | | |
28 | | namespace yb { |
29 | | |
30 | 980 | Env::~Env() { |
31 | 980 | } |
32 | | |
33 | 7.13k | Status EnvWrapper::Env::CreateDirs(const std::string& dirname) { |
34 | 7.13k | if (!FileExists(dirname)) { |
35 | 1.59k | RETURN_NOT_OK(CreateDirs(DirName(dirname))); |
36 | 1.59k | RETURN_NOT_OK(CreateDir(dirname)); |
37 | 1.59k | } |
38 | 7.13k | return VERIFY_RESULT(IsDirectory(dirname)) ? |
39 | 7.13k | Status::OK() : STATUS_FORMAT(IOError, "Not a directory: $0", dirname); |
40 | 7.13k | } |
41 | | |
42 | 2.18M | RandomAccessFile::~RandomAccessFile() { |
43 | 2.18M | } |
44 | | |
45 | 2.42M | Status WritableFile::AppendVector(const std::vector<Slice>& data_vector) { |
46 | 2.42M | return AppendSlices(data_vector.data(), data_vector.size()); |
47 | 2.42M | } |
48 | | |
49 | 1.19M | WritableFile::~WritableFile() { |
50 | 1.19M | } |
51 | | |
52 | 4 | RWFile::~RWFile() { |
53 | 4 | } |
54 | | |
55 | 3.96k | FileLock::~FileLock() { |
56 | 3.96k | } |
57 | | |
58 | | static Status DoWriteStringToFile(Env* env, const Slice& data, |
59 | | const std::string& fname, |
60 | 1.31k | bool should_sync) { |
61 | 1.31k | std::unique_ptr<WritableFile> file; |
62 | 1.31k | Status s = env->NewWritableFile(fname, &file); |
63 | 1.31k | if (!s.ok()) { |
64 | 0 | return s; |
65 | 0 | } |
66 | 1.31k | s = file->Append(data); |
67 | 1.31k | if (s.ok() && should_sync) { |
68 | 0 | s = file->Sync(); |
69 | 0 | } |
70 | 1.31k | if (s.ok()) { |
71 | 1.31k | s = file->Close(); |
72 | 1.31k | } |
73 | 1.31k | file.reset(); // Will auto-close if we did not close above |
74 | 1.31k | if (!s.ok()) { |
75 | 0 | WARN_NOT_OK(env->DeleteFile(fname), |
76 | 0 | "Failed to delete partially-written file " + fname); |
77 | 0 | } |
78 | 1.31k | return s; |
79 | 1.31k | } |
80 | | |
81 | | // TODO: move these utils into env_util |
82 | | Status WriteStringToFile(Env* env, const Slice& data, |
83 | 1.31k | const std::string& fname) { |
84 | 1.31k | return DoWriteStringToFile(env, data, fname, false); |
85 | 1.31k | } |
86 | | |
87 | | Status WriteStringToFileSync(Env* env, const Slice& data, |
88 | 0 | const std::string& fname) { |
89 | 0 | return DoWriteStringToFile(env, data, fname, true); |
90 | 0 | } |
91 | | |
92 | 157 | Status ReadFileToString(Env* env, const std::string& fname, faststring* data) { |
93 | 157 | data->clear(); |
94 | 157 | std::unique_ptr<SequentialFile> file; |
95 | 157 | Status s = env->NewSequentialFile(fname, &file); |
96 | 157 | if (!s.ok()) { |
97 | 0 | return s; |
98 | 0 | } |
99 | 157 | static const int kBufferSize = 8192; |
100 | 157 | std::unique_ptr<uint8_t[]> scratch(new uint8_t[kBufferSize]); |
101 | 322 | while (true) { |
102 | 322 | Slice fragment; |
103 | 322 | s = file->Read(kBufferSize, &fragment, scratch.get()); |
104 | 322 | if (!s.ok()) { |
105 | 0 | break; |
106 | 0 | } |
107 | 322 | data->append(fragment.data(), fragment.size()); |
108 | 322 | if (fragment.empty()) { |
109 | 157 | break; |
110 | 157 | } |
111 | 322 | } |
112 | 157 | return s; |
113 | 157 | } |
114 | | |
115 | 96.5k | Status Env::GetChildren(const std::string& dir, std::vector<std::string>* result) { |
116 | 96.5k | return GetChildren(dir, ExcludeDots::kFalse, result); |
117 | 96.5k | } |
118 | | |
119 | | Result<std::vector<std::string>> Env::GetChildren( |
120 | 95.4k | const std::string& dir, ExcludeDots exclude_dots) { |
121 | 95.4k | std::vector<std::string> result; |
122 | 95.4k | RETURN_NOT_OK(GetChildren(dir, exclude_dots, &result)); |
123 | 95.4k | return result; |
124 | 95.4k | } |
125 | | |
126 | 0 | Result<std::string> Env::GetTestDirectory() { |
127 | 0 | std::string test_dir; |
128 | 0 | RETURN_NOT_OK(GetTestDirectory(&test_dir)); |
129 | 0 | return test_dir; |
130 | 0 | } |
131 | | |
132 | 11.7k | Result<bool> Env::IsDirectory(const std::string& path) { |
133 | 11.7k | bool result = false; |
134 | 11.7k | RETURN_NOT_OK(IsDirectory(path, &result)); |
135 | 11.7k | return result; |
136 | 11.7k | } |
137 | | |
138 | | // Like IsDirectory, but non-existence of the given path is not considered an error. |
139 | 361 | Result<bool> Env::DoesDirectoryExist(const std::string& path) { |
140 | 361 | bool result = false; |
141 | 361 | Status status = IsDirectory(path, &result); |
142 | 361 | if (status.IsNotFound()) { |
143 | 0 | return false; |
144 | 0 | } |
145 | 361 | if (!status.ok()) { |
146 | 0 | return status; |
147 | 0 | } |
148 | 361 | return result; |
149 | 361 | } |
150 | | |
151 | 0 | Result<std::string> Env::Canonicalize(const std::string& path) { |
152 | 0 | string result; |
153 | 0 | RETURN_NOT_OK(Canonicalize(path, &result)); |
154 | 0 | return result; |
155 | 0 | } |
156 | | |
157 | 868 | EnvWrapper::~EnvWrapper() { |
158 | 868 | } |
159 | | |
160 | 0 | Status DeleteIfExists(const std::string& path, Env* env) { |
161 | 0 | if (env->DirExists(path)) { |
162 | 0 | return env->DeleteRecursively(path); |
163 | 0 | } |
164 | 0 | if (env->FileExists(path)) { |
165 | 0 | return env->DeleteFile(path); |
166 | 0 | } |
167 | 0 | return Status::OK(); |
168 | 0 | } |
169 | | |
170 | | Status FileFactoryWrapper::NewSequentialFile(const std::string& fname, |
171 | 87 | std::unique_ptr<SequentialFile>* result) { |
172 | 87 | return target_->NewSequentialFile(fname, result); |
173 | 87 | } |
174 | | |
175 | | Status FileFactoryWrapper::NewRandomAccessFile(const std::string& fname, |
176 | 319k | std::unique_ptr<RandomAccessFile>* result) { |
177 | 319k | return target_->NewRandomAccessFile(fname, result); |
178 | 319k | } |
179 | | |
180 | | Status FileFactoryWrapper::NewWritableFile(const std::string& fname, |
181 | 0 | std::unique_ptr<WritableFile>* result) { |
182 | 0 | return target_->NewWritableFile(fname, result); |
183 | 0 | } |
184 | | |
185 | | Status FileFactoryWrapper::NewWritableFile(const WritableFileOptions& opts, |
186 | | const std::string& fname, |
187 | 5.47k | std::unique_ptr<WritableFile>* result) { |
188 | 5.47k | return target_->NewWritableFile(opts, fname, result); |
189 | 5.47k | } |
190 | | |
191 | | Status FileFactoryWrapper::NewTempWritableFile(const WritableFileOptions& opts, |
192 | | const std::string& name_template, |
193 | | std::string* created_filename, |
194 | 693k | std::unique_ptr<WritableFile>* result) { |
195 | 693k | return target_->NewTempWritableFile(opts, name_template, created_filename, result); |
196 | 693k | } |
197 | | |
198 | | Status FileFactoryWrapper::NewRWFile(const std::string& fname, |
199 | 0 | std::unique_ptr<RWFile>* result) { |
200 | 0 | return target_->NewRWFile(fname, result); |
201 | 0 | } |
202 | | |
203 | | // Like the previous NewRWFile, but allows options to be specified. |
204 | | Status FileFactoryWrapper::NewRWFile(const RWFileOptions& opts, |
205 | | const std::string& fname, |
206 | 0 | std::unique_ptr<RWFile>* result) { |
207 | 0 | return target_->NewRWFile(opts, fname, result); |
208 | 0 | } |
209 | | |
210 | 0 | Status WritableFile::AppendSlices(const Slice* begin, const Slice* end) { |
211 | 0 | return AppendSlices(begin, end - begin); |
212 | 0 | } |
213 | | |
214 | 449k | Result<uint64_t> FileFactoryWrapper::GetFileSize(const std::string& fname) { |
215 | 449k | return target_->GetFileSize(fname); |
216 | 449k | } |
217 | | |
218 | 0 | Status WritableFileWrapper::PreAllocate(uint64_t size) { |
219 | 0 | return target_->PreAllocate(size); |
220 | 0 | } |
221 | | |
222 | 11 | Status WritableFileWrapper::Append(const Slice& data) { |
223 | 11 | return target_->Append(data); |
224 | 11 | } |
225 | | |
226 | 0 | Status WritableFileWrapper::AppendSlices(const Slice* slices, size_t num) { |
227 | 0 | return target_->AppendSlices(slices, num); |
228 | 0 | } |
229 | | |
230 | 0 | Status WritableFileWrapper::Close() { |
231 | 0 | return target_->Close(); |
232 | 0 | } |
233 | | |
234 | 0 | Status WritableFileWrapper::Flush(FlushMode mode) { |
235 | 0 | return target_->Flush(mode); |
236 | 0 | } |
237 | | |
238 | 0 | Status WritableFileWrapper::Sync() { |
239 | 0 | return target_->Sync(); |
240 | 0 | } |
241 | | |
242 | | Status EnvWrapper::NewSequentialFile(const std::string& f, |
243 | 8 | std::unique_ptr<SequentialFile>* r) { |
244 | 8 | return target_->NewSequentialFile(f, r); |
245 | 8 | } |
246 | | |
247 | | Status EnvWrapper::NewRandomAccessFile(const std::string& f, |
248 | 1.33k | std::unique_ptr<RandomAccessFile>* r) { |
249 | 1.33k | return target_->NewRandomAccessFile(f, r); |
250 | 1.33k | } |
251 | 37 | Status EnvWrapper::NewWritableFile(const std::string& f, std::unique_ptr<WritableFile>* r) { |
252 | 37 | return target_->NewWritableFile(f, r); |
253 | 37 | } |
254 | | |
255 | | Status EnvWrapper::NewWritableFile(const WritableFileOptions& o, |
256 | | const std::string& f, |
257 | 12 | std::unique_ptr<WritableFile>* r) { |
258 | 12 | return target_->NewWritableFile(o, f, r); |
259 | 12 | } |
260 | | |
261 | | Status EnvWrapper::NewTempWritableFile(const WritableFileOptions& o, const std::string& t, |
262 | 2.29k | std::string* f, std::unique_ptr<WritableFile>* r) { |
263 | 2.29k | return target_->NewTempWritableFile(o, t, f, r); |
264 | 2.29k | } |
265 | | |
266 | 1 | Status EnvWrapper::NewRWFile(const std::string& f, std::unique_ptr<RWFile>* r) { |
267 | 1 | return target_->NewRWFile(f, r); |
268 | 1 | } |
269 | | |
270 | | Status EnvWrapper::NewRWFile(const RWFileOptions& o, |
271 | | const std::string& f, |
272 | 2 | std::unique_ptr<RWFile>* r) { |
273 | 2 | return target_->NewRWFile(o, f, r); |
274 | 2 | } |
275 | | |
276 | | Status EnvWrapper::GetChildren( |
277 | 195 | const std::string& dir, ExcludeDots exclude_dots, std::vector<std::string>* r) { |
278 | 195 | return target_->GetChildren(dir, exclude_dots, r); |
279 | 195 | } |
280 | | |
281 | 8 | Status EnvWrapper::DeleteFile(const std::string& f) { |
282 | 8 | return target_->DeleteFile(f); |
283 | 8 | } |
284 | | |
285 | 7.19k | Status EnvWrapper::CreateDir(const std::string& d) { |
286 | 7.19k | return target_->CreateDir(d); |
287 | 7.19k | } |
288 | | |
289 | 5.54k | Status EnvWrapper::SyncDir(const std::string& d) { |
290 | 5.54k | return target_->SyncDir(d); |
291 | 5.54k | } |
292 | | |
293 | 0 | Status EnvWrapper::DeleteDir(const std::string& d) { |
294 | 0 | return target_->DeleteDir(d); |
295 | 0 | } |
296 | | |
297 | 1.22k | Status EnvWrapper::DeleteRecursively(const std::string& d) { |
298 | 1.22k | return target_->DeleteRecursively(d); |
299 | 1.22k | } |
300 | | |
301 | 1.77k | Result<uint64_t> EnvWrapper::GetFileSize(const std::string& f) { |
302 | 1.77k | return target_->GetFileSize(f); |
303 | 1.77k | } |
304 | | |
305 | 0 | Result<uint64_t> EnvWrapper::GetFileINode(const std::string& f) { |
306 | 0 | return target_->GetFileINode(f); |
307 | 0 | } |
308 | | |
309 | 0 | Result<uint64_t> EnvWrapper::GetFileSizeOnDisk(const std::string& f) { |
310 | 0 | return target_->GetFileSizeOnDisk(f); |
311 | 0 | } |
312 | | |
313 | 3 | Result<uint64_t> EnvWrapper::GetBlockSize(const std::string& f) { |
314 | 3 | return target_->GetBlockSize(f); |
315 | 3 | } |
316 | | |
317 | 0 | Result<Env::FilesystemStats> EnvWrapper::GetFilesystemStatsBytes(const std::string& f) { |
318 | 0 | return target_->GetFilesystemStatsBytes(f); |
319 | 0 | } |
320 | | |
321 | 0 | Status EnvWrapper::LinkFile(const std::string& s, const std::string& t) { |
322 | 0 | return target_->LinkFile(s, t); |
323 | 0 | } |
324 | | |
325 | 444 | Result<std::string> EnvWrapper::ReadLink(const std::string& s) { |
326 | 444 | return target_->ReadLink(s); |
327 | 444 | } |
328 | | |
329 | 2.29k | Status EnvWrapper::RenameFile(const std::string& s, const std::string& t) { |
330 | 2.29k | return target_->RenameFile(s, t); |
331 | 2.29k | } |
332 | | |
333 | 0 | Status EnvWrapper::LockFile(const std::string& f, FileLock** l, bool r) { |
334 | 0 | return target_->LockFile(f, l, r); |
335 | 0 | } |
336 | | |
337 | 0 | Status EnvWrapper::UnlockFile(FileLock* l) { |
338 | 0 | return target_->UnlockFile(l); |
339 | 0 | } |
340 | | |
341 | 0 | Status EnvWrapper::GetTestDirectory(std::string* path) { |
342 | 0 | return target_->GetTestDirectory(path); |
343 | 0 | } |
344 | | |
345 | 1 | Status EnvWrapper::GetExecutablePath(std::string* path) { |
346 | 1 | return target_->GetExecutablePath(path); |
347 | 1 | } |
348 | | |
349 | 519 | Status EnvWrapper::IsDirectory(const std::string& path, bool* is_dir) { |
350 | 519 | return target_->IsDirectory(path, is_dir); |
351 | 519 | } |
352 | | |
353 | 0 | Result<bool> EnvWrapper::IsExecutableFile(const std::string& path) { |
354 | 0 | return target_->IsExecutableFile(path); |
355 | 0 | } |
356 | | |
357 | | Status EnvWrapper::Walk(const std::string& root, |
358 | | DirectoryOrder order, |
359 | 2 | const WalkCallback& cb) { |
360 | 2 | return target_->Walk(root, order, cb); |
361 | 2 | } |
362 | | |
363 | 160 | Status EnvWrapper::Canonicalize(const std::string& path, std::string* result) { |
364 | 160 | return target_->Canonicalize(path, result); |
365 | 160 | } |
366 | | |
367 | 1 | Status EnvWrapper::GetTotalRAMBytes(int64_t* ram) { |
368 | 1 | return target_->GetTotalRAMBytes(ram); |
369 | 1 | } |
370 | | |
371 | 10 | Result<uint64_t> EnvWrapper::GetFreeSpaceBytes(const std::string& path) { |
372 | 10 | return target_->GetFreeSpaceBytes(path); |
373 | 10 | } |
374 | | |
375 | 0 | Result<ResourceLimits> EnvWrapper::GetUlimit(int resource) { |
376 | 0 | return target_->GetUlimit(resource); |
377 | 0 | } |
378 | | |
379 | 0 | Status EnvWrapper::SetUlimit(int resource, ResourceLimit value) { |
380 | 0 | return target_->SetUlimit(resource, value); |
381 | 0 | } |
382 | | |
383 | | Status EnvWrapper::SetUlimit( |
384 | 0 | int resource, ResourceLimit value, const std::string& resource_name) { |
385 | 0 | return target_->SetUlimit(resource, value, resource_name); |
386 | 0 | } |
387 | | |
388 | | } // namespace yb |