From 814dd011f1d0247ad454e697e4797b894beab5bc Mon Sep 17 00:00:00 2001 From: idotobi <16611056+idotobi@users.noreply.github.com> Date: Mon, 1 Mar 2021 19:00:21 +0100 Subject: [PATCH] Fix Backup on Save Mechanism (#2885) Backup (aka. rename to `filename` + `~`) the filepath that will be written to while saving. Before the fix this was not always the case as non `.xopp` extensions where overwritten to `.xopp` upon saving, but only after the backup was already created for to original filepath. --- src/control/jobs/SaveJob.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/control/jobs/SaveJob.cpp b/src/control/jobs/SaveJob.cpp index 74fa7723..1e9221d5 100644 --- a/src/control/jobs/SaveJob.cpp +++ b/src/control/jobs/SaveJob.cpp @@ -90,9 +90,13 @@ auto SaveJob::save() -> bool { fs::path const filepath = doc->getFilepath(); doc->unlock(); + auto const target = fs::path{filepath}.replace_extension(".xopp"); + if (doc->shouldCreateBackupOnSave()) { try { - Util::safeRenameFile(filepath, fs::path{filepath} += "~"); + // Note: The backup must be created for the target as this is the filepath + // which will be written to. Do not use the `filepath` variable! + Util::safeRenameFile(target, fs::path{target} += "~"); } catch (fs::filesystem_error const& fe) { g_warning("Could not create backup! Failed with %s", fe.what()); return false; @@ -100,8 +104,6 @@ auto SaveJob::save() -> bool { doc->setCreateBackupOnSave(false); } - auto const target = fs::path{filepath}.replace_extension(".xopp"); - doc->lock(); h.saveTo(target, this->control); doc->setFilepath(target);