YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
1.38k
Env::~Env() {
31
1.38k
}
32
33
72.9k
Status EnvWrapper::Env::CreateDirs(const std::string& dirname) {
34
72.9k
  if (!FileExists(dirname)) {
35
18.3k
    RETURN_NOT_OK(CreateDirs(DirName(dirname)));
36
18.3k
    RETURN_NOT_OK(CreateDir(dirname));
37
18.3k
  }
38
72.9k
  return VERIFY_RESULT(IsDirectory(dirname)) ?
39
72.9k
      Status::OK() : 
STATUS_FORMAT2
(IOError, "Not a directory: $0", dirname);
40
72.9k
}
41
42
2.88M
RandomAccessFile::~RandomAccessFile() {
43
2.88M
}
44
45
4.51M
Status WritableFile::AppendVector(const std::vector<Slice>& data_vector) {
46
4.51M
  return AppendSlices(data_vector.data(), data_vector.size());
47
4.51M
}
48
49
2.25M
WritableFile::~WritableFile() {
50
2.25M
}
51
52
7
RWFile::~RWFile() {
53
7
}
54
55
8.51k
FileLock::~FileLock() {
56
8.51k
}
57
58
static Status DoWriteStringToFile(Env* env, const Slice& data,
59
                                  const std::string& fname,
60
2.30k
                                  bool should_sync) {
61
2.30k
  std::unique_ptr<WritableFile> file;
62
2.30k
  Status s = env->NewWritableFile(fname, &file);
63
2.30k
  if (!s.ok()) {
64
0
    return s;
65
0
  }
66
2.30k
  s = file->Append(data);
67
2.30k
  if (s.ok() && should_sync) {
68
0
    s = file->Sync();
69
0
  }
70
2.30k
  if (s.ok()) {
71
2.30k
    s = file->Close();
72
2.30k
  }
73
2.30k
  file.reset();  // Will auto-close if we did not close above
74
2.30k
  if (!s.ok()) {
75
0
    WARN_NOT_OK(env->DeleteFile(fname),
76
0
                "Failed to delete partially-written file " + fname);
77
0
  }
78
2.30k
  return s;
79
2.30k
}
80
81
// TODO: move these utils into env_util
82
Status WriteStringToFile(Env* env, const Slice& data,
83
2.30k
                         const std::string& fname) {
84
2.30k
  return DoWriteStringToFile(env, data, fname, false);
85
2.30k
}
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
586
Status ReadFileToString(Env* env, const std::string& fname, faststring* data) {
93
586
  data->clear();
94
586
  std::unique_ptr<SequentialFile> file;
95
586
  Status s = env->NewSequentialFile(fname, &file);
96
586
  if (!s.ok()) {
97
0
    return s;
98
0
  }
99
586
  static const int kBufferSize = 8192;
100
586
  std::unique_ptr<uint8_t[]> scratch(new uint8_t[kBufferSize]);
101
1.32k
  while (true) {
102
1.32k
    Slice fragment;
103
1.32k
    s = file->Read(kBufferSize, &fragment, scratch.get());
104
1.32k
    if (!s.ok()) {
105
0
      break;
106
0
    }
107
1.32k
    data->append(fragment.data(), fragment.size());
108
1.32k
    if (fragment.empty()) {
109
586
      break;
110
586
    }
111
1.32k
  }
112
586
  return s;
113
586
}
114
115
161k
Status Env::GetChildren(const std::string& dir, std::vector<std::string>* result) {
116
161k
  return GetChildren(dir, ExcludeDots::kFalse, result);
117
161k
}
118
119
Result<std::vector<std::string>> Env::GetChildren(
120
159k
    const std::string& dir, ExcludeDots exclude_dots) {
121
159k
  std::vector<std::string> result;
122
159k
  RETURN_NOT_OK(GetChildren(dir, exclude_dots, &result));
123
159k
  return result;
124
159k
}
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
79.7k
Result<bool> Env::IsDirectory(const std::string& path) {
133
79.7k
  bool result = false;
134
79.7k
  RETURN_NOT_OK(IsDirectory(path, &result));
135
79.7k
  return result;
136
79.7k
}
137
138
// Like IsDirectory, but non-existence of the given path is not considered an error.
139
746
Result<bool> Env::DoesDirectoryExist(const std::string& path) {
140
746
  bool result = false;
141
746
  Status status = IsDirectory(path, &result);
142
746
  if (status.IsNotFound()) {
143
0
    return false;
144
0
  }
145
746
  if (!status.ok()) {
146
0
    return status;
147
0
  }
148
746
  return result;
149
746
}
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
1.23k
EnvWrapper::~EnvWrapper() {
158
1.23k
}
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
135
                                             std::unique_ptr<SequentialFile>* result) {
172
135
  return target_->NewSequentialFile(fname, result);
173
135
}
174
175
Status FileFactoryWrapper::NewRandomAccessFile(const std::string& fname,
176
536k
                                               std::unique_ptr<RandomAccessFile>* result) {
177
536k
  return target_->NewRandomAccessFile(fname, result);
178
536k
}
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
10.8k
                                           std::unique_ptr<WritableFile>* result) {
188
10.8k
  return target_->NewWritableFile(opts, fname, result);
189
10.8k
}
190
191
Status FileFactoryWrapper::NewTempWritableFile(const WritableFileOptions& opts,
192
                                               const std::string& name_template,
193
                                               std::string* created_filename,
194
1.19M
                                               std::unique_ptr<WritableFile>* result) {
195
1.19M
  return target_->NewTempWritableFile(opts, name_template, created_filename, result);
196
1.19M
}
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
786k
Result<uint64_t> FileFactoryWrapper::GetFileSize(const std::string& fname) {
215
786k
  return target_->GetFileSize(fname);
216
786k
}
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.23k
                                       std::unique_ptr<RandomAccessFile>* r) {
249
1.23k
  return target_->NewRandomAccessFile(f, r);
250
1.23k
}
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.46k
                                       std::string* f, std::unique_ptr<WritableFile>* r) {
263
2.46k
  return target_->NewTempWritableFile(o, t, f, r);
264
2.46k
}
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
5
                             std::unique_ptr<RWFile>* r) {
273
5
  return target_->NewRWFile(o, f, r);
274
5
}
275
276
Status EnvWrapper::GetChildren(
277
177
    const std::string& dir, ExcludeDots exclude_dots, std::vector<std::string>* r) {
278
177
  return target_->GetChildren(dir, exclude_dots, r);
279
177
}
280
281
183
Status EnvWrapper::DeleteFile(const std::string& f) {
282
183
  return target_->DeleteFile(f);
283
183
}
284
285
7.47k
Status EnvWrapper::CreateDir(const std::string& d) {
286
7.47k
  return target_->CreateDir(d);
287
7.47k
}
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.53k
Status EnvWrapper::DeleteRecursively(const std::string& d) {
298
1.53k
  return target_->DeleteRecursively(d);
299
1.53k
}
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
904
Status EnvWrapper::IsDirectory(const std::string& path, bool* is_dir) {
350
904
  return target_->IsDirectory(path, is_dir);
351
904
}
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
162
Status EnvWrapper::Canonicalize(const std::string& path, std::string* result) {
364
162
  return target_->Canonicalize(path, result);
365
162
}
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