2019-09-07 10:03:22 +00:00
|
|
|
/*
|
|
|
|
This file is part of TON Blockchain Library.
|
|
|
|
|
|
|
|
TON Blockchain Library is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
TON Blockchain Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-04-10 19:06:01 +00:00
|
|
|
Copyright 2017-2020 Telegram Systems LLP
|
2019-09-07 10:03:22 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/port/path.h"
|
|
|
|
#include "td/utils/filesystem.h"
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
|
|
|
|
#include "common/errorcode.h"
|
|
|
|
|
|
|
|
namespace ton {
|
|
|
|
|
|
|
|
namespace validator {
|
|
|
|
|
|
|
|
namespace db {
|
|
|
|
|
|
|
|
class WriteFile : public td::actor::Actor {
|
|
|
|
public:
|
|
|
|
void start_up() override {
|
|
|
|
auto R = [&]() {
|
|
|
|
td::uint32 cnt = 0;
|
|
|
|
while (true) {
|
|
|
|
cnt++;
|
|
|
|
auto res = td::mkstemp(td::CSlice{tmp_dir_});
|
|
|
|
if (res.is_ok() || cnt >= 10) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (R.is_error()) {
|
|
|
|
promise_.set_error(R.move_as_error());
|
|
|
|
stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto res = R.move_as_ok();
|
|
|
|
auto file = std::move(res.first);
|
|
|
|
auto old_name = res.second;
|
2022-05-15 14:51:24 +00:00
|
|
|
auto status = write_data_(file);
|
|
|
|
if (!status.is_error()) {
|
|
|
|
status = file.sync();
|
|
|
|
}
|
|
|
|
if (status.is_error()) {
|
2024-05-24 06:58:07 +00:00
|
|
|
td::unlink(old_name).ignore();
|
2022-05-15 14:51:24 +00:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
stop();
|
|
|
|
return;
|
2019-09-07 10:03:22 +00:00
|
|
|
}
|
|
|
|
if (new_name_.length() > 0) {
|
2022-05-15 14:51:24 +00:00
|
|
|
status = td::rename(old_name, new_name_);
|
|
|
|
if (status.is_error()) {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
} else {
|
|
|
|
promise_.set_value(std::move(new_name_));
|
|
|
|
}
|
2019-09-07 10:03:22 +00:00
|
|
|
} else {
|
|
|
|
promise_.set_value(std::move(old_name));
|
|
|
|
}
|
|
|
|
stop();
|
|
|
|
}
|
2022-05-15 14:51:24 +00:00
|
|
|
WriteFile(std::string tmp_dir, std::string new_name, std::function<td::Status(td::FileFd&)> write_data,
|
|
|
|
td::Promise<std::string> promise)
|
|
|
|
: tmp_dir_(tmp_dir), new_name_(new_name), write_data_(std::move(write_data)), promise_(std::move(promise)) {
|
|
|
|
}
|
2019-09-07 10:03:22 +00:00
|
|
|
WriteFile(std::string tmp_dir, std::string new_name, td::BufferSlice data, td::Promise<std::string> promise)
|
2022-05-15 14:51:24 +00:00
|
|
|
: tmp_dir_(tmp_dir), new_name_(new_name), promise_(std::move(promise)) {
|
|
|
|
write_data_ = [data_ptr = std::make_shared<td::BufferSlice>(std::move(data))] (td::FileFd& fd) {
|
|
|
|
auto data = std::move(*data_ptr);
|
|
|
|
while (data.size() > 0) {
|
2023-07-14 13:27:59 +00:00
|
|
|
auto piece_size = std::min<size_t>(data.size(), 1 << 30);
|
|
|
|
TRY_RESULT(s, fd.write(data.as_slice().substr(0, piece_size)));
|
2022-05-15 14:51:24 +00:00
|
|
|
data.confirm_read(s);
|
|
|
|
}
|
|
|
|
return td::Status::OK();
|
|
|
|
};
|
2019-09-07 10:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string tmp_dir_;
|
|
|
|
std::string new_name_;
|
2022-05-15 14:51:24 +00:00
|
|
|
std::function<td::Status(td::FileFd&)> write_data_;
|
2019-09-07 10:03:22 +00:00
|
|
|
td::Promise<std::string> promise_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadFile : public td::actor::Actor {
|
|
|
|
public:
|
2019-10-03 13:04:52 +00:00
|
|
|
enum Flags : td::uint32 { f_disable_log = 1 };
|
2019-09-07 10:03:22 +00:00
|
|
|
void start_up() override {
|
2019-09-18 17:46:32 +00:00
|
|
|
auto S = td::read_file(file_name_, max_length_, offset_);
|
2019-09-07 10:03:22 +00:00
|
|
|
if (S.is_ok()) {
|
|
|
|
promise_.set_result(S.move_as_ok());
|
|
|
|
} else {
|
|
|
|
// TODO check error code
|
2019-10-03 13:04:52 +00:00
|
|
|
if (flags_ & Flags::f_disable_log) {
|
|
|
|
LOG(DEBUG) << "missing file " << file_name_;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "missing file " << file_name_;
|
|
|
|
}
|
2019-09-07 10:03:22 +00:00
|
|
|
promise_.set_error(td::Status::Error(ErrorCode::notready, "file does not exist"));
|
|
|
|
}
|
|
|
|
stop();
|
|
|
|
}
|
2019-10-03 13:04:52 +00:00
|
|
|
ReadFile(std::string file_name, td::int64 offset, td::int64 max_length, td::uint32 flags,
|
|
|
|
td::Promise<td::BufferSlice> promise)
|
|
|
|
: file_name_(file_name), offset_(offset), max_length_(max_length), flags_(flags), promise_(std::move(promise)) {
|
2019-09-07 10:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string file_name_;
|
2019-09-18 17:46:32 +00:00
|
|
|
td::int64 offset_;
|
|
|
|
td::int64 max_length_;
|
2019-10-03 13:04:52 +00:00
|
|
|
td::uint32 flags_;
|
2019-09-07 10:03:22 +00:00
|
|
|
td::Promise<td::BufferSlice> promise_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace db
|
|
|
|
|
|
|
|
} // namespace validator
|
|
|
|
|
|
|
|
} // namespace ton
|