Esempio n. 1
0
        private void Begin()
        {
            Program.PrintHeader();

            Console.WriteLine("Root: {0}", this._root);

            DateTime start = DateTime.Now;

            Console.WriteLine("Started At: {0}", start);

            if (this._resume && File.Exists(this._statePath))
            {
                Console.WriteLine("Loading Saved State");
                this.LoadState(this._statePath);
                Console.WriteLine("State Loaded In: {0}", DateTime.Now - start);
            }
            else if (this._resume)
            {
                Console.WriteLine("State File not found, loading information from the file system");
            }

            ulong addedFileCount = 0;
            var   cancelled      = false;

            // Discover files from the file system
            if (!this._skipFileSystemCheck)
            {
                var originalsDiscoverer = new FileDiscoverer(root: this._root,
                                                             rootNode: this._originalsNode,
                                                             peFileDestinationRoot: this._peFilesDestinationRoot,
                                                             cancellationToken: this._cancellationSource.Token);
                originalsDiscoverer.FileDiscovered += this.AddFileToFoundLIstListOrQueueForInspection;
                originalsDiscoverer.DiscoverFiles();

                addedFileCount     += originalsDiscoverer.DiscoveredFileCount;
                this._originalsNode = originalsDiscoverer.RootNode;

                if (this._cancellationSource.IsCancellationRequested)
                {
                    Console.WriteLine();
                    Console.WriteLine("Execution cancelled: Saving state for resuming later");
                }

                if (addedFileCount > 0)
                {
                    Program.UpdateCurrentLine("New Files added: {0}", addedFileCount.ToString());
                }

                Console.WriteLine();
                Console.WriteLine("State Validated in: {0}", DateTime.Now - start);
            }

            if (addedFileCount > 0)
            {
                this.SaveCurrentStateToDisk();
            }

            // If we were cancelled, lets not continue on to process
            // the files 'cause the customer is implying we
            // should give up
            if (cancelled)
            {
                return;
            }

            ulong filesInspected = 0;

            if (this._itemsRequiringInspecting.Count > 0)
            {
                var inspectionStart = DateTime.Now;
                Console.WriteLine("Inspecting {0} File(s). Starting at: {1}", this._itemsRequiringInspecting.Count, inspectionStart);
                Console.WriteLine();

                ulong filesInspectedSinceLastSave = 0;
                int   originalTotal = this._itemsRequiringInspecting.Count;

                // Any items that require inspection have been added to the queue
                // or been placed in the inspected list, so lets inspect the ones
                // that require work
                while (this._itemsRequiringInspecting.Count > 0)
                {
                    if (filesInspectedSinceLastSave > Program.NUMBER_OF_INSPECTED_FILES_BEFORE_SAVING_STATE)
                    {
                        this.SaveCurrentStateToDisk();
                        filesInspectedSinceLastSave = 0;
                    }

                    var fileToInspect = this._itemsRequiringInspecting.Dequeue();

                    Program.UpdatePreviousLine($"Inspecting file {filesInspected} of {originalTotal}", String.Empty);
                    this.InspectFileAndUpdateState(fileToInspect);

                    filesInspected++;
                    filesInspectedSinceLastSave++;

                    lock (this)
                    {
                        if (this._cancellationSource.IsCancellationRequested)
                        {
                            Console.WriteLine();
                            cancelled = true;
                            break;
                        }
                    }
                }

                if (filesInspected > 0)
                {
                    this.SaveCurrentStateToDisk();
                }
            }
            else
            {
                Console.WriteLine("No files needed inspecting");
            }

            if (cancelled)
            {
                return;
            }

            Console.WriteLine();
            Console.WriteLine("Inspecting {0} file(s) took {1}", filesInspected, DateTime.Now - start);

            if (this._peFiles.Count == 0)
            {
                Console.WriteLine("No duplicate files found");
                return;
            }

            Console.WriteLine("Files with PE Headers: {0}", this._peFiles.Count);

            // If theres no destination directory, then we can't move anything
            if (this._peFilesDestinationRoot != null)
            {
                ulong filesMoved = 0;
                this._peFilesDestinationRoot.Create();
                foreach (FileNode peFile in this._peFiles)
                {
                    if (this.MovePEFilesToDestination(peFile))
                    {
                        filesMoved += 1;
                    }

                    lock (this)
                    {
                        if (this._cancellationSource.IsCancellationRequested)
                        {
                            Console.WriteLine();
                            cancelled = true;
                            break;
                        }
                    }
                }

                Console.WriteLine();
                Console.WriteLine("Files moved: {0}", filesMoved);
            }
            else
            {
                Console.WriteLine("Not moving file. Set destinationroot to move files");
            }
        }