diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-07-11 19:05:27 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-11 19:05:27 +0200 |
commit | 2e6ed80e3ed3cbab2cf7495903db35e224a6014f (patch) | |
tree | 24c54f911e74f63171b739abfe30848351f49be5 | |
parent | 89fbdcfa0963b58de14951cc1ad22ab46590bee0 (diff) |
Make all storage operations atomic
-rw-r--r-- | src/QuickMedia.cpp | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 47b027e..6b9dcf2 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -335,11 +335,6 @@ namespace QuickMedia { return true; } - static bool save_json_to_file(const Path &path, const Json::Value &json) { - Json::StreamWriterBuilder json_builder; - return file_overwrite(path, Json::writeString(json_builder, json)) == 0; - } - static bool save_json_to_file_atomic(const Path &path, const Json::Value &json) { Path tmp_path = path; tmp_path.append(".tmp"); @@ -433,6 +428,12 @@ namespace QuickMedia { } // TODO: Make asynchronous for_files_in_dir_sort_last_modified(content_storage_dir, [&history_items, plugin](const std::filesystem::path &filepath) { + // This can happen when QuickMedia crashes/is killed while writing to storage. + // In that case, the storage wont be corrupt but there will be .tmp files. + // TODO: Remove these .tmp files if they exist during startup + if(filepath.extension() == ".tmp") + return true; + Path fullpath(filepath.c_str()); Json::Value body; if(!read_file_as_json(fullpath, body)) { @@ -1322,7 +1323,7 @@ namespace QuickMedia { json_chapter["current"] = std::min(latest_read, num_images); json_chapter["total"] = num_images; json_chapters[chapter_title] = json_chapter; - if(!save_json_to_file(content_storage_file, content_storage_json)) { + if(!save_json_to_file_atomic(content_storage_file, content_storage_json)) { show_notification("Manga progress", "Failed to save manga progress", Urgency::CRITICAL); } @@ -1488,7 +1489,7 @@ namespace QuickMedia { json_chapter["current"] = std::min(latest_read, image_viewer.get_num_pages()); json_chapter["total"] = image_viewer.get_num_pages(); json_chapters[chapter_title] = json_chapter; - if(!save_json_to_file(content_storage_file, content_storage_json)) { + if(!save_json_to_file_atomic(content_storage_file, content_storage_json)) { show_notification("Manga progress", "Failed to save manga progress", Urgency::CRITICAL); } @@ -1514,7 +1515,7 @@ namespace QuickMedia { latest_read = focused_page; json_chapter["current"] = latest_read; json_chapters[chapter_title] = json_chapter; - if(!save_json_to_file(content_storage_file, content_storage_json)) { + if(!save_json_to_file_atomic(content_storage_file, content_storage_json)) { show_notification("Manga progress", "Failed to save manga progress", Urgency::CRITICAL); } } |