From fd293dea729e075a11b09e7c792365cdb9a1a2e6 Mon Sep 17 00:00:00 2001 From: Martin Polanka Date: Thu, 3 May 2018 14:42:05 +0200 Subject: [PATCH] Get file in cache_manager sets last modification time. Put file with usage of copying into temp file and then moving. --- src/fileman/cache_manager.cpp | 11 +++++++++-- tests/CMakeLists.txt | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/fileman/cache_manager.cpp b/src/fileman/cache_manager.cpp index b2df8321..3d650dfb 100644 --- a/src/fileman/cache_manager.cpp +++ b/src/fileman/cache_manager.cpp @@ -1,4 +1,5 @@ #include "cache_manager.h" +#include "../helpers/string_utils.h" cache_manager::cache_manager(std::shared_ptr logger) @@ -43,8 +44,10 @@ void cache_manager::get_file(const std::string &src_name, const std::string &dst fs::copy_file(source_file, destination_file, fs::copy_option::overwrite_if_exists); fs::permissions(fs::path(destination_file), fs::perms::add_perms | fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write); + // change last modification time of the file + fs::last_write_time(source_file, std::time(nullptr)); } catch (fs::filesystem_error &e) { - auto message = "Failed to copy file " + source_file.string() + " to " + dst_path + ". Error: " + e.what(); + auto message = "Failed to copy file '" + source_file.string() + "' to '" + dst_path + "'. Error: " + e.what(); logger_->warn(message); throw fm_exception(message); } @@ -54,10 +57,14 @@ void cache_manager::put_file(const std::string &src_name, const std::string &dst { fs::path source_file(src_name); fs::path destination_file = caching_dir_ / dst_name; + fs::path destination_temp_file = caching_dir_ / (dst_name + "-" + helpers::random_alphanum_string(10)); logger_->debug("Copying file {} to cache with name {}", src_name, dst_name); try { - fs::copy_file(source_file, destination_file, fs::copy_option::overwrite_if_exists); + // first copy only temporary file + fs::copy_file(source_file, destination_temp_file, fs::copy_option::overwrite_if_exists); + // and then move (atomically) the file to its original destination + fs::rename(destination_temp_file, destination_file); } catch (fs::filesystem_error &e) { auto message = "Failed to copy file " + src_name + " to cache. Error: " + e.what(); logger_->warn(message); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7245c628..df8beb37 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -69,6 +69,7 @@ add_test_suite(cache_manager cache_manager.cpp ${FILEMAN_DIR}/cache_manager.cpp ${HELPERS_DIR}/logger.cpp + ${HELPERS_DIR}/string_utils.cpp ) add_test_suite(fallback_file_manager