aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Options.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-08-15 17:40:37 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-08-15 17:40:37 -0500
commitb72247c7cc29db6d14e30cccb4ca092a2d2d46ca (patch)
treef8a57d6bf979e224a6d8e7858e40fcd49928e563 /src/slskd/Options.cs
parent9d6284676561d5817a4153ebd4a88c7675326ef5 (diff)
downloadslskd-b72247c7cc29db6d14e30cccb4ca092a2d2d46ca.tar.gz
check for overlapping shares
Diffstat (limited to 'src/slskd/Options.cs')
-rw-r--r--src/slskd/Options.cs40
1 files changed, 30 insertions, 10 deletions
diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs
index f4ea8add..43839c61 100644
--- a/src/slskd/Options.cs
+++ b/src/slskd/Options.cs
@@ -24,6 +24,7 @@ namespace slskd
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
+ using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using FluentFTP;
@@ -270,22 +271,41 @@ namespace slskd
{
var results = new List<ValidationResult>();
- foreach (var share in Shared)
+ (string Raw, string Mask, string Alias) Digest(string share)
{
var matches = Regex.Matches(share, @"^(-?)\[(.*)\](.*)$");
if (matches.Any())
{
- var alias = matches[0].Groups[2].Value;
+ return (share, Compute.MaskHash(Directory.GetParent(matches[0].Groups[3].Value).FullName), matches[0].Groups[2].Value);
+ }
- if (string.IsNullOrWhiteSpace(alias))
- {
- results.Add(new ValidationResult($"Share {share} is invalid; alias may not be null, empty or consist of only whitespace"));
- }
- else if (alias.Contains('\\') || alias.Contains('/'))
- {
- results.Add(new ValidationResult($"Share {share} is invalid; aliases may not contain path separators '/' or '\'"));
- }
+ return (share, Compute.MaskHash(Directory.GetParent(share).FullName), new Uri(share).Segments.Last());
+ }
+
+ var digestedShared = Shared
+ .Select(share => Digest(Path.TrimEndingDirectorySeparator(share)))
+ .ToHashSet();
+
+ var duplicates = digestedShared.GroupBy(share => share.Mask + share.Alias).Where(group => group.Count() > 1);
+
+ if (duplicates.Any())
+ {
+ foreach (var dupe in duplicates)
+ {
+ results.Add(new ValidationResult($"Shares {string.Join(", ", dupe.Select(s => s.Raw))} overlap. Use different alias(es) to disambiguate them."));
+ }
+ }
+
+ foreach (var share in digestedShared)
+ {
+ if (string.IsNullOrWhiteSpace(share.Alias))
+ {
+ results.Add(new ValidationResult($"Share {share.Raw} is invalid; alias may not be null, empty or consist of only whitespace"));
+ }
+ else if (share.Alias.Contains('\\') || share.Alias.Contains('/'))
+ {
+ results.Add(new ValidationResult($"Share {share.Raw} is invalid; aliases may not contain path separators '/' or '\'"));
}
}