From b10e641fe923e3bf870fe4e7dd49824798671a2f Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Tue, 4 Dec 2018 09:15:44 +0900 Subject: [PATCH] allow starting offline in monitor mode (#266) * SyncEngine: allow for multiple calls to init by saving the state * Factor out syncengine initialization code so that it can be reused * Don't exit in monitor when started offline * Always call initSyncEngine during regular syncs, this will call the SyncEngine init routine which caches the state, thus a quasi no-op. * Add forgotten http.shutdown --- src/main.d | 64 +++++++++++++++++++++++++++++++++--------------------- src/sync.d | 8 +++++++ 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/main.d b/src/main.d index 601c0967..73533ad7 100644 --- a/src/main.d +++ b/src/main.d @@ -186,9 +186,11 @@ int main(string[] args) } catch (CurlException e) { // No network connection to OneDrive Service log.error("No network connection to Microsoft OneDrive Service"); - return EXIT_FAILURE; - } - + if (!monitor) { + return EXIT_FAILURE; + } + } + // Initialize OneDrive, check for authorization auto onedrive = new OneDriveApi(cfg, debugHttp); onedrive.printAccessToken = printAccessToken; @@ -259,21 +261,18 @@ int main(string[] args) auto sync = new SyncEngine(cfg, onedrive, itemdb, selectiveSync); try { - sync.init(); - } catch (OneDriveException e) { - if (e.httpStatusCode == 400 || e.httpStatusCode == 401) { - // Authorization is invalid - log.log("\nAuthorization token invalid, use --logout to authorize the client again\n"); + if (!initSyncEngine(sync)) { onedrive.http.shutdown(); return EXIT_FAILURE; } - if (e.httpStatusCode >= 500) { - // There was a HTTP 5xx Server Side Error, message already printed + } catch (CurlException e) { + if (!monitor) { + log.log("\nNo internet connection."); onedrive.http.shutdown(); return EXIT_FAILURE; } } - + // We should only set noRemoteDelete in an upload-only scenario if ((uploadOnly)&&(noRemoteDelete)) sync.setNoRemoteDelete(); @@ -383,23 +382,20 @@ int main(string[] args) auto currTime = MonoTime.currTime(); if (currTime - lastCheckTime > checkInterval) { try { - online = testNetwork(); + if (!initSyncEngine(sync)) { + onedrive.http.shutdown(); + return EXIT_FAILURE; + } + performSync(sync, singleDirectory, downloadOnly, localFirst, uploadOnly); + if (!downloadOnly) { + // discard all events that may have been generated by the sync + m.update(false); + } } catch (CurlException e) { + // TODO better check of type of exception from Curl + // could be either timeout of operation of connection error // No network connection to OneDrive Service log.log("No network connection to Microsoft OneDrive Service, skipping sync"); - online = false; - } - if (online) { - try { - performSync(sync, singleDirectory, downloadOnly, localFirst, uploadOnly); - if (!downloadOnly) { - // discard all events that may have been generated by the sync - m.update(false); - } - } catch (CurlException e) { - // TODO better check of type of exception from Curl - log.log("No network connection to Microsoft OneDrive Service, skipping sync"); - } } // performSync complete, set lastCheckTime to current time lastCheckTime = MonoTime.currTime(); @@ -415,6 +411,24 @@ int main(string[] args) return EXIT_SUCCESS; } +bool initSyncEngine(SyncEngine sync) +{ + try { + sync.init(); + } catch (OneDriveException e) { + if (e.httpStatusCode == 400 || e.httpStatusCode == 401) { + // Authorization is invalid + log.log("\nAuthorization token invalid, use --logout to authorize the client again\n"); + return false; + } + if (e.httpStatusCode >= 500) { + // There was a HTTP 5xx Server Side Error, message already printed + return false; + } + } + return true; +} + // try to synchronize the folder three times void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, bool localFirst, bool uploadOnly) { diff --git a/src/sync.d b/src/sync.d index 0999e39c..44258494 100644 --- a/src/sync.d +++ b/src/sync.d @@ -167,6 +167,8 @@ final class SyncEngine private bool malwareDetected = false; // download filesystem issue flag private bool downloadFailed = false; + // initialization has been done + private bool initDone = false; this(Config cfg, OneDriveApi onedrive, ItemDatabase itemdb, SelectiveSync selectiveSync) { @@ -183,6 +185,11 @@ final class SyncEngine // Set accountType, defaultDriveId, defaultRootId & remainingFreeSpace once and reuse where possible JSONValue oneDriveDetails; + + if (initDone) { + return; + } + // Need to catch 400 or 5xx server side errors at initialization try { oneDriveDetails = onedrive.getDefaultDrive(); @@ -239,6 +246,7 @@ final class SyncEngine auto item = session.upload(); saveItem(item); } + initDone = true; } // Configure noRemoteDelete if function is called