From c8cda28935846068bcfdf37c86f3a13c5170e61d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 6 Aug 2021 10:01:08 -0500 Subject: implement masking --- src/slskd/Application/SharedFileCache.cs | 93 ++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 34 deletions(-) (limited to 'src/slskd/Application/SharedFileCache.cs') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index 9a5770b6..761941f2 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -51,12 +51,14 @@ namespace slskd /// public IStateMonitor State { get; } = new StateMonitor(); - private HashSet Directories { get; set; } private Dictionary Files { get; set; } private ILogger Log { get; } = Serilog.Log.ForContext(); + private HashSet MaskedDirectories { get; set; } + private Dictionary Masks { get; set; } = new Dictionary(); private IOptionsMonitor OptionsMonitor { get; set; } private SqliteConnection SQLite { get; set; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); + private HashSet UnmaskedDirectories { get; set; } /// /// Returns the contents of the cache. @@ -71,7 +73,7 @@ namespace slskd // in the root. to get around this, prime a dictionary with all known directories, and an empty Soulseek.Directory. if // there are any files in the directory, this entry will be overwritten with a new Soulseek.Directory containing the // files. if not they'll be left as is. - foreach (var directory in Directories) + foreach (var directory in MaskedDirectories) { directories.TryAdd(directory, new Directory(directory)); } @@ -117,37 +119,44 @@ namespace slskd try { + // todo: don't do this, but rather build a new cache and then swap it in + ResetCache(); + State.SetValue(state => state with { Filling = true, FillProgress = 0 }); Log.Debug("Starting shared file scan"); var sw = new Stopwatch(); - sw.Start(); var swSnapshot = 0L; + sw.Start(); - // this destroys the old table and creates a new one. empty the other cached fields to reflect this - // todo: use an A/B style table naming schema so that the cache remains available while it is filling - CreateTable(); - Directories = new HashSet(); - Files = new Dictionary(); - State.SetValue(state => state with { Directories = 0, Files = 0 }); + var shares = OptionsMonitor.CurrentValue.Directories.Shared.ToList(); // copy it so it can't change as we scan + + var masks = new Dictionary(shares + .Select(s => System.IO.Directory.GetParent(s).FullName) + .Select(s => new KeyValuePair(Compute.MaskHash(s), s))); Log.Debug("Enumerating shared directories"); swSnapshot = sw.ElapsedMilliseconds; - var directories = OptionsMonitor.CurrentValue.Directories.Shared + var unmaskedDirectories = shares .SelectMany(share => System.IO.Directory.GetDirectories(share, "*", SearchOption.AllDirectories)) + .Concat(shares) .ToHashSet(); - State.SetValue(state => state with { Directories = directories.Count }); - - Log.Debug("Found {Directories} shared directories in {Elapsed}ms. Starting file scan.", sw.ElapsedMilliseconds - swSnapshot, directories.Count); + State.SetValue(state => state with { Directories = unmaskedDirectories.Count }); + Log.Debug("Found {Directories} shared directories in {Elapsed}ms. Starting file scan.", sw.ElapsedMilliseconds - swSnapshot, unmaskedDirectories.Count); swSnapshot = sw.ElapsedMilliseconds; var files = new Dictionary(); + var maskedDirectories = new HashSet(); var current = 0; - foreach (var directory in directories) + foreach (var directory in unmaskedDirectories) { + var mask = masks.First(m => directory.StartsWith(m.Value)); + + maskedDirectories.Add(directory.ReplaceFirst(mask.Value, mask.Key)); + // 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.TopDirectoryOnly) @@ -163,11 +172,11 @@ namespace slskd Log.Warning($"File {file.Key} shared in directory {directory} has already been cached. This is probably a misconfiguration of the shared directories (is a subdirectory being re-shared?)."); } - files[file.Key] = file.Value; + files[file.Key.ReplaceFirst(mask.Value, mask.Key)] = file.Value; } current++; - State.SetValue(state => state with { FillProgress = current / (double)directories.Count, Files = files.Count }); + State.SetValue(state => state with { FillProgress = current / (double)unmaskedDirectories.Count, Files = files.Count }); } Log.Debug("Directory scan found {Files} in {Elapsed}ms. Populating filename database", files.Count, sw.ElapsedMilliseconds - swSnapshot); @@ -181,29 +190,17 @@ namespace slskd Log.Debug("Inserted {Files} records in {Elapsed}ms", files.Count, sw.ElapsedMilliseconds - swSnapshot); - Directories = directories; + UnmaskedDirectories = unmaskedDirectories; + MaskedDirectories = maskedDirectories; + Masks = masks; Files = files; - State.SetValue(state => state with - { - Filling = false, - Faulted = false, - FillProgress = 1, - Directories = Directories.Count, - Files = Files.Count, - }); - - Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {directories.Count}, Files: {files.Count}"); + State.SetValue(state => state with { Filling = false, Faulted = false, FillProgress = 1, Directories = UnmaskedDirectories.Count, Files = Files.Count }); + Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {unmaskedDirectories.Count}, Files: {files.Count}"); } catch (Exception ex) { - State.SetValue(state => state with - { - Filling = false, - Faulted = true, - FillProgress = 0, - }); - + State.SetValue(state => state with { Filling = false, Faulted = true, FillProgress = 0 }); Log.Warning(ex, "Encountered error during scan of shared files: {Message}", ex.Message); throw; } @@ -213,6 +210,24 @@ namespace slskd } } + /// + /// Substitutes the mask in the specified with the original path, if the mask is tracked + /// by the cache. + /// + /// The fully qualified filename to unmask. + /// The unmasked filename. + public string Resolve(string filename) + { + var mask = filename.Split(new[] { '/', '\\' }).FirstOrDefault(); + + if (Masks.ContainsKey(mask)) + { + return filename.ReplaceFirst(mask, Masks[mask]); + } + + return filename; + } + /// /// Searches the cache for the specified and returns the matching files. /// @@ -269,5 +284,15 @@ namespace slskd using var cmd = new SqliteCommand($"INSERT INTO cache(filename) VALUES('{filename.Replace("'", "''")}')", SQLite); cmd.ExecuteNonQuery(); } + + private void ResetCache() + { + CreateTable(); + UnmaskedDirectories = new HashSet(); + MaskedDirectories = new HashSet(); + Masks = new Dictionary(); + Files = new Dictionary(); + State.SetValue(state => state with { Directories = 0, Files = 0 }); + } } } \ No newline at end of file -- cgit v1.2.3