DotIgnoreIgnoreRule.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Attributes & FileAttributes.ReparsePoint) == 0
  48. && 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. using (var reader = dirIgnoreFile.OpenText())
  82. {
  83. return reader.ReadToEnd();
  84. }
  85. }
  86. }