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/slskd/Application/SharedFileCache.cs') 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/slskd/Application/SharedFileCache.cs') 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