コード例 #1
0
 protected virtual void OnEntryProcessed(EntryCheckResult checkResult)
 {
     if (this.EntryProcessed != null)
     {
         this.EntryProcessed(checkResult, this.entriesProcessed, this.entriesCount);
     }
 }
コード例 #2
0
        public IEnumerable<EntryCheckResult> ValidateEntries(IEnumerable<ChecksumFileEntry> checksumFileEntries, string defaultHashAlgorithm)
        {
            try
            {
                this.state = StateType.InProgress;
                var entries = checksumFileEntries.ToArray();
                this.entriesCount = entries.Length;
                this.entriesProcessed = 0;

                var results = new List<EntryCheckResult>();
                foreach (var entry in entries)
                {
                    if (this.shouldStop)
                    {
                        break;
                    }

                    this.StartProcessingEntry(entry);
                    this.entryCalculator = new FileHashCalculator();

                    if (entry.HashType != null)
                    {
                        this.entryCalculator.HashAlgorithm = CryptoUtils.ToHashAlgorithm(entry.HashType);
                    }
                    else if (entry.HashType == null && this.DefaultHashType != null)
                    {
                        this.entryCalculator.HashAlgorithm = CryptoUtils.ToHashAlgorithm(this.DefaultHashType.Value);
                    }
                    else
                    {
                        throw new InvalidOperationException("Property 'DefaultHashType' must be set if individual entry hash type is undefined");
                    }

                    this.entryCalculator.ChunkProcessed += this.OnEntryChunkProcessed;

                    string fullEntryPath;
                    if (Path.IsPathRooted(entry.Path))
                    {
                        fullEntryPath = entry.Path;
                    }
                    else
                    {
                        if (!Directory.Exists(this.BaseDir))
                        {
                            throw new InvalidOperationException("Property BaseDir is required, but directory not found");
                        }

                        fullEntryPath = Path.Combine(this.BaseDir, entry.Path);
                    }

                    EntryResultType entryResultType;
                    if (!File.Exists(fullEntryPath))
                    {
                        entryResultType = EntryResultType.NotFound;
                        this.notFoundCount++;
                    }
                    else
                    {
                        var calculatedHash = this.entryCalculator.CalculateFileHash(fullEntryPath);

                        if (this.entryCalculator.HashCalculated)
                        {
                            bool valid = ConvertUtils.ByteArraysEqual(calculatedHash, ConvertUtils.ToBytes(entry.Hash));
                            entryResultType = valid ? EntryResultType.Correct : EntryResultType.Wrong;
                            if (valid)
                            {
                                this.validCount++;
                            }
                            else
                            {
                                this.wrongCount++;
                            }
                        }
                        else
                        {
                            entryResultType = EntryResultType.Aborted;
                        }
                    }

                    var entryResult = new EntryCheckResult(entry.Hash, entry.Path, entry.HashType, entryResultType);
                    results.Add(entryResult);

                    this.entriesProcessed++;
                    this.OnEntryProcessed(entryResult);

                    // Free
                    this.entryCalculator.ChunkProcessed -= this.OnEntryChunkProcessed;
                    this.entryCalculator = null;
                }

                return results;
            }
            finally
            {
                this.state = StateType.Finished;
                this.OnFinished();
            }
        }