/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 | 23.5k | const string& destination, uint64_t size) { |
42 | 23.5k | const EnvOptions soptions; |
43 | 23.5k | Status s; |
44 | 23.5k | unique_ptr<SequentialFileReader> src_reader; |
45 | 23.5k | unique_ptr<WritableFileWriter> dest_writer; |
46 | | |
47 | 23.5k | { |
48 | 23.5k | unique_ptr<SequentialFile> srcfile; |
49 | 23.5k | s = env->NewSequentialFile(source, &srcfile, soptions); |
50 | 23.5k | unique_ptr<WritableFile> destfile; |
51 | 23.5k | if (s.ok()) { |
52 | 23.5k | s = env->NewWritableFile(destination, &destfile, soptions); |
53 | 23.5k | } else { |
54 | 0 | return s; |
55 | 0 | } |
56 | | |
57 | 23.5k | if (size == 0) { |
58 | | // default argument means copy everything |
59 | 21.4k | if (s.ok()) { |
60 | 21.4k | s = env->GetFileSize(source, &size); |
61 | 21.4k | } else { |
62 | 1 | return s; |
63 | 1 | } |
64 | 21.4k | } |
65 | 23.5k | src_reader.reset(new SequentialFileReader(std::move(srcfile))); |
66 | 23.5k | dest_writer.reset(new WritableFileWriter(std::move(destfile), soptions)); |
67 | 23.5k | } |
68 | | |
69 | 0 | uint8_t buffer[4096]; |
70 | 23.5k | Slice slice; |
71 | 47.6k | while (size > 0) { |
72 | 24.0k | size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size)); |
73 | 24.0k | if (s.ok()) { |
74 | 24.0k | s = src_reader->Read(bytes_to_read, &slice, buffer); |
75 | 24.0k | } |
76 | 24.0k | if (s.ok()) { |
77 | 24.0k | if (slice.size() == 0) { |
78 | 0 | return STATUS(Corruption, "file too small"); |
79 | 0 | } |
80 | 24.0k | s = dest_writer->Append(slice); |
81 | 24.0k | } |
82 | 24.0k | if (!s.ok()) { |
83 | 0 | return s; |
84 | 0 | } |
85 | 24.0k | size -= slice.size(); |
86 | 24.0k | } |
87 | 23.5k | return Status::OK(); |
88 | 23.5k | } |
89 | | |
90 | 222 | Status CheckIfDeleted(Env* env, const string& fname, const Status& s_del) { |
91 | 222 | 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 | 27 | } |
97 | 195 | return s_del; // Successfully deleted or IO error. |
98 | 222 | } |
99 | | |
100 | 222 | Status DeleteRecursively(Env* env, const string& dirname) { |
101 | | // Some sanity checks. |
102 | 222 | SCHECK( |
103 | 222 | dirname != "/" && dirname != "./" && dirname != "." && dirname != "", |
104 | 222 | InvalidArgument, yb::Format("Invalid folder to delete: $0", dirname)); |
105 | | |
106 | 222 | if (!env->DirExists(dirname)) { |
107 | | // Try to delete as usual file. |
108 | 219 | return CheckIfDeleted(env, dirname, env->DeleteFile(dirname)); |
109 | 219 | } |
110 | | |
111 | 3 | vector<string> subchildren; |
112 | 3 | RETURN_NOT_OK(env->GetChildren(dirname, &subchildren)); |
113 | 198 | for (const string& subchild : subchildren)3 { |
114 | 198 | if (subchild != "." && subchild != ".."195 ) { |
115 | 192 | RETURN_NOT_OK(DeleteRecursively(env, yb::JoinPathSegments(dirname, subchild))); |
116 | 192 | } |
117 | 198 | } |
118 | | |
119 | 3 | return CheckIfDeleted(env, dirname, env->DeleteDir(dirname)); |
120 | 3 | } |
121 | | |
122 | | Status DeleteSSTFile(const DBOptions* db_options, const string& fname, |
123 | 120k | uint32_t path_id) { |
124 | | // TODO(tec): support sst_file_manager for multiple path_ids |
125 | 120k | auto sfm = |
126 | 120k | static_cast<SstFileManagerImpl*>(db_options->sst_file_manager.get()); |
127 | 120k | if (sfm && path_id == 0196 ) { |
128 | 192 | return sfm->ScheduleFileDeletion(fname); |
129 | 120k | } else { |
130 | 120k | return db_options->env->DeleteFile(fname); |
131 | 120k | } |
132 | 120k | } |
133 | | |
134 | | } // namespace rocksdb |