diff --git a/Services/NtfsMftScanService.cs b/Services/NtfsMftScanService.cs new file mode 100644 index 0000000..02d2a33 --- /dev/null +++ b/Services/NtfsMftScanService.cs @@ -0,0 +1,334 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.InteropServices; +using System.Text; +using Microsoft.Win32.SafeHandles; + +namespace GameLauncher.Services +{ + /// + /// NTFS の MFT (USN Journal: FSCTL_ENUM_USN_DATA) を直接読み取り、 + /// ドライブ内のファイルおよびディレクトリツリーを爆速で一括抽出するサービス + /// + public class NtfsMftScanService + { + #region □定数(Private) + private const uint GENERIC_READ = 0x80000000; + private const uint FILE_SHARE_READ = 0x00000001; + private const uint FILE_SHARE_WRITE = 0x00000002; + private const uint OPEN_EXISTING = 3; + private const uint FSCTL_QUERY_USN_JOURNAL = 0x000900f4; + private const uint FSCTL_ENUM_USN_DATA = 0x000900b3; + private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010; + #endregion + + #region □構造体(Private) + [StructLayout(LayoutKind.Sequential)] + private struct USN_JOURNAL_DATA + { + public ulong UsnJournalID; + public long FirstUsn; + public long NextUsn; + public long LowestValidUsn; + public long MaxUsn; + public ulong MaximumSize; + public ulong AllocationDelta; + } + + [StructLayout(LayoutKind.Sequential)] + private struct MFT_ENUM_DATA_V0 + { + public ulong StartFileReferenceNumber; + public long LowUsn; + public long HighUsn; + } + + private class MftNode + { + public ulong Id; + public ulong ParentId; + public string Name = string.Empty; + public bool IsDirectory; + } + #endregion + + #region □メソッド(Public) + /// + /// 指定されたパス群(例: C:\..., Z:\...)に対して、NTFS MFT 直接読み取りを用いて高速スナップショットを生成します。 + /// + /// 走査対象のディレクトリルート一覧 + /// 生成されたスナップショット。失敗または非NTFS時は null + public static FastDirectorySnapshot? TryScanTargetsFast(IEnumerable targetDirectories) + { + var snapshot = new FastDirectorySnapshot(); + var targetsByDrive = new Dictionary>(); + + foreach (var dir in targetDirectories) + { + if (string.IsNullOrWhiteSpace(dir)) continue; + var full = Path.GetFullPath(dir).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + if (full.Length >= 2 && full[1] == ':') + { + char driveLetter = char.ToUpperInvariant(full[0]); + if (!targetsByDrive.TryGetValue(driveLetter, out var list)) + { + list = new List(); + targetsByDrive[driveLetter] = list; + } + list.Add(full); + } + } + + if (targetsByDrive.Count == 0) return null; + + bool anySuccess = false; + + foreach (var kvp in targetsByDrive) + { + char driveLetter = kvp.Key; + var targetPaths = kvp.Value; + + try + { + bool ok = ScanVolumeMft(driveLetter, targetPaths, snapshot); + if (ok) + { + anySuccess = true; + } + } + catch + { + // ドライブスキャン失敗時はスキップ + } + } + + return anySuccess ? snapshot : null; + } + #endregion + + #region □メソッド(Private) + /// + /// 1つのドライブボリュームのMFTを読み込み、指定されたパス配下のファイルをスナップショットに登録します。 + /// + private static bool ScanVolumeMft(char driveLetter, List targetPaths, FastDirectorySnapshot snapshot) + { + string volumePath = @"\\.\" + driveLetter + ":"; + using var hVolume = CreateFile( + volumePath, + GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + IntPtr.Zero, + OPEN_EXISTING, + 0, + IntPtr.Zero); + + if (hVolume.IsInvalid) + { + return false; + } + + // 1. USNジャーナル情報の取得 + var journalData = new USN_JOURNAL_DATA(); + int journalDataSize = Marshal.SizeOf(typeof(USN_JOURNAL_DATA)); + if (!DeviceIoControl(hVolume, FSCTL_QUERY_USN_JOURNAL, IntPtr.Zero, 0, out journalData, journalDataSize, out _, IntPtr.Zero)) + { + return false; + } + + // 2. MFT列挙用パラメータの準備 + var enumData = new MFT_ENUM_DATA_V0 + { + StartFileReferenceNumber = 0, + LowUsn = 0, + HighUsn = journalData.NextUsn + }; + + int enumDataSize = Marshal.SizeOf(typeof(MFT_ENUM_DATA_V0)); + IntPtr enumDataPtr = Marshal.AllocHGlobal(enumDataSize); + Marshal.StructureToPtr(enumData, enumDataPtr, false); + + const int bufferSize = 1024 * 512; // 512KBバッファ + byte[] buffer = new byte[bufferSize]; + GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); + IntPtr bufferPtr = bufferHandle.AddrOfPinnedObject(); + + // ID -> MftNode のマッピングを保持 + var nodes = new Dictionary(500000); + + try + { + while (DeviceIoControl(hVolume, FSCTL_ENUM_USN_DATA, enumDataPtr, enumDataSize, bufferPtr, bufferSize, out uint bytesReturned, IntPtr.Zero)) + { + if (bytesReturned <= 8) break; + + // 次の開始参照番号を取得 + ulong nextId = (ulong)Marshal.ReadInt64(bufferPtr); + Marshal.WriteInt64(enumDataPtr, (long)nextId); + + int offset = 8; + while (offset < bytesReturned) + { + IntPtr recordPtr = IntPtr.Add(bufferPtr, offset); + int recordLength = Marshal.ReadInt32(recordPtr); + if (recordLength <= 0) break; + + ulong fileRef = (ulong)Marshal.ReadInt64(recordPtr, 8) & 0x0000FFFFFFFFFFFFUL; + ulong parentRef = (ulong)Marshal.ReadInt64(recordPtr, 16) & 0x0000FFFFFFFFFFFFUL; + uint fileAttributes = (uint)Marshal.ReadInt32(recordPtr, 52); + int fileNameLength = Marshal.ReadInt16(recordPtr, 56); + int fileNameOffset = Marshal.ReadInt16(recordPtr, 58); + + if (fileNameLength > 0) + { + string fileName = Marshal.PtrToStringUni(IntPtr.Add(recordPtr, fileNameOffset), fileNameLength / 2); + bool isDir = (fileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + + var node = new MftNode + { + Id = fileRef, + ParentId = parentRef, + Name = fileName, + IsDirectory = isDir + }; + nodes[fileRef] = node; + } + + offset += recordLength; + } + } + } + finally + { + if (bufferHandle.IsAllocated) bufferHandle.Free(); + if (enumDataPtr != IntPtr.Zero) Marshal.FreeHGlobal(enumDataPtr); + } + + if (nodes.Count == 0) return false; + + // 3. 親IDから子IDのツリーを構築 + var childrenByParent = new Dictionary>(nodes.Count); + foreach (var node in nodes.Values) + { + if (!childrenByParent.TryGetValue(node.ParentId, out var list)) + { + list = new List(); + childrenByParent[node.ParentId] = list; + } + list.Add(node.Id); + } + + // 4. 各ターゲットパスのノードIDを特定し、その配下のエントリをスナップショットに格納 + foreach (var targetPath in targetPaths) + { + var targetNodeId = FindNodeIdByPath(driveLetter, targetPath, nodes, childrenByParent); + if (targetNodeId.HasValue) + { + PopulateSnapshotFromNode(targetNodeId.Value, targetPath, nodes, childrenByParent, snapshot); + } + } + + return true; + } + + /// + /// パス文字列から対応するノードIDを検索します。 + /// + private static ulong? FindNodeIdByPath(char driveLetter, string path, Dictionary nodes, Dictionary> childrenByParent) + { + string rootStr = driveLetter + @":\"; + if (path.Equals(rootStr.TrimEnd('\\'), StringComparison.OrdinalIgnoreCase) || path.Equals(rootStr, StringComparison.OrdinalIgnoreCase)) + { + // ドライブ直下のルートディレクトリ(通常IDは 5 / 0x0005) + return 5UL; + } + + var parts = path.Substring(3).Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries); + ulong currentParent = 5UL; // ルートID + + foreach (var part in parts) + { + bool found = false; + if (childrenByParent.TryGetValue(currentParent, out var childIds)) + { + foreach (var childId in childIds) + { + if (nodes.TryGetValue(childId, out var node) && node.IsDirectory && string.Equals(node.Name, part, StringComparison.OrdinalIgnoreCase)) + { + currentParent = node.Id; + found = true; + break; + } + } + } + if (!found) return null; + } + + return currentParent; + } + + /// + /// ノードIDから配下を再帰的に巡回し、スナップショットに登録します。 + /// + private static void PopulateSnapshotFromNode( + ulong parentNodeId, + string currentPath, + Dictionary nodes, + Dictionary> childrenByParent, + FastDirectorySnapshot snapshot) + { + if (!childrenByParent.TryGetValue(parentNodeId, out var childIds)) return; + + foreach (var childId in childIds) + { + if (!nodes.TryGetValue(childId, out var childNode)) continue; + + if (childNode.IsDirectory) + { + string childPath = Path.Combine(currentPath, childNode.Name); + snapshot.AddDirectory(currentPath, childPath); + PopulateSnapshotFromNode(childId, childPath, nodes, childrenByParent, snapshot); + } + else + { + snapshot.AddFile(currentPath, childNode.Name); + } + } + } + #endregion + + #region □P/Invoke(Private) + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] + private static extern SafeFileHandle CreateFile( + string lpFileName, + uint dwDesiredAccess, + uint dwShareMode, + IntPtr lpSecurityAttributes, + uint dwCreationDisposition, + uint dwFlagsAndAttributes, + IntPtr hTemplateFile); + + [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)] + private static extern bool DeviceIoControl( + SafeFileHandle hDevice, + uint dwIoControlCode, + IntPtr lpInBuffer, + int nInBufferSize, + out USN_JOURNAL_DATA lpOutBuffer, + int nOutBufferSize, + out uint lpBytesReturned, + IntPtr lpOverlapped); + + [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)] + private static extern bool DeviceIoControl( + SafeFileHandle hDevice, + uint dwIoControlCode, + IntPtr lpInBuffer, + int nInBufferSize, + IntPtr lpOutBuffer, + int nOutBufferSize, + out uint lpBytesReturned, + IntPtr lpOverlapped); + #endregion + } +}