| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Text.RegularExpressions; |
| | | 5 | | using BitFaster.Caching.Lru; |
| | | 6 | | using MediaBrowser.Controller.Entities; |
| | | 7 | | using MediaBrowser.Controller.IO; |
| | | 8 | | using MediaBrowser.Controller.Resolvers; |
| | | 9 | | using MediaBrowser.Model.IO; |
| | | 10 | | |
| | | 11 | | namespace Emby.Server.Implementations.Library; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Resolver rule class for ignoring files via .ignore. |
| | | 15 | | /// </summary> |
| | | 16 | | public class DotIgnoreIgnoreRule : IResolverIgnoreRule |
| | | 17 | | { |
| | 1 | 18 | | private static readonly bool IsWindows = OperatingSystem.IsWindows(); |
| | | 19 | | |
| | | 20 | | private readonly FastConcurrentLru<string, IgnoreFileCacheEntry> _directoryCache; |
| | | 21 | | private readonly FastConcurrentLru<string, ParsedIgnoreCacheEntry> _rulesCache; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="DotIgnoreIgnoreRule"/> class. |
| | | 25 | | /// </summary> |
| | | 26 | | public DotIgnoreIgnoreRule() |
| | | 27 | | { |
| | 59 | 28 | | var cacheSize = Math.Max(100, Environment.ProcessorCount * 100); |
| | 59 | 29 | | _directoryCache = new FastConcurrentLru<string, IgnoreFileCacheEntry>( |
| | 59 | 30 | | Environment.ProcessorCount, |
| | 59 | 31 | | cacheSize, |
| | 59 | 32 | | StringComparer.Ordinal); |
| | 59 | 33 | | _rulesCache = new FastConcurrentLru<string, ParsedIgnoreCacheEntry>( |
| | 59 | 34 | | Environment.ProcessorCount, |
| | 59 | 35 | | Math.Max(32, cacheSize / 4), |
| | 59 | 36 | | StringComparer.Ordinal); |
| | 59 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 335 | 40 | | public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) => IsIgnoredInternal(fileInfo, parent); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Clears the directory lookup cache. The parsed rules cache is not cleared |
| | | 44 | | /// as it validates file modification time on each access. |
| | | 45 | | /// </summary> |
| | | 46 | | public void ClearDirectoryCache() |
| | | 47 | | { |
| | 81 | 48 | | _directoryCache.Clear(); |
| | 81 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Checks whether or not the file is ignored. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="fileInfo">The file information.</param> |
| | | 55 | | /// <param name="parent">The parent BaseItem.</param> |
| | | 56 | | /// <returns>True if the file should be ignored.</returns> |
| | | 57 | | public bool IsIgnoredInternal(FileSystemMetadata fileInfo, BaseItem? parent) |
| | | 58 | | { |
| | 335 | 59 | | var searchDirectory = fileInfo.IsDirectory |
| | 335 | 60 | | ? fileInfo.FullName |
| | 335 | 61 | | : Path.GetDirectoryName(fileInfo.FullName); |
| | | 62 | | |
| | 335 | 63 | | if (string.IsNullOrEmpty(searchDirectory)) |
| | | 64 | | { |
| | 0 | 65 | | return false; |
| | | 66 | | } |
| | | 67 | | |
| | 335 | 68 | | var ignoreFile = FindIgnoreFileCached(searchDirectory); |
| | 335 | 69 | | if (ignoreFile is null) |
| | | 70 | | { |
| | 118 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | |
| | 217 | 74 | | var parsedEntry = GetParsedRules(ignoreFile); |
| | 217 | 75 | | if (parsedEntry is null) |
| | | 76 | | { |
| | | 77 | | // File was deleted after we cached the path - clear the directory cache entry and return false |
| | 1 | 78 | | _directoryCache.TryRemove(searchDirectory, out _); |
| | 1 | 79 | | return false; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // Empty file means ignore everything |
| | 216 | 83 | | if (parsedEntry.IsEmpty) |
| | | 84 | | { |
| | 2 | 85 | | return true; |
| | | 86 | | } |
| | | 87 | | |
| | 214 | 88 | | return parsedEntry.Rules.IsIgnored(GetPathToCheck(fileInfo.FullName, fileInfo.IsDirectory)); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Checks whether a path should be ignored based on an array of ignore rules. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="path">The path to check.</param> |
| | | 95 | | /// <param name="rules">The array of ignore rules.</param> |
| | | 96 | | /// <param name="isDirectory">Whether the path is a directory.</param> |
| | | 97 | | /// <returns>True if the path should be ignored.</returns> |
| | | 98 | | internal static bool CheckIgnoreRules(string path, string[] rules, bool isDirectory) |
| | 15 | 99 | | => CheckIgnoreRules(path, rules, isDirectory, IsWindows); |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Checks whether a path should be ignored based on an array of ignore rules. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="path">The path to check.</param> |
| | | 105 | | /// <param name="rules">The array of ignore rules.</param> |
| | | 106 | | /// <param name="isDirectory">Whether the path is a directory.</param> |
| | | 107 | | /// <param name="normalizePath">Whether to normalize backslashes to forward slashes (for Windows paths).</param> |
| | | 108 | | /// <returns>True if the path should be ignored.</returns> |
| | | 109 | | internal static bool CheckIgnoreRules(string path, string[] rules, bool isDirectory, bool normalizePath) |
| | | 110 | | { |
| | 25 | 111 | | var ignore = new Ignore.Ignore(); |
| | | 112 | | |
| | | 113 | | // Add each rule individually to catch and skip invalid patterns |
| | 25 | 114 | | var validRulesAdded = 0; |
| | 176 | 115 | | foreach (var rule in rules) |
| | | 116 | | { |
| | | 117 | | try |
| | | 118 | | { |
| | 63 | 119 | | ignore.Add(rule); |
| | 45 | 120 | | validRulesAdded++; |
| | 45 | 121 | | } |
| | 18 | 122 | | catch (RegexParseException) |
| | | 123 | | { |
| | | 124 | | // Ignore invalid patterns |
| | 18 | 125 | | } |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | // If no valid rules were added, fall back to ignoring everything (like an empty .ignore file) |
| | 25 | 129 | | if (validRulesAdded == 0) |
| | | 130 | | { |
| | 2 | 131 | | return true; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | // Mitigate the problem of the Ignore library not handling Windows paths correctly. |
| | | 135 | | // See https://github.com/jellyfin/jellyfin/issues/15484 |
| | 23 | 136 | | var pathToCheck = normalizePath ? path.NormalizePath('/') : path; |
| | | 137 | | |
| | | 138 | | // Add trailing slash for directories to match "folder/" |
| | 23 | 139 | | if (isDirectory) |
| | | 140 | | { |
| | 0 | 141 | | pathToCheck = string.Concat(pathToCheck.AsSpan().TrimEnd('/'), "/"); |
| | | 142 | | } |
| | | 143 | | |
| | 23 | 144 | | return ignore.IsIgnored(pathToCheck); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | private FileInfo? FindIgnoreFileCached(string directory) |
| | | 148 | | { |
| | | 149 | | // Check if we have a cached result for this directory |
| | 335 | 150 | | if (_directoryCache.TryGet(directory, out var cached)) |
| | | 151 | | { |
| | 284 | 152 | | return cached.IgnoreFileDirectory is null |
| | 284 | 153 | | ? null |
| | 284 | 154 | | : new FileInfo(Path.Join(cached.IgnoreFileDirectory, ".ignore")); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | DirectoryInfo startDir; |
| | | 158 | | try |
| | | 159 | | { |
| | 51 | 160 | | startDir = new DirectoryInfo(directory); |
| | 51 | 161 | | } |
| | 0 | 162 | | catch (ArgumentException) |
| | | 163 | | { |
| | 0 | 164 | | return null; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | // Walk up the directory tree to find .ignore file using DirectoryInfo.Parent |
| | 51 | 168 | | var checkedDirs = new List<string> { directory }; |
| | | 169 | | |
| | 522 | 170 | | for (var current = startDir; current is not null; current = current.Parent) |
| | | 171 | | { |
| | 232 | 172 | | var currentPath = current.FullName; |
| | | 173 | | |
| | | 174 | | // Check if this intermediate directory is cached |
| | 232 | 175 | | if (current != startDir && _directoryCache.TryGet(currentPath, out var parentCached)) |
| | | 176 | | { |
| | | 177 | | // Cache the result for all directories we checked |
| | 11 | 178 | | var entry = new IgnoreFileCacheEntry(parentCached.IgnoreFileDirectory); |
| | 44 | 179 | | foreach (var dir in checkedDirs) |
| | | 180 | | { |
| | 11 | 181 | | _directoryCache.AddOrUpdate(dir, entry); |
| | | 182 | | } |
| | | 183 | | |
| | 11 | 184 | | return parentCached.IgnoreFileDirectory is null |
| | 11 | 185 | | ? null |
| | 11 | 186 | | : new FileInfo(Path.Join(parentCached.IgnoreFileDirectory, ".ignore")); |
| | | 187 | | } |
| | | 188 | | |
| | 221 | 189 | | var ignoreFile = new FileInfo(Path.Join(currentPath, ".ignore")); |
| | 221 | 190 | | if (ignoreFile.Exists) |
| | | 191 | | { |
| | | 192 | | // Cache for all directories we checked |
| | 11 | 193 | | var entry = new IgnoreFileCacheEntry(currentPath); |
| | 46 | 194 | | foreach (var dir in checkedDirs) |
| | | 195 | | { |
| | 12 | 196 | | _directoryCache.AddOrUpdate(dir, entry); |
| | | 197 | | } |
| | | 198 | | |
| | 11 | 199 | | return ignoreFile; |
| | | 200 | | } |
| | | 201 | | |
| | 210 | 202 | | if (current != startDir) |
| | | 203 | | { |
| | 167 | 204 | | checkedDirs.Add(currentPath); |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | // No .ignore file found - cache null result for all directories |
| | 29 | 209 | | var nullEntry = new IgnoreFileCacheEntry((string?)null); |
| | 448 | 210 | | foreach (var dir in checkedDirs) |
| | | 211 | | { |
| | 195 | 212 | | _directoryCache.AddOrUpdate(dir, nullEntry); |
| | | 213 | | } |
| | | 214 | | |
| | 29 | 215 | | return null; |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | private ParsedIgnoreCacheEntry? GetParsedRules(FileInfo ignoreFile) |
| | | 219 | | { |
| | 217 | 220 | | if (!ignoreFile.Exists) |
| | | 221 | | { |
| | 1 | 222 | | _rulesCache.TryRemove(ignoreFile.FullName, out _); |
| | 1 | 223 | | return null; |
| | | 224 | | } |
| | | 225 | | |
| | 216 | 226 | | var lastModified = ignoreFile.LastWriteTimeUtc; |
| | 216 | 227 | | var fileLength = ignoreFile.Length; |
| | 216 | 228 | | var key = ignoreFile.FullName; |
| | | 229 | | |
| | | 230 | | // Check cache |
| | 216 | 231 | | if (_rulesCache.TryGet(key, out var cached)) |
| | | 232 | | { |
| | 205 | 233 | | if (cached.FileLastModified == lastModified && cached.FileLength == fileLength) |
| | | 234 | | { |
| | 204 | 235 | | return cached; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | // Stale - need to reparse |
| | 1 | 239 | | _rulesCache.TryRemove(key, out _); |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | // Parse the file |
| | 12 | 243 | | var parsedEntry = ParseIgnoreFile(ignoreFile, lastModified, fileLength); |
| | 12 | 244 | | _rulesCache.AddOrUpdate(key, parsedEntry); |
| | 12 | 245 | | return parsedEntry; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | private static ParsedIgnoreCacheEntry ParseIgnoreFile(FileInfo ignoreFile, DateTime lastModified, long fileLength) |
| | | 249 | | { |
| | 12 | 250 | | if (ignoreFile.LinkTarget is null && fileLength == 0) |
| | | 251 | | { |
| | 1 | 252 | | return new ParsedIgnoreCacheEntry |
| | 1 | 253 | | { |
| | 1 | 254 | | Rules = new Ignore.Ignore(), |
| | 1 | 255 | | FileLastModified = lastModified, |
| | 1 | 256 | | FileLength = fileLength, |
| | 1 | 257 | | IsEmpty = true |
| | 1 | 258 | | }; |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | // Resolve symlinks |
| | 11 | 262 | | var resolvedFile = FileSystemHelper.ResolveLinkTarget(ignoreFile, returnFinalTarget: true) ?? ignoreFile; |
| | 11 | 263 | | if (!resolvedFile.Exists) |
| | | 264 | | { |
| | 0 | 265 | | return new ParsedIgnoreCacheEntry |
| | 0 | 266 | | { |
| | 0 | 267 | | Rules = new Ignore.Ignore(), |
| | 0 | 268 | | FileLastModified = lastModified, |
| | 0 | 269 | | FileLength = fileLength, |
| | 0 | 270 | | IsEmpty = true |
| | 0 | 271 | | }; |
| | | 272 | | } |
| | | 273 | | |
| | 11 | 274 | | var content = File.ReadAllText(resolvedFile.FullName); |
| | 11 | 275 | | if (string.IsNullOrWhiteSpace(content)) |
| | | 276 | | { |
| | 1 | 277 | | return new ParsedIgnoreCacheEntry |
| | 1 | 278 | | { |
| | 1 | 279 | | Rules = new Ignore.Ignore(), |
| | 1 | 280 | | FileLastModified = lastModified, |
| | 1 | 281 | | FileLength = fileLength, |
| | 1 | 282 | | IsEmpty = true |
| | 1 | 283 | | }; |
| | | 284 | | } |
| | | 285 | | |
| | 10 | 286 | | var rules = content.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| | 10 | 287 | | var ignore = new Ignore.Ignore(); |
| | 10 | 288 | | var validRulesAdded = 0; |
| | | 289 | | |
| | 40 | 290 | | foreach (var rule in rules) |
| | | 291 | | { |
| | | 292 | | try |
| | | 293 | | { |
| | 10 | 294 | | ignore.Add(rule); |
| | 10 | 295 | | validRulesAdded++; |
| | 10 | 296 | | } |
| | 0 | 297 | | catch (RegexParseException) |
| | | 298 | | { |
| | | 299 | | // Ignore invalid patterns |
| | 0 | 300 | | } |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | // No valid rules means treat as empty (ignore all) |
| | 10 | 304 | | return new ParsedIgnoreCacheEntry |
| | 10 | 305 | | { |
| | 10 | 306 | | Rules = ignore, |
| | 10 | 307 | | FileLastModified = lastModified, |
| | 10 | 308 | | FileLength = fileLength, |
| | 10 | 309 | | IsEmpty = validRulesAdded == 0 |
| | 10 | 310 | | }; |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | private static string GetPathToCheck(string path, bool isDirectory) |
| | | 314 | | { |
| | | 315 | | // Normalize Windows paths |
| | 214 | 316 | | var pathToCheck = IsWindows ? path.NormalizePath('/') : path; |
| | | 317 | | |
| | | 318 | | // Add trailing slash for directories to match "folder/" |
| | 214 | 319 | | if (isDirectory) |
| | | 320 | | { |
| | 1 | 321 | | pathToCheck = string.Concat(pathToCheck.AsSpan().TrimEnd('/'), "/"); |
| | | 322 | | } |
| | | 323 | | |
| | 214 | 324 | | return pathToCheck; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | private readonly record struct IgnoreFileCacheEntry(string? IgnoreFileDirectory); |
| | | 328 | | |
| | | 329 | | private sealed class ParsedIgnoreCacheEntry |
| | | 330 | | { |
| | | 331 | | public required Ignore.Ignore Rules { get; init; } |
| | | 332 | | |
| | | 333 | | public required DateTime FileLastModified { get; init; } |
| | | 334 | | |
| | | 335 | | public required long FileLength { get; init; } |
| | | 336 | | |
| | | 337 | | public required bool IsEmpty { get; init; } |
| | | 338 | | } |
| | | 339 | | } |