aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs
diff options
context:
space:
mode:
authorBond-009 <bond.009@outlook.com>2026-02-14 12:07:30 +0100
committerGitHub <noreply@github.com>2026-02-14 12:07:30 +0100
commit29582ed461b693368ec56567c2e40cfa20ef4bf5 (patch)
tree04721b833e8e6108c2e13c4f0ea9f4dc7b2ae946 /Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs
parentca6d499680f9fbb369844a11eb0e0213b66bb00b (diff)
parent3b6985986709473c69ba785460c702c6bbe3771d (diff)
Merge branch 'master' into issue15137
Diffstat (limited to 'Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs')
-rw-r--r--Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs135
1 files changed, 87 insertions, 48 deletions
diff --git a/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs b/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs
index bafe3ad43..ef5d24c70 100644
--- a/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs
+++ b/Emby.Server.Implementations/Library/DotIgnoreIgnoreRule.cs
@@ -1,6 +1,8 @@
using System;
using System.IO;
+using System.Text.RegularExpressions;
using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.IO;
@@ -11,28 +13,24 @@ namespace Emby.Server.Implementations.Library;
/// </summary>
public class DotIgnoreIgnoreRule : IResolverIgnoreRule
{
+ private static readonly bool IsWindows = OperatingSystem.IsWindows();
+
private static FileInfo? FindIgnoreFile(DirectoryInfo directory)
{
- var ignoreFile = new FileInfo(Path.Join(directory.FullName, ".ignore"));
- if (ignoreFile.Exists)
- {
- return ignoreFile;
- }
-
- var parentDir = directory.Parent;
- if (parentDir is null)
+ for (var current = directory; current is not null; current = current.Parent)
{
- return null;
+ var ignorePath = Path.Join(current.FullName, ".ignore");
+ if (File.Exists(ignorePath))
+ {
+ return new FileInfo(ignorePath);
+ }
}
- return FindIgnoreFile(parentDir);
+ return null;
}
/// <inheritdoc />
- public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent)
- {
- return IsIgnored(fileInfo, parent);
- }
+ public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) => IsIgnored(fileInfo, parent);
/// <summary>
/// Checks whether or not the file is ignored.
@@ -42,60 +40,101 @@ public class DotIgnoreIgnoreRule : IResolverIgnoreRule
/// <returns>True if the file should be ignored.</returns>
public static bool IsIgnored(FileSystemMetadata fileInfo, BaseItem? parent)
{
- if (fileInfo.IsDirectory)
- {
- var dirIgnoreFile = FindIgnoreFile(new DirectoryInfo(fileInfo.FullName));
- if (dirIgnoreFile is null)
- {
- return false;
- }
+ var searchDirectory = fileInfo.IsDirectory
+ ? new DirectoryInfo(fileInfo.FullName)
+ : new DirectoryInfo(Path.GetDirectoryName(fileInfo.FullName) ?? string.Empty);
- // Fast path in case the ignore files isn't a symlink and is empty
- if ((dirIgnoreFile.Attributes & FileAttributes.ReparsePoint) == 0
- && dirIgnoreFile.Length == 0)
- {
- return true;
- }
-
- // ignore the directory only if the .ignore file is empty
- // evaluate individual files otherwise
- return string.IsNullOrWhiteSpace(GetFileContent(dirIgnoreFile));
- }
-
- var parentDirPath = Path.GetDirectoryName(fileInfo.FullName);
- if (string.IsNullOrEmpty(parentDirPath))
+ if (string.IsNullOrEmpty(searchDirectory.FullName))
{
return false;
}
- var folder = new DirectoryInfo(parentDirPath);
- var ignoreFile = FindIgnoreFile(folder);
+ var ignoreFile = FindIgnoreFile(searchDirectory);
if (ignoreFile is null)
{
return false;
}
- string ignoreFileString = GetFileContent(ignoreFile);
-
- if (string.IsNullOrWhiteSpace(ignoreFileString))
+ // Fast path in case the ignore files isn't a symlink and is empty
+ if (ignoreFile.LinkTarget is null && ignoreFile.Length == 0)
{
// Ignore directory if we just have the file
return true;
}
- // If file has content, base ignoring off the content .gitignore-style rules
- var ignoreRules = ignoreFileString.Split('\n', StringSplitOptions.RemoveEmptyEntries);
- var ignore = new Ignore.Ignore();
- ignore.Add(ignoreRules);
+ var content = GetFileContent(ignoreFile);
+ return string.IsNullOrWhiteSpace(content)
+ || CheckIgnoreRules(fileInfo.FullName, content, fileInfo.IsDirectory);
+ }
- return ignore.IsIgnored(fileInfo.FullName);
+ private static bool CheckIgnoreRules(string path, string ignoreFileContent, bool isDirectory)
+ {
+ // If file has content, base ignoring off the content .gitignore-style rules
+ var rules = ignoreFileContent.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
+ return CheckIgnoreRules(path, rules, isDirectory);
}
- private static string GetFileContent(FileInfo dirIgnoreFile)
+ /// <summary>
+ /// Checks whether a path should be ignored based on an array of ignore rules.
+ /// </summary>
+ /// <param name="path">The path to check.</param>
+ /// <param name="rules">The array of ignore rules.</param>
+ /// <param name="isDirectory">Whether the path is a directory.</param>
+ /// <returns>True if the path should be ignored.</returns>
+ internal static bool CheckIgnoreRules(string path, string[] rules, bool isDirectory)
+ => CheckIgnoreRules(path, rules, isDirectory, IsWindows);
+
+ /// <summary>
+ /// Checks whether a path should be ignored based on an array of ignore rules.
+ /// </summary>
+ /// <param name="path">The path to check.</param>
+ /// <param name="rules">The array of ignore rules.</param>
+ /// <param name="isDirectory">Whether the path is a directory.</param>
+ /// <param name="normalizePath">Whether to normalize backslashes to forward slashes (for Windows paths).</param>
+ /// <returns>True if the path should be ignored.</returns>
+ internal static bool CheckIgnoreRules(string path, string[] rules, bool isDirectory, bool normalizePath)
{
- using (var reader = dirIgnoreFile.OpenText())
+ var ignore = new Ignore.Ignore();
+
+ // Add each rule individually to catch and skip invalid patterns
+ var validRulesAdded = 0;
+ foreach (var rule in rules)
+ {
+ try
+ {
+ ignore.Add(rule);
+ validRulesAdded++;
+ }
+ catch (RegexParseException)
+ {
+ // Ignore invalid patterns
+ }
+ }
+
+ // If no valid rules were added, fall back to ignoring everything (like an empty .ignore file)
+ if (validRulesAdded == 0)
{
- return reader.ReadToEnd();
+ return true;
}
+
+ // Mitigate the problem of the Ignore library not handling Windows paths correctly.
+ // See https://github.com/jellyfin/jellyfin/issues/15484
+ var pathToCheck = normalizePath ? path.NormalizePath('/') : path;
+
+ // Add trailing slash for directories to match "folder/"
+ if (isDirectory)
+ {
+ pathToCheck = string.Concat(pathToCheck.AsSpan().TrimEnd('/'), "/");
+ }
+
+ return ignore.IsIgnored(pathToCheck);
+ }
+
+ private static string GetFileContent(FileInfo ignoreFile)
+ {
+ ignoreFile = FileSystemHelper.ResolveLinkTarget(ignoreFile, returnFinalTarget: true) ?? ignoreFile;
+ return ignoreFile.Exists
+ ? File.ReadAllText(ignoreFile.FullName)
+ : string.Empty;
}
}