Esempio n. 1
0
        private static bool ReadJournal(
            SafeFileHandle hVol,
            ref Unmanaged.READ_USN_JOURNAL_DATA readData
            )
        {
            const int JournalBufferSize = 65536;

            var unmanagedReadData = Marshal.AllocHGlobal(Marshal.SizeOf <Unmanaged.READ_USN_JOURNAL_DATA>());

            Marshal.StructureToPtr(readData, unmanagedReadData, false);
            var journalBuffer = Marshal.AllocHGlobal(JournalBufferSize);

            try
            {
                uint dwBytes;
                if (!Unmanaged.DeviceIoControl(
                        hVol,
                        Unmanaged.EIOControlCode.FsctlReadUsnJournal,
                        unmanagedReadData,
                        (uint)Marshal.SizeOf <Unmanaged.READ_USN_JOURNAL_DATA>(),
                        journalBuffer,
                        (uint)JournalBufferSize,
                        out dwBytes,
                        IntPtr.Zero
                        )
                    )
                {
                    Console.WriteLine("Read journal failed {0}", Marshal.GetLastWin32Error());

                    return(false);
                }

                var dwRetBytes = dwBytes - sizeof(Int64);

                // Find the first record
                var record = journalBuffer + sizeof(Int64);

                while (dwRetBytes > 0)
                {
                    //if (   interested.compare(record->FileName) == 0)
                    //{

                    //}
                }

                return
                    (true);
            }
            finally
            {
                Marshal.FreeHGlobal(journalBuffer);
                Marshal.FreeHGlobal(unmanagedReadData);
            }
        }
Esempio n. 2
0
        private static bool QueryJournal(
            SafeFileHandle hVol,
            out Unmanaged.USN_JOURNAL_DATA_V0 journalData
            )
        {
            var unmanagedJournalData = Marshal.AllocHGlobal(Marshal.SizeOf <Unmanaged.USN_JOURNAL_DATA_V0>());

            try
            {
                uint dwBytes;
                if (!Unmanaged.DeviceIoControl(
                        hVol,
                        Unmanaged.EIOControlCode.FsctlQueryUsnJournal,
                        IntPtr.Zero,
                        0u,
                        unmanagedJournalData,
                        (uint)Marshal.SizeOf <Unmanaged.USN_JOURNAL_DATA_V0>(), //Unmanaged.USN_JOURNAL_DATA_V0.SizeOf,
                        out dwBytes,
                        IntPtr.Zero
                        )
                    )
                {
                    Console.WriteLine("Query journal failed {0}", Marshal.GetLastWin32Error());

                    journalData = new Unmanaged.USN_JOURNAL_DATA_V0();
                    return(false);
                }

                journalData = Marshal.PtrToStructure <Unmanaged.USN_JOURNAL_DATA_V0>(unmanagedJournalData);

                return
                    (true);
            }
            finally
            {
                Marshal.FreeHGlobal(unmanagedJournalData);
            }
        }
Esempio n. 3
0
        //private static readonly IntPtr InvalidPtr = new IntPtr(-1L);

        static void Main(string[] args)
        {
            throw new NotImplementedException("C# wrapper about Change Journals does not implemented completely. It's possible to to this, but makes little sense because it will suffer from poor performance. The better way to utilize Change Journals in C# is to refactor CppExample project to dll, leave all logic in C++, but performs callback to C# with FINAL RESULTS.");

            var hVol = Unmanaged.CreateFile(
                "\\\\.\\c:",
                Unmanaged.DesiredAccessEnum.GENERIC_READ | Unmanaged.DesiredAccessEnum.GENERIC_WRITE,
                Unmanaged.SharedModeEnum.FILE_SHARE_READ | Unmanaged.SharedModeEnum.FILE_SHARE_WRITE,
                IntPtr.Zero,
                Unmanaged.CreationDispositionEnum.OPEN_EXISTING,
                0,
                IntPtr.Zero
                );

            if (hVol.IsInvalid)
            {
                Console.WriteLine("CreateFile failed {0}", Marshal.GetLastWin32Error());
                return;
            }

            using (hVol)
            {
                Unmanaged.USN_JOURNAL_DATA_V0 journalData;
                if (!QueryJournal(hVol, out journalData))
                {
                    return;
                }

                var readData = new Unmanaged.READ_USN_JOURNAL_DATA(
                    0,
                    0xFFFFFFFF,
                    0,
                    0,
                    0,
                    journalData.UsnJournalID
                    );

                Console.WriteLine("Journal ID: {0}", journalData.UsnJournalID);
                Console.WriteLine("FirstUsn: {0}", journalData.FirstUsn);

                while (readData.StartUsn < journalData.NextUsn)
                {
                    //memset(journalBuffer, 0, JOURNAL_BUFFER_SIZE);

                    //if (!DeviceIoControl(
                    //    hVol,
                    //    FSCTL_READ_USN_JOURNAL,
                    //    &ReadData,
                    //    sizeof(ReadData),
                    //    &journalBuffer,
                    //    JOURNAL_BUFFER_SIZE,
                    //    &dwBytes,
                    //    NULL
                    //    )
                    //)
                    //{
                    //    printf("Read journal failed (%d)\n", GetLastError());
                    //    return -1;
                    //}

                    var rjr = ReadJournal(
                        hVol,
                        ref readData
                        );

                    //DWORD dwRetBytes = dwBytes - sizeof(USN);

                    //// Find the first record
                    //auto record = (PUSN_RECORD)(((PUCHAR)journalBuffer) + sizeof(USN));

                    //while (dwRetBytes > 0)
                    //{
                    //    if (interested.compare(record->FileName) == 0)
                    //    {
                    //        printf("USN: %I64x\n", record->Usn);
                    //        printf(
                    //            "File name: %.*S\n",
                    //            record->FileNameLength / 2,
                    //            record->FileName
                    //            );

                    //        auto fullPath = GetFullPath(
                    //            hVol,
                    //            JournalData.NextUsn,
                    //            record
                    //            );

                    //        printf("Full path: %.*S\n", fullPath.length(), fullPath.c_str());
                    //        printf("Reason: %x\n", record->Reason);

                    //        SYSTEMTIME systemTime;
                    //        FileTimeToSystemTime(
                    //            (FILETIME*)&record->TimeStamp,
                    //            &systemTime
                    //            );

                    //        printf("Time stamp: %u.%u.%u %u:%u:%u.%u\n", systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds);

                    //        printf("\n");
                    //    }

                    //    dwRetBytes -= record->RecordLength;

                    //    // Find the next record
                    //    record = (PUSN_RECORD)(((PCHAR)record) + record->RecordLength);
                    //}
                    //// Update starting USN for next call
                    //ReadData.StartUsn = *(USN*)&journalBuffer;
                }

                Console.WriteLine("finished");
            }
        }