Пример #1
0
        /// <summary>
        /// Returns a list of all files and folders changed since the USN
        /// </summary>
        /// <param name="startUsn">The USN number to start the list from, set to zero to get all</param>
        /// <returns>A list of files and folders changed since the USN</returns>
        private List <KeyValuePair <string, Win32USN.USN_RECORD> > BuildUSNTable(long startUsn)
        {
            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.MFT_ENUM_DATA startParams = new Win32USN.MFT_ENUM_DATA();
                startParams.StartFileReferenceNumber = 0;
                startParams.LowUsn  = Math.Max(startUsn, m_journal.LowestValidUsn);
                startParams.HighUsn = m_journal.NextUsn;

                while (more)
                {
                    if (!Win32USN.DeviceIoControl(m_volumeHandle, Win32USN.EIOControlCode.FsctlEnumUsnData,
                                                  ref startParams, (uint)Marshal.SizeOf(typeof(Win32USN.MFT_ENUM_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.StartFileReferenceNumber = (ulong)ExtractUsnEntries(bytesRead, allocatedMemory, records, out more);
                }

                return(ParseRecordList(records));
            }
            finally
            {
                if (allocatedMemory != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(allocatedMemory);
                    allocatedMemory = IntPtr.Zero;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Constructs a new USN helper instance
        /// </summary>
        /// <param name="path">The path to the folder to perform USN services</param>
        /// <param name="volumeRoot">The root volume where the USN lookup is performed</param>
        internal USNHelper(string path, string volumeRoot)
        {
            if (!System.IO.Path.IsPathRooted(path))
            {
                throw new Exception(string.Format("Path {0} is not rooted", path));
            }

            m_path = path;

            try{
                string devicename = @"\\.\" + System.IO.Path.GetPathRoot(path).TrimEnd('\\');
                if (volumeRoot != null)
                {
                    volumeRoot = volumeRoot.TrimEnd('\\');
                }

                m_volumeHandle = Win32USN.CreateFile(volumeRoot == null ? devicename : volumeRoot, Win32USN.EFileAccess.GenericRead, Win32USN.EFileShare.ReadWrite, IntPtr.Zero, Win32USN.ECreationDisposition.OpenExisting, Win32USN.EFileAttributes.BackupSemantics, IntPtr.Zero);
                if (m_volumeHandle == null || m_volumeHandle.IsInvalid)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                uint bytesReturned = 0;
                if (!Win32USN.DeviceIoControl(m_volumeHandle, Win32USN.EIOControlCode.FsctlQueryUsnJournal, null, 0, out m_journal, (uint)Marshal.SizeOf(typeof(Win32USN.USN_JOURNAL_DATA)), ref bytesReturned, IntPtr.Zero))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                Win32USN.BY_HANDLE_FILE_INFORMATION fileInfo;
                using (SafeFileHandle driveHandle = Win32USN.CreateFile(System.IO.Path.GetPathRoot(path), Win32USN.EFileAccess.GenericRead, Win32USN.EFileShare.ReadWrite, IntPtr.Zero, Win32USN.ECreationDisposition.OpenExisting, Win32USN.EFileAttributes.BackupSemantics, IntPtr.Zero))
                    if (!Win32USN.GetFileInformationByHandle(driveHandle, out fileInfo))
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                m_volumeRootFileNameReferenceNumber = ((ulong)fileInfo.FileIndexHigh << 32) | ((ulong)fileInfo.FileIndexLow);
            }
            catch (Exception e) {
                /*  if (m_volumeHandle != null){
                 *    m_volumeHandle.Dispose();
                 *    m_volumeHandle = null;
                 * }*/
                Console.WriteLine("USNHelper() : init error : " + e.ToString());
                throw;
            }
            if (this.FileSystemEntries.Count == 0)
            {
                throw new Exception("Strings.USNHelper.SafeGuardError");
            }
            Console.WriteLine("USNHelper() : initialization successfully done");
        }