PowerToys/Plugins/Wox.Plugin.FindFile/MFTSearch/VolumeMonitor.cs

140 lines
5.5 KiB
C#
Raw Normal View History

2014-10-23 23:23:40 +08:00
using System;
using System.Collections.Generic;
2014-10-27 18:22:25 +08:00
using System.Diagnostics;
2014-10-23 23:23:40 +08:00
using System.Runtime.InteropServices;
using System.Threading;
2014-10-22 18:36:49 +08:00
2014-10-27 18:22:25 +08:00
namespace Wox.Plugin.FindFile.MFTSearch
2014-10-23 23:23:40 +08:00
{
internal class VolumeMonitor
{
public Action<USNRecord> RecordAddedEvent;
public Action<USNRecord> RecordDeletedEvent;
public Action<USNRecord, USNRecord> RecordRenameEvent;
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
public void Monitor(List<string> volumes, MFTSearcherCache db)
{
foreach (var volume in volumes)
{
2014-11-03 18:49:18 +08:00
Monitor(volume, db);
2014-10-23 23:23:40 +08:00
}
}
2014-11-03 18:49:18 +08:00
public void Monitor(string volume, MFTSearcherCache db)
{
if (string.IsNullOrEmpty(volume)) throw new InvalidOperationException("Volume cant't be null or empty string.");
if (!db.ContainsVolume(volume)) throw new InvalidOperationException(string.Format("Volume {0} must be scaned first."));
ThreadPool.QueueUserWorkItem(o => MonitorThread(volume, db));
}
2014-10-23 23:23:40 +08:00
private PInvokeWin32.READ_USN_JOURNAL_DATA SetupInputData4JournalRead(string volume, uint reason)
{
IntPtr pMonitorVolume = MFTSearcher.GetVolumeJournalHandle(volume);
uint bytesReturned = 0;
PInvokeWin32.USN_JOURNAL_DATA ujd = new PInvokeWin32.USN_JOURNAL_DATA();
2014-10-27 18:22:25 +08:00
MFTSearcher.QueryUSNJournal(pMonitorVolume, out ujd, out bytesReturned);
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
// 构建输入参数
PInvokeWin32.READ_USN_JOURNAL_DATA rujd = new PInvokeWin32.READ_USN_JOURNAL_DATA();
rujd.StartUsn = ujd.NextUsn;
rujd.ReasonMask = reason;
rujd.ReturnOnlyOnClose = 1;
rujd.Timeout = 0;
rujd.BytesToWaitFor = 1;
rujd.UsnJournalID = ujd.UsnJournalID;
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
return rujd;
}
2014-10-22 18:36:49 +08:00
2014-11-03 18:49:18 +08:00
private void MonitorThread(string volume, MFTSearcherCache db)
{
2014-10-23 23:23:40 +08:00
IntPtr pbuffer = Marshal.AllocHGlobal(0x1000);
PInvokeWin32.READ_USN_JOURNAL_DATA rujd = SetupInputData4JournalRead(volume, 0xFFFFFFFF);
UInt32 cbRead;
IntPtr prujd;
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
while (true)
{
prujd = Marshal.AllocHGlobal(Marshal.SizeOf(rujd));
PInvokeWin32.ZeroMemory(prujd, Marshal.SizeOf(rujd));
Marshal.StructureToPtr(rujd, prujd, true);
2014-10-22 18:36:49 +08:00
2014-10-27 18:22:25 +08:00
IntPtr pVolume = MFTSearcher.GetVolumeJournalHandle(volume);
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
bool fok = PInvokeWin32.DeviceIoControl(pVolume,
PInvokeWin32.FSCTL_READ_USN_JOURNAL,
prujd, Marshal.SizeOf(typeof(PInvokeWin32.READ_USN_JOURNAL_DATA)),
pbuffer, 0x1000, out cbRead, IntPtr.Zero);
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
IntPtr pRealData = new IntPtr(pbuffer.ToInt32() + Marshal.SizeOf(typeof(Int64)));
uint offset = 0;
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
if (fok)
{
while (offset + Marshal.SizeOf(typeof(Int64)) < cbRead)
{
PInvokeWin32.USN_RECORD usn = new PInvokeWin32.USN_RECORD(new IntPtr(pRealData.ToInt32() + (int)offset));
ProcessUSN(usn, volume, db);
offset += usn.RecordLength;
}
}
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
Marshal.FreeHGlobal(prujd);
rujd.StartUsn = Marshal.ReadInt64(pbuffer);
}
}
2014-11-03 18:49:18 +08:00
2014-10-23 23:23:40 +08:00
private void ProcessUSN(PInvokeWin32.USN_RECORD usn, string volume, MFTSearcherCache db)
{
if (MaskEqual(usn.Reason, USNChangeReason.USN_REASONS["USN_REASON_RENAME_NEW_NAME"]))
ProcessRenameNewName(usn, volume, db);
if ((usn.Reason & USNChangeReason.USN_REASONS["USN_REASON_FILE_CREATE"]) != 0)
ProcessFileCreate(usn, volume, db);
if (MaskEqual(usn.Reason, USNChangeReason.USN_REASONS["USN_REASON_FILE_DELETE"]))
ProcessFileDelete(usn, volume, db);
}
2014-11-03 18:49:18 +08:00
2014-10-23 23:23:40 +08:00
private void ProcessFileDelete(PInvokeWin32.USN_RECORD usn, string volume, MFTSearcherCache db)
{
var cached = db.FindByFrn(volume, usn.FRN);
2014-11-03 18:49:18 +08:00
if (cached != null)
2014-10-23 23:23:40 +08:00
{
2014-10-27 18:22:25 +08:00
MFTSearcher.FillPath(volume, cached, db);
2014-10-23 23:23:40 +08:00
var deleteok = db.DeleteRecord(volume, usn.FRN);
2014-11-03 18:49:18 +08:00
Debug.WriteLine(string.Format(">>>> DeleteFIle {0} {1}.", cached.FullPath, deleteok ? "successful" : "fail"));
2014-10-23 23:23:40 +08:00
if (RecordDeletedEvent != null)
RecordDeletedEvent(cached);
}
}
2014-11-03 18:49:18 +08:00
2014-10-23 23:23:40 +08:00
private void ProcessRenameNewName(PInvokeWin32.USN_RECORD usn, string volume, MFTSearcherCache db)
{
USNRecord newRecord = USNRecord.ParseUSN(volume, usn);
2014-10-27 18:22:25 +08:00
db.UpdateRecord(volume, newRecord);
2014-11-03 18:49:18 +08:00
var oldRecord = db.FindByFrn(volume, usn.FRN);
if (oldRecord != null)
2014-10-23 23:23:40 +08:00
{
2014-11-03 18:49:18 +08:00
Debug.WriteLine(string.Format(">>>> RenameFile {0} to {1}", oldRecord.FullPath, newRecord.FullPath));
if (RecordRenameEvent != null) RecordRenameEvent(oldRecord, newRecord);
2014-10-23 23:23:40 +08:00
}
}
2014-11-03 18:49:18 +08:00
2014-10-23 23:23:40 +08:00
private void ProcessFileCreate(PInvokeWin32.USN_RECORD usn, string volume, MFTSearcherCache db)
{
USNRecord record = USNRecord.ParseUSN(volume, usn);
2014-10-27 18:22:25 +08:00
db.AddRecord(volume, record);
2014-10-23 23:23:40 +08:00
Debug.WriteLine(string.Format(">>>> NewFile: {0}", record.FullPath));
if (RecordAddedEvent != null)
RecordAddedEvent(record);
}
2014-10-22 18:36:49 +08:00
2014-10-23 23:23:40 +08:00
private bool MaskEqual(uint target, uint compare)
{
return (target & compare) != 0;
}
}
}