-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Expand file tree
/
Copy pathDiskBackup.cpp
More file actions
280 lines (222 loc) · 8.1 KB
/
DiskBackup.cpp
File metadata and controls
280 lines (222 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <Disks/DiskBackup.h>
#include <Common/logger_useful.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <Backups/BackupFactory.h>
#include <Disks/DiskFactory.h>
namespace DB
{
namespace ErrorCodes
{
extern const int UNSUPPORTED_METHOD;
}
namespace
{
class DiskBackupDirectoryIterator final : public IDirectoryIterator
{
public:
explicit DiskBackupDirectoryIterator(std::string dir_path_, std::vector<std::string> files_)
: dir_path(std::move(dir_path_)), files(std::move(files_))
{
}
void next() override { ++index; }
bool isValid() const override { return index < files.size(); }
String path() const override
{
return fmt::format("{}/{}", dir_path, files[index]);
}
String name() const override { return files[index]; }
private:
std::string dir_path;
std::vector<std::string> files;
size_t index = 0;
};
}
DiskBackup::DiskBackup(std::shared_ptr<IBackup> backup_,
PathPrefixReplacement path_prefix_replacement_,
const Poco::Util::AbstractConfiguration & config,
const String & config_prefix)
: IDisk("backup", config, config_prefix)
, backup(std::move(backup_))
, path_prefix_replacement(std::move(path_prefix_replacement_))
, logger(getLogger("DiskBackup"))
{
}
ReservationPtr DiskBackup::reserve(UInt64)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support reserve method");
}
ReservationPtr DiskBackup::reserve(UInt64, const ReservationConstraints &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support reserve method");
}
std::optional<UInt64> DiskBackup::getTotalSpace() const
{
return backup->getTotalSize();
}
std::optional<UInt64> DiskBackup::getAvailableSpace() const
{
return 0;
}
std::optional<UInt64> DiskBackup::getUnreservedSpace() const
{
return 0;
}
bool DiskBackup::existsFileOrDirectory(const String & path) const
{
std::string replaced_path = replacePathPrefix(path);
return backup->fileExists(replaced_path) || backup->directoryExists(replaced_path);
}
bool DiskBackup::existsFile(const String & path) const
{
std::string replaced_path = replacePathPrefix(path);
return backup->fileExists(replaced_path);
}
bool DiskBackup::existsDirectory(const String & path) const
{
std::string replaced_path = replacePathPrefix(path);
return backup->directoryExists(replaced_path);
}
size_t DiskBackup::getFileSize(const String & path) const
{
std::string replaced_path = replacePathPrefix(path);
return backup->getFileSize(replaced_path);
}
void DiskBackup::createDirectory(const String & path)
{
std::string replaced_path = replacePathPrefix(path);
if (!backup->directoryExists(replaced_path))
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support createDirectory method");
}
void DiskBackup::createDirectories(const String & path)
{
std::string replaced_path = replacePathPrefix(path);
if (!backup->directoryExists(replaced_path))
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support createDirectories method");
}
void DiskBackup::moveDirectory(const String &, const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support moveDirectory method");
}
DirectoryIteratorPtr DiskBackup::iterateDirectory(const String & path) const
{
std::string replaced_path = replacePathPrefix(path);
return std::make_unique<DiskBackupDirectoryIterator>(replaced_path, backup->listFiles(replaced_path, false /*recursive*/));
}
void DiskBackup::moveFile(const String &, const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support moveFile method");
}
void DiskBackup::replaceFile(const String &, const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support replaceFile method");
}
std::unique_ptr<ReadBufferFromFileBase>
DiskBackup::readFile(const String & path, const ReadSettings &, std::optional<size_t>) const
{
std::string replaced_path = replacePathPrefix(path);
return backup->readFile(replaced_path);
}
std::unique_ptr<WriteBufferFromFileBase> DiskBackup::writeFile(const String &, size_t, WriteMode, const WriteSettings &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support writeFile method");
}
std::vector<String> DiskBackup::getBlobPath(const String &) const
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support getBlobPath method");
}
void DiskBackup::writeFileUsingBlobWritingFunction(const String &, WriteMode, WriteBlobFunction &&)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support writeFileUsingBlobWritingFunction method");
}
void DiskBackup::removeFile(const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support removeFile method");
}
void DiskBackup::removeFileIfExists(const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support removeFileIfExists method");
}
void DiskBackup::removeDirectory(const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support removeDirectory method");
}
void DiskBackup::removeRecursive(const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support removeRecursive method");
}
void DiskBackup::listFiles(const String & path, std::vector<String> & file_names) const
{
std::string replaced_path = replacePathPrefix(path);
file_names = backup->listFiles(replaced_path, false /*recursive*/);
}
void DiskBackup::setLastModified(const String & path, const Poco::Timestamp & timestamp)
{
std::string replaced_path = replacePathPrefix(path);
std::lock_guard lock(mutex);
last_modified[replaced_path] = timestamp;
}
Poco::Timestamp DiskBackup::getLastModified(const String & path) const
{
std::string replaced_path = replacePathPrefix(path);
{
std::lock_guard lock(mutex);
auto it = last_modified.find(replaced_path);
if (it != last_modified.end())
return it->second;
}
return backup->getTimestamp();
}
time_t DiskBackup::getLastChanged(const String &) const
{
return backup->getTimestamp();
}
void DiskBackup::createHardLink(const String &, const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support createHardLink method");
}
void DiskBackup::truncateFile(const String &, size_t)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support truncateFile method");
}
void DiskBackup::createFile(const String &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support createFile method");
}
void DiskBackup::setReadOnly(const String &)
{
}
void DiskBackup::copyDirectoryContent(
const String & /*from_dir*/,
const std::shared_ptr<IDisk> & /*to_disk*/,
const String & /*to_dir*/,
const ReadSettings & /*read_settings*/,
const WriteSettings & /*write_settings*/,
const std::function<void()> & /*cancellation_hook*/)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support copyDirectoryContent method");
}
SyncGuardPtr DiskBackup::getDirectorySyncGuard(const String &) const
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support getDirectorySyncGuard method");
}
void DiskBackup::applyNewSettings(const Poco::Util::AbstractConfiguration &, ContextPtr, const String &, const DisksMap &)
{
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "DiskBackup does not support applyNewSettings method");
}
DataSourceDescription DiskBackup::getDataSourceDescription() const
{
DataSourceDescription description;
description.type = DataSourceType::ObjectStorage;
description.object_storage_type = ObjectStorageType::None;
description.metadata_type = MetadataStorageType::None;
description.description = "DiskBackup";
return description;
}
String DiskBackup::replacePathPrefix(const String & path) const
{
if (path.starts_with(path_prefix_replacement.from))
return path_prefix_replacement.to + path.substr(path_prefix_replacement.from.size());
return path;
}
}