//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ This program is free software: you can redistribute it and/or modify
// │ it under the terms of the GNU Affero General Public License as published
// │ by the Free Software Foundation, version 3.
// │
// │ This program is distributed in the hope that it will be useful,
// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// │ GNU Affero General Public License for more details.
// │
// │ You should have received a copy of the GNU Affero General Public License
// │ along with this program. If not, see https://www.gnu.org/licenses/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
using Microsoft.Extensions.Options;
namespace slskd.Shares
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Serilog;
using slskd.Files;
using slskd.Relay;
using Soulseek;
///
/// Provides control and interactions with configured shares and shared files.
///
public class ShareService : IShareService
{
///
/// Initializes a new instance of the class.
///
///
///
///
///
public ShareService(
FileService fileService,
IShareRepositoryFactory shareRepositoryFactory,
IOptionsMonitor optionsMonitor,
IShareScanner scanner = null)
{
var options = optionsMonitor.CurrentValue;
CacheStorageMode = options.Shares.Cache.StorageMode.ToEnum();
ShareRepositoryFactory = shareRepositoryFactory;
var host = new Host(Program.LocalHostName);
var repository = ShareRepositoryFactory.CreateFromHost(host.Name);
Local = (host, repository);
AllRepositories = [repository];
Scanner = scanner ?? new ShareScanner(
workerCount: options.Shares.Cache.Workers,
fileService: fileService);
Scanner.StateMonitor.OnChange(cacheState =>
{
var (previous, current) = cacheState;
State.SetValue(state => state with
{
// scan is pending if faulted, or if state DIDN'T just transition from filling to not filling AND a scan was already pending
ScanPending = current.Faulted || current.Cancelled || (!(previous.Filling && !current.Filling) && state.ScanPending),
Scanning = current.Filling,
Faulted = current.Faulted,
Cancelled = current.Cancelled,
ScanProgress = current.FillProgress,
Directories = current.Directories,
Files = current.Files,
});
});
OptionsMonitor = optionsMonitor;
OptionsMonitor.OnChange(options => Configure(options));
StateMonitor = State;
Configure(OptionsMonitor.CurrentValue);
}
///
/// Gets the list of share hosts.
///
public IReadOnlyList Hosts => HostDictionary.Values.Select(v => v.Host).Prepend(Local.Host).ToList().AsReadOnly();
///
/// Gets the local share host.
///
public Host LocalHost => Local.Host;
///
/// Gets the state monitor for the service.
///
public IStateMonitor StateMonitor { get; }
private IShareRepositoryFactory ShareRepositoryFactory { get; }
private IShareScanner Scanner { get; }
private SemaphoreSlim ScannerSyncRoot { get; } = new SemaphoreSlim(1, 1);
private string LastOptionsHash { get; set; }
private IOptionsMonitor OptionsMonitor { get; }
private ConcurrentDictionary HostDictionary { get; set; } = new();
private IManagedState State { get; } = new ManagedState();
private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1);
private StorageMode CacheStorageMode { get; }
private ILogger Log { get; } = Serilog.Log.ForContext();
private (Host Host, IShareRepository Repository) Local { get; set; }
private IReadOnlyList AllRepositories { get; set; }
///
/// Adds a new, or updates an existing, share host.
///
/// The host to add or update.
public void AddOrUpdateHost(Host host)
{
// we assume that if this method is called that the caller has verified that the
// file for the host exists and is valid.
HostDictionary.AddOrUpdate(
key: host.Name,
addValueFactory: (key) => (host, ShareRepositoryFactory.CreateFromHost(host.Name)),
updateValueFactory: (_, existing) => (host, existing.Repository));
AllRepositories = HostDictionary.Values
.Select(value => value.Repository)
.Prepend(Local.Repository)
.ToList()
.AsReadOnly();
State.SetValue(state => state with
{
Hosts = Hosts.Select(host => host.Name).ToArray(),
Directories = Hosts.SelectMany(host => host.Shares).Sum(share => share.Directories ?? 0),
Files = Hosts.SelectMany(host => host.Shares).Sum(share => share.Files ?? 0),
});
}
///
/// Returns the entire contents of the share.
///
/// The entire contents of the share.
public Task> BrowseAsync(Share share = null)
{
var directories = new ConcurrentDictionary();
string prefix = null;
if (share != null)
{
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 repositories.SelectMany(r => r.ListDirectories(prefix)))
{
directories.TryAdd(directory, new Directory(directory));
}
var files = repositories.SelectMany(r => r.ListFiles(
parentDirectory: prefix,
includeFullPath: true)); // includes the full *masked* filename; directory and file name and extension. not the local filename.
var groups = files
.GroupBy(file => file.Filename.GetNormalizedDirectoryName())
.Select(group => new Directory(group.Key, group.Select(f =>
{
return new File(
f.Code,
f.Filename.GetNormalizedFileName(), // we can send the full path, or just the filename. save bandwidth and omit the path.
f.Size,
f.Extension,
f.Attributes);
}).OrderBy(f => f.Filename)));
// merge the dictionary containing all directories with the Soulseek.Directory instances containing their files.
// entries with no files will remain untouched.
foreach (var group in groups)
{
directories.AddOrUpdate(group.Name, group, (_, _) => group);
}
var results = directories.Values.AsEnumerable();
return Task.FromResult(results);
}
///
/// Dumps the local share cache to a file.
///
/// The destination file.
/// The operation context.
public Task DumpAsync(string filename)
{
Local.Repository.DumpTo(filename);
return Task.CompletedTask;
}
///
/// Returns the share host with the specified .
///
/// The name of the host.
/// The host, if found.
/// A value indicating whether the host was found.
public bool TryGetHost(string name, out Host host)
{
if (HostDictionary.TryGetValue(name, out var record))
{
host = record.Host;
return true;
}
host = null;
return false;
}
///
/// Removes the share host with the specified .
///
/// The name of the host.
/// A value indicating whether the host was removed.
public bool TryRemoveHost(string name)
{
var removed = false;
if (HostDictionary.TryRemove(name, out _))
{
removed = true;
}
AllRepositories = HostDictionary.Values
.Select(value => value.Repository)
.Prepend(Local.Repository)
.ToList()
.AsReadOnly();
State.SetValue(state => state with
{
Hosts = Hosts.Select(host => host.Name).ToArray(),
Directories = Hosts.SelectMany(host => host.Shares).Sum(share => share.Directories ?? 0),
Files = Hosts.SelectMany(host => host.Shares).Sum(share => share.Files ?? 0),
});
return removed;
}
///
/// Returns the contents of the specified .
///
/// The directory for which the contents are to be listed.
/// The contents of the directory.
public Task ListDirectoryAsync(string directory)
{
var files = AllRepositories.SelectMany(r => r.ListFiles(directory, includeFullPath: false));
return Task.FromResult(new Directory(directory, files));
}
///
/// Resolves the local filename of the specified , if the mask is associated with a
/// configured share.
///
/// The fully qualified filename to resolve.
/// The resolved local filename.
///
/// Thrown when the specified remote filename can not be associated with a configured share.
///
public Task<(string Host, string Filename, long Size)> ResolveFileAsync(string remoteFilename)
{
var (resolvedFilename, size) = Local.Repository.FindFileInfo(remoteFilename);
// when we're debugging the relay (running as both controller and agent on the same instance)
// always resolve files from remote hosts, ignoring local. this is a crappy hack, but it is the
// least crappy way to maintain sanity while trying to debug this feature.
if (OptionsMonitor.CurrentValue.Relay.Mode.ToEnum() == RelayMode.Debug)
{
Log.Warning("Ignoring resolved local file to facilitate Relay debugging.");
}
else if (!string.IsNullOrEmpty(resolvedFilename))
{
Log.Debug("Resolved remote file to {ResolvedFilename} on local host", resolvedFilename);
return Task.FromResult((Program.LocalHostName, resolvedFilename, size));
}
else
{
Log.Debug("Failed to resolve remote file on local host; searching remote hosts: {Hosts}", HostDictionary.Values.Select(h => h.Host.Name));
}
// file not found locally. begin searching other hosts one by one.
// this is the slow, dumb way to do this, but it's plenty fast in this context.
foreach (var host in HostDictionary.Values)
{
(resolvedFilename, size) = host.Repository.FindFileInfo(remoteFilename);
if (!string.IsNullOrEmpty(resolvedFilename))
{
Log.Debug("Resolved remote file to {ResolvedFilename} on host {Host}", remoteFilename, host.Host.Name);
return Task.FromResult((host.Host.Name, resolvedFilename, size));
}
Log.Debug("Failed to resolve remote file on host {Host}", host.Host.Name);
}
throw new NotFoundException($"The requested filename '{remoteFilename}' could not be resolved to a physical file");
}
///
/// Returns the list of all started at or after the specified
/// unix timestamp.
///
///
/// Note that this returns the scan history for local shares only.
///
/// A unix timestamp that serves as the lower bound of the time-based listing.
/// The operation context, including the list of found scans.
public Task> ListScansAsync(long startedAtOrAfter = 0)
=> Task.FromResult(Local.Repository.ListScans(startedAtOrAfter));
///
/// Requests that a share scan is performed.
///
public void RequestScan()
{
Local.Repository.FlagLatestScanAsSuspect();
State.SetValue(state => state with { ScanPending = true });
}
///
/// Searches the cache for the specified and returns the matching files.
///
/// The query for which to search.
/// An optional row limit.
/// The matching files.
public Task> SearchAsync(SearchQuery query, int? limit = null)
{
var results = AllRepositories.SelectMany(r => r.Search(query, limit));
return Task.FromResult(results);
}
///
/// Scans the configured shares.
///
/// The operation context.
/// Thrown when a scan is already in progress.
public async Task ScanAsync()
{
// try to obtain the semaphore, or fail if it has already been obtained
// the scanner has an identical check, but because this method changes state we need it here, too
if (!await ScannerSyncRoot.WaitAsync(millisecondsTimeout: 0))
{
Log.Warning("Shared file scan rejected; scan is already in progress.");
throw new ShareScanInProgressException("Shared files are already being scanned.");
}
try
{
State.SetValue(state => state with { Ready = false });
await Scanner.ScanAsync(Local.Host.Shares, OptionsMonitor.CurrentValue.Shares, Local.Repository);
Log.Debug("Backing up shared file cache database...");
using (var backupRepository = ShareRepositoryFactory.CreateFromHostBackup(Local.Host.Name))
{
Local.Repository.BackupTo(backupRepository);
}
Log.Debug("Shared file cache database backup complete");
Log.Debug("Recomputing share statistics...");
ComputeShareStatistics();
Log.Debug("Share statistics updated: {Shares}", Local.Host.Shares.ToJson());
State.SetValue(state => state with
{
Directories = Hosts.SelectMany(host => host.Shares).Sum(share => share.Directories ?? 0),
Files = Hosts.SelectMany(host => host.Shares).Sum(share => share.Files ?? 0),
Ready = true,
});
}
finally
{
ScannerSyncRoot.Release();
}
}
///
/// Cancels the currently running scan, if one is running.
///
/// A value indicating whether a scan was cancelled.
public bool TryCancelScan()
{
return Scanner.TryCancelScan();
}
///
/// Initializes the service and shares.
///
/// A value indicating whether a full re-scan of shares should be performed.
/// The operation context.
public async Task InitializeAsync(bool forceRescan = false)
{
Log.Information("Initializing shares");
// _probably_ redundant, but to be safe
State.SetValue(state => state with { Ready = false });
try
{
if (forceRescan)
{
Log.Warning("Performing a forced re-scan of shares");
await ScanAsync();
}
else if (CacheStorageMode == StorageMode.Memory)
{
Log.Information("Share cache StorageMode is 'Memory'. Attempting to load from backup...");
using var backupRepository = ShareRepositoryFactory.CreateFromHostBackup(Local.Host.Name);
if (backupRepository.TryValidate(out _))
{
Log.Information("Share cache backup validated. Attempting to restore...");
Local.Repository.RestoreFrom(backupRepository);
Log.Information("Share cache successfully restored from backup");
}
else
{
Log.Warning("Share cache backup is missing, corrupt, or is out of date");
throw new ShareInitializationException("Share cache backup is missing, corrupt, or is out of date");
}
}
else if (CacheStorageMode == StorageMode.Disk)
{
Log.Information("Share cache StorageMode is 'Disk'. Attempting to validate...");
if (Local.Repository.TryValidate(out _))
{
// no-op
}
else
{
Log.Warning("Share cache is missing, corrupt, or is out of date. Attempting to load from backup...");
using var backupRepository = ShareRepositoryFactory.CreateFromHostBackup(Local.Host.Name);
if (backupRepository.TryValidate(out _))
{
Log.Information("Share cache backup validated. Attempting to restore...");
Local.Repository.RestoreFrom(backupRepository);
Log.Information("Share cache successfully restored from backup");
}
else
{
Log.Warning("Share cache and backup are both missing, corrupt, or is out of date");
throw new ShareInitializationException("Share cache and backup are both missing, corrupt, or is out of date");
}
}
}
var options = OptionsMonitor.CurrentValue.Shares;
var latestScan = Local.Repository.FindLatestScan();
Log.Debug("Latest scan: {Scan}, current options {@Options}", latestScan, options);
if (latestScan == default)
{
throw new ShareInitializationException("Shares not yet scanned");
}
if (!latestScan.EndedAt.HasValue)
{
throw new ShareInitializationException("Previous share scan did not complete");
}
if (latestScan.Suspect)
{
throw new ShareInitializationException("Previous share scan was marked as suspect");
}
if (latestScan.OptionsJson != options.ToJson())
{
throw new ShareInitializationException("Share options changed since previous scan");
}
Local.Repository.EnableKeepalive(true);
Log.Debug("Recomputing share statistics...");
ComputeShareStatistics();
Log.Debug("Share statistics updated");
// one of several things happened above before we got here:
// this method was called with forceRescan = true
// the storage mode is memory, and we loaded the in-memory db from a valid backup
// the storage mode is disk, and the file is there and valid
// the storage mode is disk but either missing or invalid, and we restored from a valid backup
// we failed to load the cache from disk or backup, and performed a full scan successfully
// at this point there is a valid (existing, schema matching expected schema) database at the primary connection string
State.SetValue(state => state with
{
Scanning = false,
Faulted = false,
Ready = true,
ScanProgress = 1,
Hosts = Hosts.Select(host => host.Name).ToList().AsReadOnly(),
Directories = Hosts.SelectMany(host => host.Shares).Sum(share => share.Directories ?? 0),
Files = Hosts.SelectMany(host => host.Shares).Sum(share => share.Files ?? 0),
});
}
catch (Exception ex)
{
Log.Warning(ex, "Error initializing shares: {Message}", ex.Message);
if (!forceRescan)
{
Log.Warning("Re-attempting initialization");
await InitializeAsync(forceRescan: true);
}
else
{
Log.Error("Failed to initialize shares, and an attempt to force a full scan to repair failed");
throw;
}
}
}
private void ComputeShareStatistics()
{
foreach (var share in Local.Host.Shares)
{
var prefix = share.RemotePath + (share.RemotePath.EndsWith('\\') ? string.Empty : '\\');
var dirs = Local.Repository.CountDirectories(prefix);
var files = Local.Repository.CountFiles(prefix);
share.UpdateStatistics(dirs, files);
}
}
private void Configure(Options options)
{
SyncRoot.Wait();
try
{
var optionsHash = Compute.Sha1Hash(string.Join(';', options.Shares.Directories));
if (optionsHash == LastOptionsHash)
{
return;
}
var shares = options.Shares.Directories
.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();
Local = (Local.Host with { Shares = shares }, Local.Repository);
LastOptionsHash = optionsHash;
}
finally
{
SyncRoot.Release();
}
}
}
}