// Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System.Runtime.CompilerServices; using Duplicati.Library.Interface; namespace Duplicati.Library.SourceProvider; /// /// Helper class that wraps multiple source providers into a single source provider /// public class Combiner(IEnumerable providers) : ISourceProvider { /// public string MountedPath => string.Empty; /// /// The providers to combine /// private readonly List providers = providers.SelectMany(x => x is Combiner c ? c.providers.AsEnumerable() : [x]).ToList(); /// public Task Initialize(CancellationToken cancellationToken) => Task.WhenAll(providers.Select(x => x.Initialize(cancellationToken))); /// public async IAsyncEnumerable Enumerate([EnumeratorCancellation] CancellationToken cancellationToken) { foreach (var provider in providers) { await foreach (var entry in provider.Enumerate(cancellationToken).ConfigureAwait(false)) yield return entry; } } /// /// Provides access to the combined providers /// public IEnumerable Providers => providers; /// /// Gets a filesystem entry for a given path /// /// The path to get /// True if the path is a folder /// The cancellation token /// The filesystem entry public async Task GetEntry(string path, bool isFolder, CancellationToken cancellationToken) { foreach (var provider in providers) if (!string.IsNullOrWhiteSpace(MountedPath) && provider.MountedPath.StartsWith(path)) { var subpath = provider.MountedPath.Substring(path.Length); return await provider.GetEntry(subpath, isFolder, cancellationToken).ConfigureAwait(false); } foreach (var provider in providers.Where(x => string.IsNullOrWhiteSpace(x.MountedPath))) { var res = await provider.GetEntry(path, isFolder, cancellationToken).ConfigureAwait(false); if (res != null) return res; } return null; } /// /// Creates a combined source provider from a list of providers /// /// The providers to combine /// The combined provider public static ISourceProvider Combine(IEnumerable providers) { var providerTotal = providers.SelectMany(x => x is Combiner c ? c.providers.AsEnumerable() : [x]).ToList(); return providerTotal.Count == 1 ? providerTotal[0] : new Combiner(providers); } /// public void Dispose() { foreach (var provider in providers) provider.Dispose(); } }