Use absolute file paths.

This commit is contained in:
levlam 2021-06-22 21:26:41 +03:00
parent ad55377cba
commit 08a92a2347
5 changed files with 52 additions and 29 deletions

View File

@ -3455,9 +3455,6 @@ void Client::start_up() {
set_context(context); set_context(context);
set_tag(bot_token_id_); set_tag(bot_token_id_);
auto r_absolute_dir = td::realpath(td::string(".") + TD_DIR_SLASH, true);
CHECK(r_absolute_dir.is_ok());
absolute_dir_ = r_absolute_dir.move_as_ok();
auto suff = bot_token_with_dc_ + TD_DIR_SLASH; auto suff = bot_token_with_dc_ + TD_DIR_SLASH;
#if TD_PORT_WINDOWS #if TD_PORT_WINDOWS
for (auto &c : suff) { for (auto &c : suff) {
@ -3466,11 +3463,7 @@ void Client::start_up() {
} }
} }
#endif #endif
dir_ = td::string(".") + TD_DIR_SLASH + suff; dir_ = parameters_->working_directory_ + suff;
if (absolute_dir_.back() != TD_DIR_SLASH) {
absolute_dir_ += TD_DIR_SLASH;
}
absolute_dir_ += suff;
class TdCallback : public td::TdCallback { class TdCallback : public td::TdCallback {
public: public:
@ -4464,8 +4457,8 @@ void Client::on_closed() {
td::ActorId<Client> parent_; td::ActorId<Client> parent_;
void start_up() override { void start_up() override {
CHECK(!dir_.empty()); CHECK(dir_.size() >= 24);
CHECK(dir_[0] == '.'); CHECK(dir_.back() == TD_DIR_SLASH);
td::rmrf(dir_).ignore(); td::rmrf(dir_).ignore();
stop(); stop();
} }
@ -7657,7 +7650,7 @@ void Client::do_get_file(object_ptr<td_api::file> file, PromisedQueryPtr query)
auto file_id = file->id_; auto file_id = file->id_;
file_download_listeners_[file_id].push_back(std::move(query)); file_download_listeners_[file_id].push_back(std::move(query));
if (file->local_->is_downloading_completed_) { if (file->local_->is_downloading_completed_) {
Slice relative_path = td::PathView::relative(file->local_->path_, absolute_dir_, true); Slice relative_path = td::PathView::relative(file->local_->path_, dir_, true);
if (!relative_path.empty()) { if (!relative_path.empty()) {
auto r_stat = td::stat(file->local_->path_); auto r_stat = td::stat(file->local_->path_);
if (r_stat.is_ok() && r_stat.ok().is_reg_ && r_stat.ok().size_ == file->size_) { if (r_stat.is_ok() && r_stat.ok().is_reg_ && r_stat.ok().size_ == file->size_) {
@ -8293,7 +8286,7 @@ void Client::json_store_file(td::JsonObjectScope &object, const td_api::file *fi
object("file_path", td::JsonRawString(file->local_->path_)); object("file_path", td::JsonRawString(file->local_->path_));
} }
} else { } else {
Slice relative_path = td::PathView::relative(file->local_->path_, absolute_dir_, true); Slice relative_path = td::PathView::relative(file->local_->path_, dir_, true);
if (!relative_path.empty() && file->local_->downloaded_size_ <= MAX_DOWNLOAD_FILE_SIZE) { if (!relative_path.empty() && file->local_->downloaded_size_ <= MAX_DOWNLOAD_FILE_SIZE) {
object("file_path", relative_path); object("file_path", relative_path);
} }

View File

@ -902,7 +902,6 @@ class Client : public WebhookActor::Callback {
int64 current_bot_resolve_query_id_ = 1; int64 current_bot_resolve_query_id_ = 1;
td::string dir_; td::string dir_;
td::string absolute_dir_;
td::ActorOwn<td::ClientActor> td_client_; td::ActorOwn<td::ClientActor> td_client_;
td::ActorContext context_; td::ActorContext context_;
std::queue<PromisedQueryPtr> cmd_queue_; std::queue<PromisedQueryPtr> cmd_queue_;

View File

@ -266,7 +266,7 @@ void ClientManager::start_up() {
td::vector<td::uint64> failed_to_replay_log_event_ids; td::vector<td::uint64> failed_to_replay_log_event_ids;
td::int64 loaded_event_count = 0; td::int64 loaded_event_count = 0;
binlog binlog
->init("tqueue.binlog", ->init(parameters_->working_directory_ + "tqueue.binlog",
[&](const td::BinlogEvent &event) { [&](const td::BinlogEvent &event) {
if (tqueue_binlog->replay(event, *tqueue).is_error()) { if (tqueue_binlog->replay(event, *tqueue).is_error()) {
failed_to_replay_log_event_ids.push_back(event.id_); failed_to_replay_log_event_ids.push_back(event.id_);
@ -295,7 +295,8 @@ void ClientManager::start_up() {
// init webhook_db // init webhook_db
auto concurrent_webhook_db = td::make_unique<td::BinlogKeyValue<td::ConcurrentBinlog>>(); auto concurrent_webhook_db = td::make_unique<td::BinlogKeyValue<td::ConcurrentBinlog>>();
auto status = concurrent_webhook_db->init("webhooks_db.binlog", td::DbKey::empty(), scheduler_id); auto status = concurrent_webhook_db->init(parameters_->working_directory_ + "webhooks_db.binlog", td::DbKey::empty(),
scheduler_id);
LOG_IF(FATAL, status.is_error()) << "Can't open webhooks_db.binlog " << status.error(); LOG_IF(FATAL, status.is_error()) << "Can't open webhooks_db.binlog " << status.error();
parameters_->shared_data_->webhook_db_ = std::move(concurrent_webhook_db); parameters_->shared_data_->webhook_db_ = std::move(concurrent_webhook_db);

View File

@ -54,6 +54,8 @@ struct SharedData {
}; };
struct ClientParameters { struct ClientParameters {
td::string working_directory_;
bool local_mode_ = false; bool local_mode_ = false;
td::int32 api_id_ = 0; td::int32 api_id_ = 0;

View File

@ -36,6 +36,7 @@
#include "td/utils/MemoryLog.h" #include "td/utils/MemoryLog.h"
#include "td/utils/misc.h" #include "td/utils/misc.h"
#include "td/utils/OptionParser.h" #include "td/utils/OptionParser.h"
#include "td/utils/PathView.h"
#include "td/utils/port/IPAddress.h" #include "td/utils/port/IPAddress.h"
#include "td/utils/port/path.h" #include "td/utils/port/path.h"
#include "td/utils/port/rlimit.h" #include "td/utils/port/rlimit.h"
@ -208,7 +209,7 @@ int main(int argc, char *argv[]) {
int default_verbosity_level = 0; int default_verbosity_level = 0;
int memory_verbosity_level = VERBOSITY_NAME(INFO); int memory_verbosity_level = VERBOSITY_NAME(INFO);
td::int64 log_max_file_size = 2000000000; td::int64 log_max_file_size = 2000000000;
td::string working_directory; td::string working_directory = PSTRING() << "." << TD_DIR_SLASH;
td::string temporary_directory; td::string temporary_directory;
td::string username; td::string username;
td::string groupname; td::string groupname;
@ -362,29 +363,54 @@ int main(int argc, char *argv[]) {
TRY_STATUS_PREFIX(td::change_user(username, groupname), "Can't change effective user: "); TRY_STATUS_PREFIX(td::change_user(username, groupname), "Can't change effective user: ");
} }
if (!working_directory.empty()) { {
TRY_STATUS_PREFIX(td::chdir(working_directory), "Can't set working directory: "); TRY_RESULT_PREFIX_ASSIGN(working_directory, td::realpath(working_directory, true),
"Invalid working directory specified: ");
if (working_directory.empty()) {
return td::Status::Error("Working directory can't be empty");
}
if (working_directory.back() != TD_DIR_SLASH) {
working_directory += TD_DIR_SLASH;
}
TRY_STATUS_PREFIX(td::mkpath(working_directory, 0750), "Failed to create working directory: ");
auto r_temp_file = td::mkstemp(working_directory);
if (r_temp_file.is_error()) {
return td::Status::Error(PSLICE() << "Can't create files in the directory \"" << working_directory
<< "\". Use --dir option to specify a writable working directory");
}
r_temp_file.ok_ref().first.close();
td::unlink(r_temp_file.ok().second).ensure();
} }
if (!temporary_directory.empty()) { if (!temporary_directory.empty()) {
if (td::PathView(temporary_directory).is_relative()) {
temporary_directory = working_directory + temporary_directory;
}
TRY_STATUS_PREFIX(td::set_temporary_dir(temporary_directory), "Can't set temporary directory: "); TRY_STATUS_PREFIX(td::set_temporary_dir(temporary_directory), "Can't set temporary directory: ");
} }
auto temp_dir = td::get_temporary_dir(); { // check temporary directory
if (temp_dir.empty()) { auto temp_dir = td::get_temporary_dir();
return td::Status::Error("Can't find directory for temporary files. Use --temp-dir option to specify it"); if (temp_dir.empty()) {
} return td::Status::Error("Can't find directory for temporary files. Use --temp-dir option to specify it");
}
auto r_temp_file = td::mkstemp(temp_dir); auto r_temp_file = td::mkstemp(temp_dir);
if (r_temp_file.is_error()) { if (r_temp_file.is_error()) {
return td::Status::Error( return td::Status::Error(PSLICE()
PSLICE() << "Can't create files in the directory \"" << temp_dir << "Can't create files in the directory \"" << temp_dir
<< "\". Use --temp-dir option to specify another directory for temporary files"); << "\". Use --temp-dir option to specify another directory for temporary files");
}
r_temp_file.ok_ref().first.close();
td::unlink(r_temp_file.ok().second).ensure();
} }
r_temp_file.ok_ref().first.close();
td::unlink(r_temp_file.ok().second).ensure();
if (!log_file_path.empty()) { if (!log_file_path.empty()) {
if (td::PathView(log_file_path).is_relative()) {
log_file_path = working_directory + log_file_path;
}
TRY_STATUS_PREFIX(file_log.init(log_file_path, log_max_file_size), "Can't open log file: "); TRY_STATUS_PREFIX(file_log.init(log_file_path, log_max_file_size), "Can't open log file: ");
log.set_first(&ts_log); log.set_first(&ts_log);
} }
@ -397,6 +423,8 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
parameters->working_directory_ = std::move(working_directory);
if (parameters->default_max_webhook_connections_ <= 0) { if (parameters->default_max_webhook_connections_ <= 0) {
parameters->default_max_webhook_connections_ = parameters->local_mode_ ? 100 : 40; parameters->default_max_webhook_connections_ = parameters->local_mode_ ? 100 : 40;
} }