YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/file_util.cc
Line
Count
Source (jump to first uncovered line)
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#include "yb/rocksdb/util/file_util.h"
22
23
#include <algorithm>
24
#include <vector>
25
26
#include "yb/rocksdb/env.h"
27
#include "yb/rocksdb/options.h"
28
#include "yb/rocksdb/util/file_reader_writer.h"
29
#include "yb/rocksdb/util/sst_file_manager_impl.h"
30
31
#include "yb/util/path_util.h"
32
33
namespace rocksdb {
34
35
using std::string;
36
using std::vector;
37
using strings::Substitute;
38
39
// Utility function to copy a file up to a specified length
40
Status CopyFile(Env* env, const string& source,
41
22.4k
                const string& destination, uint64_t size) {
42
22.4k
  const EnvOptions soptions;
43
22.4k
  Status s;
44
22.4k
  unique_ptr<SequentialFileReader> src_reader;
45
22.4k
  unique_ptr<WritableFileWriter> dest_writer;
46
47
22.4k
  {
48
22.4k
    unique_ptr<SequentialFile> srcfile;
49
22.4k
    s = env->NewSequentialFile(source, &srcfile, soptions);
50
22.4k
    unique_ptr<WritableFile> destfile;
51
22.4k
    if (s.ok()) {
52
22.4k
      s = env->NewWritableFile(destination, &destfile, soptions);
53
1
    } else {
54
1
      return s;
55
1
    }
56
57
22.4k
    if (size == 0) {
58
      // default argument means copy everything
59
20.9k
      if (s.ok()) {
60
20.9k
        s = env->GetFileSize(source, &size);
61
1
      } else {
62
1
        return s;
63
1
      }
64
22.4k
    }
65
22.4k
    src_reader.reset(new SequentialFileReader(std::move(srcfile)));
66
22.4k
    dest_writer.reset(new WritableFileWriter(std::move(destfile), soptions));
67
22.4k
  }
68
69
22.4k
  uint8_t buffer[4096];
70
22.4k
  Slice slice;
71
45.3k
  while (size > 0) {
72
22.9k
    size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size));
73
22.9k
    if (s.ok()) {
74
22.9k
      s = src_reader->Read(bytes_to_read, &slice, buffer);
75
22.9k
    }
76
22.9k
    if (s.ok()) {
77
22.9k
      if (slice.size() == 0) {
78
0
        return STATUS(Corruption, "file too small");
79
0
      }
80
22.9k
      s = dest_writer->Append(slice);
81
22.9k
    }
82
22.9k
    if (!s.ok()) {
83
0
      return s;
84
0
    }
85
22.9k
    size -= slice.size();
86
22.9k
  }
87
22.4k
  return Status::OK();
88
22.4k
}
89
90
218
Status CheckIfDeleted(Env* env, const string& fname, const Status& s_del) {
91
218
  if (!s_del.ok()) {
92
    // NotFound is ok, the file was concurrently deleted.
93
27
    if (env->FileExists(fname).IsNotFound()) {
94
27
      return Status::OK(); // Already deleted.
95
27
    }
96
191
  }
97
191
  return s_del; // Successfully deleted or IO error.
98
191
}
99
100
218
Status DeleteRecursively(Env* env, const string& dirname) {
101
  // Some sanity checks.
102
218
  SCHECK(
103
218
      dirname != "/" && dirname != "./" && dirname != "." && dirname != "",
104
218
      InvalidArgument, yb::Format("Invalid folder to delete: $0", dirname));
105
106
218
  if (!env->DirExists(dirname)) {
107
    // Try to delete as usual file.
108
215
    return CheckIfDeleted(env, dirname, env->DeleteFile(dirname));
109
215
  }
110
111
3
  vector<string> subchildren;
112
3
  RETURN_NOT_OK(env->GetChildren(dirname, &subchildren));
113
194
  for (const string& subchild : subchildren) {
114
194
    if (subchild != "." && subchild != "..") {
115
188
      RETURN_NOT_OK(DeleteRecursively(env, yb::JoinPathSegments(dirname, subchild)));
116
188
    }
117
194
  }
118
119
3
  return CheckIfDeleted(env, dirname, env->DeleteDir(dirname));
120
3
}
121
122
Status DeleteSSTFile(const DBOptions* db_options, const string& fname,
123
110k
                     uint32_t path_id) {
124
  // TODO(tec): support sst_file_manager for multiple path_ids
125
110k
  auto sfm =
126
110k
      static_cast<SstFileManagerImpl*>(db_options->sst_file_manager.get());
127
110k
  if (sfm && path_id == 0) {
128
192
    return sfm->ScheduleFileDeletion(fname);
129
110k
  } else {
130
110k
    return db_options->env->DeleteFile(fname);
131
110k
  }
132
110k
}
133
134
}  // namespace rocksdb