From f2fe7183899b32875dae959ada9cccb5e81ac5c1 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Mon, 27 Jan 2020 08:42:00 +1100 Subject: [PATCH] Implement #763 - Delete local files after sync (#767) * Implement #763 - Delete local files after sync --- config | 1 + src/config.d | 9 +++++++-- src/main.d | 20 ++++++++++++++++---- src/sync.d | 43 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/config b/config index 8ec9d3af..c9586bcd 100644 --- a/config +++ b/config @@ -32,3 +32,4 @@ # sync_root_files = "false" # classify_as_big_delete = "1000" # user_agent = "" +# remove_source_files = "false" \ No newline at end of file diff --git a/src/config.d b/src/config.d index 434754d2..ea7e3c34 100644 --- a/src/config.d +++ b/src/config.d @@ -64,6 +64,8 @@ final class Config longValues["monitor_fullscan_frequency"] = 10; // Number of children in a path that is locally removed which will be classified as a 'big data delete' longValues["classify_as_big_delete"] = 1000; + // Delete source after successful transfer + boolValues["remove_source_files"] = false; // Determine the users home directory. // Need to avoid using ~ here as expandTilde() below does not interpret correctly when running under init.d or systemd scripts @@ -166,6 +168,7 @@ final class Config boolValues["monitor"] = false; boolValues["synchronize"] = false; boolValues["force"] = false; + boolValues["remove_source_files"] = false; // Application Startup option validation try { @@ -269,6 +272,9 @@ final class Config "remove-directory", "Remove a directory on OneDrive - no sync will be performed.", &stringValues["remove_directory"], + "remove-source-files", + "Remove source file after successful transfer to OneDrive when using --upload-only", + &boolValues["remove_source_files"], "single-directory", "Specify a single local directory within the OneDrive root to sync.", &stringValues["single_directory"], @@ -444,7 +450,7 @@ void outputLongHelp(Option[] opt) "--skip-file", "--source-directory", "--syncdir", - "--user-agent" ]; + "--user-agent" ]; writeln(`OneDrive - a client for OneDrive Cloud Services Usage: @@ -478,4 +484,3 @@ unittest cfg.load("config"); assert(cfg.getValueString("sync_dir") == "~/OneDrive"); } - diff --git a/src/main.d b/src/main.d index c0935376..b540b09d 100644 --- a/src/main.d +++ b/src/main.d @@ -274,7 +274,7 @@ int main(string[] args) // Are we able to reach the OneDrive Service bool online = false; - + // dry-run database setup if (cfg.getValueBool("dry_run")) { // Make a copy of the original items.sqlite3 for use as the dry run copy if it exists @@ -544,9 +544,21 @@ int main(string[] args) } } - // We should only set noRemoteDelete in an upload-only scenario - if ((cfg.getValueBool("upload_only"))&&(cfg.getValueBool("no_remote_delete"))) sync.setNoRemoteDelete(); - + // Do we need to configure specific --upload-only options? + if (cfg.getValueBool("upload_only")) { + // --upload-only was passed in or configured + // was --no-remote-delete passed in or configured + if (cfg.getValueBool("no_remote_delete")) { + // Configure the noRemoteDelete flag + sync.setNoRemoteDelete(); + } + // was --remove-source-files passed in or configured + if (cfg.getValueBool("remove_source_files")) { + // Configure the localDeleteAfterUpload flag + sync.setLocalDeleteAfterUpload(); + } + } + // Do we configure to disable the upload validation routine if (cfg.getValueBool("disable_upload_validation")) sync.setDisableUploadValidation(); diff --git a/src/sync.d b/src/sync.d index 6da679a4..0da06960 100644 --- a/src/sync.d +++ b/src/sync.d @@ -15,9 +15,12 @@ static import log; // threshold after which files will be uploaded using an upload session private long thresholdFileSize = 4 * 2^^20; // 4 MiB -// flag to set whether local files should be deleted +// flag to set whether local files should be deleted from OneDrive private bool noRemoteDelete = false; +// flag to set whether the local file should be deleted once it is successfully uploaded to OneDrive +private bool localDeleteAfterUpload = false; + // flag to set if we are running as uploadOnly private bool uploadOnly = false; @@ -378,6 +381,13 @@ final class SyncEngine } } + // Configure uploadOnly if function is called + // By default, uploadOnly = false; + void setUploadOnly() + { + uploadOnly = true; + } + // Configure noRemoteDelete if function is called // By default, noRemoteDelete = false; // Meaning we will process local deletes to delete item on OneDrive @@ -386,11 +396,12 @@ final class SyncEngine noRemoteDelete = true; } - // Configure uploadOnly if function is called - // By default, uploadOnly = false; - void setUploadOnly() + // Configure localDeleteAfterUpload if function is called + // By default, localDeleteAfterUpload = false; + // Meaning we will not delete any local file after upload is successful + void setLocalDeleteAfterUpload() { - uploadOnly = true; + localDeleteAfterUpload = true; } // Configure singleDirectoryScope if function is called @@ -2378,10 +2389,29 @@ final class SyncEngine if (!itemdb.selectByPath(path, defaultDriveId, item)) { // item is not in the database, upload new file uploadNewFile(path); + + // did the upload fail? if (!uploadFailed) { // upload did not fail + // Issue #763 - Delete local files after sync handling + // are we in an --upload-only scenario? + if (uploadOnly) { + // are we in a delete local file after upload? + if (localDeleteAfterUpload) { + // Log that we are deleting a local item + log.log("Removing local file as --upload-only & --remove-source-files configured"); + // are we in a --dry-run scenario? + if (!dryRun) { + // No --dry-run ... process local file delete + log.vdebug("Removing local file: ", path); + safeRemove(path); + } + } + } + + // how much space is left on OneDrive after upload? remainingFreeSpace = (remainingFreeSpace - fileSize); - log.vlog("Remaining free space: ", remainingFreeSpace); + log.vlog("Remaining free space on OneDrive: ", remainingFreeSpace); } } } else { @@ -3686,4 +3716,5 @@ final class SyncEngine log.vdebug("Generated Fake OneDrive Response: ", fakeResponse); return fakeResponse; } + }