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.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";
}
}

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;
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;