DotIgnoreIgnoreRule.cs 3.1 KB

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