/// <summary>
 /// Adds the specified file hash to the index.</summary>
 public FileHash AddFileHash(FileHash hash)
 {
     // if a hash is not yet on the index, add it and return the provided pointer
     // otherwise return the pointer of the object that is already in the index
     if (Hashes.Add(hash))
     {
         return(hash);
     }
     else
     {
         return(Hashes.FirstOrDefault(x => x.Equals(hash)));
     }
 }
Exemple #2
0
        /// <summary>
        /// Generates hash for all files in the archive and adds them to the hash index.</summary>
        /// <param name="cancellationToken">Cancellation token for the async operation.</param>
        /// <param name="progress">Progress object used to report the progress of the operation.</param>
        /// <param name="processingFile">Progress object used to provide feedback over the file that is currently being hashed.</param>
        public async Task HashFilesAsync(CancellationToken cancellationToken, IProgress <int> progress, IProgress <string> processingFile)
        {
            await Task.Factory.StartNew(() =>
            {
                var scanNodes        = GetFileNodes();
                var nodeCount        = scanNodes.Count();
                int currentNodeCount = 0;

                foreach (FileNode node in scanNodes)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    // Report the current progress
                    currentNodeCount++;
                    if (processingFile != null)
                    {
                        processingFile.Report(node.FullSessionName);
                    }

                    string checkSum;
                    try { checkSum = FileHash.CalculateChecksum(node.FullSessionName); }
                    catch (Exception ex)
                    {
                        // The file couldn't be hashed for some reason, don't add it to the index
                        MessageService.SendMessage(node, "FileScanException", new ApplicationException("Could not hash " + node.FullName, ex));

                        continue;
                    }
                    finally
                    {
                        if (progress != null)
                        {
                            progress.Report((int)((double)currentNodeCount / nodeCount * 100));
                        }
                    }

                    node.Hash     = new FileHash(node.FullSessionName, checkSum);
                    node.Checksum = checkSum;
                    node.Hash.AddNode(node);
                }
            }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            LastScanDate = DateTime.Now;
        }