aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/slskd/Common/FileSafety.cs23
-rw-r--r--tests/slskd.Tests.Unit/Common/FileSafety/StripPathRootTests.cs150
2 files changed, 173 insertions, 0 deletions
diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs
index 93a91faa..a175ebbd 100644
--- a/src/slskd/Common/FileSafety.cs
+++ b/src/slskd/Common/FileSafety.cs
@@ -365,4 +365,27 @@ public static class FileSafety
return string.Join(sep, segments);
}
+
+ /// <summary>
+ /// Removes the root of the specified <paramref name="path"/>, including Window's drive prefix (e.g. C:\),
+ /// UNC paths on any OS (e.g. \\server on Windows, //server on Linux), and drops the hashed prefix added by
+ /// Soulseek Qt to hide the path on disk (e.g. @@abcde).
+ /// </summary>
+ /// <param name="path">The path to strip.</param>
+ /// <param name="os">An optional operating system override, for testing.</param>
+ /// <returns>The path with the root stripped, if one was present.</returns>
+ public static string StripPathRoot(string path, OSPlatform? os = null)
+ {
+ // flip slashes the correct way
+ path = LocalizePath(path, os);
+
+ // strip C:\ or //server, if present (regardless of slash variant)
+ path = DriveRootRegex.Replace(path, string.Empty);
+ path = UncRootRegex.Replace(path, string.Empty);
+
+ // strip @@abcde prefixes used by SoulseekQt to obscure paths
+ path = SoulseekQtRootRegex.Replace(path, string.Empty);
+
+ return path;
+ }
} \ No newline at end of file
diff --git a/tests/slskd.Tests.Unit/Common/FileSafety/StripPathRootTests.cs b/tests/slskd.Tests.Unit/Common/FileSafety/StripPathRootTests.cs
new file mode 100644
index 00000000..f3638c9e
--- /dev/null
+++ b/tests/slskd.Tests.Unit/Common/FileSafety/StripPathRootTests.cs
@@ -0,0 +1,150 @@
+using System.Runtime.InteropServices;
+using Xunit;
+
+namespace slskd.Tests.Unit.Common;
+
+public partial class FileSafetyTests
+{
+ public class StripPathRootTests
+ {
+ [Fact]
+ public void NullOs_UsesPlatformDefault_DoesNotThrow()
+ {
+ var result = FileSafety.StripPathRoot("foo/bar");
+
+ Assert.NotNull(result);
+ }
+
+ [Theory]
+ [InlineData("C:\\Music\\Artist", "Music/Artist")]
+ [InlineData("C:/Music/Artist", "Music/Artist")]
+ [InlineData("D:\\path\\to\\file", "path/to/file")]
+ [InlineData("Z:\\", "")]
+ [InlineData("Z:/", "")]
+ [InlineData("Z:", "")] // bare drive letter, no separator
+ [InlineData("c:\\Music", "Music")] // lowercase drive letter
+ [InlineData("A:/single", "single")]
+ public void Linux_Strips_DriveRoot(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Linux);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("C:\\Music\\Artist", "Music\\Artist")]
+ [InlineData("C:/Music/Artist", "Music\\Artist")]
+ [InlineData("D:\\path\\to\\file", "path\\to\\file")]
+ [InlineData("Z:\\", "")]
+ [InlineData("Z:", "")]
+ [InlineData("c:\\Music", "Music")]
+ public void Windows_Strips_DriveRoot(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Windows);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("//server/share/Music", "share/Music")]
+ [InlineData("//server/Music", "Music")]
+ [InlineData("//192.168.1.1/share/folder", "share/folder")]
+ [InlineData("//server", "")] // no path after server
+ public void Linux_Strips_UncRoot(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Linux);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("\\\\server\\share\\Music", "share\\Music")]
+ [InlineData("\\\\server\\Music", "Music")]
+ [InlineData("\\\\192.168.1.1\\share\\folder", "share\\folder")]
+ [InlineData("\\\\server", "")] // no path after server
+ public void Windows_Strips_UncRoot(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Windows);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("@@abcde/Music/Artist", "Music/Artist")]
+ [InlineData("@@abcdefgh/Music", "Music")]
+ [InlineData("@@abcde\\Music\\Artist", "Music/Artist")]
+ public void Linux_Strips_SoulseekQtPrefix(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Linux);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("@@abcde\\Music\\Artist", "Music\\Artist")]
+ [InlineData("@@abcdefgh\\Music", "Music")]
+ [InlineData("@@abcde/Music/Artist", "Music\\Artist")]
+ public void Windows_Strips_SoulseekQtPrefix(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Windows);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("@@abc/Music")] // only 3 alphanumeric chars — does not match
+ [InlineData("@@abcd/Music")] // only 4 — does not match
+ public void DoesNotStrip_SoulseekQtPrefix_When_Prefix_Too_Short(string input)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Linux);
+
+ Assert.StartsWith("@@", result);
+ }
+
+ [Theory]
+ [InlineData("Music/Artist", "Music/Artist")]
+ [InlineData("Artist/Album/song.flac", "Artist/Album/song.flac")]
+ [InlineData("just_a_filename.flac", "just_a_filename.flac")]
+ [InlineData("", "")]
+ [InlineData("foo", "foo")]
+ public void Linux_ReturnsUnchanged_Given_Relative_Path(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Linux);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("Music\\Artist", "Music\\Artist")]
+ [InlineData("Artist\\Album\\song.flac", "Artist\\Album\\song.flac")]
+ [InlineData("just_a_filename.flac", "just_a_filename.flac")]
+ [InlineData("foo", "foo")]
+ public void Windows_ReturnsUnchanged_Given_Relative_Path(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Windows);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("/home/user/Music", "/home/user/Music")]
+ [InlineData("/Music", "/Music")]
+ [InlineData("/single", "/single")]
+ public void Linux_ReturnsUnchanged_Given_Single_ForwardSlash_Prefix(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Linux);
+
+ Assert.Equal(expected, result);
+ }
+
+ [Theory]
+ [InlineData("\\Music", "\\Music")]
+ [InlineData("\\home\\user", "\\home\\user")]
+ public void Windows_ReturnsUnchanged_Given_Single_BackwardSlash_Prefix(string input, string expected)
+ {
+ var result = FileSafety.StripPathRoot(input, OSPlatform.Windows);
+
+ Assert.Equal(expected, result);
+ }
+ }
+}