From badeb9a7f0e9cff11a9d49676d2d8c61fb6e0947 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 23 Jun 2021 02:03:22 +0300 Subject: [PATCH] Automatically detect whether colon can be used in a directory name. --- telegram-bot-api/Client.cpp | 10 +++++----- telegram-bot-api/ClientParameters.h | 1 + telegram-bot-api/telegram-bot-api.cpp | 11 +++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index a653445..7c7f8a4 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -3456,13 +3456,13 @@ void Client::start_up() { set_tag(bot_token_id_); auto suff = bot_token_with_dc_ + TD_DIR_SLASH; -#if TD_PORT_WINDOWS - for (auto &c : suff) { - if (c == ':') { - c = '~'; + if (!parameters_->allow_colon_in_filenames_) { + for (auto &c : suff) { + if (c == ':') { + c = '~'; + } } } -#endif dir_ = parameters_->working_directory_ + suff; class TdCallback : public td::TdCallback { diff --git a/telegram-bot-api/ClientParameters.h b/telegram-bot-api/ClientParameters.h index e686887..a0f2333 100644 --- a/telegram-bot-api/ClientParameters.h +++ b/telegram-bot-api/ClientParameters.h @@ -55,6 +55,7 @@ struct SharedData { struct ClientParameters { td::string working_directory_; + bool allow_colon_in_filenames_ = true; bool local_mode_ = false; diff --git a/telegram-bot-api/telegram-bot-api.cpp b/telegram-bot-api/telegram-bot-api.cpp index 6454651..c43ad34 100644 --- a/telegram-bot-api/telegram-bot-api.cpp +++ b/telegram-bot-api/telegram-bot-api.cpp @@ -382,6 +382,17 @@ int main(int argc, char *argv[]) { } r_temp_file.ok_ref().first.close(); td::unlink(r_temp_file.ok().second).ensure(); + + auto r_temp_dir = td::mkdtemp(working_directory, "1:a"); + if (r_temp_dir.is_error()) { + parameters->allow_colon_in_filenames_ = false; + r_temp_dir = td::mkdtemp(working_directory, "1~a"); + if (r_temp_dir.is_error()) { + return td::Status::Error(PSLICE() << "Can't create directories in the directory \"" << working_directory + << "\". Use --dir option to specify a writable working directory"); + } + } + td::rmdir(r_temp_dir.ok()).ensure(); } if (!temporary_directory.empty()) {