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
This commit is contained in:
abraunegg 2018-04-07 17:06:57 +10:00
parent 8463e60acc
commit f1aa190a91
2 changed files with 55 additions and 27 deletions

View file

@ -1,6 +1,14 @@
import std.stdio; import std.stdio;
import std.file; import std.file;
import std.datetime; 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 // enable verbose logging
bool verbose; bool verbose;
@ -9,12 +17,7 @@ void log(T...)(T args)
{ {
writeln(args); writeln(args);
// Write to log file // Write to log file
string logFileName = "/var/log/onedrive/onedrive.log"; logfileWriteLine(args);
auto currentTime = Clock.currTime();
auto timeString = currentTime.toString();
File logFile = File(logFileName, "a");
logFile.writeln(timeString, " ", args);
logFile.close();
} }
void vlog(T...)(T args) void vlog(T...)(T args)
@ -22,12 +25,7 @@ void vlog(T...)(T args)
if (verbose) { if (verbose) {
writeln(args); writeln(args);
// Write to log file // Write to log file
string logFileName = "/var/log/onedrive/onedrive.log"; logfileWriteLine(args);
auto currentTime = Clock.currTime();
auto timeString = currentTime.toString();
File logFile = File(logFileName, "a");
logFile.writeln(timeString, " ", args);
logFile.close();
} }
} }
@ -35,10 +33,28 @@ void error(T...)(T args)
{ {
stderr.writeln(args); stderr.writeln(args);
// Write to log file // 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 currentTime = Clock.currTime();
auto timeString = currentTime.toString(); auto timeString = currentTime.toString();
File logFile = File(logFileName, "a"); File logFile = File(logFileName, "a");
logFile.writeln(timeString, " ", args); logFile.writeln(timeString, " ", args);
logFile.close(); 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";
}
} }

View file

@ -4,13 +4,12 @@ import std.getopt, std.file, std.path, std.process, std.stdio;
import config, itemdb, monitor, onedrive, selective, sync, util; import config, itemdb, monitor, onedrive, selective, sync, util;
static import log; static import log;
// only download remote changes
bool downloadOnly;
int main(string[] args) int main(string[] args)
{ {
// configuration directory // configuration directory
string configDirName = environment.get("XDG_CONFIG_HOME", "~/.config") ~ "/onedrive"; string configDirName = environment.get("XDG_CONFIG_HOME", "~/.config") ~ "/onedrive";
// only download remote changes
bool downloadOnly;
// override the sync directory // override the sync directory
string syncDirName; string syncDirName;
// enable monitor mode // enable monitor mode
@ -44,6 +43,8 @@ int main(string[] args)
bool synchronize; bool synchronize;
// Local sync - Upload local changes first before downloading changes from OneDrive // Local sync - Upload local changes first before downloading changes from OneDrive
bool localFirst; bool localFirst;
// Upload Only
bool uploadOnly;
try { try {
auto opt = getopt( 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, "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, "syncdir", "Set the directory used to sync the files that are synced", &syncDirName,
"synchronize", "Perform a synchronization", &synchronize, "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, "verbose|v", "Print more details, useful for debugging", &log.verbose,
"version", "Print the version and exit", &printVersion "version", "Print the version and exit", &printVersion
); );
@ -190,7 +192,7 @@ int main(string[] args)
} }
// Perform the sync // Perform the sync
performSync(sync, singleDirectory, localFirst); performSync(sync, singleDirectory, downloadOnly, localFirst, uploadOnly);
} }
} }
@ -240,7 +242,7 @@ int main(string[] args)
lastCheckTime = currTime; lastCheckTime = currTime;
online = testNetwork(); online = testNetwork();
if (online) { if (online) {
performSync(sync, singleDirectory, localFirst); performSync(sync, singleDirectory, downloadOnly, localFirst, uploadOnly);
if (!downloadOnly) { if (!downloadOnly) {
// discard all events that may have been generated by the sync // discard all events that may have been generated by the sync
m.update(false); m.update(false);
@ -262,7 +264,7 @@ int main(string[] args)
} }
// try to synchronize the folder three times // 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; int count;
string remotePath = "/"; string remotePath = "/";
@ -281,21 +283,31 @@ void performSync(SyncEngine sync, string singleDirectory, bool localFirst)
// we were requested to sync a single directory // we were requested to sync a single directory
log.vlog("Syncing changes from this selected path: ", singleDirectory); log.vlog("Syncing changes from this selected path: ", singleDirectory);
if (localFirst) { if (localFirst) {
log.vlog("Syncing changes from selected local path first before downloading changes from OneDrive ..."); if (uploadOnly){
sync.scanForDifferences(localPath); log.vlog("Syncing changes from selected local path only - NOT syncing data changes from OneDrive ...");
sync.applyDifferencesSingleDirectory(remotePath); 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 { } else {
log.vlog("Syncing changes from selected OneDrive path first before uploading local changes ..."); log.vlog("Syncing changes from selected OneDrive path first before uploading local changes ...");
sync.applyDifferencesSingleDirectory(remotePath); sync.applyDifferencesSingleDirectory(remotePath);
sync.scanForDifferences(localPath); sync.scanForDifferences(localPath);
} }
} else { } else {
// original onedrive client logic below if (!uploadOnly){
sync.applyDifferences(); // original onedrive client logic below
if (!downloadOnly) {
sync.scanForDifferences(localPath);
// ensure that the current state is updated
sync.applyDifferences(); sync.applyDifferences();
if (!downloadOnly) {
sync.scanForDifferences(localPath);
// ensure that the current state is updated
sync.applyDifferences();
}
} else {
// upload only
sync.scanForDifferences(localPath);
} }
} }
count = -1; count = -1;