| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.ComponentModel;
- using System.Runtime.InteropServices;
- using Y.FileQueryEngine.Win32.Structures;
- using Y.FileQueryEngine.Win32;
- using Y.FileQueryEngine.Win32.Constants;
- namespace Y.FileQueryEngine.UsnOperation
- {
- public class UsnOperator : IDisposable
- {
- public delegate void GetEntriesHandler(DriveInfo drive, List<UsnEntry> data);
- protected USN_JOURNAL_DATA ntfsUsnJournalData;
- public DriveInfo Drive
- {
- get;
- private set;
- }
- public string DriveLetter
- {
- get
- {
- return this.Drive.Name.TrimEnd(new char[] { '\\', ':' });
- }
- }
- public IntPtr DriveRootHandle
- {
- get;
- private set;
- }
- public UsnOperator(DriveInfo drive)
- {
- if (string.Compare(drive.DriveFormat, "ntfs", true) != 0)
- {
- throw new ArgumentException("USN journal only exists in NTFS drive.");
- }
- this.Drive = drive;
- this.DriveRootHandle = this.GetRootHandle();
- this.ntfsUsnJournalData = new USN_JOURNAL_DATA();
- }
- public UsnJournalData GetUsnJournal()
- {
- UsnErrorCode usnErrorCode = this.QueryUSNJournal();
- UsnJournalData result = null;
- if (usnErrorCode == UsnErrorCode.SUCCESS)
- {
- result = new UsnJournalData(this.Drive, this.ntfsUsnJournalData);
- }
- return result;
- }
- public bool CreateUsnJournal(UInt64 maximumSize = 0x10000000, UInt64 allocationDelta = 0x100000)
- {
- uint bytesReturnedCount;
- var createUsnJournalData = new CREATE_USN_JOURNAL_DATA();
- createUsnJournalData.MaximumSize = maximumSize;
- createUsnJournalData.AllocationDelta = allocationDelta;
- int sizeCujd = Marshal.SizeOf(createUsnJournalData);
- IntPtr cujdBuffer = GetHeapGlobalPtr(sizeCujd);
- Marshal.StructureToPtr(createUsnJournalData, cujdBuffer, true);
- bool isSuccess = Win32Api.DeviceIoControl(
- this.DriveRootHandle,
- UsnControlCode.FSCTL_CREATE_USN_JOURNAL,
- cujdBuffer,
- sizeCujd,
- IntPtr.Zero,
- 0,
- out bytesReturnedCount,
- IntPtr.Zero);
- Marshal.FreeHGlobal(cujdBuffer);
- return isSuccess;
- }
- public List<UsnEntry> GetEntries()
- {
- var result = new List<UsnEntry>();
- UsnErrorCode usnErrorCode = this.QueryUSNJournal();
- if (usnErrorCode == UsnErrorCode.SUCCESS)
- {
- MFT_ENUM_DATA mftEnumData = new MFT_ENUM_DATA();
- mftEnumData.StartFileReferenceNumber = 0;
- mftEnumData.LowUsn = 0;
- mftEnumData.HighUsn = this.ntfsUsnJournalData.NextUsn;
- int sizeMftEnumData = Marshal.SizeOf(mftEnumData);
- IntPtr ptrMftEnumData = GetHeapGlobalPtr(sizeMftEnumData);
- Marshal.StructureToPtr(mftEnumData, ptrMftEnumData, true);
- int ptrDataSize = sizeof(UInt64) + 10000;
- IntPtr ptrData = GetHeapGlobalPtr(ptrDataSize);
- uint outBytesCount;
- while (false != Win32Api.DeviceIoControl(
- this.DriveRootHandle,
- UsnControlCode.FSCTL_ENUM_USN_DATA,
- ptrMftEnumData,
- sizeMftEnumData,
- ptrData,
- ptrDataSize,
- out outBytesCount,
- IntPtr.Zero))
- {
- // ptrData includes following struct:
- //typedef struct
- //{
- // USN LastFileReferenceNumber;
- // USN_RECORD_V2 Record[1];
- //} *PENUM_USN_DATA;
- IntPtr ptrUsnRecord = new IntPtr(ptrData.ToInt32() + sizeof(Int64));
- while (outBytesCount > 60)
- {
- var usnRecord = new USN_RECORD_V2(ptrUsnRecord);
- result.Add(new UsnEntry(usnRecord));
- ptrUsnRecord = new IntPtr(ptrUsnRecord.ToInt32() + usnRecord.RecordLength);
- outBytesCount -= usnRecord.RecordLength;
- }
- Marshal.WriteInt64(ptrMftEnumData, Marshal.ReadInt64(ptrData, 0));
- }
- Marshal.FreeHGlobal(ptrData);
- Marshal.FreeHGlobal(ptrMftEnumData);
- }
- return result;
- }
- public void GetEntries(long usn, GetEntriesHandler handler, int count)
- {
- List<UsnEntry> result = new List<UsnEntry>();
- UsnErrorCode usnErrorCode = this.QueryUSNJournal();
- if (usnErrorCode == UsnErrorCode.SUCCESS)
- {
- MFT_ENUM_DATA mftEnumData = new MFT_ENUM_DATA();
- mftEnumData.StartFileReferenceNumber = 0;
- mftEnumData.LowUsn = usn;
- mftEnumData.HighUsn = this.ntfsUsnJournalData.NextUsn;
- int sizeMftEnumData = Marshal.SizeOf(mftEnumData);
- IntPtr ptrMftEnumData = GetHeapGlobalPtr(sizeMftEnumData);
- Marshal.StructureToPtr(mftEnumData, ptrMftEnumData, true);
- int ptrDataSize = sizeof(UInt64) + 10000;
- IntPtr ptrData = GetHeapGlobalPtr(ptrDataSize);
- uint outBytesCount;
- while (false != Win32Api.DeviceIoControl(
- this.DriveRootHandle,
- UsnControlCode.FSCTL_ENUM_USN_DATA,
- ptrMftEnumData,
- sizeMftEnumData,
- ptrData,
- ptrDataSize,
- out outBytesCount,
- IntPtr.Zero))
- {
- IntPtr ptrUsnRecord = new IntPtr(ptrData.ToInt32() + sizeof(Int64));
- while (outBytesCount > 60)
- {
- var usnRecord = new USN_RECORD_V2(ptrUsnRecord);
- result.Add(new UsnEntry(usnRecord));
- ptrUsnRecord = new IntPtr(ptrUsnRecord.ToInt32() + usnRecord.RecordLength);
- outBytesCount -= usnRecord.RecordLength;
- if (result.Count >= count)
- {
- handler?.Invoke(Drive, result);
- result = new List<UsnEntry>();
- }
- }
- Marshal.WriteInt64(ptrMftEnumData, Marshal.ReadInt64(ptrData, 0));
- }
- Marshal.FreeHGlobal(ptrData);
- Marshal.FreeHGlobal(ptrMftEnumData);
- }
- handler?.Invoke(Drive, result);
- }
- public bool UsnIsExist(long usn)
- {
- bool rs = false;
- UsnErrorCode usnErrorCode = QueryUSNJournal();
- if (ntfsUsnJournalData.NextUsn < usn) return rs;
- if (usnErrorCode == UsnErrorCode.SUCCESS)
- {
- MFT_ENUM_DATA mftEnumData = new MFT_ENUM_DATA();
- mftEnumData.StartFileReferenceNumber = 0;
- mftEnumData.LowUsn = usn;
- mftEnumData.HighUsn = usn;
- int sizeMftEnumData = Marshal.SizeOf(mftEnumData);
- IntPtr ptrMftEnumData = GetHeapGlobalPtr(sizeMftEnumData);
- Marshal.StructureToPtr(mftEnumData, ptrMftEnumData, true);
- int ptrDataSize = sizeof(UInt64) + 10000;
- IntPtr ptrData = GetHeapGlobalPtr(ptrDataSize);
- uint outBytesCount;
- while (false != Win32Api.DeviceIoControl(
- this.DriveRootHandle,
- UsnControlCode.FSCTL_ENUM_USN_DATA,
- ptrMftEnumData,
- sizeMftEnumData,
- ptrData,
- ptrDataSize,
- out outBytesCount,
- IntPtr.Zero))
- {
- IntPtr ptrUsnRecord = new IntPtr(ptrData.ToInt32() + sizeof(Int64));
- while (outBytesCount > 60)
- {
- var usnRecord = new USN_RECORD_V2(ptrUsnRecord);
- ptrUsnRecord = new IntPtr(ptrUsnRecord.ToInt32() + usnRecord.RecordLength);
- outBytesCount -= usnRecord.RecordLength;
- rs = true;
- }
- Marshal.WriteInt64(ptrMftEnumData, Marshal.ReadInt64(ptrData, 0));
- }
- Marshal.FreeHGlobal(ptrData);
- Marshal.FreeHGlobal(ptrMftEnumData);
- }
- return rs;
- }
- private static IntPtr GetHeapGlobalPtr(int size)
- {
- IntPtr buffer = Marshal.AllocHGlobal(size);
- Win32Api.ZeroMemory(buffer, size);
- return buffer;
- }
- private UsnErrorCode QueryUSNJournal()
- {
- int sizeUsnJournalData = Marshal.SizeOf(this.ntfsUsnJournalData);
- USN_JOURNAL_DATA tempUsnJournalData;
- uint bytesReturnedCount;
- bool isSuccess = Win32Api.DeviceIoControl(
- this.DriveRootHandle,
- UsnControlCode.FSCTL_QUERY_USN_JOURNAL,
- IntPtr.Zero,
- 0,
- out tempUsnJournalData,
- sizeUsnJournalData,
- out bytesReturnedCount,
- IntPtr.Zero);
- this.ntfsUsnJournalData = tempUsnJournalData;
- //if (isSuccess)
- //{
- // return tempUsnJournalData;
- //}
- //else
- //{
- //int win32ErrorCode = Marshal.GetLastWin32Error();
- //if (Enum.IsDefined(typeof(UsnErrorCode), win32ErrorCode))
- //{
- // var usnErrorCode = (UsnErrorCode)win32ErrorCode;
- //}
- // throw new IOException("Drive returned false for Query Usn Journal", new Win32Exception(win32ErrorCode));
- //}
- return (UsnErrorCode)Marshal.GetLastWin32Error();
- }
- private IntPtr GetRootHandle()
- {
- string volume = string.Format("\\\\.\\{0}:", this.DriveLetter);
- var result = Win32Api.CreateFile(
- volume,
- Win32ApiConstant.GENERIC_READ,
- Win32ApiConstant.FILE_SHARE_READ | Win32ApiConstant.FILE_SHARE_WRITE,
- IntPtr.Zero,
- Win32ApiConstant.OPEN_EXISTING,
- 0,
- IntPtr.Zero);
- if (result.ToInt32() == Win32ApiConstant.INVALID_HANDLE_VALUE)
- {
- throw new IOException("Drive returned invalid root handle", new Win32Exception(Marshal.GetLastWin32Error()));
- }
- return result;
- }
- public void Dispose()
- {
- if (this.DriveRootHandle != null && this.DriveRootHandle.ToInt32() != Win32ApiConstant.INVALID_HANDLE_VALUE)
- {
- Win32Api.CloseHandle(this.DriveRootHandle);
- }
- }
- }
- }
|