From f1aa190a91ed689e3178d74fa330642969078cd6 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Sat, 7 Apr 2018 17:06:57 +1000 Subject: [PATCH] Add uploadOnly flag and add username to logfile name * Add specific uploadOnly flag so that the onedrive client only uploads data from the local directory and does not download changes from OneDrive * Change the logfile name to include the username running the onedrive client --- src/log.d | 42 +++++++++++++++++++++++++++++------------- src/main.d | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/src/log.d b/src/log.d index 06f6a4ab..3aa1d712 100644 --- a/src/log.d +++ b/src/log.d @@ -1,6 +1,14 @@ import std.stdio; import std.file; import std.datetime; +import core.sys.posix.pwd, core.sys.posix.unistd, core.stdc.string : strlen; +import std.algorithm : splitter; + +// shared string variable for username +string username; +static this() { + username = getUserName(); +} // enable verbose logging bool verbose; @@ -9,12 +17,7 @@ void log(T...)(T args) { writeln(args); // Write to log file - string logFileName = "/var/log/onedrive/onedrive.log"; - auto currentTime = Clock.currTime(); - auto timeString = currentTime.toString(); - File logFile = File(logFileName, "a"); - logFile.writeln(timeString, " ", args); - logFile.close(); + logfileWriteLine(args); } void vlog(T...)(T args) @@ -22,12 +25,7 @@ void vlog(T...)(T args) if (verbose) { writeln(args); // Write to log file - string logFileName = "/var/log/onedrive/onedrive.log"; - auto currentTime = Clock.currTime(); - auto timeString = currentTime.toString(); - File logFile = File(logFileName, "a"); - logFile.writeln(timeString, " ", args); - logFile.close(); + logfileWriteLine(args); } } @@ -35,10 +33,28 @@ void error(T...)(T args) { stderr.writeln(args); // Write to log file - string logFileName = "/var/log/onedrive/onedrive.log"; + logfileWriteLine(args); +} + +private void logfileWriteLine(T...)(T args) +{ + // Write to log file + string logFileName = "/var/log/onedrive/" ~ .username ~ ".onedrive.log"; auto currentTime = Clock.currTime(); auto timeString = currentTime.toString(); File logFile = File(logFileName, "a"); logFile.writeln(timeString, " ", args); logFile.close(); +} + +private string getUserName() +{ + auto pw = getpwuid(getuid); + auto uinfo = pw.pw_gecos[0 .. strlen(pw.pw_gecos)].splitter(','); + if (!uinfo.empty && uinfo.front.length){ + return uinfo.front.idup; + } else { + // Unknown user? + return "unknown"; + } } \ No newline at end of file diff --git a/src/main.d b/src/main.d index 00386fd7..06449d97 100644 --- a/src/main.d +++ b/src/main.d @@ -4,13 +4,12 @@ import std.getopt, std.file, std.path, std.process, std.stdio; import config, itemdb, monitor, onedrive, selective, sync, util; static import log; -// only download remote changes -bool downloadOnly; - int main(string[] args) { // configuration directory string configDirName = environment.get("XDG_CONFIG_HOME", "~/.config") ~ "/onedrive"; + // only download remote changes + bool downloadOnly; // override the sync directory string syncDirName; // enable monitor mode @@ -44,6 +43,8 @@ int main(string[] args) bool synchronize; // Local sync - Upload local changes first before downloading changes from OneDrive bool localFirst; + // Upload Only + bool uploadOnly; try { auto opt = getopt( @@ -65,6 +66,7 @@ int main(string[] args) "source-directory", "Source directory to rename or move on OneDrive - no sync will be performed.", &sourceDirectory, "syncdir", "Set the directory used to sync the files that are synced", &syncDirName, "synchronize", "Perform a synchronization", &synchronize, + "upload-only", "Only upload to OneDrive, do not sync changes from OneDrive locally", &uploadOnly, "verbose|v", "Print more details, useful for debugging", &log.verbose, "version", "Print the version and exit", &printVersion ); @@ -190,7 +192,7 @@ int main(string[] args) } // Perform the sync - performSync(sync, singleDirectory, localFirst); + performSync(sync, singleDirectory, downloadOnly, localFirst, uploadOnly); } } @@ -240,7 +242,7 @@ int main(string[] args) lastCheckTime = currTime; online = testNetwork(); if (online) { - performSync(sync, singleDirectory, localFirst); + performSync(sync, singleDirectory, downloadOnly, localFirst, uploadOnly); if (!downloadOnly) { // discard all events that may have been generated by the sync m.update(false); @@ -262,7 +264,7 @@ int main(string[] args) } // try to synchronize the folder three times -void performSync(SyncEngine sync, string singleDirectory, bool localFirst) +void performSync(SyncEngine sync, string singleDirectory, bool downloadOnly, bool localFirst, bool uploadOnly) { int count; string remotePath = "/"; @@ -281,21 +283,31 @@ void performSync(SyncEngine sync, string singleDirectory, bool localFirst) // we were requested to sync a single directory log.vlog("Syncing changes from this selected path: ", singleDirectory); if (localFirst) { - log.vlog("Syncing changes from selected local path first before downloading changes from OneDrive ..."); - sync.scanForDifferences(localPath); - sync.applyDifferencesSingleDirectory(remotePath); + if (uploadOnly){ + log.vlog("Syncing changes from selected local path only - NOT syncing data changes from OneDrive ..."); + sync.scanForDifferences(localPath); + } else { + log.vlog("Syncing changes from selected local path first before downloading changes from OneDrive ..."); + sync.scanForDifferences(localPath); + sync.applyDifferencesSingleDirectory(remotePath); + } } else { log.vlog("Syncing changes from selected OneDrive path first before uploading local changes ..."); sync.applyDifferencesSingleDirectory(remotePath); sync.scanForDifferences(localPath); } } else { - // original onedrive client logic below - sync.applyDifferences(); - if (!downloadOnly) { - sync.scanForDifferences(localPath); - // ensure that the current state is updated + if (!uploadOnly){ + // original onedrive client logic below sync.applyDifferences(); + if (!downloadOnly) { + sync.scanForDifferences(localPath); + // ensure that the current state is updated + sync.applyDifferences(); + } + } else { + // upload only + sync.scanForDifferences(localPath); } } count = -1;