Exemplo n.º 1
0
        /** Called by our worker thread, this function handles a single item in the queue. */
        protected void WorkerCompute(QueueItem item)
        {
            // is it a directory?
            if (Directory.Exists(item.fullpath))
            {
                string[] files = Directory.GetFileSystemEntries(item.fullpath);
                foreach (string f in files)
                {
                    //QueueItem(f,item.partialpath+"\\"+JustTheFileName(f));
                    Enqueue(new QueueItem(f, item.partialpath + "\\" + JustTheFileName(f)));
                    this.Invoke(new UpdateEnqueuedLabelDelegate(UpdateEnqueuedLabel), new object[1] {
                        queue.Count()
                    });
                }
                return;
            }

            //	Console.WriteLine(item.partialpath);

            // it's not a directory. Is it a md5 file?
            if (IsMD5File(item.fullpath))
            {
                //Console.WriteLine("caught md5 file");
                StreamReader sr = new StreamReader(item.fullpath);
                string       s;

                // read each lkine. If the line looks like an md5 hash, add it
                // to the database.
                while ((s = sr.ReadLine()) != null)
                {
                    Match m = Regex.Match(s, @"^([0-9a-fA-F]{32})\s+(.+)$", RegexOptions.None);
                    if (m.Success && m.Groups.Count == 3)
                    {
                        string p    = m.Groups[2].Value.Trim();
                        string path = p.Replace("/", "\\");
                        string hash = m.Groups[1].Value.Trim().ToLower();
                        kb.AddRecord(path, hash);
                    }
                }

                sr.Close();
                listView.Invoke(new EventHandler(this.ReverifyAllItems));

                // don't return; we also compute the hash of the md5sum file. (why not?)
            }

            // compute the md5 hash
            FileStream fsr = null;

            try
            {
                currentlyProcessingLabel.Invoke(new SetCurrentlyProcessingDelegate(SetCurrentlyProcessing), new object[] { item.partialpath });

                fsr       = new FileStream(item.fullpath, FileMode.Open, FileAccess.Read);
                item.size = fsr.Length;

                // wrap the file system's stream in our progress stream. The progress stream
                // updates the thermometer/progress bar as the file is read.
                progStream = new ProgressStream(fsr, progressBar);

                // compute the hash
                // Is it just me, or is this MD5 routine slow?
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                md5.Initialize();
                byte[] hash = md5.ComputeHash(progStream);
                progStream = null;

                // we're done. Add the data to the screen
                listView.Invoke(new AddFileToGridDelegate(AddFileToGrid), new object[] { item, ByteArrayToHexadecimalString(hash) });

                md5.Clear();
            }
            catch (Exception e)
            {
                // did they click the abort button?
                if (e.Message.Equals("aborted"))
                {
                    queue.Clear();
                    this.Invoke(new UpdateEnqueuedLabelDelegate(UpdateEnqueuedLabel), new object[1] {
                        queue.Count()
                    });
                }
                else if (!quitting)
                {
                    ReportError("Couldn't process " + item.fullpath + "\r\n\r\nIs it open by another application?");
                }
                return;
            }
            finally
            {
                currentlyProcessingLabel.Invoke(new SetCurrentlyProcessingDelegate(SetCurrentlyProcessing), new object[] { "(idle)" });
                if (fsr != null)
                {
                    fsr.Close();
                }
            }
        }