Merge pull request #18275 from Shadowghost/fix/17364-dotignore-library-root

Confine ignore file handling to library root
This commit is contained in:
Cody Robibero 2026-11-20 07:14:33 -05:00 • committed by GitHub
commit be19e7c778
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 364 additions and 20 deletions

View file

@ -728,7 +728,7 @@ namespace Emby.Server.Implementations
_pluginManager.CreatePlugins();
Resolve<ILibraryManager>().AddParts(
GetExports<IResolverIgnoreRule>(),
GetExports<IResolverIgnoreRule>(type => ServiceProvider.GetService(type) ?? CreateInstanceSafe(type)),
GetExports<IItemResolver>(),
GetExports<IIntroProvider>(),
GetExports<IBaseItemComparer>(),

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using BitFaster.Caching.Lru;
using MediaBrowser.Controller.Entities;
@ -16,10 +17,14 @@ namespace Emby.Server.Implementations.Library;
public class DotIgnoreIgnoreRule : IResolverIgnoreRule
{
private static readonly bool IsWindows = OperatingSystem.IsWindows();
private static readonly StringComparison PathComparison = IsWindows ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
private readonly FastConcurrentLru<string, IgnoreFileCacheEntry> _directoryCache;
private readonly FastConcurrentLru<string, ParsedIgnoreCacheEntry> _rulesCache;
private Func<IEnumerable<string>>? _libraryRootsProvider;
private volatile string[]? _libraryRoots;
/// <summary>
/// Initializes a new instance of the <see cref="DotIgnoreIgnoreRule"/> class.
/// </summary>
@ -40,11 +45,23 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule
public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) => IsIgnoredInternal(fileInfo, parent);
/// <summary>
/// Clears the directory lookup cache. The parsed rules cache is not cleared
/// Sets the provider of library root paths. The search for a .ignore file stops at the
/// outermost library root containing the path instead of walking up to the filesystem root.
/// </summary>
/// <param name="libraryRootsProvider">The library root paths provider.</param>
public void SetLibraryRootsProvider(Func<IEnumerable<string>> libraryRootsProvider)
{
_libraryRootsProvider = libraryRootsProvider;
ClearDirectoryCache();
}
/// <summary>
/// Clears the directory lookup cache and the library roots. The parsed rules cache is not cleared
/// as it validates file modification time on each access.
/// </summary>
public void ClearDirectoryCache()
{
_libraryRoots = null;
_directoryCache.Clear();
}
@ -164,7 +181,9 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule
return null;
}
// Walk up the directory tree to find .ignore file using DirectoryInfo.Parent
// Walk up the directory tree to find .ignore file using DirectoryInfo.Parent,
// never leaving the library: probing above it can hit autofs or network mounts (#17364)
var stopDirectory = GetOutermostLibraryRoot(startDir.FullName);
var checkedDirs = new List<string> { directory };
for (var current = startDir; current is not null; current = current.Parent)
@ -203,6 +222,11 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule
{
checkedDirs.Add(currentPath);
}
if (stopDirectory is not null && IsSamePath(currentPath, stopDirectory))
{
break;
}
}
// No .ignore file found - cache null result for all directories
@ -215,6 +239,58 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule
return null;
}
private string? GetOutermostLibraryRoot(string path)
{
string? outermost = null;
foreach (var root in GetLibraryRoots())
{
if ((outermost is null || root.Length < outermost.Length)
&& IsSameOrChildPath(path, root))
{
outermost = root;
}
}
return outermost;
}
private string[] GetLibraryRoots()
{
var roots = _libraryRoots;
if (roots is not null)
{
return roots;
}
var provider = _libraryRootsProvider;
if (provider is null)
{
return [];
}
roots = provider()
.Where(r => !string.IsNullOrEmpty(r))
.Select(r => Path.TrimEndingDirectorySeparator(Path.GetFullPath(r)))
.ToArray();
_libraryRoots = roots;
return roots;
}
private static bool IsSameOrChildPath(string path, string root)
{
if (IsSamePath(path, root))
{
return true;
}
// A filesystem root such as "/" keeps its trailing separator after trimming
return path.StartsWith(root, PathComparison)
&& (Path.EndsInDirectorySeparator(root) || path[root.Length] == Path.DirectorySeparatorChar);
}
private static bool IsSamePath(string path1, string path2)
=> Path.TrimEndingDirectorySeparator(path1.AsSpan()).Equals(Path.TrimEndingDirectorySeparator(path2.AsSpan()), PathComparison);
private ParsedIgnoreCacheEntry? GetParsedRules(FileInfo ignoreFile)
{
if (!ignoreFile.Exists)

View file

@ -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<ExtraResolver>(), 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,40 @@ namespace Emby.Server.Implementations.Library
return info;
}
private IEnumerable<string> 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<string>();
}
// Reads the .mblink files directly; going through RootFolder.Children would re-enter the resolver.
private IEnumerable<string> GetLibraryRootPaths()
{
// 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();
}
private CollectionTypeOptions? GetCollectionType(string path)
{
var files = _fileSystem.GetFilePaths(path, [".collection"], true, false);

View file

@ -470,4 +470,105 @@ public class DotIgnoreIgnoreRuleTest
Directory.Delete(tempDir, true);
}
}
[Fact]
public void LibraryRoot_IgnoreFileAboveRoot_IsNotConsulted()
{
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);
try
{
File.WriteAllText(Path.Combine(tempDir, ".ignore"), string.Empty);
var rule = new DotIgnoreIgnoreRule();
var fileInfo = new FileSystemMetadata
{
FullName = Path.Combine(movieDir, "Movie (2020).mkv"),
IsDirectory = false
};
// Without library roots the walk reaches the filesystem root
Assert.True(rule.ShouldIgnore(fileInfo, null));
rule.SetLibraryRootsProvider(() => [libraryRoot + Path.DirectorySeparatorChar]);
Assert.False(rule.ShouldIgnore(fileInfo, null));
// A .ignore in the library root itself still applies
File.WriteAllText(Path.Combine(libraryRoot, ".ignore"), "*.mkv");
rule.ClearDirectoryCache();
Assert.True(rule.ShouldIgnore(fileInfo, null));
}
finally
{
Directory.Delete(tempDir, true);
}
}
[Fact]
public void LibraryRoot_NestedRoots_StopAtOutermost()
{
var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var outerRoot = Path.Combine(tempDir, "Media");
var innerRoot = Path.Combine(outerRoot, "Movies");
Directory.CreateDirectory(innerRoot);
try
{
File.WriteAllText(Path.Combine(outerRoot, ".ignore"), "*.mkv");
var rule = new DotIgnoreIgnoreRule();
rule.SetLibraryRootsProvider(() => [innerRoot, outerRoot]);
var fileInfo = new FileSystemMetadata
{
FullName = Path.Combine(innerRoot, "Movie.mkv"),
IsDirectory = false
};
Assert.True(rule.ShouldIgnore(fileInfo, null));
}
finally
{
Directory.Delete(tempDir, true);
}
}
[Fact]
public void LibraryRoot_PathOutsideLibraries_WalksToFilesystemRoot()
{
var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var libraryRoot = Path.Combine(tempDir, "Movies");
var otherDir = Path.Combine(tempDir, "Other", "Sub");
Directory.CreateDirectory(libraryRoot);
Directory.CreateDirectory(otherDir);
try
{
File.WriteAllText(Path.Combine(tempDir, ".ignore"), string.Empty);
var rule = new DotIgnoreIgnoreRule();
rule.SetLibraryRootsProvider(() => [libraryRoot]);
// A sibling whose name only shares the root's prefix is not inside the library
var fileInfo = new FileSystemMetadata
{
FullName = Path.Combine(tempDir, "MoviesExtra", "Movie.mkv"),
IsDirectory = false
};
Assert.True(rule.ShouldIgnore(fileInfo, null));
fileInfo = new FileSystemMetadata
{
FullName = Path.Combine(otherDir, "Movie.mkv"),
IsDirectory = false
};
Assert.True(rule.ShouldIgnore(fileInfo, null));
}
finally
{
Directory.Delete(tempDir, true);
}
}
}

View file

@ -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<Mock<IServerConfigurationManager>>();
configMock.Setup(c => c.ApplicationPaths.ProgramDataPath).Returns(_tempDir);
configMock.Setup(c => c.ApplicationPaths.DefaultUserViewsPath).Returns(_userViewsPath);
fixture.Freeze<Mock<IServerApplicationHost>>()
.Setup(h => h.ExpandVirtualPath(It.IsAny<string>()))
.Returns<string>(path => path);
fixture.Inject<IFileSystem>(new ManagedFileSystem(
NullLogger<ManagedFileSystem>.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<Emby.Server.Implementations.Library.LibraryManager>().Do(s => s.AddParts(
[dotIgnoreRule],
[],
[],
Array.Empty<IBaseItemComparer>(),
[]))
.Create();
}
}

View file

@ -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<JellyfinApplicationFactory>
{
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<IServerApplicationPaths>().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<DotIgnoreIgnoreRule>().ClearDirectoryCache();
var fileInfo = new FileSystemMetadata
{
FullName = Path.Combine(movieDir, "Movie (2020).mkv"),
IsDirectory = false
};
Assert.False(_factory.Services.GetRequiredService<ILibraryManager>().IgnoreFile(fileInfo, new Folder { Path = movieDir }));
}
finally
{
Directory.Delete(libraryDir, true);
Directory.Delete(tempDir, true);
}
}
}