示例#1
0
        void DirEnum_FileFound(object sender, FileFoundEventArgs e)
        {
            FileCount++;
            workerThread.ReportProgress(1, e.fileInfo);
            string MD5Hash = Kavitesh.Hashing.MD5Hash.GetMD5HashFromFile(e.fileInfo.FullName);

            DBQueries.CreateFileEntry(_dbEngine, e.fileInfo, MD5Hash);
        }
示例#2
0
        public void ShouldCancel()
        {
            OnFileFound += EventBehaviour_OnFileFound;

            FileFoundEventArgs args = new FileFoundEventArgs(@"c:\temp\1.txt");

            OnFileFound.Invoke(this, args);
            if (args.Cancel)
            {
                return;
            }
        }
        private void FindFileEx(FileFoundEventArgs args, int slength)
        {
            _fpattern.CopyTo(_ff.Buffer, slength);
            _ff.Buffer[slength + _fpattern.Length] = Zero;
            var hFile = Kernel32.FindFirstFileEx(
                _ff.BufferAddress,
                Kernel32.FindexInfoLevels.FindExInfoBasic,
                out _ff.Value,
                Kernel32.FindexSearchOps.FindExSearchNameMatch,
                IntPtr.Zero,
                Kernel32.FindexAdditionalFlags.FindFirstExLargeFetch);

            if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) ||
                (IntPtr.Size == 8 && hFile.ToInt64() == -1L))
            {
                Win32Error(Marshal.GetLastWin32Error());
                return;
            }

            try
            {
                do
                {
                    var sposition = slength;
                    for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++)
                    {
                        _ff.Buffer[sposition++] = _ff.Value.cFileName[ix];
                    }
                    if (sposition == _ff.Buffer.Length)
                    {
                        throw new PathTooLongException();
                    }
                    if (_ff.Value.IgnoredByName)
                    {
                        continue;
                    }
                    if ((_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        continue;
                    }
                    //got one
                    args.SetNameOffsets(UncPrefixLength, slength, sposition);
                    _fileFound(args);
                } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value));
            }
            finally
            {
                Kernel32.FindClose(hFile);
            }
        }
        private void Find()
        {
            Ensure.NotNull(_fileFound, "FileFoundEventHandler");
            var hdl = GCHandle.Alloc(_ff.Buffer, GCHandleType.Pinned);

            try
            {
                var args = new FileFoundEventArgs(_ff);
                _ff.BufferAddress = hdl.AddrOfPinnedObject();
                FindFileEx(args, _baseOffset);
            }
            finally
            {
                _ff.BufferAddress = IntPtr.Zero;
                hdl.Free();
            }
        }
示例#5
0
        private void AddFile(FileInfo file)
        {
            if (!Allowed(file) || (base.Dictionary != null && base.Dictionary.ContainsKey(file.FullName)))
            {
                return;
            }

            if (FileFound != null)
            {
                FileFoundEventArgs args = new FileFoundEventArgs(false, file);
                FileFound(this, args);
                if (args.Ignore)
                {
                    return;
                }
            }
            base.Add(file);
        }
示例#6
0
        /// <summary> Performs the search raising the FileFound event for each entry matching the request </summary>
        public void Find()
        {
            Check.NotNull(FileFound);
            GCHandle hdl = GCHandle.Alloc(_ff.Buffer, GCHandleType.Pinned);

            try
            {
                FileFoundEventArgs args = new FileFoundEventArgs(_ff);
                _ff.BufferAddress = hdl.AddrOfPinnedObject();

                FindFileEx(args, _baseOffset);
            }
            finally
            {
                _ff.BufferAddress = IntPtr.Zero;
                hdl.Free();
            }
        }
示例#7
0
        protected override void OnFileFound(object sender, FileFoundEventArgs args)
        {
            var file   = args.Item;
            var target = FileUtils.GetDestinationFile(Source, file, Destination);

            // Block so we don't queue too many files at once.
            while (FileQueue.Count > MaxThreads * 5 && !cancellation.IsCancellationRequested)
            {
                Thread.Sleep(500);
            }

            if (cancellation.IsCancellationRequested)
            {
                return;
            }

            FileQueue.Enqueue(new MultiThreadedFileOperationState(file, target));
        }
示例#8
0
 private void EventBehaviour_OnFileFound(object sender, FileFoundEventArgs eventArgs)
 {
     eventArgs.Cancel = true;
 }
示例#9
0
        private void FindFileEx(FileFoundEventArgs args, int slength)
        {
            Kernel32.FINDEX_INFO_LEVELS      findInfoLevel   = Kernel32.FINDEX_INFO_LEVELS.FindExInfoStandard;
            Kernel32.FINDEX_ADDITIONAL_FLAGS additionalFlags = 0;

            if (Environment.OSVersion.Version.Major >= 6)
            {
                //Ignore short-names
                findInfoLevel = Kernel32.FINDEX_INFO_LEVELS.FindExInfoBasic;
                //Use large fetch table
                additionalFlags = Kernel32.FINDEX_ADDITIONAL_FLAGS.FindFirstExLargeFetch;
            }

            _fpattern.CopyTo(_ff.Buffer, slength);
            _ff.Buffer[slength + _fpattern.Length] = ZERO;

            IntPtr hFile = Kernel32.FindFirstFileEx(
                _ff.BufferAddress,
                findInfoLevel,
                out _ff.Value,
                Kernel32.FINDEX_SEARCH_OPS.FindExSearchNameMatch,
                IntPtr.Zero,
                additionalFlags);

            if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) ||
                (IntPtr.Size == 8 && hFile.ToInt64() == -1L))
            {
                Win32Error(Marshal.GetLastWin32Error());
                return;
            }

            bool traverseDirs = _recursive && IsWild();

            try
            {
                do
                {
                    int sposition = slength;
                    for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++)
                    {
                        _ff.Buffer[sposition++] = _ff.Value.cFileName[ix];
                    }

                    if (sposition == _ff.Buffer.Length)
                    {
                        throw new PathTooLongException();
                    }

                    if (!_ff.Value.IgnoredByName)
                    {
                        bool isDirectory = (_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory;

                        if ((_includeFolders && isDirectory) || (_includeFiles && !isDirectory))
                        {
                            args.SetNameOffsets(UncPrefixLength, slength, sposition);
                            FileFound(this, args);
                        }
                        if (traverseDirs && isDirectory)
                        {
                            _ff.Buffer[sposition++] = SLASH;
                            FindFileEx(args, sposition);
                        }
                    }
                } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value));
            }
            finally
            {
                Kernel32.FindClose(hFile);
            }

            // Recursive search for patterns other than '*' and '*.*' requires we enum directories again
            if (_recursive && !traverseDirs)
            {
                _ff.Buffer[slength]     = '*';
                _ff.Buffer[slength + 1] = ZERO;

                hFile = Kernel32.FindFirstFileEx(
                    _ff.BufferAddress,
                    findInfoLevel,
                    out _ff.Value,
                    Kernel32.FINDEX_SEARCH_OPS.FindExSearchNameMatch,
                    IntPtr.Zero,
                    additionalFlags);

                if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) ||
                    (IntPtr.Size == 8 && hFile.ToInt64() == -1L))
                {
                    Win32Error(Marshal.GetLastWin32Error());
                    return;
                }

                try
                {
                    do
                    {
                        int sposition = slength;
                        for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++)
                        {
                            _ff.Buffer[sposition++] = _ff.Value.cFileName[ix];
                        }

                        if (sposition == _ff.Buffer.Length)
                        {
                            throw new PathTooLongException();
                        }

                        if (!_ff.Value.IgnoredByName)
                        {
                            bool isDirectory = (_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory;
                            if (isDirectory)
                            {
                                _ff.Buffer[sposition++] = SLASH;
                                FindFileEx(args, sposition);
                            }
                        }
                    } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value));
                }
                finally
                {
                    Kernel32.FindClose(hFile);
                }
            }
        }
 private void QueryProc_FileFound(object sender, FileFoundEventArgs e)
 {
     Dispatch(new Action(() => viewModel.SearchResults.Add(e.fileInfo.FullName)));
 }
示例#11
0
        private void FindFileEx(FileFoundEventArgs args, int slength)
        {
            Kernel32.FINDEX_INFO_LEVELS findInfoLevel = Kernel32.FINDEX_INFO_LEVELS.FindExInfoStandard;
            Kernel32.FINDEX_ADDITIONAL_FLAGS additionalFlags = 0;

            if (Environment.OSVersion.Version.Major >= 6)
            {
                //Ignore short-names
                findInfoLevel = Kernel32.FINDEX_INFO_LEVELS.FindExInfoBasic;
                //Use large fetch table
                additionalFlags = Kernel32.FINDEX_ADDITIONAL_FLAGS.FindFirstExLargeFetch;
            }

            _fpattern.CopyTo(_ff.Buffer, slength);
            _ff.Buffer[slength + _fpattern.Length] = ZERO;

            IntPtr hFile = Kernel32.FindFirstFileEx(
                _ff.BufferAddress,
                findInfoLevel,
                out _ff.Value,
                Kernel32.FINDEX_SEARCH_OPS.FindExSearchNameMatch,
                IntPtr.Zero,
                additionalFlags);

            if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) ||
                (IntPtr.Size == 8 && hFile.ToInt64() == -1L))
            {
                Win32Error(Marshal.GetLastWin32Error());
                return;
            }

            bool traverseDirs = _recursive && IsWild();

            try
            {
                do
                {
                    int sposition = slength;
                    for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++)
                        _ff.Buffer[sposition++] = _ff.Value.cFileName[ix];

                    if (sposition == _ff.Buffer.Length)
                        throw new PathTooLongException();

                    if (!_ff.Value.IgnoredByName)
                    {
                        bool isDirectory = (_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory;

                        if ((_includeFolders && isDirectory) || (_includeFiles && !isDirectory))
                        {
                            args.SetNameOffsets(UncPrefixLength, slength, sposition);
                            FileFound(this, args);
                        }
                        if (traverseDirs && isDirectory)
                        {
                            _ff.Buffer[sposition++] = SLASH;
                            FindFileEx(args, sposition);
                        }
                    }
                } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value));
            }
            finally
            {
                Kernel32.FindClose(hFile);
            }

            // Recursive search for patterns other than '*' and '*.*' requires we enum directories again
            if (_recursive && !traverseDirs)
            {
                _ff.Buffer[slength] = '*';
                _ff.Buffer[slength + 1] = ZERO;

                hFile = Kernel32.FindFirstFileEx(
                    _ff.BufferAddress,
                    findInfoLevel,
                    out _ff.Value,
                    Kernel32.FINDEX_SEARCH_OPS.FindExSearchNameMatch,
                    IntPtr.Zero,
                    additionalFlags);

                if ((IntPtr.Size == 4 && hFile.ToInt32() == -1) ||
                    (IntPtr.Size == 8 && hFile.ToInt64() == -1L))
                {
                    Win32Error(Marshal.GetLastWin32Error());
                    return;
                }

                try
                {
                    do
                    {
                        int sposition = slength;
                        for (int ix = 0; ix < Kernel32.MAX_PATH && sposition < _ff.Buffer.Length && _ff.Value.cFileName[ix] != 0; ix++)
                            _ff.Buffer[sposition++] = _ff.Value.cFileName[ix];

                        if (sposition == _ff.Buffer.Length)
                            throw new PathTooLongException();

                        if (!_ff.Value.IgnoredByName)
                        {
                            bool isDirectory = (_ff.Value.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory;
                            if (isDirectory)
                            {
                                _ff.Buffer[sposition++] = SLASH;
                                FindFileEx(args, sposition);
                            }
                        }
                    } while (!args.CancelEnumeration && Kernel32.FindNextFile(hFile, out _ff.Value));
                }
                finally
                {
                    Kernel32.FindClose(hFile);
                }
            }
        }
示例#12
0
        /// <summary> Performs the search raising the FileFound event for each entry matching the request </summary>
        public void Find()
        {
            Check.NotNull(FileFound);
            GCHandle hdl = GCHandle.Alloc(_ff.Buffer, GCHandleType.Pinned);
            try
            {
                FileFoundEventArgs args = new FileFoundEventArgs(_ff);
                _ff.BufferAddress = hdl.AddrOfPinnedObject();

                FindFileEx(args, _baseOffset);
            }
            finally
            {
                _ff.BufferAddress = IntPtr.Zero;
                hdl.Free();
            }
        }
        void _dirFileEnumeration_FileFound(object sender, FileFoundEventArgs e)
        {
            /* _HashInfoFiles is dictionary containing files of different hashes.
             * Key is hash and value is list of file details with same hash.
             * _FileSizeInfo contains key-value of file size and one file detail.
             * _FileSizeInfo basically helps check if there are any duplicate files
             * based on the file size. If yes, then file detail will be added in
             * _HashInfoFiles dictionary.
             */

            FileEnumTotalFilesScanned++;

            FileEnumCurrentFile = new FileDetail("", e.fileInfo.Name, e.fileInfo.FullName, (UInt64)e.fileInfo.Length);

            FileDetail fileDetail = null;

            if (_FileSizeInfo.TryGetValue(FileEnumCurrentFile.Size, out fileDetail))
            {
                /*
                 * We found a duplicate entry if we reach this code section.
                 * Get the first file from _FileSizeInfo dictionary. Check if hash value is calculated.
                 * If not, this is the first entry so calculate the hash value and then add it to
                 * _HashInfoFiles dictionary based on the hash. This will be the first hash entry in the
                 * _HashInfoFiles dictionary
                 */
                List <FileDetail> list = null;
                if (string.IsNullOrEmpty(fileDetail.Hash))
                {
                    fileDetail.Hash = Hashing.MD5Hash.GetMD5HashFromFile(fileDetail.FullFilePath);
                    HashCurrentFile = fileDetail;
                    list            = new List <FileDetail>();
                    list.Add(fileDetail);
                    _HashInfoFiles.Add(fileDetail.Hash, list);
                }

                list = null;

                /*
                 * This is second entry or subsequent file, so calculate the hash for this file.
                 * Then check if that hash exists in the _HashInfoFiles dictionary. If yes, then simply
                 * add it to the list of files denoted by value in dictionary.
                 * If this is unique hash then simply create a key,value entry and add it to _HashInfoFiles.
                 */
                FileEnumCurrentFile.Hash = Hashing.MD5Hash.GetMD5HashFromFile(FileEnumCurrentFile.FullFilePath);
                HashCurrentFile          = FileEnumCurrentFile;

                if (_HashInfoFiles.TryGetValue(FileEnumCurrentFile.Hash, out list))
                {
                    list.Add(FileEnumCurrentFile);
                }
                else
                {
                    list = new List <FileDetail>();
                    list.Add(FileEnumCurrentFile);
                    _HashInfoFiles.Add(FileEnumCurrentFile.Hash, list);
                }
            }
            else
            {
                /*
                 * If there is no key with the file size, then simply add the file info to the
                 * _FileSizeInfo dictionary without doing any hash calculation. Hash will be calculated
                 * only if another match is found.
                 */
                fileDetail = new FileDetail(FileEnumCurrentFile);
                _FileSizeInfo.Add(FileEnumCurrentFile.Size, fileDetail);
                NumberOfBuckets = _FileSizeInfo.Count;
            }
        }