aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-06-14 09:57:18 -0500
committerGitHub <noreply@github.com>2022-06-14 09:57:18 -0500
commit69aa6d9ac78e0dbf8a4e70e4a6657c61f03da494 (patch)
tree70c9c77b4ec20b3a0adbb22edd9a29495c473244
parent8fcb6d4f058607a085ba2a76d2b4af6a1863e4bd (diff)
parenta9d54c1d51beeda6e09e29f8d68614ee887d58f2 (diff)
downloadslskd-69aa6d9ac78e0dbf8a4e70e4a6657c61f03da494.tar.gz
Merge pull request #511 from slskd/shares
Simplify share masking and allow sharing of root paths
-rw-r--r--docs/config.md4
-rw-r--r--src/slskd/Options.cs59
-rw-r--r--src/slskd/Shares/API/Controllers/SharesController.cs2
-rw-r--r--src/slskd/Shares/API/DTO/SummarizedShare.cs1
-rw-r--r--src/slskd/Shares/Domain/Share.cs14
-rw-r--r--src/slskd/Shares/SharedFileCache.cs4
6 files changed, 31 insertions, 53 deletions
diff --git a/docs/config.md b/docs/config.md
index a3fde410..74d7b24d 100644
--- a/docs/config.md
+++ b/docs/config.md
@@ -107,7 +107,7 @@ directories:
Any number of shared directories can be configured.
-Paths must be absolute, meaning they must begin with `/`, `X:\`, or `\\`, depending on the system. Relative paths, such as `~/directory` or `../directory`, and root paths (e.g. an entire drive, mount or network share) are not supported.
+Paths must be absolute, meaning they must begin with `/`, `X:\`, or `\\`, depending on the system. Relative paths, such as `~/directory` or `../directory`, are not supported. Sharing a root mount on a unix-like OS (`/`) is also not supported.
Shares can be excluded by prefixing them with `-` or `!`. This is useful in situations where sharing a subdirectory of a share isn't desired, for example, if a user wanted to share their entire music library, but not their personal recordings:
@@ -126,7 +126,7 @@ directories:
- '[Music]\users\John Doe\Music'
```
-To a remote user, any files within the aliased path will appear as though they are shared from a directory named as the alias.
+If no alias is specified, the name of the shared folder is used (e.g. `D:\Music` is aliased to `Music`). To a remote user, any files within the aliased path will appear as though they are shared from a directory named as the alias.
Aliases:
* Must be unique
diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs
index 041824de..c4de3554 100644
--- a/src/slskd/Options.cs
+++ b/src/slskd/Options.cs
@@ -327,54 +327,45 @@ namespace slskd
{
var results = new List<ValidationResult>();
- (string Raw, string Mask, string Alias, string Path) Digest(string share)
+ bool IsBlankPath(string share) => Regex.IsMatch(share.ToLocalOSPath(), @"^(!|-){0,1}(\[.*\])$");
+ Shared.Where(share => IsBlankPath(share)).ToList()
+ .ForEach(blank => results.Add(new ValidationResult($"Share {blank} doees not specify a path")));
+
+ bool IsRootMount(string share) => Regex.IsMatch(share.ToLocalOSPath(), @"^(!|-){0,1}(\[.*\])/$");
+ Shared.Where(share => IsRootMount(share)).ToList()
+ .ForEach(blank => results.Add(new ValidationResult($"Share {blank} specifies a root mount, which is not supported.")));
+
+ // starts with '/', 'X:', or '\\'
+ bool IsAbsolutePath(string share) => Regex.IsMatch(share.ToLocalOSPath(), @"^(!|-){0,1}(\[.*\])?(\/|[a-zA-Z]:|\\\\).*$");
+ Shared.Where(share => !IsAbsolutePath(share)).ToList()
+ .ForEach(relativePath => results.Add(new ValidationResult($"Share {relativePath} contains a relative path; only absolute paths are supported.")));
+
+ (string Raw, string Alias, string Path) Digest(string share)
{
- var matches = Regex.Matches(share, @"^(-?)\[(.*)\](.*)$");
+ var matches = Regex.Matches(share, @"^(!|-){0,1}\[(.*)\](.*)$");
if (matches.Any())
{
- return (share, Compute.MaskHash(Directory.GetParent(matches[0].Groups[3].Value)?.FullName ?? share), matches[0].Groups[2].Value, matches[0].Groups[3].Value);
+ return (share, matches[0].Groups[2].Value, matches[0].Groups[3].Value);
}
- return (share, Compute.MaskHash(Directory.GetParent(share)?.FullName ?? share), share.Split(new[] { '/', '\\' }).Last(), share);
- }
-
- bool IsRoot((string Raw, string Mask, string Alias, string Path) share) => share.Path == "/" || share.Path == "\\" || Path.GetPathRoot(share.Path) == share.Path;
-
- // starts with '/', 'X:\', or '\\'
- bool IsAbsolutePath(string share) => Regex.IsMatch(share.ToLocalOSPath(), @"^(!|-){0,1}(\[.*\])?(\/|[a-zA-Z]:\\|\\\\).*$");
-
- var relativePaths = Shared.Where(share => !IsAbsolutePath(share));
- foreach (var relativePath in relativePaths)
- {
- results.Add(new ValidationResult($"Share {relativePath} contains a relative path; only absolute paths are supported."));
+ return (share, share.Split(new[] { '/', '\\' }).Last(), share);
}
var digestedShared = Shared
.Select(share => Digest(share.TrimEnd('/', '\\')))
.ToHashSet();
- var roots = digestedShared.Where(share => IsRoot(share));
- foreach (var root in roots)
- {
- results.Add(new ValidationResult($"Share {root.Raw} is a root path, which is not supported."));
- }
-
- digestedShared = digestedShared.Where(share => !IsRoot(share)).ToHashSet();
-
- var overlapping = digestedShared.GroupBy(share => share.Mask + share.Alias).Where(group => group.Count() > 1);
- foreach (var overlap in overlapping)
- {
- results.Add(new ValidationResult($"Shares {string.Join(", ", overlap.Select(s => $"'{s.Raw}'"))} overlap"));
- }
+ // make sure all aliases are distinct
+ digestedShared.GroupBy(share => share.Alias).Where(group => group.Count() > 1).ToList()
+ .ForEach(overlap => results.Add(new ValidationResult($"Shares {string.Join(", ", overlap.Select(s => $"'{s.Raw}'"))} collide. Specify an alias for one or both to disambiguate them.")));
- var duplicates = digestedShared.GroupBy(share => share.Path).Where(group => group.Count() > 1);
- foreach (var dupe in duplicates)
- {
- results.Add(new ValidationResult($"Shares {string.Join(", ", dupe.Select(s => $"'{s.Raw}'"))} alias the same path"));
- }
+ // make sure the same path hasn't been specified twice, under different aliases
+ digestedShared.GroupBy(share => share.Path).Where(group => group.Count() > 1).ToList()
+ .ForEach(dupe => results.Add(new ValidationResult($"Shares {string.Join(", ", dupe.Select(s => $"'{s.Raw}'"))} alias the same path")));
- foreach (var share in digestedShared.Where(s => s.Alias != null))
+ // make sure each share has an alias, or is alias-able, and that the alias is valid
+ foreach (var share in digestedShared)
{
if (string.IsNullOrWhiteSpace(share.Alias))
{
diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs
index 41178bac..56023925 100644
--- a/src/slskd/Shares/API/Controllers/SharesController.cs
+++ b/src/slskd/Shares/API/Controllers/SharesController.cs
@@ -74,7 +74,6 @@ namespace slskd.Shares.API
Alias = share.Alias,
IsExcluded = share.IsExcluded,
LocalPath = share.LocalPath,
- Mask = share.Mask,
Raw = share.Raw,
RemotePath = share.RemotePath,
Directories = directories.Count(),
@@ -119,7 +118,6 @@ namespace slskd.Shares.API
Alias = share.Alias,
IsExcluded = share.IsExcluded,
LocalPath = share.LocalPath,
- Mask = share.Mask,
Raw = share.Raw,
RemotePath = share.RemotePath,
Directories = directories.Count(),
diff --git a/src/slskd/Shares/API/DTO/SummarizedShare.cs b/src/slskd/Shares/API/DTO/SummarizedShare.cs
index a1d6ed7b..4e296d1c 100644
--- a/src/slskd/Shares/API/DTO/SummarizedShare.cs
+++ b/src/slskd/Shares/API/DTO/SummarizedShare.cs
@@ -23,7 +23,6 @@ namespace slskd.Shares
public string Alias { get; init; }
public bool IsExcluded { get; init; }
public string LocalPath { get; init; }
- public string Mask { get; init; }
public string Raw { get; init; }
public string RemotePath { get; init; }
public int Directories { get; init; }
diff --git a/src/slskd/Shares/Domain/Share.cs b/src/slskd/Shares/Domain/Share.cs
index 9bdbc002..6a6d1f66 100644
--- a/src/slskd/Shares/Domain/Share.cs
+++ b/src/slskd/Shares/Domain/Share.cs
@@ -33,17 +33,15 @@ namespace slskd.Shares
/// <param name="alias"></param>
/// <param name="isExcluded"></param>
/// <param name="localPath"></param>
- /// <param name="mask"></param>
/// <param name="raw"></param>
/// <param name="remotePath"></param>
[JsonConstructor]
- public Share (string id, string alias, bool isExcluded, string localPath, string mask, string raw, string remotePath)
+ public Share (string id, string alias, bool isExcluded, string localPath, string raw, string remotePath)
{
Id = id;
Alias = alias;
IsExcluded = isExcluded;
LocalPath = localPath;
- Mask = mask;
Raw = raw;
RemotePath = remotePath;
}
@@ -78,14 +76,7 @@ namespace slskd.Shares
LocalPath = share;
}
- var parent = System.IO.Directory.GetParent(LocalPath).FullName.TrimEnd('/', '\\');
-
- Mask = Compute.MaskHash(parent);
-
- var maskedPath = LocalPath.ReplaceFirst(parent, Mask);
-
- var aliasedSegment = LocalPath[(parent.Length + 1)..];
- RemotePath = maskedPath.ReplaceFirst(aliasedSegment, Alias);
+ RemotePath = Alias;
Id = Compute.Sha1Hash(RemotePath);
}
@@ -94,7 +85,6 @@ namespace slskd.Shares
public string Alias { get; init; }
public bool IsExcluded { get; init; }
public string LocalPath { get; init; }
- public string Mask { get; init; }
public string Raw { get; init; }
public string RemotePath { get; init; }
}
diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs
index 6b7a3ca3..5588a4bb 100644
--- a/src/slskd/Shares/SharedFileCache.cs
+++ b/src/slskd/Shares/SharedFileCache.cs
@@ -375,11 +375,11 @@ namespace slskd.Shares
}
// find the share with a matching mask and alias/local directory
- var share = Shares.FirstOrDefault(share => share.Mask == parts[0] && share.Alias == parts[1]);
+ var share = Shares.FirstOrDefault(share => share.Alias == parts[0]);
if (share == default)
{
- Log.Warning("Failed to resolve shared file {Filename}; unable to resolve share from mask '{Mask}' and alias '{Alias}'", filename, parts[0], parts[1]);
+ Log.Warning("Failed to resolve shared file {Filename}; unable to resolve share from alias '{Alias}'", filename, parts[0]);
return resolved;
}