| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Text.RegularExpressions; |
| | | 4 | | using MediaBrowser.Controller.Entities; |
| | | 5 | | using MediaBrowser.Controller.IO; |
| | | 6 | | using MediaBrowser.Controller.Resolvers; |
| | | 7 | | using MediaBrowser.Model.IO; |
| | | 8 | | |
| | | 9 | | namespace Emby.Server.Implementations.Library; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Resolver rule class for ignoring files via .ignore. |
| | | 13 | | /// </summary> |
| | | 14 | | public class DotIgnoreIgnoreRule : IResolverIgnoreRule |
| | | 15 | | { |
| | 1 | 16 | | private static readonly bool IsWindows = OperatingSystem.IsWindows(); |
| | | 17 | | |
| | | 18 | | private static FileInfo? FindIgnoreFile(DirectoryInfo directory) |
| | | 19 | | { |
| | 1822 | 20 | | for (var current = directory; current is not null; current = current.Parent) |
| | | 21 | | { |
| | 794 | 22 | | var ignorePath = Path.Join(current.FullName, ".ignore"); |
| | 794 | 23 | | if (File.Exists(ignorePath)) |
| | | 24 | | { |
| | 0 | 25 | | return new FileInfo(ignorePath); |
| | | 26 | | } |
| | | 27 | | } |
| | | 28 | | |
| | 117 | 29 | | return null; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 83 | 33 | | public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent) => IsIgnored(fileInfo, parent); |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Checks whether or not the file is ignored. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="fileInfo">The file information.</param> |
| | | 39 | | /// <param name="parent">The parent BaseItem.</param> |
| | | 40 | | /// <returns>True if the file should be ignored.</returns> |
| | | 41 | | public static bool IsIgnored(FileSystemMetadata fileInfo, BaseItem? parent) |
| | | 42 | | { |
| | 117 | 43 | | var searchDirectory = fileInfo.IsDirectory |
| | 117 | 44 | | ? new DirectoryInfo(fileInfo.FullName) |
| | 117 | 45 | | : new DirectoryInfo(Path.GetDirectoryName(fileInfo.FullName) ?? string.Empty); |
| | | 46 | | |
| | 117 | 47 | | if (string.IsNullOrEmpty(searchDirectory.FullName)) |
| | | 48 | | { |
| | 0 | 49 | | return false; |
| | | 50 | | } |
| | | 51 | | |
| | 117 | 52 | | var ignoreFile = FindIgnoreFile(searchDirectory); |
| | 117 | 53 | | if (ignoreFile is null) |
| | | 54 | | { |
| | 117 | 55 | | return false; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Fast path in case the ignore files isn't a symlink and is empty |
| | 0 | 59 | | if (ignoreFile.LinkTarget is null && ignoreFile.Length == 0) |
| | | 60 | | { |
| | | 61 | | // Ignore directory if we just have the file |
| | 0 | 62 | | return true; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | var content = GetFileContent(ignoreFile); |
| | 0 | 66 | | return string.IsNullOrWhiteSpace(content) |
| | 0 | 67 | | || CheckIgnoreRules(fileInfo.FullName, content, fileInfo.IsDirectory); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private static bool CheckIgnoreRules(string path, string ignoreFileContent, bool isDirectory) |
| | | 71 | | { |
| | | 72 | | // If file has content, base ignoring off the content .gitignore-style rules |
| | 0 | 73 | | var rules = ignoreFileContent.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries |
| | 0 | 74 | | return CheckIgnoreRules(path, rules, isDirectory); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Checks whether a path should be ignored based on an array of ignore rules. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="path">The path to check.</param> |
| | | 81 | | /// <param name="rules">The array of ignore rules.</param> |
| | | 82 | | /// <param name="isDirectory">Whether the path is a directory.</param> |
| | | 83 | | /// <returns>True if the path should be ignored.</returns> |
| | | 84 | | internal static bool CheckIgnoreRules(string path, string[] rules, bool isDirectory) |
| | 15 | 85 | | => CheckIgnoreRules(path, rules, isDirectory, IsWindows); |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Checks whether a path should be ignored based on an array of ignore rules. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="path">The path to check.</param> |
| | | 91 | | /// <param name="rules">The array of ignore rules.</param> |
| | | 92 | | /// <param name="isDirectory">Whether the path is a directory.</param> |
| | | 93 | | /// <param name="normalizePath">Whether to normalize backslashes to forward slashes (for Windows paths).</param> |
| | | 94 | | /// <returns>True if the path should be ignored.</returns> |
| | | 95 | | internal static bool CheckIgnoreRules(string path, string[] rules, bool isDirectory, bool normalizePath) |
| | | 96 | | { |
| | 25 | 97 | | var ignore = new Ignore.Ignore(); |
| | | 98 | | |
| | | 99 | | // Add each rule individually to catch and skip invalid patterns |
| | 25 | 100 | | var validRulesAdded = 0; |
| | 176 | 101 | | foreach (var rule in rules) |
| | | 102 | | { |
| | | 103 | | try |
| | | 104 | | { |
| | 63 | 105 | | ignore.Add(rule); |
| | 45 | 106 | | validRulesAdded++; |
| | 45 | 107 | | } |
| | 18 | 108 | | catch (RegexParseException) |
| | | 109 | | { |
| | | 110 | | // Ignore invalid patterns |
| | 18 | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // If no valid rules were added, fall back to ignoring everything (like an empty .ignore file) |
| | 25 | 115 | | if (validRulesAdded == 0) |
| | | 116 | | { |
| | 2 | 117 | | return true; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | // Mitigate the problem of the Ignore library not handling Windows paths correctly. |
| | | 121 | | // See https://github.com/jellyfin/jellyfin/issues/15484 |
| | 23 | 122 | | var pathToCheck = normalizePath ? path.NormalizePath('/') : path; |
| | | 123 | | |
| | | 124 | | // Add trailing slash for directories to match "folder/" |
| | 23 | 125 | | if (isDirectory) |
| | | 126 | | { |
| | 0 | 127 | | pathToCheck = string.Concat(pathToCheck.AsSpan().TrimEnd('/'), "/"); |
| | | 128 | | } |
| | | 129 | | |
| | 23 | 130 | | return ignore.IsIgnored(pathToCheck); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private static string GetFileContent(FileInfo ignoreFile) |
| | | 134 | | { |
| | 0 | 135 | | ignoreFile = FileSystemHelper.ResolveLinkTarget(ignoreFile, returnFinalTarget: true) ?? ignoreFile; |
| | 0 | 136 | | return ignoreFile.Exists |
| | 0 | 137 | | ? File.ReadAllText(ignoreFile.FullName) |
| | 0 | 138 | | : string.Empty; |
| | | 139 | | } |
| | | 140 | | } |