From 8e750b867c8c335bc922a9071ac9230d251252c9 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 23 Jul 2021 20:56:44 +0300 Subject: [PATCH] Simplify PSLICE usage in FileLoaderUtils. --- td/telegram/files/FileLoaderUtils.cpp | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/td/telegram/files/FileLoaderUtils.cpp b/td/telegram/files/FileLoaderUtils.cpp index 32438ab29..8860d6fe0 100644 --- a/td/telegram/files/FileLoaderUtils.cpp +++ b/td/telegram/files/FileLoaderUtils.cpp @@ -29,13 +29,13 @@ namespace td { int VERBOSITY_NAME(file_loader) = VERBOSITY_NAME(DEBUG) + 2; namespace { -Result> try_create_new_file(Result result_name) { - TRY_RESULT(name, std::move(result_name)); +Result> try_create_new_file(CSlice name) { + LOG(DEBUG) << "Trying to create new file " << name; TRY_RESULT(fd, FileFd::open(name, FileFd::Read | FileFd::Write | FileFd::CreateNew, 0640)); return std::make_pair(std::move(fd), name.str()); } -Result> try_open_file(Result result_name) { - TRY_RESULT(name, std::move(result_name)); +Result> try_open_file(CSlice name) { + LOG(DEBUG) << "Trying to open file " << name; TRY_RESULT(fd, FileFd::open(name, FileFd::Read, 0640)); return std::make_pair(std::move(fd), name.str()); } @@ -76,33 +76,26 @@ Result> open_temp_file(FileType file_type) { template bool for_suggested_file_name(CSlice name, bool use_pmc, bool use_random, F &&callback) { - auto try_callback = [&](Result r_path) { - if (r_path.is_error()) { - return true; - } - LOG(DEBUG) << "Trying " << r_path.ok(); - return callback(r_path.move_as_ok()); - }; auto cleaned_name = clean_filename(name); PathView path_view(cleaned_name); auto stem = path_view.file_stem(); auto ext = path_view.extension(); bool active = true; if (!stem.empty() && !G()->parameters().ignore_file_names) { - active = try_callback(PSLICE() << stem << Ext{ext}); + active = callback(PSLICE() << stem << Ext{ext}); for (int i = 0; active && i < 10; i++) { - active = try_callback(PSLICE() << stem << "_(" << i << ")" << Ext{ext}); + active = callback(PSLICE() << stem << "_(" << i << ")" << Ext{ext}); } for (int i = 2; active && i < 12 && use_random; i++) { - active = try_callback(PSLICE() << stem << "_(" << RandSuff{i} << ")" << Ext{ext}); + active = callback(PSLICE() << stem << "_(" << RandSuff{i} << ")" << Ext{ext}); } } else if (use_pmc) { auto pmc = G()->td_db()->get_binlog_pmc(); int32 file_id = to_integer(pmc->get("perm_file_id")); pmc->set("perm_file_id", to_string(file_id + 1)); - active = try_callback(PSLICE() << "file_" << file_id << Ext{ext}); + active = callback(PSLICE() << "file_" << file_id << Ext{ext}); if (active) { - active = try_callback(PSLICE() << "file_" << file_id << "_" << RandSuff{6} << Ext{ext}); + active = callback(PSLICE() << "file_" << file_id << "_" << RandSuff{6} << Ext{ext}); } } return active;