YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
2.58k
inline CHECKED_STATUS CheckFileExistsResult(const Status& status) {
50
2.58k
  return status;
51
2.58k
}
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
2.58k
inline CHECKED_STATUS FileExists(Env* env, const std::string& path) {
59
2.58k
  return CheckFileExistsResult(env->FileExists(path));
60
2.58k
}
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
862
    CreateIfMissing create_if_missing) {
72
862
  RETURN_NOT_OK_PREPEND(
73
862
      FileExists(env, src_dir), Format("Source directory does not exist: $0", src_dir));
74
75
862
  Status s = FileExists(env, dest_dir);
76
862
  if (!s.ok()) {
77
862
    if (create_if_missing) {
78
862
      RETURN_NOT_OK_PREPEND(
79
862
          env->CreateDir(dest_dir), Format("Cannot create destination directory: $0", dest_dir));
80
0
    } else {
81
0
      return s.CloneAndPrepend(Format("Destination directory does not exist: $0", dest_dir));
82
0
    }
83
862
  }
84
85
  // Copy files.
86
862
  std::vector<string> files;
87
862
  RETURN_NOT_OK_PREPEND(
88
862
      env->GetChildren(src_dir, &files),
89
862
      Format("Cannot get list of files for directory: $0", src_dir));
90
91
6.89k
  for (const string& file : files) {
92
6.89k
    if (file != "." && file != "..") {
93
5.17k
      const auto src_path = JoinPathSegments(src_dir, file);
94
5.17k
      const auto dest_path = JoinPathSegments(dest_dir, file);
95
96
5.17k
      if (use_hard_links) {
97
5.17k
        s = env->LinkFile(src_path, dest_path);
98
99
5.17k
        if (s.ok()) {
100
5.17k
          continue;
101
5.17k
        }
102
0
      }
103
104
0
      if (env->DirExists(src_path)) {
105
0
        RETURN_NOT_OK_PREPEND(
106
0
            CopyDirectory(env, src_path, dest_path, use_hard_links, CreateIfMissing::kTrue),
107
0
            Format("Cannot copy directory: $0", src_path));
108
0
      } 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
0
    }
113
6.89k
  }
114
115
862
  return Status::OK();
116
862
}
117
118
}  // namespace yb
119
120
#endif  // YB_UTIL_FILE_UTIL_H