From 87cd18379d0c69b1d2c4389d78dd4a9c52878501 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Thu, 6 Dec 2018 18:28:03 +0900 Subject: [PATCH] Deal with items that disappear during upload (Issue: #258) (#276) * Deal with disappearing items during upload (Issue: #258) --- src/monitor.d | 6 ++++++ src/sync.d | 50 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/monitor.d b/src/monitor.d index 090cb961..17cc9db8 100644 --- a/src/monitor.d +++ b/src/monitor.d @@ -66,6 +66,12 @@ final class Monitor private void addRecursive(string dirname) { + // skip non existing/disappeared items + if (!exists(dirname)) { + log.vlog("Not adding non-existing/disappeared directory: ", dirname); + return; + } + // skip filtered items if (dirname != ".") { if (selectiveSync.isNameExcluded(baseName(dirname))) { diff --git a/src/sync.d b/src/sync.d index 9066bd1b..2bc6a0b9 100644 --- a/src/sync.d +++ b/src/sync.d @@ -1238,6 +1238,12 @@ final class SyncEngine uploadCreateDir(path); } // recursively traverse children + // the above operation takes time and the directory might have + // disappeared in the meantime + if (!exists(path)) { + log.vlog("Directory disappeared during upload: ", path); + return; + } auto entries = dirEntries(path, SpanMode.shallow, false); foreach (DirEntry entry; entries) { uploadNewItems(entry.name); @@ -1369,7 +1375,6 @@ final class SyncEngine private void uploadNewFile(string path) { Item parent; - // Check the database for the parent //enforce(itemdb.selectByPath(dirName(path), defaultDriveId, parent), "The parent item is not in the local database"); if (itemdb.selectByPath(dirName(path), defaultDriveId, parent)) { @@ -1423,23 +1428,23 @@ final class SyncEngine // check what 'account type' this is as this issue only affects OneDrive Business so we need some extra logic here if (accountType == "personal"){ // Original file upload logic - if (getSize(path) <= thresholdFileSize) { + if (thisFileSize <= thresholdFileSize) { try { - response = onedrive.simpleUpload(path, parent.driveId, parent.id, baseName(path)); - } catch (OneDriveException e) { - if (e.httpStatusCode == 504) { - // HTTP request returned status code 504 (Gateway Timeout) - // Try upload as a session - try { - response = session.upload(path, parent.driveId, parent.id, baseName(path)); - } catch (OneDriveException e) { - // error uploading file - return; - } + response = onedrive.simpleUpload(path, parent.driveId, parent.id, baseName(path)); + } catch (OneDriveException e) { + if (e.httpStatusCode == 504) { + // HTTP request returned status code 504 (Gateway Timeout) + // Try upload as a session + try { + response = session.upload(path, parent.driveId, parent.id, baseName(path)); + } catch (OneDriveException e) { + // error uploading file + return; } - else throw e; } - writeln(" done."); + else throw e; + } + writeln(" done."); } else { // File larger than threshold - use a session to upload writeln(""); @@ -1448,6 +1453,10 @@ final class SyncEngine writeln(" done."); } catch (OneDriveException e) { // error uploading file + log.vlog("Upload failed with OneDriveException: ", e.msg); + return; + } catch (FileException e) { + log.vlog("Upload failed with File Exception: ", e.msg); return; } } @@ -1493,9 +1502,14 @@ final class SyncEngine // Update the item's metadata on OneDrive string id = response["id"].str; string cTag = response["cTag"].str; - SysTime mtime = timeLastModified(path).toUTC(); - // use the cTag instead of the eTag because OneDrive may update the metadata of files AFTER they have been uploaded - uploadLastModifiedTime(parent.driveId, id, cTag, mtime); + if (exists(path)) { + SysTime mtime = timeLastModified(path).toUTC(); + // use the cTag instead of the eTag because OneDrive may update the metadata of files AFTER they have been uploaded + uploadLastModifiedTime(parent.driveId, id, cTag, mtime); + } else { + // will be removed in different event! + log.log("File disappeared after upload: ", path); + } return; } else { // OneDrive Business Account - always use a session to upload