From 45e03ecab7d71bbe42fa1c79773bdfc260537639 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 11 Jul 2020 15:05:34 +0200 Subject: Use atomic rename instead of .finished lock files --- src/QuickMedia.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 6cf6875..8a01dc6 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -1135,8 +1135,7 @@ namespace QuickMedia { Path image_path = content_cache_dir; image_path.join(std::to_string(image_index + 1)); - Path image_finished_path(image_path.data + ".finished"); - if(get_file_type(image_finished_path) != FileType::FILE_NOT_FOUND && get_file_type(image_path) == FileType::REGULAR) { + if(get_file_type(image_path) == FileType::REGULAR) { std::string image_data; if(file_get_content(image_path, image_data) == 0) { if(image_texture.loadFromMemory(image_data.data(), image_data.size())) { @@ -1192,8 +1191,7 @@ namespace QuickMedia { Path image_filepath = content_cache_dir_; image_filepath.join(std::to_string(page++)); - Path lockfile_path(image_filepath.data + ".finished"); - if(get_file_type(lockfile_path) != FileType::FILE_NOT_FOUND) + if(get_file_type(image_filepath) != FileType::FILE_NOT_FOUND) return true; std::string image_content; @@ -1223,15 +1221,19 @@ namespace QuickMedia { } } - if(file_overwrite(image_filepath, image_content) != 0) { - show_notification("Storage", "Failed to save image to file: " + image_filepath.data, Urgency::CRITICAL); + Path image_filepath_tmp(image_filepath.data + ".tmp"); + + if(file_overwrite(image_filepath_tmp, image_content) != 0) { + show_notification("Storage", "Failed to save image to file: " + image_filepath_tmp.data, Urgency::CRITICAL); return false; } - if(create_lock_file(lockfile_path) != 0) { - show_notification("Storage", "Failed to save image finished state to file: " + lockfile_path.data, Urgency::CRITICAL); + if(rename(image_filepath_tmp.data.c_str(), image_filepath.data.c_str()) != 0) { + perror("rename"); + show_notification("Storage", "Failed to save image to file: " + image_filepath_tmp.data, Urgency::CRITICAL); return false; } + return true; }); }); -- cgit v1.2.3