From 0d05274f32cbd9aa8abaeecb4d8a974d70332987 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Thu, 29 Nov 2018 19:31:44 +0900 Subject: [PATCH] Fix renaming of files sync issues (#252) * issue #249 do not safeRename the target if it is in sync * issue #249 only remove actual path if the associated ids agree --- src/sync.d | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/sync.d b/src/sync.d index e246f8cb..1e9d862f 100644 --- a/src/sync.d +++ b/src/sync.d @@ -740,9 +740,22 @@ final class SyncEngine if (oldPath != newPath) { log.log("Moving ", oldPath, " to ", newPath); if (exists(newPath)) { - // TODO: force remote sync by deleting local item - log.vlog("The destination is occupied, renaming the conflicting file..."); - safeRename(newPath); + Item localNewItem; + if (itemdb.selectByPath(newPath, defaultDriveId, localNewItem)) { + if (isItemSynced(localNewItem, newPath)) { + log.vlog("Destination is in sync and will be overwritten"); + } else { + // TODO: force remote sync by deleting local item + log.vlog("The destination is occupied, renaming the conflicting file..."); + safeRename(newPath); + } + } else { + // to be overwritten item is not already in the itemdb, so it should + // be synced. Do a safe rename here, too. + // TODO: force remote sync by deleting local item + log.vlog("The destination is occupied by new file, renaming the conflicting file..."); + safeRename(newPath); + } } rename(oldPath, newPath); } @@ -853,14 +866,30 @@ final class SyncEngine Item item; if (!itemdb.selectById(i[0], i[1], item)) continue; // check if the item is in the db string path = itemdb.computePath(i[0], i[1]); - log.log("Deleting item ", path); + log.log("Trying to delete item ", path); itemdb.deleteById(item.driveId, item.id); if (item.remoteDriveId != null) { // delete the linked remote folder itemdb.deleteById(item.remoteDriveId, item.remoteId); } + bool needsRemoval = false; if (exists(path)) { // path exists on the local system + // make sure that the path refers to the correct item + Item pathItem; + if (itemdb.selectByPath(path, item.driveId, pathItem)) { + if (pathItem.id == item.id) { + needsRemoval = true; + } else { + log.log("Skipped due to id difference!"); + } + } else { + // item has disappeared completely + needsRemoval = true; + } + } + if (needsRemoval) { + log.log("Deleting item ", path); if (isFile(path)) { remove(path); } else {