Correctly handle '~' when present in 'log_dir' configuration option (#1258)

* Handle '~' when present in 'log_dir' and expand based on user and shell environment  variables to ensure correct expansion.
This commit is contained in:
abraunegg 2021-02-06 20:46:56 +11:00 committed by GitHub
parent e163d570d7
commit 66a172d13a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 6 deletions

View file

@ -10,6 +10,7 @@ final class Config
public string defaultSyncDir = "~/OneDrive";
public string defaultSkipFile = "~*|.~*|*.tmp";
public string defaultSkipDir = "";
public string defaultLogFileDir = "/var/log/onedrive/";
// application set items
public string refreshTokenFilePath = "";
public string deltaLinkFilePath = "";
@ -50,7 +51,7 @@ final class Config
stringValues["sync_dir"] = defaultSyncDir;
stringValues["skip_file"] = defaultSkipFile;
stringValues["skip_dir"] = defaultSkipDir;
stringValues["log_dir"] = "/var/log/onedrive/";
stringValues["log_dir"] = defaultLogFileDir;
stringValues["drive_id"] = "";
stringValues["user_agent"] = "";
boolValues["upload_only"] = false;

View file

@ -425,13 +425,14 @@ int main(string[] args)
}
// sync_dir environment handling to handle ~ expansion properly
bool shellEnvSet = false;
if ((environment.get("SHELL") == "") && (environment.get("USER") == "")){
log.vdebug("sync_dir: No SHELL or USER environment variable configuration detected");
// No shell or user set, so expandTilde() will fail - usually headless system running under init.d / systemd or potentially Docker
// Does the 'currently configured' sync_dir include a ~
if (canFind(cfg.getValueString("sync_dir"), "~")) {
// A ~ was found
log.vdebug("sync_dir: A '~' was found in sync_dir, using the calculated 'homePath' to replace '~'");
// A ~ was found in sync_dir
log.vdebug("sync_dir: A '~' was found in sync_dir, using the calculated 'homePath' to replace '~' as no SHELL or USER environment variable set");
syncDir = cfg.homePath ~ strip(cfg.getValueString("sync_dir"), "~");
} else {
// No ~ found in sync_dir, use as is
@ -440,6 +441,7 @@ int main(string[] args)
}
} else {
// A shell and user is set, expand any ~ as this will be expanded correctly if present
shellEnvSet = true;
log.vdebug("sync_dir: Getting syncDir from config value sync_dir");
if (canFind(cfg.getValueString("sync_dir"), "~")) {
log.vdebug("sync_dir: A '~' was found in configured sync_dir, automatically expanding as SHELL and USER environment variable is set");
@ -452,10 +454,35 @@ int main(string[] args)
// vdebug syncDir as set and calculated
log.vdebug("syncDir: ", syncDir);
// Configure logging if enabled
// Configure the logging directory if different from application default
// log_dir environment handling to handle ~ expansion properly
string logDir = cfg.getValueString("log_dir");
if (logDir != cfg.defaultLogFileDir) {
// user modified log_dir entry
// if 'log_dir' contains a '~' this needs to be expanded correctly
if (canFind(cfg.getValueString("log_dir"), "~")) {
// ~ needs to be expanded correctly
if (!shellEnvSet) {
// No shell or user set, so expandTilde() will fail - usually headless system running under init.d / systemd or potentially Docker
log.vdebug("log_dir: A '~' was found in log_dir, using the calculated 'homePath' to replace '~' as no SHELL or USER environment variable set");
logDir = cfg.homePath ~ strip(cfg.getValueString("log_dir"), "~");
} else {
// A shell and user is set, expand any ~ as this will be expanded correctly if present
log.vdebug("log_dir: A '~' was found in log_dir, using SHELL or USER environment variable to expand '~'");
logDir = expandTilde(cfg.getValueString("log_dir"));
}
} else {
// '~' not found in log_dir entry, use as is
logDir = cfg.getValueString("log_dir");
}
// update log_dir with normalised path, with '~' expanded correctly
cfg.setValueString("log_dir", logDir);
}
// Configure logging only if enabled
if (cfg.getValueBool("enable_logging")){
// Read in a user defined log directory or use the default
string logDir = cfg.getValueString("log_dir");
// Initialise using the configured logging directory
log.vlog("Using logfile dir: ", logDir);
log.init(logDir);
}