aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-12-04 10:50:55 -0600
committerGitHub <noreply@github.com>2021-12-04 10:50:55 -0600
commit9ba9581e3de9812db5f0b31e833db1bbc499c7bb (patch)
treed95ec2d91afb59d879f9b9ebfb2bc9cd46a5c938
parent6d0d042c9ce584cb76248513b477e905dae6c87c (diff)
parent7596778656ec695fb03e106ffe2e03d5ee2caee0 (diff)
downloadslskd-9ba9581e3de9812db5f0b31e833db1bbc499c7bb.tar.gz
Merge pull request #294 from slskd/verify-writeable
Verify that downloads and incomplete directories are writeable when configuration changes
-rw-r--r--src/slskd/Common/Validation/DirectoryExistsAttributes.cs39
-rw-r--r--src/slskd/Options.cs4
2 files changed, 40 insertions, 3 deletions
diff --git a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs
index 45d7f71b..77a46777 100644
--- a/src/slskd/Common/Validation/DirectoryExistsAttributes.cs
+++ b/src/slskd/Common/Validation/DirectoryExistsAttributes.cs
@@ -17,24 +17,61 @@
namespace slskd.Validation
{
+ using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
+ using System.Linq;
/// <summary>
/// Validates that the directory at the specified path exists.
/// </summary>
public class DirectoryExistsAttribute : ValidationAttribute
{
+ public DirectoryExistsAttribute(bool ensureWriteable = false)
+ {
+ EnsureWriteable = ensureWriteable;
+ }
+
+ private bool EnsureWriteable { get; set; }
+
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
var dir = Path.GetFullPath(value?.ToString());
- if (!dir.StartsWith(Program.DefaultAppDirectory) && !string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
+ // default directories are exempt from validation, as there's an additional check (and creation)
+ // at startup for these directories.
+ if (new[] { Program.DefaultDownloadsDirectory, Program.DefaultIncompleteDirectory }.Contains(dir))
+ {
+ return ValidationResult.Success;
+ }
+
+ // empty values are valid, because they will fall back to defaults
+ if (string.IsNullOrEmpty(dir))
+ {
+ return ValidationResult.Success;
+ }
+
+ if (!Directory.Exists(dir))
{
return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-existent directory '{dir}'.");
}
+
+ if (EnsureWriteable)
+ {
+ try
+ {
+ var file = Guid.NewGuid().ToString();
+ var probe = Path.Combine(dir, file);
+ File.WriteAllText(probe, string.Empty);
+ File.Delete(probe);
+ }
+ catch (Exception)
+ {
+ return new ValidationResult($"The {validationContext.DisplayName} field specifies a directory '{dir}' that exists, but is not writeable.");
+ }
+ }
}
return ValidationResult.Success;
diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs
index 7546449f..15b954f7 100644
--- a/src/slskd/Options.cs
+++ b/src/slskd/Options.cs
@@ -273,7 +273,7 @@ namespace slskd
[Argument(default, "incomplete")]
[EnvironmentVariable("INCOMPLETE_DIR")]
[Description("path where incomplete downloads are saved")]
- [DirectoryExists]
+ [DirectoryExists(ensureWriteable: true)]
[RequiresRestart]
public string Incomplete { get; private set; } = Program.DefaultIncompleteDirectory;
@@ -283,7 +283,7 @@ namespace slskd
[Argument('o', "downloads")]
[EnvironmentVariable("DOWNLOADS_DIR")]
[Description("path where downloaded files are saved")]
- [DirectoryExists]
+ [DirectoryExists(ensureWriteable: true)]
[RequiresRestart]
public string Downloads { get; private set; } = Program.DefaultDownloadsDirectory;