Skip to content

Commit

Permalink
Get file in cache_manager sets last modification time. Put file with …
Browse files Browse the repository at this point in the history
…usage of copying into temp file and then moving.
  • Loading branch information
Neloop committed May 5, 2018
1 parent 0fe8caa commit fd293de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/fileman/cache_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cache_manager.h"
#include "../helpers/string_utils.h"


cache_manager::cache_manager(std::shared_ptr<spdlog::logger> logger)
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fd293de

Please sign in to comment.