Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow starting offline in monitor mode #266

Merged
merged 4 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 39 additions & 25 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,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;
norbusan marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Initialize OneDrive, check for authorization
auto onedrive = new OneDriveApi(cfg, debugHttp);
onedrive.printAccessToken = printAccessToken;
Expand Down Expand Up @@ -256,21 +258,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;
norbusan marked this conversation as resolved.
Show resolved Hide resolved
}
}

// We should only set noRemoteDelete in an upload-only scenario
if ((uploadOnly)&&(noRemoteDelete)) sync.setNoRemoteDelete();

Expand Down Expand Up @@ -375,23 +374,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();
Expand All @@ -407,6 +403,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)
{
Expand Down
8 changes: 8 additions & 0 deletions src/sync.d
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
Expand Down Expand Up @@ -239,6 +246,7 @@ final class SyncEngine
auto item = session.upload();
saveItem(item);
}
initDone = true;
}

// Configure noRemoteDelete if function is called
Expand Down