DotIgnoreIgnoreRule.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.IO;
  6. using MediaBrowser.Controller.Resolvers;
  7. using MediaBrowser.Model.IO;
  8. namespace Emby.Server.Implementations.Library;
  9. /// <summary>
  10. /// Resolver rule class for ignoring files via .ignore.
  11. /// </summary>
  12. public class DotIgnoreIgnoreRule : IResolverIgnoreRule
  13. {
  14. private static FileInfo? FindIgnoreFile(DirectoryInfo directory)
  15. {
  16. var ignoreFile = new FileInfo(Path.Join(directory.FullName, ".ignore"));
  17. if (ignoreFile.Exists)
  18. {
  19. return ignoreFile;
  20. }
  21. var parentDir = directory.Parent;
  22. if (parentDir is null)
  23. {
  24. return null;
  25. }
  26. return FindIgnoreFile(parentDir);
  27. }
  28. /// <inheritdoc />
  29. public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem? parent)
  30. {
  31. return IsIgnored(fileInfo, parent);
  32. }
  33. /// <summary>
  34. /// Checks whether or not the file is ignored.
  35. /// </summary>
  36. /// <param name="fileInfo">The file information.</param>
  37. /// <param name="parent">The parent BaseItem.</param>
  38. /// <returns>True if the file should be ignored.</returns>
  39. public static bool IsIgnored(FileSystemMetadata fileInfo, BaseItem? parent)
  40. {
  41. if (fileInfo.IsDirectory)
  42. {
  43. var dirIgnoreFile = FindIgnoreFile(new DirectoryInfo(fileInfo.FullName));
  44. if (dirIgnoreFile is null)
  45. {
  46. return false;
  47. }
  48. // Fast path in case the ignore files isn't a symlink and is empty
  49. if (dirIgnoreFile.LinkTarget is null && dirIgnoreFile.Length == 0)
  50. {
  51. return true;
  52. }
  53. // ignore the directory only if the .ignore file is empty
  54. // evaluate individual files otherwise
  55. return string.IsNullOrWhiteSpace(GetFileContent(dirIgnoreFile));
  56. }
  57. var parentDirPath = Path.GetDirectoryName(fileInfo.FullName);
  58. if (string.IsNullOrEmpty(parentDirPath))
  59. {
  60. return false;
  61. }
  62. var folder = new DirectoryInfo(parentDirPath);
  63. var ignoreFile = FindIgnoreFile(folder);
  64. if (ignoreFile is null)
  65. {
  66. return false;
  67. }
  68. string ignoreFileString = GetFileContent(ignoreFile);
  69. if (string.IsNullOrWhiteSpace(ignoreFileString))
  70. {
  71. // Ignore directory if we just have the file
  72. return true;
  73. }
  74. // If file has content, base ignoring off the content .gitignore-style rules
  75. var ignoreRules = ignoreFileString.Split('\n', StringSplitOptions.RemoveEmptyEntries);
  76. var ignore = new Ignore.Ignore();
  77. ignore.Add(ignoreRules);
  78. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  79. {
  80. // Mitigate the problem of the Ignore library not handling Windows paths correctly.
  81. // See https://github.com/jellyfin/jellyfin/issues/15484
  82. return ignore.IsIgnored(fileInfo.FullName.NormalizePath('/'));
  83. }
  84. return ignore.IsIgnored(fileInfo.FullName);
  85. }
  86. private static string GetFileContent(FileInfo dirIgnoreFile)
  87. {
  88. dirIgnoreFile = FileSystemHelper.ResolveLinkTarget(dirIgnoreFile, returnFinalTarget: true) ?? dirIgnoreFile;
  89. if (!dirIgnoreFile.Exists)
  90. {
  91. return string.Empty;
  92. }
  93. using (var reader = dirIgnoreFile.OpenText())
  94. {
  95. return reader.ReadToEnd();
  96. }
  97. }
  98. }