FileQueryEngine.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using Y.FileQueryEngine.UsnOperation;
  6. namespace Y.FileQueryEngine.QueryEngine
  7. {
  8. public class FileQueryEngine
  9. {
  10. /// <summary>
  11. /// When its values is 1407374883553285(0x5000000000005L), it means this file/folder is under drive root
  12. /// </summary>
  13. protected const UInt64 ROOT_FILE_REFERENCE_NUMBER = 0x5000000000005L;
  14. protected static readonly string excludeFolders = string.Join("|",
  15. new string[]
  16. {
  17. "$RECYCLE.BIN",
  18. "System Volume Information",
  19. "$AttrDef",
  20. "$BadClus",
  21. "$BitMap",
  22. "$Boot",
  23. "$LogFile",
  24. "$Mft",
  25. "$MftMirr",
  26. "$Secure",
  27. "$TxfLog",
  28. "$UpCase",
  29. "$Volume",
  30. "$Extend"
  31. }).ToUpper();
  32. /// <summary>
  33. /// 获取所有NTFS文件系统的固定磁盘
  34. /// </summary>
  35. /// <returns></returns>
  36. public static IEnumerable<DriveInfo> GetFixedNtfsDrives()
  37. {
  38. return DriveInfo.GetDrives()
  39. .Where(d => d.DriveType == DriveType.Fixed && d.DriveFormat.ToUpper() == "NTFS");
  40. }
  41. /// <summary>
  42. /// 获取所有NTFS文件系统的磁盘
  43. /// </summary>
  44. /// <returns></returns>
  45. public static IEnumerable<DriveInfo> GetReadyNtfsDrives()
  46. {
  47. return DriveInfo.GetDrives()
  48. .Where(d => d.IsReady && d.DriveFormat.ToUpper() == "NTFS");
  49. }
  50. public static List<FileAndDirectoryEntry> GetAllFiles(DriveInfo drive)
  51. {
  52. List<FileAndDirectoryEntry> result = new List<FileAndDirectoryEntry>();
  53. var usnOperator = new UsnOperator(drive);
  54. var usnEntries = usnOperator.GetEntries().Where(e => !excludeFolders.Contains(e.FileName.ToUpper()));
  55. var folders = usnEntries.Where(e => e.IsFolder).ToArray();
  56. List<FrnFilePath> paths = GetFolderPath(folders, drive);
  57. result.AddRange(usnEntries.Join(
  58. paths,
  59. usn => usn.ParentFileReferenceNumber,
  60. path => path.FileReferenceNumber,
  61. (usn, path) => new FileAndDirectoryEntry(usn, path.Path)));
  62. return result;
  63. }
  64. private static List<FrnFilePath> GetFolderPath(UsnEntry[] folders, DriveInfo drive)
  65. {
  66. Dictionary<UInt64, FrnFilePath> pathDic = new Dictionary<ulong, FrnFilePath>();
  67. pathDic.Add(ROOT_FILE_REFERENCE_NUMBER,
  68. new FrnFilePath(ROOT_FILE_REFERENCE_NUMBER, null, string.Empty, drive.Name.TrimEnd('\\')));
  69. foreach (var folder in folders)
  70. {
  71. pathDic.Add(folder.FileReferenceNumber,
  72. new FrnFilePath(folder.FileReferenceNumber, folder.ParentFileReferenceNumber, folder.FileName));
  73. }
  74. Stack<UInt64> treeWalkStack = new Stack<ulong>();
  75. foreach (var key in pathDic.Keys)
  76. {
  77. treeWalkStack.Clear();
  78. FrnFilePath currentValue = pathDic[key];
  79. if (string.IsNullOrWhiteSpace(currentValue.Path)
  80. && currentValue.ParentFileReferenceNumber.HasValue
  81. && pathDic.ContainsKey(currentValue.ParentFileReferenceNumber.Value))
  82. {
  83. FrnFilePath parentValue = pathDic[currentValue.ParentFileReferenceNumber.Value];
  84. while (string.IsNullOrWhiteSpace(parentValue.Path)
  85. && parentValue.ParentFileReferenceNumber.HasValue
  86. && pathDic.ContainsKey(parentValue.ParentFileReferenceNumber.Value))
  87. {
  88. currentValue = parentValue;
  89. if (currentValue.ParentFileReferenceNumber.HasValue
  90. && pathDic.ContainsKey(currentValue.ParentFileReferenceNumber.Value))
  91. {
  92. treeWalkStack.Push(key);
  93. parentValue = pathDic[currentValue.ParentFileReferenceNumber.Value];
  94. }
  95. else
  96. {
  97. parentValue = null;
  98. break;
  99. }
  100. }
  101. if (parentValue != null)
  102. {
  103. currentValue.Path = BuildPath(currentValue, parentValue);
  104. while (treeWalkStack.Count() > 0)
  105. {
  106. UInt64 walkedKey = treeWalkStack.Pop();
  107. FrnFilePath walkedNode = pathDic[walkedKey];
  108. FrnFilePath parentNode = pathDic[walkedNode.ParentFileReferenceNumber.Value];
  109. walkedNode.Path = BuildPath(walkedNode, parentNode);
  110. }
  111. }
  112. }
  113. }
  114. var result = pathDic.Values.Where(p => !string.IsNullOrWhiteSpace(p.Path) && p.Path.StartsWith(drive.Name)).ToList();
  115. return result;
  116. }
  117. private static string BuildPath(FrnFilePath currentNode, FrnFilePath parentNode)
  118. {
  119. return string.Concat(new string[] { parentNode.Path, "\\", currentNode.FileName });
  120. }
  121. }
  122. }