/
datastore_restore.cpp
166 lines (152 loc) · 6.47 KB
/
datastore_restore.cpp
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
/*
* Copyright 2022-2023 Project Tsurugi.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <boost/filesystem/operations.hpp>
#include <boost/foreach.hpp>
#include <glog/logging.h>
#include <limestone/logging.h>
#include "logging_helper.h"
#include <limestone/api/datastore.h>
#include <limestone/status.h>
#include "internal.h"
namespace limestone::api {
static constexpr const char *version_error_prefix = "/:limestone unsupported backup persistent format version: "
"see https://github.com/project-tsurugi/tsurugidb/blob/master/docs/upgrade-guide.md";
status datastore::restore(std::string_view from, bool keep_backup) const noexcept {
VLOG_LP(log_debug) << "restore begin, from directory = " << from << " , keep_backup = " << std::boolalpha << keep_backup;
auto from_dir = boost::filesystem::path(std::string(from));
// log_dir version check
boost::filesystem::path manifest_path = from_dir / std::string(internal::manifest_file_name);
if (!boost::filesystem::exists(manifest_path)) {
VLOG_LP(log_info) << "no manifest file in backup";
LOG(ERROR) << version_error_prefix << " (version mismatch: version 0, server supports version 1)";
return status::err_broken_data;
}
std::string ver_err;
int vc = internal::is_supported_version(manifest_path, ver_err);
if (vc == 0) {
LOG(ERROR) << version_error_prefix << " (" << ver_err << ")";
return status::err_broken_data;
}
if (vc < 0) {
VLOG_LP(log_info) << ver_err;
LOG(ERROR) << "/:limestone backup data is corrupted, can not use.";
return status::err_broken_data;
}
BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::directory_iterator(location_), boost::filesystem::directory_iterator())) {
if(!boost::filesystem::is_directory(p)) {
try {
boost::filesystem::remove(p);
} catch (boost::filesystem::filesystem_error& ex) {
LOG_LP(ERROR) << ex.what() << " file = " << p.string();
return status::err_permission_error;
}
}
}
BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::directory_iterator(from_dir), boost::filesystem::directory_iterator())) {
try {
boost::filesystem::copy_file(p, location_ / p.filename());
}
catch (boost::filesystem::filesystem_error& ex) {
LOG_LP(ERROR) << ex.what() << " file = " << p.string();
return status::err_permission_error;
}
}
if (!keep_backup) {
BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::directory_iterator(from_dir), boost::filesystem::directory_iterator())) {
try {
boost::filesystem::remove(p);
}
catch (boost::filesystem::filesystem_error& ex) {
LOG_LP(WARNING) << ex.what() << " file = " << p.string();
}
}
}
return status::ok;
}
// prusik era
status datastore::restore(std::string_view from, std::vector<file_set_entry>& entries) {
VLOG_LP(log_debug) << "restore (from prusik) begin, from directory = " << from;
auto from_dir = boost::filesystem::path(std::string(from));
// log_dir version check
int manifest_count = 0;
for (auto & ent : entries) {
if (ent.destination_path().string() != internal::manifest_file_name) {
continue;
}
boost::filesystem::path src{ent.source_path()};
if (src.is_absolute()) {
// use it
} else {
src = from_dir / src;
}
if (!boost::filesystem::exists(src) || !boost::filesystem::is_regular_file(src)) {
LOG_LP(ERROR) << "file not found : file = " << src.string();
return status::err_not_found;
}
std::string ver_err;
int vc = internal::is_supported_version(src, ver_err);
if (vc == 0) {
LOG(ERROR) << version_error_prefix << " (" << ver_err << ")";
return status::err_broken_data;
}
if (vc < 0) {
VLOG_LP(log_info) << ver_err;
LOG(ERROR) << "/:limestone backup data is corrupted, can not use.";
return status::err_broken_data;
}
manifest_count++;
}
if (manifest_count < 1) { // XXX: change to != 1 ??
VLOG_LP(log_info) << "no manifest file in backup";
LOG(ERROR) << version_error_prefix << " (version mismatch: version 0, server supports version 1)";
return status::err_broken_data;
}
// purge logdir
// FIXME: copied this code from (old) restore(), fix duplicate
BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::directory_iterator(location_), boost::filesystem::directory_iterator())) {
if(!boost::filesystem::is_directory(p)) {
try {
boost::filesystem::remove(p);
} catch (boost::filesystem::filesystem_error& ex) {
LOG_LP(ERROR) << ex.what() << " file = " << p.string();
return status::err_permission_error;
}
}
}
for (auto & ent : entries) {
boost::filesystem::path src{ent.source_path()};
boost::filesystem::path dst{ent.destination_path()};
if (src.is_absolute()) {
// use it
} else {
src = from_dir / src;
}
// TODO: location check (for security)
// TODO: assert dst.is_relative()
if (!boost::filesystem::exists(src) || !boost::filesystem::is_regular_file(src)) {
LOG_LP(ERROR) << "file not found : file = " << src.string();
return status::err_not_found;
}
try {
boost::filesystem::copy_file(src, location_ / dst);
} catch (boost::filesystem::filesystem_error& ex) {
LOG_LP(ERROR) << ex.what() << " file = " << src.string();
return status::err_permission_error;
}
}
return status::ok;
}
} // namespace limestone::api