File size: 7,461 Bytes
8304113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ff5f5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8304113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using FastSeekWpf.NativeInterop;

namespace FastSeekWpf.Core;

public class UsnWatcher : IDisposable
{
    private readonly IntPtr _handle;
    private readonly NtfsDrive _drive;
    private readonly Action<IndexEvent> _onEvent;
    private bool _disposed;
    private bool _running;

    public long NextUsn { get; private set; }
    public ulong JournalId { get; private set; }

    public UsnWatcher(NtfsDrive drive, Action<IndexEvent> onEvent, JournalCheckpoint? checkpoint)
    {
        _drive = drive;
        _onEvent = onEvent;

        _handle = Win32Api.CreateFileW(
            drive.DevicePath,
            0,
            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))
            throw new IOException($"Failed to open drive {drive.Letter}:");

        var journalData = new USN_JOURNAL_DATA_V0();
        IntPtr outBuf = Marshal.AllocHGlobal(Marshal.SizeOf<USN_JOURNAL_DATA_V0>());
        try
        {
            bool ok = Win32Api.DeviceIoControl(
                _handle, Win32Api.FSCTL_QUERY_USN_JOURNAL,
                IntPtr.Zero, 0,
                outBuf, (uint)Marshal.SizeOf<USN_JOURNAL_DATA_V0>(),
                out uint bytesReturned,
                IntPtr.Zero);

            if (!ok)
                throw new IOException("Failed to query USN journal");

            journalData = Marshal.PtrToStructure<USN_JOURNAL_DATA_V0>(outBuf);
        }
        finally
        {
            Marshal.FreeHGlobal(outBuf);
        }

        if (checkpoint != null)
        {
            if (checkpoint.JournalId != journalData.UsnJournalID)
                throw new IOException("Journal ID mismatch — rescan needed");
            if (checkpoint.NextUsn < journalData.FirstUsn || checkpoint.NextUsn > journalData.NextUsn)
                throw new IOException("Saved USN outside journal range — rescan needed");
            NextUsn = checkpoint.NextUsn;
        }
        else
        {
            NextUsn = journalData.NextUsn;
        }

        JournalId = journalData.UsnJournalID;
    }

    public static UsnWatcher CreateForCheckpoint(NtfsDrive drive, Action<IndexEvent> onEvent, JournalCheckpoint checkpoint)
    {
        return new UsnWatcher(drive, onEvent, checkpoint);
    }

    public JournalCheckpoint Checkpoint()
    {
        return new JournalCheckpoint
        {
            NextUsn = NextUsn,
            JournalId = JournalId,
            DriveLetter = _drive.Letter
        };
    }

    public int Drain()
    {
        int count = 0;
        while (true)
        {
            long before = NextUsn;
            Poll();
            if (NextUsn == before) break;
            count++;
        }
        return count;
    }

    /// <summary>Synchronous run loop — matches Rust UsnWatcher::run()</summary>
    public void Run()
    {
        byte[] buffer = new byte[65536];
        while (!_disposed)
        {
            Thread.Sleep(500);
            Poll();
        }
    }

    /// <summary>Synchronous run with shared checkpoint updates — matches Rust UsnWatcher::run_shared()</summary>
    public void RunShared(List<JournalCheckpoint> sharedCheckpoints)
    {
        byte[] buffer = new byte[65536];
        while (!_disposed)
        {
            Thread.Sleep(500);
            Poll();
            lock (sharedCheckpoints)
            {
                sharedCheckpoints.RemoveAll(c => c.DriveLetter == _drive.Letter);
                sharedCheckpoints.Add(Checkpoint());
            }
        }
    }

    public async Task RunAsync(CancellationToken ct)
    {
        _running = true;
        byte[] buffer = new byte[65536];

        while (!_disposed && !ct.IsCancellationRequested && _running)
        {
            Poll();
            await Task.Delay(500, ct);
        }
    }

    private void Poll()
    {
        var readData = new READ_USN_JOURNAL_DATA_V0
        {
            StartUsn = NextUsn,
            ReasonMask = Win32Api.USN_REASON_FILE_CREATE
                | Win32Api.USN_REASON_FILE_DELETE
                | Win32Api.USN_REASON_RENAME_NEW_NAME
                | Win32Api.USN_REASON_RENAME_OLD_NAME,
            ReturnOnlyOnClose = 0,
            Timeout = 0,
            BytesToWaitFor = 0,
            UsnJournalID = JournalId
        };

        int readDataSize = Marshal.SizeOf<READ_USN_JOURNAL_DATA_V0>();
        IntPtr readDataPtr = Marshal.AllocHGlobal(readDataSize);
        IntPtr bufferPtr = Marshal.AllocHGlobal(65536);

        try
        {
            Marshal.StructureToPtr(readData, readDataPtr, false);

            bool ok = Win32Api.DeviceIoControl(
                _handle, Win32Api.FSCTL_READ_USN_JOURNAL,
                readDataPtr, (uint)readDataSize,
                bufferPtr, 65536,
                out uint bytesReturned,
                IntPtr.Zero);

            if (!ok || bytesReturned <= 8) return;

            NextUsn = Marshal.ReadInt64(bufferPtr);
            int offset = 8;

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

                if (record.RecordLength == 0) break;

                ProcessRecord(record, bufferPtr, offset);
                offset += (int)record.RecordLength;
            }
        }
        finally
        {
            Marshal.FreeHGlobal(readDataPtr);
            Marshal.FreeHGlobal(bufferPtr);
        }
    }

    private void ProcessRecord(USN_RECORD_V2 record, IntPtr bufferBase, int offset)
    {
        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(bufferBase, nameOffset + i * 2);
        string name = new string(nameChars);

        bool isDir = (record.FileAttributes & 0x10) != 0;
        ulong fileRef = record.FileReferenceNumber;
        ulong parentRef = record.ParentFileReferenceNumber;
        uint reason = record.Reason;
        var kind = isDir ? FileKind.Directory : FileKind.File;

        if ((reason & Win32Api.USN_REASON_FILE_DELETE) != 0)
        {
            _onEvent(new IndexEvent.Deleted(fileRef));
            return;
        }

        if ((reason & Win32Api.USN_REASON_RENAME_NEW_NAME) != 0)
        {
            _onEvent(new IndexEvent.Moved(fileRef, parentRef, name, kind));
            return;
        }

        if ((reason & Win32Api.USN_REASON_FILE_CREATE) != 0)
        {
            _onEvent(new IndexEvent.Created(new FileRecord
            {
                FileRef = fileRef,
                ParentRef = parentRef,
                Name = name,
                Kind = kind
            }));
        }
    }

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

    ~UsnWatcher() => Dispose();
}