diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-08-10 16:59:40 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-10 16:59:40 -0500 |
| commit | 52cf12d65d55f6c7431c669991c7fd0cb130cc0f (patch) | |
| tree | 66a1f8dd25cf23e3ab2b712c348f8657f1f9e37e /src/slskd/Shares | |
| parent | 2bb92971e5e6414ad1d01aaca92808cbfe96fc72 (diff) | |
| parent | 9353755d0ee0f2764dcf6e5574758ee57ff73cd6 (diff) | |
| download | slskd-52cf12d65d55f6c7431c669991c7fd0cb130cc0f.tar.gz | |
Merge pull request #1418 from slskd/lists
Ensure lists stored as class property values are read only and properly materialized
Diffstat (limited to 'src/slskd/Shares')
| -rw-r--r-- | src/slskd/Shares/ShareScanner.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 22 |
2 files changed, 23 insertions, 8 deletions
diff --git a/src/slskd/Shares/ShareScanner.cs b/src/slskd/Shares/ShareScanner.cs index bf069991..6e32f81e 100644 --- a/src/slskd/Shares/ShareScanner.cs +++ b/src/slskd/Shares/ShareScanner.cs @@ -88,7 +88,7 @@ namespace slskd.Shares private FileService Files { get; } private int WorkerCount { get; } private ILogger Log { get; } = Serilog.Log.ForContext<ShareScanner>(); - private List<Share> Shares { get; set; } + private IReadOnlyList<Share> Shares { get; set; } private ISoulseekFileFactory SoulseekFileFactory { get; } private IManagedState<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>(); private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); @@ -161,6 +161,8 @@ namespace slskd.Shares var swSnapshot = 0L; sw.Start(); + // note: this should be the only place where we are assigning Shares, so that this is 1) synchronized by SyncRoot + // and 2) stable throughout the scan. if this ever changes we'll have to snapshot the value and use it instead Shares = shares.ToList(); if (!Shares.Any()) @@ -168,7 +170,10 @@ namespace slskd.Shares Log.Warning("Aborting shared file scan; no shares configured."); } - Shares.ForEach(s => Log.Debug(s.ToJson())); + foreach (var share in Shares) + { + Log.Debug(share.ToJson()); + } Shares.Where(s => !s.IsExcluded).ToList() .ForEach(s => Log.Information("Sharing {Local} as {Remote}", s.LocalPath, s.RemotePath)); diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 19579458..6068a744 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -59,7 +59,7 @@ namespace slskd.Shares Local = (host, repository); - AllRepositories = new List<IShareRepository>(new[] { repository }); + AllRepositories = [repository]; Scanner = scanner ?? new ShareScanner( workerCount: options.Shares.Cache.Workers, @@ -116,7 +116,7 @@ namespace slskd.Shares private StorageMode CacheStorageMode { get; } private ILogger Log { get; } = Serilog.Log.ForContext<ShareService>(); private (Host Host, IShareRepository Repository) Local { get; set; } - private IEnumerable<IShareRepository> AllRepositories { get; set; } + private IReadOnlyList<IShareRepository> AllRepositories { get; set; } /// <summary> /// Adds a new, or updates an existing, share host. @@ -133,7 +133,9 @@ namespace slskd.Shares AllRepositories = HostDictionary.Values .Select(value => value.Repository) - .Prepend(Local.Repository); + .Prepend(Local.Repository) + .ToList() + .AsReadOnly(); State.SetValue(state => state with { @@ -158,17 +160,20 @@ namespace slskd.Shares prefix = share.RemotePath + (share.RemotePath.EndsWith('\\') ? string.Empty : '\\'); } + // snapshot the current list of repositories + var repositories = AllRepositories.ToList(); + // Soulseek requires that each directory in the tree have an entry in the list returned in a browse response. if // missing, files that are nested within directories which contain only directories (no files) are displayed as being // 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 AllRepositories.SelectMany(r => r.ListDirectories(prefix))) + foreach (var directory in repositories.SelectMany(r => r.ListDirectories(prefix))) { directories.TryAdd(directory, new Directory(directory)); } - var files = AllRepositories.SelectMany(r => r.ListFiles(prefix, includeFullPath: true)); + var files = repositories.SelectMany(r => r.ListFiles(prefix, includeFullPath: true)); var groups = files .GroupBy(file => file.Filename.GetNormalizedDirectoryName()) @@ -239,7 +244,9 @@ namespace slskd.Shares AllRepositories = HostDictionary.Values .Select(value => value.Repository) - .Prepend(Local.Repository); + .Prepend(Local.Repository) + .ToList() + .AsReadOnly(); State.SetValue(state => state with { @@ -315,6 +322,9 @@ namespace slskd.Shares /// Returns the list of all <see cref="Scan"/> started at or after the specified <paramref name="startedAtOrAfter"/> /// unix timestamp. /// </summary> + /// <remarks> + /// Note that this returns the scan history for local shares only. + /// </remarks> /// <param name="startedAtOrAfter">A unix timestamp that serves as the lower bound of the time-based listing.</param> /// <returns>The operation context, including the list of found scans.</returns> public Task<IEnumerable<Scan>> ListScansAsync(long startedAtOrAfter = 0) |
