diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-06-02 08:38:47 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-02 08:38:47 -0500 |
| commit | 2bd24b38f046d9e7b37825db6af2756e43ba0dfc (patch) | |
| tree | fd1f5b246cdc8597e4bc9176fd70bb6067bebfe9 /src/slskd/Shares/SharedFileCache.cs | |
| parent | 1a6075b612dd5067eb4e8376ac9034050ad74477 (diff) | |
| parent | 9e0013c0317adbaa007fce8dbea06928b1d159ef (diff) | |
| download | slskd-2bd24b38f046d9e7b37825db6af2756e43ba0dfc.tar.gz | |
Merge pull request #493 from slskd/shares
Add a 'Shares' tab to the 'System' page, along with a button to rescan shares
Diffstat (limited to 'src/slskd/Shares/SharedFileCache.cs')
| -rw-r--r-- | src/slskd/Shares/SharedFileCache.cs | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index ac996fa6..6b7a3ca3 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -16,7 +16,6 @@ // </copyright> using System.IO; -using Microsoft.Extensions.Options; namespace slskd.Shares { @@ -51,8 +50,11 @@ namespace slskd.Shares /// <summary> /// Scans the configured shares and fills the cache. /// </summary> + /// <remarks>Initiates the scan, then yields execution back to the caller; does not wait for the operation to complete.</remarks> + /// <param name="shares">The list of shares from which to fill the cache.</param> + /// <param name="filters">The list of regular expressions used to exclude files or paths from scanning.</param> /// <returns>The operation context.</returns> - Task FillAsync(); + Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters); /// <summary> /// Returns the contents of the specified <paramref name="directory"/>. @@ -85,13 +87,9 @@ namespace slskd.Shares /// <summary> /// Initializes a new instance of the <see cref="SharedFileCache"/> class. /// </summary> - /// <param name="optionsMonitor"></param> /// <param name="soulseekFileFactory"></param> - public SharedFileCache( - IOptionsMonitor<Options> optionsMonitor, - ISoulseekFileFactory soulseekFileFactory = null) + public SharedFileCache(ISoulseekFileFactory soulseekFileFactory = null) { - OptionsMonitor = optionsMonitor; SoulseekFileFactory = soulseekFileFactory ?? new SoulseekFileFactory(); } @@ -100,15 +98,14 @@ namespace slskd.Shares /// </summary> public IStateMonitor<SharedFileCacheState> StateMonitor => State; - private IManagedState<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>(); private ILogger Log { get; } = Serilog.Log.ForContext<SharedFileCache>(); private HashSet<string> MaskedDirectories { get; set; } private Dictionary<string, File> MaskedFiles { get; set; } - private IOptionsMonitor<Options> OptionsMonitor { get; set; } private List<Share> Shares { get; set; } + private ISoulseekFileFactory SoulseekFileFactory { get; } private SqliteConnection SQLite { get; set; } + private IManagedState<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); - private ISoulseekFileFactory SoulseekFileFactory { get; } /// <summary> /// Returns the contents of the cache. @@ -164,8 +161,10 @@ namespace slskd.Shares /// <summary> /// Scans the configured shares and fills the cache. /// </summary> + /// <param name="shares">The list of shares from which to fill the cache.</param> + /// <param name="filters">The list of regular expressions used to exclude files or paths from scanning.</param> /// <returns>The operation context.</returns> - public async Task FillAsync() + public async Task FillAsync(IEnumerable<Share> shares, IEnumerable<Regex> filters) { // obtain the semaphore, or fail if it can't be obtained immediately, indicating that a scan is running. if (!await SyncRoot.WaitAsync(millisecondsTimeout: 0)) @@ -176,7 +175,14 @@ namespace slskd.Shares try { - State.SetValue(state => state with { Filling = true, Filled = false, FillProgress = 0 }); + State.SetValue(state => state with + { + Filling = true, + Filled = false, + FillProgress = 0, + Shares = Enumerable.Empty<Share>().ToList().AsReadOnly(), + }); + Log.Debug("Starting shared file scan"); await Task.Yield(); @@ -187,12 +193,7 @@ namespace slskd.Shares var swSnapshot = 0L; sw.Start(); - Shares = OptionsMonitor.CurrentValue.Directories.Shared - .Select(share => share.TrimEnd('/', '\\')) - .ToHashSet() // remove duplicates - .Select(share => new Share(share)) // convert to Shares - .OrderByDescending(share => share.LocalPath.Length) // process subdirectories first. this allows them to be aliased separately from their parent - .ToList(); + Shares = shares.ToList(); if (!Shares.Any()) { @@ -236,9 +237,6 @@ namespace slskd.Shares Log.Debug("Found {Directories} shared directories (and {Excluded} were excluded) in {Elapsed}ms. Starting file scan.", unmaskedDirectories.Count, excludedDirectories.Count(), sw.ElapsedMilliseconds - swSnapshot); swSnapshot = sw.ElapsedMilliseconds; - var filters = OptionsMonitor.CurrentValue.Filters.Share - .Select(filter => new Regex(filter, RegexOptions.Compiled)); - var files = new Dictionary<string, File>(); var maskedDirectories = new HashSet<string>(); var current = 0; @@ -300,7 +298,16 @@ namespace slskd.Shares MaskedDirectories = maskedDirectories; MaskedFiles = files; - State.SetValue(state => state with { Filling = false, Faulted = false, Filled = true, FillProgress = 1, Directories = MaskedDirectories.Count, Files = MaskedFiles.Count }); + State.SetValue(state => state with { + Filling = false, + Faulted = false, + Filled = true, + FillProgress = 1, + Directories = MaskedDirectories.Count, + Files = MaskedFiles.Count, + Shares = shares.ToList().AsReadOnly(), + }); + Log.Debug($"Shared file cache recreated in {sw.ElapsedMilliseconds}ms. Directories: {unmaskedDirectories.Count}, Files: {files.Count}"); } catch (Exception ex) @@ -451,7 +458,7 @@ namespace slskd.Shares SQLite = new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared"); SQLite.Open(); - using var cmd = new SqliteCommand("CREATE VIRTUAL TABLE cache USING fts5(filename)", SQLite); + using var cmd = new SqliteCommand("DROP TABLE IF EXISTS cache; CREATE VIRTUAL TABLE cache USING fts5(filename);", SQLite); cmd.ExecuteNonQuery(); } |
