Ejemplo n.º 1
0
        /// <summary>
        /// Unused internal function that can be used to read all USN records
        /// </summary>
        /// <param name="lastUsn">The USN number to start from</param>
        private void GetChangedItems(long lastUsn)
        {
            const int ALLOCATED_MEMORY = 64 * 1024;

            IntPtr allocatedMemory = IntPtr.Zero;
            List <KeyValuePair <string, Win32USN.USN_RECORD> > records = new List <KeyValuePair <string, Win32USN.USN_RECORD> >();

            try
            {
                uint bytesRead = 0;
                bool more      = true;
                allocatedMemory = Marshal.AllocHGlobal(ALLOCATED_MEMORY);

                Win32USN.READ_USN_JOURNAL_DATA startParams = new Win32USN.READ_USN_JOURNAL_DATA();
                startParams.UsnJournalID      = m_journal.UsnJournalID;
                startParams.StartUsn          = lastUsn;
                startParams.ReasonMask        = Win32USN.USNReason.USN_REASON_ANY;
                startParams.ReturnOnlyOnClose = 0;
                startParams.Timeout           = 0;
                startParams.BytesToWaitFor    = 0;

                while (more)
                {
                    if (!Win32USN.DeviceIoControl(m_volumeHandle, Win32USN.EIOControlCode.FsctlReadUsnJournal,
                                                  startParams, (uint)Marshal.SizeOf(typeof(Win32USN.READ_USN_JOURNAL_DATA)),
                                                  allocatedMemory, ALLOCATED_MEMORY,
                                                  ref bytesRead, IntPtr.Zero))
                    {
                        int errorCode = Marshal.GetLastWin32Error();

                        //If we get no error or EOF the enumeration is completed
                        if (errorCode == Win32USN.ERROR_HANDLE_EOF || errorCode == Win32USN.ERROR_SUCCESS)
                        {
                            break;
                        }
                        else
                        {
                            throw new Win32Exception(errorCode);
                        }
                    }

                    startParams.StartUsn = ExtractUsnEntries(bytesRead, allocatedMemory, records, out more);
                }

                //Records now contains all Usn entries
            }
            finally
            {
                if (allocatedMemory != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(allocatedMemory);
                    allocatedMemory = IntPtr.Zero;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Unused internal function that can be used to read all USN records
        /// </summary>
        /// <param name="lastUsn">The USN number to start from</param>
        private void GetChangedItems(long lastUsn)
        {
            const int ALLOCATED_MEMORY = 64 * 1024;

            IntPtr allocatedMemory = IntPtr.Zero;
            List<KeyValuePair<string, Win32USN.USN_RECORD>> records = new List<KeyValuePair<string, Win32USN.USN_RECORD>>();

            try
            {
                uint bytesRead = 0;
                bool more = true;
                allocatedMemory = Marshal.AllocHGlobal(ALLOCATED_MEMORY);

                Win32USN.READ_USN_JOURNAL_DATA startParams = new Win32USN.READ_USN_JOURNAL_DATA();
                startParams.UsnJournalID = m_journal.UsnJournalID;
                startParams.StartUsn = lastUsn;
                startParams.ReasonMask = Win32USN.USNReason.USN_REASON_ANY;
                startParams.ReturnOnlyOnClose = 0;
                startParams.Timeout = 0;
                startParams.BytesToWaitFor = 0;

                while (more)
                {
                    if (!Win32USN.DeviceIoControl(m_volumeHandle, Win32USN.EIOControlCode.FsctlReadUsnJournal,
                            startParams, (uint)Marshal.SizeOf(typeof(Win32USN.READ_USN_JOURNAL_DATA)),
                                allocatedMemory, ALLOCATED_MEMORY,
                                ref bytesRead, IntPtr.Zero))
                    {
                        int errorCode = Marshal.GetLastWin32Error();

                        //If we get no error or EOF the enumeration is completed
                        if (errorCode == Win32USN.ERROR_HANDLE_EOF || errorCode == Win32USN.ERROR_SUCCESS)
                            break;
                        else
                            throw new Win32Exception(errorCode);
                    }

                    startParams.StartUsn = ExtractUsnEntries(bytesRead, allocatedMemory, records, out more);
                }

                //Records now contains all Usn entries
            }
            finally
            {
                if (allocatedMemory != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(allocatedMemory);
                    allocatedMemory = IntPtr.Zero;
                }
            }
        }