DotIgnoreIgnoreRule.cs 3.1 KB

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