From e8406b719e76d0402cc81dc8e05350f9073fb1ff Mon Sep 17 00:00:00 2001 From: abraunegg Date: Wed, 17 Jan 2024 09:46:36 +1100 Subject: [PATCH] Reinstate safeRename for online item moves Reinstate safeRename for online item moves --- src/sync.d | 15 ++++++++------- src/util.d | 12 ++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/sync.d b/src/sync.d index 2f012ad9..12ffc8d9 100644 --- a/src/sync.d +++ b/src/sync.d @@ -1749,10 +1749,10 @@ class SyncEngine { // Has the user configured to IGNORE local data protection rules? if (bypassDataPreservation) { - // The user has configured to ignore data safety checks and overwrite local data rather than preserve & rename + // The user has configured to ignore data safety checks and overwrite local data rather than preserve & safeBackup addLogEntry("WARNING: Local Data Protection has been disabled. You may experience data loss on this file: " ~ newItemPath, ["info", "notify"]); } else { - // local data protection is configured, rename the local file, passing in if we are performing a --dry-run or not + // local data protection is configured, safeBackup the local file, passing in if we are performing a --dry-run or not safeBackup(newItemPath, dryRun); } } @@ -1764,10 +1764,10 @@ class SyncEngine { // Has the user configured to IGNORE local data protection rules? if (bypassDataPreservation) { - // The user has configured to ignore data safety checks and overwrite local data rather than preserve & rename + // The user has configured to ignore data safety checks and overwrite local data rather than preserve & safeBackup addLogEntry("WARNING: Local Data Protection has been disabled. You may experience data loss on this file: " ~ newItemPath, ["info", "notify"]); } else { - // local data protection is configured, rename the local file, passing in if we are performing a --dry-run or not + // local data protection is configured, safeBackup the local file, passing in if we are performing a --dry-run or not safeBackup(newItemPath, dryRun); } } @@ -1863,11 +1863,11 @@ class SyncEngine { // Try and rename path, catch any exception generated try { - // Rename this item, passing in if we are performing a --dry-run or not - safeBackup(changedItemPath, dryRun); - // If we are in a --dry-run situation? , the actual rename did not occur - but we need to track like it did if(!dryRun) { + // Rename this item, passing in if we are performing a --dry-run or not + safeRename(existingItemPath, changedItemPath, dryRun); + // Flag that the item was moved | renamed itemWasMoved = true; @@ -1875,6 +1875,7 @@ class SyncEngine { // Otherwise when we do the DB check, the move on the file system, the file technically has a newer timestamp // which is 'correct' .. but we need to report locally the online timestamp here as the move was made online if (changedOneDriveItem.type == ItemType.file) { + // Set the timestamp addLogEntry("Calling setTimes() for this file: " ~ changedItemPath, ["debug"]); setTimes(changedItemPath, changedOneDriveItem.mtime, changedOneDriveItem.mtime); } diff --git a/src/util.d b/src/util.d index 68ae4182..7eee3e3a 100644 --- a/src/util.d +++ b/src/util.d @@ -96,6 +96,18 @@ void safeBackup(const(char)[] path, bool dryRun) { } } +// Rename the given item, and only performs the function if not in a --dry-run scenario +void safeRename(const(char)[] oldPath, const(char)[] newPath, bool dryRun) { + // Perform the rename + if (!dryRun) { + addLogEntry("Calling rename(oldPath, newPath)", ["debug"]); + // Use rename() as Linux is POSIX compliant, we have an atomic operation where at no point in time the 'to' is missing. + rename(oldPath, newPath); + } else { + addLogEntry("DRY-RUN: Skipping local file rename", ["debug"]); + } +} + // Deletes the specified file without throwing an exception if it does not exists void safeRemove(const(char)[] path) { if (exists(path)) remove(path);