aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Program.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-05-19 22:25:27 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-05-19 22:25:27 -0500
commit95df56cab13a1b30800c7514dcd07c55ebd8f587 (patch)
tree74559b3175e50cc0dea01a8c208283332d108417 /src/slskd/Program.cs
parenta6aabcf7fbd2241de75e7db7a4caa6d0b61d4678 (diff)
downloadslskd-95df56cab13a1b30800c7514dcd07c55ebd8f587.tar.gz
simplify options validation, verify directories
Diffstat (limited to 'src/slskd/Program.cs')
-rw-r--r--src/slskd/Program.cs69
1 files changed, 38 insertions, 31 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index d0e73c4e..8869f441 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -183,7 +183,7 @@ namespace slskd
return;
}
- // the application isn't bein run in command mode. load all configuration values and proceed
+ // the application isn't being run in command mode. load all configuration values and proceed
// with bootstrapping.
try
{
@@ -199,20 +199,6 @@ namespace slskd
Console.WriteLine(result.GetResultView("Invalid configuration:"));
return;
}
-
- var cert = Options.Web.Https.Certificate;
-
- if (!string.IsNullOrEmpty(cert.Pfx) && !X509.TryValidate(cert.Pfx, cert.Password, out var certResult))
- {
- Console.WriteLine($"Invalid HTTPs certificate: {certResult}");
- return;
- }
-
- if (Options.Debug)
- {
- Console.WriteLine("Configuration:");
- Console.WriteLine(Configuration.GetDebugView());
- }
}
catch (Exception ex)
{
@@ -220,30 +206,21 @@ namespace slskd
return;
}
- // ensure the application directory exists and is writeable. the most comprehensive way to test this is to try
- // to write a file to it.
- try
+ if (Options.Debug)
{
- if (!Directory.Exists(Options.Directories.App))
- {
- Directory.CreateDirectory(Options.Directories.App);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"App directory {Options.Directories.App} does not exist, and could not be created: {ex.Message}");
- return;
+ Console.WriteLine($"Configuration:\n{Configuration.GetDebugView()}");
}
try
{
- var probe = Path.Combine(Options.Directories.App, "probe");
- File.WriteAllText(Path.Combine(Options.Directories.App, "probe"), string.Empty);
- File.Delete(probe);
+ VerifyOrCreateDirectory(Options.Directories.App);
+ VerifyOrCreateDirectory(Options.Directories.Incomplete);
+ VerifyOrCreateDirectory(Options.Directories.Downloads);
+ VerifyOrCreateDirectory(Options.Directories.Shared, verifyWriteable: false);
}
catch (Exception ex)
{
- Console.WriteLine($"App directory {Options.Directories.App} is not writeable: {ex.Message}");
+ Console.WriteLine($"Filesystem exception: {ex.Message}");
return;
}
@@ -492,5 +469,35 @@ namespace slskd
Console.WriteLine(banner);
}
+
+ private static void VerifyOrCreateDirectory(string directory, bool verifyWriteable = true)
+ {
+ try
+ {
+ if (!Directory.Exists(directory))
+ {
+ Directory.CreateDirectory(directory);
+ }
+ }
+ catch (Exception ex)
+ {
+ throw new IOException($"Directory {directory} does not exist, and could not be created: {ex.Message}", ex);
+ }
+
+ if (verifyWriteable)
+ {
+ try
+ {
+ var file = Guid.NewGuid().ToString();
+ var probe = Path.Combine(directory, file);
+ File.WriteAllText(probe, string.Empty);
+ File.Delete(probe);
+ }
+ catch (Exception ex)
+ {
+ throw new IOException($"Directory {directory} is not writeable: {ex.Message}", ex);
+ }
+ }
+ }
}
} \ No newline at end of file