diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Validation/DirectoryExistsAttributes.cs | 39 | ||||
| -rw-r--r-- | src/slskd/Options.cs | 4 |
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; |
