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 // shared string variable for username
string username; string username;
string logFilePath;
static this() { static this() {
username = getUserName(); username = getUserName();
logFilePath = "/var/log/onedrive/";
} }
// enable verbose logging // enable verbose logging
bool verbose; 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) void log(T...)(T args)
{ {
writeln(args); writeln(args);
@ -46,7 +65,7 @@ void error(T...)(T args)
private void logfileWriteLine(T...)(T args) private void logfileWriteLine(T...)(T args)
{ {
// Write to log file // Write to log file
string logFileName = "/var/log/onedrive/" ~ .username ~ ".onedrive.log"; string logFileName = .logFilePath ~ .username ~ ".onedrive.log";
auto currentTime = Clock.currTime(); auto currentTime = Clock.currTime();
auto timeString = currentTime.toString(); auto timeString = currentTime.toString();
File logFile; File logFile;

View file

@ -6,7 +6,7 @@ static import log;
int main(string[] args) 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 // 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"); string configPath = environment.get("XDG_CONFIG_HOME");
if (configPath == ""){ if (configPath == ""){
@ -35,8 +35,10 @@ int main(string[] args)
bool printVersion; bool printVersion;
// Additional options added to support MyNAS Storage Appliance // Additional options added to support MyNAS Storage Appliance
// debug the HTTP(S) operations if required // Debug the HTTPS submit operations if required
bool debugHttp; bool debugHttp;
// Debug the HTTPS response operations if required
bool debugHttpSubmit;
// This allows for selective directory syncing instead of everything under ~/OneDrive/ // This allows for selective directory syncing instead of everything under ~/OneDrive/
string singleDirectory; string singleDirectory;
// Create a single root directory on OneDrive // Create a single root directory on OneDrive
@ -102,8 +104,7 @@ int main(string[] args)
} }
// Configure Logging // Configure Logging
string logFilePath = "/var/log/onedrive/"; log.init();
if (!exists(logFilePath)) mkdirRecurse(logFilePath);
// load configuration // load configuration
log.vlog("Loading config ..."); log.vlog("Loading config ...");
@ -139,6 +140,8 @@ int main(string[] args)
log.error("No network connection"); log.error("No network connection");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// Initialize OneDrive, check for authorization
auto onedrive = new OneDriveApi(cfg, debugHttp); auto onedrive = new OneDriveApi(cfg, debugHttp);
onedrive.printAccessToken = printAccessToken; onedrive.printAccessToken = printAccessToken;
if (!onedrive.init()) { if (!onedrive.init()) {
@ -150,10 +153,17 @@ int main(string[] args)
// if --synchronize or --monitor not passed in, exit & display help // if --synchronize or --monitor not passed in, exit & display help
auto performSyncOK = false; auto performSyncOK = false;
if (synchronize || monitor) { if (synchronize || monitor) {
performSyncOK = true; 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) { if (!performSyncOK) {
writeln("\n--synchronize or --monitor missing from your command options or use --help for further assistance\n"); 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"); 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); if (!exists(syncDir)) mkdirRecurse(syncDir);
chdir(syncDir); chdir(syncDir);
// Initialise the sync engine // Configure selective sync by parsing and getting a regex for skip_file config component
log.log("Initializing the Synchronization Engine ...");
auto selectiveSync = new SelectiveSync(); auto selectiveSync = new SelectiveSync();
selectiveSync.load(cfg.syncListFilePath); selectiveSync.load(cfg.syncListFilePath);
selectiveSync.setMask(cfg.getValue("skip_file")); selectiveSync.setMask(cfg.getValue("skip_file"));
// Initialise the sync engine
log.log("Initializing the Synchronization Engine ...");
auto sync = new SyncEngine(cfg, onedrive, itemdb, selectiveSync); auto sync = new SyncEngine(cfg, onedrive, itemdb, selectiveSync);
sync.init(); sync.init();