aboutsummaryrefslogtreecommitdiff
path: root/src/DownloadUtils.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-07-11 15:05:34 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-11 15:05:34 +0200
commit45e03ecab7d71bbe42fa1c79773bdfc260537639 (patch)
treeeb16a6a101a659e7a537baa1d47d9862b5bb137c /src/DownloadUtils.cpp
parent7ec17eeb4a8d754aec18d72839430e9b492c8273 (diff)
Use atomic rename instead of .finished lock files
Diffstat (limited to 'src/DownloadUtils.cpp')
-rw-r--r--src/DownloadUtils.cpp14
1 files changed, 10 insertions, 4 deletions
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<CommandArg> &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;