Esempio n. 1
0
        // We've finished processing a file.
        private void FinishedFile(FileListFile file, FileListProcessorStatus status)
        {
            // Log the result.
            FrmLog.Instance.AddLine("         Status: " + status.ToString());
            FrmLog.Instance.AddLine();

            // Notify the GUI.
            OnProgress(file, status, 100, CalculatePercentage(100));
            numFilesProcessed++;
        }
Esempio n. 2
0
        // Fires the Progress event.
        private void OnProgress(FileListFile file, FileListProcessorStatus status, int percentCompleteFile, int percentCompleteTotal)
        {
            if (Progress != null)
            {
                // Package our event arguments.
                ProgressEventArgs e = new ProgressEventArgs();
                e.File   = file;
                e.Status = status;
                e.PercentCompleteFile  = percentCompleteFile;
                e.PercentCompleteTotal = percentCompleteTotal;

                Progress(this, e);
            }
        }
Esempio n. 3
0
        /// <summary>Start the processor.</summary>
        public void Start()
        {
            if (IsProcessing)
            {
                return;
            }

            // Move to the directory of the file verification list.
            try {
                Environment.CurrentDirectory = new FileInfo(fileList.sourceFile).Directory.FullName;
            } catch (Exception ex) {
                OnCompletion(ex);
                return;
            }

            numFilesProcessed = 0;

            ProgressStream fs = null;
            string         hash;

            System.Security.Cryptography.HashAlgorithm hasher;
            try {
                foreach (FileListFile f in fileList.filelist)
                {
                    // Start logging the results.
                    FrmLog.Instance.AddLine("           File: " + f.name);
                    if (f.sizeSpecified)
                    {
                        FrmLog.Instance.AddLine("Documented Size: " + f.size);
                    }
                    FrmLog.Instance.AddLine("Documented Hash: " + f.Value.ToUpper(System.Globalization.CultureInfo.InvariantCulture));

                    // See if we're supposed to ignore this file.
                    if (f.Ignore)
                    {
                        FinishedFile(f, FileListProcessorStatus.Ignored);
                        continue;
                    }

                    // Announce that we're starting on this file.
                    OnProgress(f, FileListProcessorStatus.InProcess, 0, CalculatePercentage(0));
                    currentFile = f;

                    // Open the file.
                    try {
                        fs = new ProgressStream(File.OpenRead(f.name), 5);

                        // If the file isn't the same size, it shouldn't be considered valid.
                        FrmLog.Instance.AddLine("    Actual Size: " + fs.Length.ToString());
                        if ((currentFile.sizeSpecified) && (currentFile.size != (ulong)fs.Length))
                        {
                            fs.Close();
                            FinishedFile(f, FileListProcessorStatus.WrongSize);
                            continue;
                        }

                        fs.ProgressUpdate += new ProgressStream.ProgressUpdateEventHandler(this.fs_ProgressUpdate);
                    } catch (FileNotFoundException) {
                        FinishedFile(f, FileListProcessorStatus.NotFound);
                        continue;
                    } catch (Exception ex) {
                        if (ex is ThreadAbortException)
                        {
                            throw;
                        }
                        FinishedFile(f, FileListProcessorStatus.Error);
                        continue;
                    }

                    // Hash the file.
                    try {
                        hasher = Algorithm.GetHasherFromType(f.type);
                        FrmLog.Instance.AddLine(" Hash Algorithm: " + f.type.ToString());
                        hash = Classless.Hasher.Utilities.ByteToHexadecimal(hasher.ComputeHash(fs)).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
                        FrmLog.Instance.AddLine("Calculated Hash: " + hash);
                    } catch (Exception ex) {
                        if (ex is ThreadAbortException)
                        {
                            throw;
                        }
                        FinishedFile(f, FileListProcessorStatus.Error);
                        continue;
                    } finally {
                        fs.Close();
                    }

                    // Check the hash.
                    if (f.Value.ToUpper(System.Globalization.CultureInfo.InvariantCulture) == hash)
                    {
                        FinishedFile(f, FileListProcessorStatus.Good);
                    }
                    else
                    {
                        FinishedFile(f, FileListProcessorStatus.Bad);
                    }
                }
            } catch (Exception e) {
                if (fs != null)
                {
                    fs.Close();
                }
                OnCompletion(e);
            } finally {
                isProcessing = false;
            }

            OnCompletion(null);
        }