From 06852f180bc4bdf2c9ef14ba774347e2da8949da Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Jul 2021 08:11:19 -0500 Subject: change type of Shared to array --- src/slskd/Options.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 4a032c47..b51e9486 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -241,7 +241,7 @@ namespace slskd [Description("path to shared files")] [DirectoryExists] [RequiresRestart] - public string Shared { get; private set; } = Program.DefaultSharedDirectory; + public string[] Shared { get; private set; } = new[] { Program.DefaultSharedDirectory }; } /// -- cgit v1.2.3 From 6e0867604f1fd571f4ab3307c0522fe5f11df76b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Jul 2021 08:11:40 -0500 Subject: tweak directory validation to log warnings for missing shares --- src/slskd/Program.cs | 58 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index b5275756..fa1be786 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -212,19 +212,6 @@ namespace slskd return; } - try - { - VerifyOrCreateDirectory(OptionsAtStartup.Directories.App); - VerifyOrCreateDirectory(OptionsAtStartup.Directories.Incomplete); - VerifyOrCreateDirectory(OptionsAtStartup.Directories.Downloads); - VerifyOrCreateDirectory(OptionsAtStartup.Directories.Shared, verifyWriteable: false); - } - catch (Exception ex) - { - Console.WriteLine($"Filesystem exception: {ex.Message}"); - return; - } - Log.Logger = (OptionsAtStartup.Debug ? new LoggerConfiguration().MinimumLevel.Debug() : new LoggerConfiguration().MinimumLevel.Information()) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("slskd.API.Authentication.PassthroughAuthenticationHandler", LogEventLevel.Information) @@ -249,6 +236,26 @@ namespace slskd var logger = Log.ForContext(typeof(Program)); + try + { + VerifyDirectory(OptionsAtStartup.Directories.App, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(OptionsAtStartup.Directories.Incomplete, createIfMissing: true, verifyWriteable: true); + VerifyDirectory(OptionsAtStartup.Directories.Downloads, createIfMissing: true, verifyWriteable: true); + + foreach (var share in OptionsAtStartup.Directories.Shared) + { + if (!VerifyDirectory(share, createIfMissing: false, verifyWriteable: false)) + { + logger.Warning("Shared directory {Directory} does not exist", share); + } + } + } + catch (Exception ex) + { + logger.Error(ex, "Filesystem exception: {Message}", ex.Message); + return; + } + if (!OptionsAtStartup.NoLogo) { PrintLogo(Version); @@ -505,19 +512,26 @@ namespace slskd Console.WriteLine(banner); } - private static void VerifyOrCreateDirectory(string directory, bool verifyWriteable = true) + private static bool VerifyDirectory(string directory, bool createIfMissing = true, bool verifyWriteable = true) { - try + if (!Directory.Exists(directory)) { - if (!Directory.Exists(directory)) + if (createIfMissing) + { + try + { + Directory.CreateDirectory(directory); + } + catch (Exception ex) + { + throw new IOException($"Directory {directory} does not exist, and could not be created: {ex.Message}", ex); + } + } + else { - Directory.CreateDirectory(directory); + return false; } } - catch (Exception ex) - { - throw new IOException($"Directory {directory} does not exist, and could not be created: {ex.Message}", ex); - } if (verifyWriteable) { @@ -533,6 +547,8 @@ namespace slskd throw new IOException($"Directory {directory} is not writeable: {ex.Message}", ex); } } + + return true; } } } \ No newline at end of file -- cgit v1.2.3 From 0ffa057dd94c1dcfe090631e8ad53099e743653e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Jul 2021 08:14:37 -0500 Subject: iterate over shares when resolving browse response --- src/slskd/Application/Service.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Service.cs b/src/slskd/Application/Service.cs index 1d2aec25..ef47cf24 100644 --- a/src/slskd/Application/Service.cs +++ b/src/slskd/Application/Service.cs @@ -19,6 +19,7 @@ namespace slskd { using System; using System.Collections.Concurrent; + using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; @@ -325,10 +326,16 @@ namespace slskd /// A Task resolving an IEnumerable of Soulseek.Directory. private Task BrowseResponseResolver(string username, IPEndPoint endpoint) { - var directories = System.IO.Directory - .GetDirectories(OptionsMonitor.CurrentValue.Directories.Shared, "*", SearchOption.AllDirectories) - .Select(dir => new Soulseek.Directory(dir.Replace("/", @"\"), System.IO.Directory.GetFiles(dir) - .Select(f => new Soulseek.File(1, Path.GetFileName(f), new FileInfo(f).Length, Path.GetExtension(f))))); + var shares = OptionsMonitor.CurrentValue.Directories.Shared; + var directories = new List(); + + foreach (var share in shares) + { + directories.AddRange(System.IO.Directory + .GetDirectories(share, "*", SearchOption.AllDirectories) + .Select(dir => new Soulseek.Directory(dir.Replace("/", @"\"), System.IO.Directory.GetFiles(dir) + .Select(f => new Soulseek.File(1, Path.GetFileName(f), new FileInfo(f).Length, Path.GetExtension(f)))))); + } return Task.FromResult(new BrowseResponse(directories)); } -- cgit v1.2.3 From 455e66727a149ea8040aad3fcb1306507312fa32 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Jul 2021 09:09:41 -0500 Subject: pluralize Directory --- src/slskd/Application/ISharedFileCache.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/ISharedFileCache.cs b/src/slskd/Application/ISharedFileCache.cs index a7b8bf40..97133e40 100644 --- a/src/slskd/Application/ISharedFileCache.cs +++ b/src/slskd/Application/ISharedFileCache.cs @@ -25,12 +25,12 @@ namespace slskd { event EventHandler<(int Directories, int Files)> Refreshed; - string Directory { get; } + IEnumerable Directories { get; } DateTime? LastFill { get; } long TTL { get; } void Fill(); - IEnumerable Search(SearchQuery query); + IEnumerable Search(SearchQuery query); } } \ No newline at end of file -- cgit v1.2.3 From ad91fc0692e56a0bec49870c0f1d4bd78e967938 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Jul 2021 09:10:20 -0500 Subject: iterate over each share and merge into a single dictionary after each --- src/slskd/Application/SharedFileCache.cs | 40 +++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index 00496a47..125872a0 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -34,17 +34,17 @@ namespace slskd /// /// Initializes a new instance of the class. /// - /// + /// /// - public SharedFileCache(string directory, long ttl) + public SharedFileCache(IEnumerable directories, long ttl) { - Directory = directory; + Directories = directories; TTL = ttl; } public event EventHandler<(int Directories, int Files)> Refreshed; - public string Directory { get; } + public IEnumerable Directories { get; } public DateTime? LastFill { get; set; } public long TTL { get; } @@ -53,7 +53,7 @@ namespace slskd private ReaderWriterLockSlim SyncRoot { get; } = new ReaderWriterLockSlim(); /// - /// Scans the configured and fills the cache. + /// Scans the configured and fills the cache. /// public void Fill() { @@ -68,20 +68,38 @@ namespace slskd { CreateTable(); - var directories = System.IO.Directory.GetDirectories(Directory, "*", SearchOption.AllDirectories); + var directories = 0; + var files = new Dictionary(); - Files = System.IO.Directory.GetFiles(Directory, "*", SearchOption.AllDirectories) - .Select(f => new Soulseek.File(1, f.Replace("/", @"\"), new FileInfo(f).Length, Path.GetExtension(f))) - .ToDictionary(f => f.Filename, f => f); + foreach (var directory in Directories) + { + directories += System.IO.Directory.GetDirectories(directory, "*", SearchOption.AllDirectories).Length; + + // recursively find all files in the directory and stick a record in a dictionary, keyed on the sanitized + // filename and with a value of a Soulseek.File object + var newFiles = System.IO.Directory.GetFiles(directory, "*", SearchOption.AllDirectories) + .Select(f => new Soulseek.File(1, f.Replace("/", @"\"), new FileInfo(f).Length, Path.GetExtension(f))) + .ToDictionary(f => f.Filename, f => f); + + // merge the new dictionary with the rest + // this will overwrite any duplicate keys, but keys are the fully qualified name + foreach (var file in newFiles) + { + Console.WriteLine(file.Key); + files[file.Key] = file.Value; + } + } // potentially optimize with multi-valued insert // https://stackoverflow.com/questions/16055566/insert-multiple-rows-in-sqlite - foreach (var file in Files) + foreach (var file in files) { InsertFilename(file.Key); } - Refreshed?.Invoke(this, (directories.Length, Files.Count)); + Files = files; + + Refreshed?.Invoke(this, (directories, Files.Count)); } finally { -- cgit v1.2.3 From b97e6c65e658b3452e4f7cfd81c9a0d4114df7df Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Jul 2021 09:14:14 -0500 Subject: log warning if shared files are in conflict --- src/slskd/Application/SharedFileCache.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index 125872a0..45a810fc 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -83,9 +83,14 @@ namespace slskd // merge the new dictionary with the rest // this will overwrite any duplicate keys, but keys are the fully qualified name + // the only time this *should* cause problems is if one of the shares is a subdirectory of another. foreach (var file in newFiles) { - Console.WriteLine(file.Key); + if (files.ContainsKey(file.Key)) + { + Console.WriteLine($"[WARNING] File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories."); + } + files[file.Key] = file.Value; } } -- cgit v1.2.3 From 540dfc80285c5d62b5a97ada9fa91b12060e88b5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Jul 2021 09:15:22 -0500 Subject: remove DirectoryExists validation on shares --- src/slskd/Options.cs | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index b51e9486..c9a107cc 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -239,7 +239,6 @@ namespace slskd [Argument('s', "shared")] [EnvironmentVariable("SHARED_DIR")] [Description("path to shared files")] - [DirectoryExists] [RequiresRestart] public string[] Shared { get; private set; } = new[] { Program.DefaultSharedDirectory }; } -- cgit v1.2.3 From c643d91c7feb9ab360539c17380e24673f0a102b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 28 Jul 2021 22:46:58 -0500 Subject: serialize array defaults --- .../Common/Configuration/DefaultValueConfigurationSource.cs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index eae4b2eb..a13850dc 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -20,6 +20,7 @@ namespace slskd.Configuration using System; using System.Linq; using System.Reflection; + using System.Text.Json; using Microsoft.Extensions.Configuration; /// @@ -110,6 +111,14 @@ namespace slskd.Configuration Data[key] = value.ToString(); } } + else + { + // serialize array defaults and stick them on the parent key + // (not indexed by array position). this value is "stuck", and + // we want to show that in the config debug view. this isn't really + // functional, just illustrative. + Data[key] = JsonSerializer.Serialize(property.GetValue(defaults)); + } } } } -- cgit v1.2.3