YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/env_util.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_UTIL_ENV_UTIL_H
33
#define YB_UTIL_ENV_UTIL_H
34
35
#include <memory>
36
#include <string>
37
38
#include "yb/gutil/macros.h"
39
#include "yb/util/env.h"
40
41
namespace yb {
42
namespace env_util {
43
44
// Searches for the suitable "YugaByte distribution root" directory that contains the given
45
// subdirectory. If the YB_HOME environment variable is specified, the value of that variable is
46
// immediately returned. Otherwise, we start walking up from the directory of the current executable
47
// until we are in a directory that has a subdirectory with the given name. Then we return the path
48
// of that directory (not of the subdirectory).
49
std::string GetRootDir(const std::string& search_for_dir);
50
51
Status OpenFileForWrite(Env *env, const std::string &path,
52
                        std::shared_ptr<WritableFile> *file);
53
54
Status OpenFileForWrite(const WritableFileOptions& opts,
55
                        Env *env, const std::string &path,
56
                        std::shared_ptr<WritableFile> *file);
57
58
Status OpenFileForRandom(Env *env, const std::string &path,
59
                         std::shared_ptr<RandomAccessFile> *file);
60
61
Status OpenFileForSequential(Env *env, const std::string &path,
62
                             std::shared_ptr<SequentialFile> *file);
63
64
// Read exactly 'n' bytes from the given file. If fewer than 'n' bytes
65
// are read, returns an IOError. This differs from the underlying
66
// RandomAccessFile::Read(), which may return a "short read".
67
//
68
// Similar to RandomAccessFile::Read(), '*result' is modified to point
69
// to the bytes which were read. These bytes may be a copy placed in
70
// the 'scratch' buffer, or result may point into the underlying file
71
// (e.g. via mmap or other zero-copy mechanism).
72
//
73
// NOTE: even if this returns an error, some data _may_ be read into
74
// the provided scratch buffer, but no guarantee that that will be the
75
// case.
76
Status ReadFully(RandomAccessFile* file, uint64_t offset, size_t n,
77
                 Slice* result, uint8_t* scratch);
78
79
// Creates the directory given by 'path', unless it already exists.
80
//
81
// If 'created' is not NULL, sets it to true if the directory was
82
// created, false otherwise.
83
Status CreateDirIfMissing(Env* env, const std::string& path,
84
                          bool* created = NULL);
85
86
// Copy the contents of file source_path to file dest_path.
87
// This is not atomic, and if there is an error while reading or writing,
88
// a partial copy may be left in 'dest_path'. Does not fsync the parent
89
// directory of dest_path -- if you need durability then do that yourself.
90
Status CopyFile(
91
    Env* env, const std::string& source_path, const std::string& dest_path,
92
    WritableFileOptions opts = WritableFileOptions());
93
94
// Deletes a file or directory when this object goes out of scope.
95
//
96
// The deletion may be cancelled by calling .Cancel().
97
// This is typically useful for cleaning up temporary files if the
98
// creation of the tmp file may fail.
99
class ScopedFileDeleter {
100
 public:
101
  ScopedFileDeleter(Env* env, std::string path);
102
  ~ScopedFileDeleter();
103
  ScopedFileDeleter(ScopedFileDeleter&& other) = default;
104
105
  // Do not delete the file when this object goes out of scope.
106
1.21M
  void Cancel() {
107
1.21M
    should_delete_ = false;
108
1.21M
  }
109
110
0
  const std::string& path() {
111
0
    return path_;
112
0
  }
113
114
 private:
115
  Env* const env_;
116
  const std::string path_;
117
  bool should_delete_;
118
119
  DISALLOW_COPY_AND_ASSIGN(ScopedFileDeleter);
120
};
121
122
} // namespace env_util
123
} // namespace yb
124
125
#endif  // YB_UTIL_ENV_UTIL_H