Resolve std.file.FileException when checking logfile path

* Resolve std.file.FileException@std/file.d(2954): /var/log/onedrive/: Permission denied when there is no permission to create the directory on application startup
This commit is contained in:
abraunegg 2018-05-16 19:19:43 +10:00
parent bf7986df14
commit 2827467296
2 changed files with 38 additions and 7 deletions

View file

@ -7,13 +7,32 @@ import std.algorithm : splitter;
// shared string variable for username
string username;
string logFilePath;
static this() {
username = getUserName();
logFilePath = "/var/log/onedrive/";
}
// enable verbose logging
bool verbose;
void init()
{
if (!exists(logFilePath)){
// logfile path does not exist
try {
mkdirRecurse(logFilePath);
}
catch (std.file.FileException e) {
// we got an error ..
writeln("\nUnable to create /var/log/onedrive/ ");
writeln("Please manually create /var/log/onedrive/ and set appropriate permissions to allow write access");
writeln("The client activity log will be located in the users home directory\n");
}
}
}
void log(T...)(T args)
{
writeln(args);
@ -46,7 +65,7 @@ void error(T...)(T args)
private void logfileWriteLine(T...)(T args)
{
// Write to log file
string logFileName = "/var/log/onedrive/" ~ .username ~ ".onedrive.log";
string logFileName = .logFilePath ~ .username ~ ".onedrive.log";
auto currentTime = Clock.currTime();
auto timeString = currentTime.toString();
File logFile;

View file

@ -6,7 +6,7 @@ static import log;
int main(string[] args)
{
// Determine the user home directory.
// Determine the users configuration directory.
// Need to avoid using ~ here as expandTilde() below does not interpret correctly when running under init.d scripts
string configPath = environment.get("XDG_CONFIG_HOME");
if (configPath == ""){
@ -35,8 +35,10 @@ int main(string[] args)
bool printVersion;
// Additional options added to support MyNAS Storage Appliance
// debug the HTTP(S) operations if required
// Debug the HTTPS submit operations if required
bool debugHttp;
// Debug the HTTPS response operations if required
bool debugHttpSubmit;
// This allows for selective directory syncing instead of everything under ~/OneDrive/
string singleDirectory;
// Create a single root directory on OneDrive
@ -102,8 +104,7 @@ int main(string[] args)
}
// Configure Logging
string logFilePath = "/var/log/onedrive/";
if (!exists(logFilePath)) mkdirRecurse(logFilePath);
log.init();
// load configuration
log.vlog("Loading config ...");
@ -139,6 +140,8 @@ int main(string[] args)
log.error("No network connection");
return EXIT_FAILURE;
}
// Initialize OneDrive, check for authorization
auto onedrive = new OneDriveApi(cfg, debugHttp);
onedrive.printAccessToken = printAccessToken;
if (!onedrive.init()) {
@ -150,10 +153,17 @@ int main(string[] args)
// if --synchronize or --monitor not passed in, exit & display help
auto performSyncOK = false;
if (synchronize || monitor) {
performSyncOK = true;
}
// create-directory, remove-directory, source-directory, destination-directory
// are activities that dont perform a sync no error message for these items either
if (((createDirectory != "") || (removeDirectory != "")) || ((sourceDirectory != "") && (destinationDirectory != "")) ) {
performSyncOK = true;
}
if (!performSyncOK) {
writeln("\n--synchronize or --monitor missing from your command options or use --help for further assistance\n");
writeln("No OneDrive sync will be performed without either of these two arguments being present\n");
@ -171,11 +181,13 @@ int main(string[] args)
if (!exists(syncDir)) mkdirRecurse(syncDir);
chdir(syncDir);
// Initialise the sync engine
log.log("Initializing the Synchronization Engine ...");
// Configure selective sync by parsing and getting a regex for skip_file config component
auto selectiveSync = new SelectiveSync();
selectiveSync.load(cfg.syncListFilePath);
selectiveSync.setMask(cfg.getValue("skip_file"));
// Initialise the sync engine
log.log("Initializing the Synchronization Engine ...");
auto sync = new SyncEngine(cfg, onedrive, itemdb, selectiveSync);
sync.init();