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/DownloadUtils.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/DownloadUtils.cpp') diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index e02fa6b..cb87890 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -49,9 +49,10 @@ namespace QuickMedia { DownloadResult download_to_string_cache(const std::string &url, std::string &result, const std::vector &additional_args, bool use_tor, bool use_browser_useragent) { Path media_dir = get_cache_dir().join("media"); Path media_file_path = Path(media_dir).join(cppcodec::base64_rfc4648::encode(url)); - Path media_finished_path(media_file_path.data + ".finished"); - if(get_file_type(media_finished_path) != FileType::FILE_NOT_FOUND && get_file_type(media_file_path) == FileType::REGULAR) { + Path media_file_path_tmp(media_file_path.data + ".tmp"); + if(get_file_type(media_file_path) == FileType::REGULAR) { if(file_get_content(media_file_path, result) == 0) { + fprintf(stderr, "Loaded %s from cache\n", url.c_str()); return DownloadResult::OK; } else { fprintf(stderr, "Failed to get content of cached media file: %s\n", media_file_path.data.c_str()); @@ -60,8 +61,13 @@ namespace QuickMedia { } else { DownloadResult download_result = download_to_string(url, result, additional_args, use_tor, use_browser_useragent); if(download_result == DownloadResult::OK) { - if(create_directory_recursive(media_dir) == 0 && file_overwrite(media_file_path, result) == 0) { - create_lock_file(media_finished_path); + if(create_directory_recursive(media_dir) == 0 && file_overwrite(media_file_path_tmp, result) == 0) { + if(rename(media_file_path_tmp.data.c_str(), media_file_path.data.c_str()) != 0) { + perror("rename"); + download_result = DownloadResult::ERR; + } + } else { + download_result = DownloadResult::ERR; } } return download_result; -- cgit v1.2.3