YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/file_util.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
13
#ifndef YB_UTIL_FILE_UTIL_H
14
#define YB_UTIL_FILE_UTIL_H
15
16
#include <float.h>
17
#include <string.h>
18
19
#include <chrono>
20
#include <cstdarg>
21
#include <sstream>
22
#include <string>
23
#include <type_traits>
24
25
#include <boost/mpl/and.hpp>
26
27
#include "yb/util/status_fwd.h"
28
#include "yb/util/env.h"
29
#include "yb/util/env_util.h"
30
#include "yb/util/faststring.h"
31
#include "yb/util/format.h"
32
#include "yb/util/path_util.h"
33
#include "yb/util/tostring.h"
34
#include "yb/util/type_traits.h"
35
36
namespace yb {
37
38
YB_STRONGLY_TYPED_BOOL(CreateIfMissing);
39
YB_STRONGLY_TYPED_BOOL(UseHardLinks);
40
41
// TODO(unify_env): Temporary workaround until Env/Files from rocksdb and yb are unified
42
// (https://github.com/yugabyte/yugabyte-db/issues/1661).
43
44
// Following function returns OK if the file at `path` exists.
45
// NotFound if the named file does not exist, the calling process does not have permission to
46
//          determine whether this file exists, or if the path is invalid.
47
// IOError if an IO Error was encountered.
48
// Uses specified `env` environment implementation to do the actual file existence checking.
49
5.44k
inline CHECKED_STATUS CheckFileExistsResult(const Status& status) {
50
5.44k
  return status;
51
5.44k
}
52
53
0
inline CHECKED_STATUS CheckFileExistsResult(bool exists) {
54
0
  return exists ? Status::OK() : STATUS(NotFound, "");
55
0
}
56
57
template <class Env>
58
5.44k
inline CHECKED_STATUS FileExists(Env* env, const std::string& path) {
59
5.44k
  return CheckFileExistsResult(env->FileExists(path));
60
5.44k
}
61
62
using yb::env_util::CopyFile;
63
64
// Copies directory from `src_dir` to `dest_dir` using `env`.
65
// use_hard_links specifies whether to create hard links instead of actual file copying.
66
// create_if_missing specifies whether to create dest dir if doesn't exist or return an error.
67
// Returns error status in case of I/O errors.
68
template <class TEnv>
69
CHECKED_STATUS CopyDirectory(
70
    TEnv* env, const string& src_dir, const string& dest_dir, UseHardLinks use_hard_links,
71
1.82k
    CreateIfMissing create_if_missing) {
72
1.82k
  RETURN_NOT_OK_PREPEND(
73
1.82k
      FileExists(env, src_dir), Format("Source directory does not exist: $0", src_dir));
74
75
1.82k
  Status s = FileExists(env, dest_dir);
76
1.82k
  if (!s.ok()) {
77
1.82k
    if (create_if_missing) {
78
1.82k
      RETURN_NOT_OK_PREPEND(
79
1.82k
          env->CreateDir(dest_dir), Format("Cannot create destination directory: $0", dest_dir));
80
1.82k
    } else {
81
0
      return s.CloneAndPrepend(Format("Destination directory does not exist: $0", dest_dir));
82
0
    }
83
1.82k
  }
84
85
  // Copy files.
86
1.82k
  std::vector<string> files;
87
1.82k
  RETURN_NOT_OK_PREPEND(
88
1.82k
      env->GetChildren(src_dir, &files),
89
1.82k
      Format("Cannot get list of files for directory: $0", src_dir));
90
91
14.4k
  for (const string& file : files) {
92
14.4k
    if (file != "." && 
file != ".."12.6k
) {
93
10.8k
      const auto src_path = JoinPathSegments(src_dir, file);
94
10.8k
      const auto dest_path = JoinPathSegments(dest_dir, file);
95
96
10.8k
      if (use_hard_links) {
97
10.8k
        s = env->LinkFile(src_path, dest_path);
98
99
10.8k
        if (s.ok()) {
100
10.7k
          continue;
101
10.7k
        }
102
10.8k
      }
103
104
27
      if (env->DirExists(src_path)) {
105
27
        RETURN_NOT_OK_PREPEND(
106
27
            CopyDirectory(env, src_path, dest_path, use_hard_links, CreateIfMissing::kTrue),
107
27
            Format("Cannot copy directory: $0", src_path));
108
27
      } else {
109
0
        RETURN_NOT_OK_PREPEND(
110
0
            CopyFile(env, src_path, dest_path), Format("Cannot copy file: $0", src_path));
111
0
      }
112
27
    }
113
14.4k
  }
114
115
1.82k
  return Status::OK();
116
1.82k
}
117
118
}  // namespace yb
119
120
#endif  // YB_UTIL_FILE_UTIL_H