FileQueryEngine.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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<string> GetAllFiles(DriveInfo drive)
  51. {
  52. List<string> result = new List<string>();
  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. var range = usnEntries.Join(
  58. paths,
  59. usn => usn.ParentFileReferenceNumber,
  60. path => path.FileReferenceNumber,
  61. (usn, path) => string.Concat(path.Path + "\\" + usn.FileName));
  62. result.AddRange(range);
  63. return result;
  64. }
  65. public static List<FileAndDirectoryEntry> GetAllFileEntrys(DriveInfo drive)
  66. {
  67. List<FileAndDirectoryEntry> result = new List<FileAndDirectoryEntry>();
  68. var usnOperator = new UsnOperator(drive);
  69. var usnEntries = usnOperator.GetEntries().Where(e => !excludeFolders.Contains(e.FileName.ToUpper()));
  70. var folders = usnEntries.Where(e => e.IsFolder).ToArray();
  71. List<FrnFilePath> paths = GetFolderPath(folders, drive);
  72. var range = usnEntries.Join(
  73. paths,
  74. usn => usn.ParentFileReferenceNumber,
  75. path => path.FileReferenceNumber,
  76. (usn, path) => new FileAndDirectoryEntry(usn, path.Path));
  77. result.AddRange(range);
  78. return result;
  79. }
  80. private static List<FrnFilePath> GetFolderPath(UsnEntry[] folders, DriveInfo drive)
  81. {
  82. Dictionary<UInt64, FrnFilePath> pathDic = new Dictionary<ulong, FrnFilePath>();
  83. pathDic.Add(ROOT_FILE_REFERENCE_NUMBER,
  84. new FrnFilePath(ROOT_FILE_REFERENCE_NUMBER, null, string.Empty, drive.Name.TrimEnd('\\')));
  85. foreach (var folder in folders)
  86. {
  87. pathDic.Add(folder.FileReferenceNumber,
  88. new FrnFilePath(folder.FileReferenceNumber, folder.ParentFileReferenceNumber, folder.FileName));
  89. }
  90. Stack<UInt64> treeWalkStack = new Stack<ulong>();
  91. foreach (var key in pathDic.Keys)
  92. {
  93. treeWalkStack.Clear();
  94. FrnFilePath currentValue = pathDic[key];
  95. if (string.IsNullOrWhiteSpace(currentValue.Path)
  96. && currentValue.ParentFileReferenceNumber.HasValue
  97. && pathDic.ContainsKey(currentValue.ParentFileReferenceNumber.Value))
  98. {
  99. FrnFilePath parentValue = pathDic[currentValue.ParentFileReferenceNumber.Value];
  100. while (string.IsNullOrWhiteSpace(parentValue.Path)
  101. && parentValue.ParentFileReferenceNumber.HasValue
  102. && pathDic.ContainsKey(parentValue.ParentFileReferenceNumber.Value))
  103. {
  104. currentValue = parentValue;
  105. if (currentValue.ParentFileReferenceNumber.HasValue
  106. && pathDic.ContainsKey(currentValue.ParentFileReferenceNumber.Value))
  107. {
  108. treeWalkStack.Push(key);
  109. parentValue = pathDic[currentValue.ParentFileReferenceNumber.Value];
  110. }
  111. else
  112. {
  113. parentValue = null;
  114. break;
  115. }
  116. }
  117. if (parentValue != null)
  118. {
  119. currentValue.Path = BuildPath(currentValue, parentValue);
  120. while (treeWalkStack.Count() > 0)
  121. {
  122. UInt64 walkedKey = treeWalkStack.Pop();
  123. FrnFilePath walkedNode = pathDic[walkedKey];
  124. FrnFilePath parentNode = pathDic[walkedNode.ParentFileReferenceNumber.Value];
  125. walkedNode.Path = BuildPath(walkedNode, parentNode);
  126. }
  127. }
  128. }
  129. }
  130. var result = pathDic.Values.Where(p => !string.IsNullOrWhiteSpace(p.Path) && p.Path.StartsWith(drive.Name)).ToList();
  131. return result;
  132. }
  133. private static string BuildPath(FrnFilePath currentNode, FrnFilePath parentNode)
  134. {
  135. return string.Concat(new string[] { parentNode.Path, "\\", currentNode.FileName });
  136. }
  137. }
  138. }