From d46109aa695ef7ba461c5db774e2a0577ab656b9 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Fri, 2 Oct 2026 22:48:08 +0200 Subject: Confine ignore file handling to library root --- .../Library/LibraryManager.cs | 46 ++++++++++++++-------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'Emby.Server.Implementations/Library/LibraryManager.cs') diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 16616dd328..6d8e9abc45 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -184,6 +184,7 @@ namespace Emby.Server.Implementations.Library _peopleRepository = peopleRepository; _pathManager = pathManager; _dotIgnoreIgnoreRule = dotIgnoreIgnoreRule; + _dotIgnoreIgnoreRule.SetLibraryRootsProvider(GetLibraryRootPaths); _localization = localization; _directoryService = directoryService; _extraResolver = new ExtraResolver(loggerFactory.CreateLogger(), namingOptions, directoryService); @@ -1627,23 +1628,7 @@ namespace Emby.Server.Implementations.Library { Name = Path.GetFileName(dir), - Locations = _fileSystem.GetFilePaths(dir, false) - .Where(i => Path.GetExtension(i.AsSpan()).Equals(ShortcutFileExtension, StringComparison.OrdinalIgnoreCase)) - .Select(i => - { - try - { - return _appHost.ExpandVirtualPath(_fileSystem.ResolveShortcut(i)); - } - catch (Exception ex) - { - _logger.LogError(ex, "Error resolving shortcut file {File}", i); - return null; - } - }) - .Where(i => i is not null) - .Order() - .ToArray(), + Locations = GetVirtualFolderLocations(dir).Order().ToArray(), CollectionType = GetCollectionType(dir) }; @@ -1671,6 +1656,33 @@ namespace Emby.Server.Implementations.Library return info; } + private IEnumerable GetVirtualFolderLocations(string dir) + { + return _fileSystem.GetFilePaths(dir, false) + .Where(i => Path.GetExtension(i.AsSpan()).Equals(ShortcutFileExtension, StringComparison.OrdinalIgnoreCase)) + .Select(i => + { + try + { + return _appHost.ExpandVirtualPath(_fileSystem.ResolveShortcut(i)); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error resolving shortcut file {File}", i); + return null; + } + }) + .OfType(); + } + + // Reads the .mblink files directly; going through RootFolder.Children would re-enter the resolver. + private IEnumerable GetLibraryRootPaths() + { + return _fileSystem.GetDirectoryPaths(_configurationManager.ApplicationPaths.DefaultUserViewsPath) + .SelectMany(GetVirtualFolderLocations) + .ToList(); + } + private CollectionTypeOptions? GetCollectionType(string path) { var files = _fileSystem.GetFilePaths(path, [".collection"], true, false); -- cgit v1.2.3 From 6561d58ad512bdc4dfd6dacf55a5b03b2a143623 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Sat, 24 Oct 2026 11:33:46 +0200 Subject: Fix review comments --- .../Library/LibraryManager.cs | 9 ++- .../LibraryManager/LibraryRootIgnoreTests.cs | 93 ++++++++++++++++++++++ .../DotIgnoreLibraryRootTests.cs | 55 +++++++++++++ 3 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs create mode 100644 tests/Jellyfin.Server.Integration.Tests/DotIgnoreLibraryRootTests.cs (limited to 'Emby.Server.Implementations/Library/LibraryManager.cs') diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 6d8e9abc45..c877c8bf2c 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -1678,7 +1678,14 @@ namespace Emby.Server.Implementations.Library // Reads the .mblink files directly; going through RootFolder.Children would re-enter the resolver. private IEnumerable GetLibraryRootPaths() { - return _fileSystem.GetDirectoryPaths(_configurationManager.ApplicationPaths.DefaultUserViewsPath) + // root/default only exists once the user root folder or a library has been created + var userViewsPath = _configurationManager.ApplicationPaths.DefaultUserViewsPath; + if (!_fileSystem.DirectoryExists(userViewsPath)) + { + return []; + } + + return _fileSystem.GetDirectoryPaths(userViewsPath) .SelectMany(GetVirtualFolderLocations) .ToList(); } diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs new file mode 100644 index 0000000000..f37aadbe8d --- /dev/null +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/LibraryManager/LibraryRootIgnoreTests.cs @@ -0,0 +1,93 @@ +using System; +using System.IO; +using AutoFixture; +using AutoFixture.AutoMoq; +using Emby.Naming.Common; +using Emby.Server.Implementations.IO; +using Emby.Server.Implementations.Library; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Sorting; +using MediaBrowser.Model.IO; +using Microsoft.Extensions.Logging.Abstractions; +using Moq; +using Xunit; + +namespace Jellyfin.Server.Implementations.Tests.Library.LibraryManager; + +public sealed class LibraryRootIgnoreTests : IDisposable +{ + private readonly string _tempDir; + private readonly string _userViewsPath; + private readonly string _libraryRoot; + private readonly FileSystemMetadata _movieFile; + + public LibraryRootIgnoreTests() + { + _tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + _userViewsPath = Path.Combine(_tempDir, "root", "default"); + _libraryRoot = Path.Combine(_tempDir, "media", "Movies"); + var movieDir = Path.Combine(_libraryRoot, "Movie (2020)"); + Directory.CreateDirectory(movieDir); + + // Ignores everything below it, but sits above the library root + File.WriteAllText(Path.Combine(_tempDir, ".ignore"), string.Empty); + + _movieFile = new FileSystemMetadata + { + FullName = Path.Combine(movieDir, "Movie (2020).mkv"), + IsDirectory = false + }; + } + + public void Dispose() + { + Directory.Delete(_tempDir, true); + } + + [Fact] + public void IgnoreFile_IgnoreFileAboveMblinkLibraryRoot_IsNotConsulted() + { + var libraryDir = Path.Combine(_userViewsPath, "Movies"); + Directory.CreateDirectory(libraryDir); + File.WriteAllText(Path.Combine(libraryDir, "Movies.mblink"), _libraryRoot); + + Assert.False(CreateLibraryManager().IgnoreFile(_movieFile, null)); + } + + [Fact] + public void IgnoreFile_UserViewsPathMissing_WalksToFilesystemRoot() + { + // Before root/default exists there are no library roots, so the .ignore above still applies + Assert.False(Directory.Exists(_userViewsPath)); + Assert.True(CreateLibraryManager().IgnoreFile(_movieFile, null)); + } + + private Emby.Server.Implementations.Library.LibraryManager CreateLibraryManager() + { + var fixture = new Fixture().Customize(new AutoMoqCustomization()); + fixture.Register(() => new NamingOptions()); + var configMock = fixture.Freeze>(); + configMock.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns(_tempDir); + configMock.Setup(c => c.ApplicationPaths.DefaultUserViewsPath).Returns(_userViewsPath); + fixture.Freeze>() + .Setup(h => h.ExpandVirtualPath(It.IsAny())) + .Returns(path => path); + fixture.Inject(new ManagedFileSystem( + NullLogger.Instance, + configMock.Object.ApplicationPaths, + [new MbLinkShortcutHandler()])); + + // The same instance LibraryManager hands the roots to must be the one evaluating the rules + var dotIgnoreRule = new DotIgnoreIgnoreRule(); + fixture.Inject(dotIgnoreRule); + + return fixture.Build().Do(s => s.AddParts( + [dotIgnoreRule], + [], + [], + Array.Empty(), + [])) + .Create(); + } +} diff --git a/tests/Jellyfin.Server.Integration.Tests/DotIgnoreLibraryRootTests.cs b/tests/Jellyfin.Server.Integration.Tests/DotIgnoreLibraryRootTests.cs new file mode 100644 index 0000000000..2fd598e6b2 --- /dev/null +++ b/tests/Jellyfin.Server.Integration.Tests/DotIgnoreLibraryRootTests.cs @@ -0,0 +1,55 @@ +using System; +using System.IO; +using Emby.Server.Implementations.Library; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.IO; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Jellyfin.Server.Integration.Tests; + +public sealed class DotIgnoreLibraryRootTests : IClassFixture +{ + private readonly JellyfinApplicationFactory _factory; + + public DotIgnoreLibraryRootTests(JellyfinApplicationFactory factory) + { + _factory = factory; + } + + [Fact] + public void IgnoreFile_IgnoreFileAboveLibraryRoot_IsNotConsulted() + { + _ = _factory.CreateClient(); + + var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + var libraryRoot = Path.Combine(tempDir, "Movies"); + var movieDir = Path.Combine(libraryRoot, "Movie (2020)"); + Directory.CreateDirectory(movieDir); + var libraryDir = Path.Combine(_factory.Services.GetRequiredService().DefaultUserViewsPath, "dotignore-root-test"); + Directory.CreateDirectory(libraryDir); + + try + { + File.WriteAllText(Path.Combine(tempDir, ".ignore"), string.Empty); + File.WriteAllText(Path.Combine(libraryDir, "Movies.mblink"), libraryRoot); + + // The rule the resolver evaluates must be the singleton LibraryManager hands the roots to + _factory.Services.GetRequiredService().ClearDirectoryCache(); + var fileInfo = new FileSystemMetadata + { + FullName = Path.Combine(movieDir, "Movie (2020).mkv"), + IsDirectory = false + }; + + Assert.False(_factory.Services.GetRequiredService().IgnoreFile(fileInfo, new Folder { Path = movieDir })); + } + finally + { + Directory.Delete(libraryDir, true); + Directory.Delete(tempDir, true); + } + } +} -- cgit v1.2.3