Files
GameLauncher/Services/NtfsMftScanService.cs
T
armonica 4c94cf814f 機能追加: NTFS MFT (USN Journal) 直接読み取りによる高速ファイル走査サービスを追加
低レベルボリュームハンドルから FSCTL_ENUM_USN_DATA を呼び出し、ドライブ全体の MFT/USN レコードを直接メモリへ読み出して対象フォルダ配下のインデックスを秒速生成する NtfsMftScanService を実装しました。
2026-10-20 11:30:07 +09:00

335 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;
namespace GameLauncher.Services
{
/// <summary>
/// NTFS の MFT (USN Journal: FSCTL_ENUM_USN_DATA) を直接読み取り、
/// ドライブ内のファイルおよびディレクトリツリーを爆速で一括抽出するサービス
/// </summary>
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)
/// <summary>
/// 指定されたパス群(例: C:\..., Z:\...)に対して、NTFS MFT 直接読み取りを用いて高速スナップショットを生成します。
/// </summary>
/// <param name="targetDirectories">走査対象のディレクトリルート一覧</param>
/// <returns>生成されたスナップショット。失敗または非NTFS時は null</returns>
public static FastDirectorySnapshot? TryScanTargetsFast(IEnumerable<string> targetDirectories)
{
var snapshot = new FastDirectorySnapshot();
var targetsByDrive = new Dictionary<char, List<string>>();
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<string>();
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)
/// <summary>
/// 1つのドライブボリュームのMFTを読み込み、指定されたパス配下のファイルをスナップショットに登録します。
/// </summary>
private static bool ScanVolumeMft(char driveLetter, List<string> 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<ulong, MftNode>(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<ulong, List<ulong>>(nodes.Count);
foreach (var node in nodes.Values)
{
if (!childrenByParent.TryGetValue(node.ParentId, out var list))
{
list = new List<ulong>();
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;
}
/// <summary>
/// パス文字列から対応するノードIDを検索します。
/// </summary>
private static ulong? FindNodeIdByPath(char driveLetter, string path, Dictionary<ulong, MftNode> nodes, Dictionary<ulong, List<ulong>> 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;
}
/// <summary>
/// ノードIDから配下を再帰的に巡回し、スナップショットに登録します。
/// </summary>
private static void PopulateSnapshotFromNode(
ulong parentNodeId,
string currentPath,
Dictionary<ulong, MftNode> nodes,
Dictionary<ulong, List<ulong>> 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
}
}