File size: 15,064 Bytes
70d9fc3
 
 
 
 
 
 
 
 
 
 
1459e6c
70d9fc3
 
 
b299dd7
 
b04a10b
70d9fc3
 
 
 
 
1459e6c
f4192b6
 
 
 
 
 
 
70d9fc3
1459e6c
6eb91a5
 
aa2084e
6eb91a5
70d9fc3
 
1459e6c
8821ae4
 
 
 
 
 
 
 
1459e6c
b299dd7
70d9fc3
b299dd7
 
70d9fc3
b299dd7
 
70e3a96
f4192b6
 
 
 
 
 
 
 
70e3a96
b04a10b
e3e4995
 
f4192b6
b04a10b
e3e4995
 
b04a10b
c0302db
b299dd7
 
 
 
 
70d9fc3
b299dd7
 
 
 
 
70e3a96
5cec55c
b299dd7
70e3a96
b299dd7
 
70e3a96
b299dd7
70e3a96
b299dd7
 
 
 
 
 
 
 
 
 
 
 
70e3a96
 
1459e6c
b299dd7
70e3a96
b299dd7
 
70e3a96
1459e6c
b299dd7
70e3a96
 
 
b299dd7
 
 
 
 
 
aa2084e
70e3a96
 
1459e6c
b299dd7
 
70d9fc3
 
b299dd7
70d9fc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b299dd7
70d9fc3
1459e6c
b299dd7
 
 
 
 
 
70d9fc3
 
 
 
 
1459e6c
 
4188672
1459e6c
6eb91a5
4188672
1459e6c
4188672
f4192b6
4188672
 
 
b299dd7
b04a10b
70d9fc3
 
4188672
 
70d9fc3
 
 
 
 
 
b299dd7
 
70d9fc3
 
 
 
b04a10b
b299dd7
 
70d9fc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b04a10b
70d9fc3
 
 
 
 
 
 
 
 
 
 
 
6eb91a5
b04a10b
4188672
70d9fc3
 
1459e6c
b299dd7
70e3a96
a464e40
c0302db
b04a10b
b299dd7
a464e40
b04a10b
 
c0302db
b04a10b
 
c0302db
b04a10b
 
 
c0302db
b04a10b
 
c0302db
b299dd7
 
c0302db
 
1459e6c
b04a10b
70d9fc3
b04a10b
 
70d9fc3
 
b04a10b
 
70d9fc3
 
 
 
b04a10b
 
70d9fc3
 
 
 
 
b04a10b
 
70d9fc3
b04a10b
1459e6c
b04a10b
 
70d9fc3
 
 
 
 
1459e6c
b04a10b
 
 
 
 
70d9fc3
 
b299dd7
70d9fc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b299dd7
70d9fc3
 
 
 
 
 
 
 
 
 
 
 
 
b299dd7
70d9fc3
 
 
 
 
 
 
b299dd7
 
 
70d9fc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6eb91a5
 
 
 
4188672
6eb91a5
 
 
 
 
 
 
70d9fc3
 
 
 
a464e40
70d9fc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using FastSeekWpf.NativeInterop;

namespace FastSeekWpf.Core;

public class MftReader : IDisposable
{
    private readonly IntPtr _handle;
    private readonly NtfsDrive _drive;
    private bool _disposed;

    private const int FALLBACK_BUF = 4 * 1024 * 1024;
    private const int DIRECT_BUF = 4 * 1024 * 1024;

    public NtfsDrive Drive => _drive;

    public MftReader(NtfsDrive drive)
    {
        _drive = drive;
        _handle = Win32Api.CreateFileW(
            drive.DevicePath,
            Win32Api.GENERIC_READ,
            Win32Api.FILE_SHARE_READ | Win32Api.FILE_SHARE_WRITE | Win32Api.FILE_SHARE_DELETE,
            IntPtr.Zero,
            Win32Api.OPEN_EXISTING,
            Win32Api.FILE_FLAG_BACKUP_SEMANTICS,
            IntPtr.Zero);

        if (_handle == new IntPtr(-1))
        {
            int err = Marshal.GetLastWin32Error();
            throw new IOException($"Failed to open volume {drive.DevicePath}: {Win32Error(err)} (code={err})");
        }
    }

    // Try direct first, fall back to FSCTL β€” matches Rust main.rs exactly
    public (ScanResult scan, string method) ScanAny()
    {
        var direct = ScanDirect();
        if (direct != null)
            return (direct, "direct");
        return (Scan(), "ioctl");
    }

    // Primary: direct $MFT file read β€” matches Rust scan_direct()
    public ScanResult? ScanDirect()
    {
        int? recordSize = ReadMftRecordSize();
        if (!recordSize.HasValue) return null;

        string mftPath = $"{_drive.Root}$MFT";
        Logger.Log($"[{Drive.Letter}] Opening $MFT: {mftPath}");

        IntPtr mftHandle = Win32Api.CreateFileW(
            mftPath,
            Win32Api.GENERIC_READ,
            Win32Api.FILE_SHARE_READ | Win32Api.FILE_SHARE_WRITE | Win32Api.FILE_SHARE_DELETE,
            IntPtr.Zero,
            Win32Api.OPEN_EXISTING,
            Win32Api.FILE_FLAG_BACKUP_SEMANTICS | Win32Api.FILE_FLAG_SEQUENTIAL_SCAN,
            IntPtr.Zero);

        if (mftHandle == new IntPtr(-1))
        {
            int err = Marshal.GetLastWin32Error();
            Logger.Log($"[{Drive.Letter}] CreateFileW({mftPath}) failed: {Win32Error(err)} (code={err})");
            return null;
        }

        try
        {
            var records = new List<CompactRecord>();
            var nameData = new List<char>();
            byte[] buffer = new byte[DIRECT_BUF];
            ulong mftIndex = 0;
            int leftover = 0;

            while (true)
            {
                uint bytesRead = 0;
                bool ok = Win32Api.ReadFile(
                    mftHandle, buffer, leftover, (uint)(buffer.Length - leftover), out bytesRead, IntPtr.Zero);

                if (!ok || bytesRead == 0)
                    break;

                int total = leftover + (int)bytesRead;
                int offset = 0;

                while (offset + recordSize.Value <= total)
                {
                    bool applied = ApplyFixup(buffer, offset, recordSize.Value);
                    if (applied)
                    {
                        ParseFileRecord(
                            buffer.AsSpan(offset, recordSize.Value),
                            mftIndex,
                            records,
                            nameData);
                    }

                    mftIndex += 1;
                    offset += recordSize.Value;
                }

                // Align down to record boundary β€” matches Rust offset = total - (total % record_size)
                offset = total - (total % recordSize.Value);

                leftover = total - offset;
                if (leftover > 0)
                {
                    // Copy tail to front β€” matches Rust std::ptr::copy
                    Buffer.BlockCopy(buffer, offset, buffer, 0, leftover);
                }
            }

            Logger.Log($"[{Drive.Letter}] Direct scan: {records.Count} records from {mftIndex} positions");
            return new ScanResult(records, nameData);
        }
        finally
        {
            Win32Api.CloseHandle(mftHandle);
        }
    }

    // Fallback: FSCTL_ENUM_USN_DATA (4 MB buffer) β€” matches Rust scan() exactly
    public ScanResult Scan()
    {
        var records = new List<CompactRecord>();
        var nameData = new List<char>();
        byte[] buffer = new byte[FALLBACK_BUF];

        var enumData = new MFT_ENUM_DATA_V0
        {
            StartFileReferenceNumber = 0,
            LowUsn = 0,
            HighUsn = long.MaxValue
        };

        int enumDataSize = Marshal.SizeOf<MFT_ENUM_DATA_V0>();
        IntPtr enumDataPtr = Marshal.AllocHGlobal(enumDataSize);
        Marshal.StructureToPtr(enumData, enumDataPtr, false);

        try
        {
            while (true)
            {
                IntPtr bufferPtr = Marshal.AllocHGlobal(buffer.Length);
                try
                {
                    uint bytesReturned = 0;
                    bool ok = Win32Api.DeviceIoControl(
                        _handle,
                        Win32Api.FSCTL_ENUM_USN_DATA,
                        enumDataPtr,
                        (uint)enumDataSize,
                        bufferPtr,
                        (uint)buffer.Length,
                        out bytesReturned,
                        IntPtr.Zero);

                    if (!ok)
                    {
                        int error = Marshal.GetLastWin32Error();
                        // CRITICAL: GetLastWin32Error returns RAW Win32 error (38), NOT HRESULT (0x80070026)
                        if (error == Win32Api.ERROR_HANDLE_EOF)
                        {
                            Logger.Log($"[{Drive.Letter}] FSCTL_ENUM_USN_DATA: EOF");
                            break;
                        }
                        if (error == Win32Api.ERROR_INSUFFICIENT_BUFFER)
                        {
                            Logger.Log($"[{Drive.Letter}] FSCTL buffer too small ({buffer.Length}), retrying");
                            buffer = new byte[buffer.Length * 2];
                            continue;
                        }
                        Logger.Log($"[{Drive.Letter}] FSCTL_ENUM_USN_DATA failed: {Win32Error(error)} (code={error})");
                        break;
                    }

                    if (bytesReturned <= 8)
                        break;

                    ulong nextRef = (ulong)Marshal.ReadInt64(bufferPtr);
                    enumData.StartFileReferenceNumber = nextRef;
                    Marshal.StructureToPtr(enumData, enumDataPtr, false);

                    int offset = 8;
                    int usnRecordSize = Marshal.SizeOf<USN_RECORD_V2>();
                    while (offset + usnRecordSize <= (int)bytesReturned)
                    {
                        IntPtr recordPtr = IntPtr.Add(bufferPtr, offset);
                        var record = Marshal.PtrToStructure<USN_RECORD_V2>(recordPtr);

                        int recLen = (int)record.RecordLength;
                        if (recLen == 0 || offset + recLen > (int)bytesReturned)
                            break;

                        int nameOffset = offset + (int)record.FileNameOffset;
                        int nameLen = record.FileNameLength / 2;

                        var nameChars = new char[nameLen];
                        for (int i = 0; i < nameLen; i++)
                            nameChars[i] = (char)Marshal.ReadInt16(bufferPtr, nameOffset + i * 2);

                        int arenaOff = nameData.Count;
                        nameData.AddRange(nameChars);

                        records.Add(new CompactRecord
                        {
                            FileRef = record.FileReferenceNumber,
                            ParentRef = record.ParentFileReferenceNumber,
                            NameOff = (uint)arenaOff,
                            NameLen = (ushort)nameLen,
                            IsDir = (record.FileAttributes & 0x10) != 0
                        });

                        offset += recLen;
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(bufferPtr);
                }
            }
        }
        finally
        {
            Marshal.FreeHGlobal(enumDataPtr);
        }

        Logger.Log($"[{Drive.Letter}] FSCTL scan: {records.Count} records");
        return new ScanResult(records, nameData);
    }

    // Read MFT record size from NTFS boot sector β€” matches Rust read_mft_record_size()
    private int? ReadMftRecordSize()
    {
        Win32Api.SetFilePointerEx(_handle, 0, out _, 0);

        byte[] boot = new byte[512];
        uint br = 0;
        bool ok = Win32Api.ReadFile(_handle, boot, 512, out br, IntPtr.Zero);
        if (!ok || br < 512)
            return null;

        if (boot[3] != 'N' || boot[4] != 'T' || boot[5] != 'F' || boot[6] != 'S')
            return null;

        ushort bytesPerSector = BinaryPrimitives.ReadUInt16LittleEndian(boot.AsSpan(0x0B, 2));
        byte sectorsPerCluster = boot[0x0D];
        int clusterSize = bytesPerSector * sectorsPerCluster;

        sbyte raw = (sbyte)boot[0x40];
        int recordSize = raw > 0 ? raw * clusterSize : 1 << (-raw);

        Logger.Log($"[{Drive.Letter}] Boot: bps={bytesPerSector}, spc={sectorsPerCluster}, clusterSize={clusterSize}, recordSize={recordSize}");
        return recordSize;
    }

    // Apply NTFS multi-sector fixup β€” matches Rust apply_fixup() exactly
    private static bool ApplyFixup(byte[] record, int offset, int recordSize)
    {
        if (recordSize < 48 || record[offset] != 'F' || record[offset + 1] != 'I'
            || record[offset + 2] != 'L' || record[offset + 3] != 'E')
            return false;

        ushort fixupOff = BinaryPrimitives.ReadUInt16LittleEndian(record.AsSpan(offset + 4, 2));
        ushort fixupCnt = BinaryPrimitives.ReadUInt16LittleEndian(record.AsSpan(offset + 6, 2));

        if (fixupCnt < 2 || fixupOff + fixupCnt * 2 > recordSize)
            return false;

        byte check0 = record[offset + fixupOff];
        byte check1 = record[offset + fixupOff + 1];

        for (int i = 1; i < fixupCnt; i++)
        {
            int end = i * 512 - 2;
            if (end + 1 >= recordSize) break;

            if (record[offset + end] != check0 || record[offset + end + 1] != check1)
                return false;

            // RESTORE real bytes from fixup array β€” matches Rust exactly
            record[offset + end] = record[offset + fixupOff + i * 2];
            record[offset + end + 1] = record[offset + fixupOff + i * 2 + 1];
        }

        return true;
    }

    // Parse one MFT FILE record β€” matches Rust parse_file_record() exactly
    private static void ParseFileRecord(
        ReadOnlySpan<byte> record,
        ulong mftIndex,
        List<CompactRecord> records,
        List<char> nameData)
    {
        ushort flags = BinaryPrimitives.ReadUInt16LittleEndian(record.Slice(0x16, 2));
        if ((flags & 0x01) == 0) return; // Record not in use

        bool isDir = (flags & 0x02) != 0;
        ushort seq = BinaryPrimitives.ReadUInt16LittleEndian(record.Slice(0x10, 2));
        ulong fileRef = mftIndex | ((ulong)seq << 48);

        ushort firstAttr = BinaryPrimitives.ReadUInt16LittleEndian(record.Slice(0x14, 2));
        int aoff = firstAttr;

        byte bestNs = 255;
        (int pos, int len, ulong parent)? bestName = null;

        while (aoff + 8 <= record.Length)
        {
            uint atype = BinaryPrimitives.ReadUInt32LittleEndian(record.Slice(aoff, 4));
            if (atype == 0xFFFFFFFF) break;

            uint alen = BinaryPrimitives.ReadUInt32LittleEndian(record.Slice(aoff + 4, 4));
            if (alen == 0 || aoff + alen > record.Length) break;

            if (atype == 0x30 && record[aoff + 8] == 0) // $FILE_NAME, resident
            {
                uint vlen = BinaryPrimitives.ReadUInt32LittleEndian(record.Slice(aoff + 16, 4));
                ushort voff = BinaryPrimitives.ReadUInt16LittleEndian(record.Slice(aoff + 20, 2));
                int vs = aoff + voff;

                if (vs + 66 <= record.Length && vlen >= 66)
                {
                    ulong parent = BinaryPrimitives.ReadUInt64LittleEndian(record.Slice(vs, 8));
                    int nlen = record[vs + 64];
                    byte ns = record[vs + 65];

                    if (vs + 66 + nlen * 2 <= record.Length)
                    {
                        if (ns == 2) // DOS 8.3 name β€” skip
                        {
                            aoff += (int)alen;
                            continue;
                        }

                        byte priority = ns switch
                        {
                            1 => 0, // Win32
                            3 => 1, // Win32 + DOS
                            0 => 2, // POSIX
                            _ => 3,
                        };

                        if (priority < bestNs)
                        {
                            bestNs = priority;
                            bestName = (vs + 66, nlen, parent);
                            if (priority == 0) break;
                        }
                    }
                }
            }

            aoff += (int)alen;
        }

        if (bestName.HasValue)
        {
            var (namePos, nlen, parent) = bestName.Value;
            int arenaOff = nameData.Count;

            for (int i = 0; i < nlen; i++)
            {
                int p = namePos + i * 2;
                nameData.Add((char)BinaryPrimitives.ReadUInt16LittleEndian(record.Slice(p, 2)));
            }

            records.Add(new CompactRecord
            {
                FileRef = fileRef,
                ParentRef = parent,
                NameOff = (uint)arenaOff,
                NameLen = (ushort)nlen,
                IsDir = isDir
            });
        }
    }

    private static string Win32Error(int code)
    {
        try
        {
            return new System.ComponentModel.Win32Exception(code).Message;
        }
        catch
        {
            return $"error {code} (0x{code:X})";
        }
    }

    public void Dispose()
    {
        if (!_disposed)
        {
            Win32Api.CloseHandle(_handle);
            _disposed = true;
        }
        GC.SuppressFinalize(this);
    }

    ~MftReader() => Dispose();
}

public class CompactRecord
{
    public ulong FileRef { get; set; }
    public ulong ParentRef { get; set; }
    public uint NameOff { get; set; }
    public ushort NameLen { get; set; }
    public bool IsDir { get; set; }
}

public class ScanResult
{
    public List<CompactRecord> Records { get; }
    public List<char> NameData { get; }

    public ScanResult(List<CompactRecord> records, List<char> nameData)
    {
        Records = records;
        NameData = nameData;
    }
}