From 86f5e46c2dfe595d8a970cc2f9e49d2520f14b55 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 9 Nov 2021 08:52:59 -0600 Subject: verify directories before config --- src/slskd/Program.cs | 76 ++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 3aba9ec3..2403dbbf 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -198,8 +198,23 @@ namespace slskd return; } - // the application isn't being run in command mode. load all configuration values and proceed - // with bootstrapping. + // the application isn't being run in command mode. verify (create if needed) default application + // directories. if the downloads or complete directories are overridden in config, those will be + // validated after the config is loaded. + try + { + VerifyDirectory(AppDirectory, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(Path.Combine(AppDirectory, "data"), createIfMissing: true, verifyWriteable: true); + VerifyDirectory(DefaultDownloadsDirectory, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(DefaultIncompleteDirectory, createIfMissing: true, verifyWriteable: true); + } + catch (Exception ex) + { + Console.WriteLine($"Filesystem exception: {ex.Message}"); + return; + } + + // load and validate the configuration try { Configuration = new ConfigurationBuilder() @@ -226,36 +241,6 @@ namespace slskd return; } - try - { - VerifyDirectory(AppDirectory, createIfMissing: true, verifyWriteable: true); - VerifyDirectory(Path.Combine(AppDirectory, "data"), createIfMissing: true, verifyWriteable: true); - - VerifyDirectory(OptionsAtStartup.Directories.Incomplete, createIfMissing: true, verifyWriteable: true); - VerifyDirectory(OptionsAtStartup.Directories.Downloads, createIfMissing: true, verifyWriteable: true); - } - catch (Exception ex) - { - Console.WriteLine($"Filesystem exception: {ex.Message}"); - return; - } - - // for convenience, try to copy the example configuration file to the application directory if no configuration - // file exists. if any part fails, just move on silently. - if (!IOFile.Exists(ConfigurationFile)) - { - try - { - var source = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.example.yml"); - var destination = ConfigurationFile; - IOFile.Copy(source, destination); - } - catch - { - // no-op - } - } - Log.Logger = (OptionsAtStartup.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information()) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("System.Net.Http.HttpClient", OptionsAtStartup.Debug ? LogEventLevel.Warning : LogEventLevel.Fatal) @@ -299,11 +284,6 @@ namespace slskd PrintLogo(Version); } - if (!IOFile.Exists(ConfigurationFile)) - { - logger.Warning($"Configuration file '{ConfigurationFile}' could not be found and was not loaded."); - } - logger.Information("Version: {Version}", Version); if (IsCanary) @@ -313,9 +293,29 @@ namespace slskd logger.Warning($"Please report any issues here: {IssuesUrl}"); } - logger.Information("Instance Name: {InstanceName}", OptionsAtStartup.InstanceName); logger.Information("Invocation ID: {InvocationId}", InvocationId); logger.Information("Process ID: {ProcessId}", ProcessId); + logger.Information("Instance Name: {InstanceName}", OptionsAtStartup.InstanceName); + + logger.Information("Using application directory {AppDirectory}", AppDirectory); + logger.Information("Using configuration file {ConfigurationFile}", ConfigurationFile); + + // for convenience, try to copy the example configuration file to the application directory if no configuration + // file exists. if any part fails, just move on silently. + if (!IOFile.Exists(ConfigurationFile)) + { + try + { + logger.Warning("Configuration file {ConfigurationFile} does not exist; creating from example", ConfigurationFile); + var source = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.example.yml"); + var destination = ConfigurationFile; + IOFile.Copy(source, destination); + } + catch (Exception ex) + { + logger.Error("Failed to create configuration file {ConfigurationFile}: {Message}", ConfigurationFile, ex.Message); + } + } if (!string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki)) { -- cgit v1.2.3