예제 #1
0
        private void LogToFile(QueueItem qi)
        {
            if (String.IsNullOrEmpty(LogFile) || _LogLevel == ProcessLogLevel.None)
                return;

            bool LogIt = false;
            string LogLine = DateTime.Now.ToString() + "|" + qi.Action.ToString() + "|" + qi.Type.ToString() + "|" + qi.Results.ToString() + "|" + qi.File.FullName;

            if (qi.Results == QueueItemResult.HashMatch)
            {
                if ((_LogLevel & ProcessLogLevel.Good) == ProcessLogLevel.Good)
                {
                    LogLine += "|Hash Match - File OK (" + qi.FileHash + ")";
                    LogIt = true;
                }
            }
            else if (qi.Results == QueueItemResult.HashMisMatch)
            {
                if ((_LogLevel & ProcessLogLevel.Bad) == ProcessLogLevel.Bad)
                {
                    LogLine += "|Hash Mis-Match - File Corrupt (Hash Should be: " + qi.TestHash + " - Actual Hash: " + qi.FileHash + ")";
                    LogIt = true;
                }
            }
            else if (qi.Results == QueueItemResult.HashGenerated || qi.Results == QueueItemResult.HashAppended)
            {
                if ((_LogLevel & ProcessLogLevel.Locked) == ProcessLogLevel.Locked)
                {
                    LogLine += "|Hash Generated - File Locked (" + qi.FileHash + ")";
                    LogIt = true;
                }
            }
            else if (qi.Results == QueueItemResult.FileNotFound)
            {
                if ((_LogLevel & ProcessLogLevel.Missing) == ProcessLogLevel.Missing)
                {
                    LogLine += "|Missing File - File Not Found (" + qi.File.Name + ")";
                    LogIt = true;
                }
            }
            else
            {
                if ((_LogLevel & ProcessLogLevel.Skipped) == ProcessLogLevel.Skipped)
                {
                    LogLine += "|File Skipped (" + qi.Results.ToString() + ")";
                    LogIt = true;
                }
            }

            if (LogIt)
            {
                File.AppendAllText(LogFile, LogLine + Environment.NewLine);
            }
        }
예제 #2
0
        private bool AppendHashToFileName(QueueItem item)
        {
            if (item.File.Exists)
            {
                // Determine which QueueItem hash value is avaliable
                string FileHash;
                if (!String.IsNullOrEmpty(item.FileHash))
                {
                    FileHash = item.FileHash;
                }
                else if (!String.IsNullOrEmpty(item.TestHash))
                {
                    FileHash = item.TestHash;
                }
                else
                {
                    // This would be pretty pointless if we didnt have a hash to append
                    item.SetResultStatus(QueueItemResult.HashAppendFailed);
                    return false;
                }

                // Generate the new file name
                string NewFileName;
                if (item.File.Name.IndexOf('.') > -1)
                {
                    NewFileName = item.File.Name.Substring(0, item.File.Name.LastIndexOf('.')) + " [" + FileHash + "]" + item.File.Extension;
                }
                else
                {
                    NewFileName = item.File.Name + "[" + FileHash + "]";
                }

                // Rename the file, catching common exceptions
                try
                {
                    item.File.MoveTo(Path.Combine(item.File.DirectoryName, NewFileName));
                }
                catch (FileLoadException)
                {
                    item.SetResultStatus(QueueItemResult.FileAccessViolation);
                    return false;
                }
                catch (Exception ex)
                {
                    item.SetResultStatus(QueueItemResult.HashAppendFailed);
                    return false;
                }
            }
            else
            {
                // File didnt exist
                item.SetResultStatus(QueueItemResult.FileNotFound);
                return false;
            }

            // Success
            item.SetResultStatus(QueueItemResult.HashAppended);
            return true;
        }
예제 #3
0
        private bool CalculateFileHash(QueueItem item, BackgroundWorker bw)
        {
            if (item == null)
                throw new ArgumentNullException("item");

            if (item.File.Exists)
            {
                string FileHash = String.Empty;

                if (item.Type == HashType.CRC)
                {
                    uint crc;
                    try
                    {
                        crc = Crc32.GetFileCRC32(item.File.FullName, ref _BytesProcessed, bw);
                    }
                    catch (Exception ex)
                    {
                        item.SetResultStatus(QueueItemResult.FileAccessViolation);
                        return false;
                    }
                    FileHash = StringUtilities.UInt32ToHex(crc);
                }
                else if (item.Type == HashType.MD5)
                {
                    byte[] md5;
                    try
                    {
                        md5 = Md5.GetFileMD5(item.File.FullName, ref _BytesProcessed, bw);
                    }
                    catch (Exception ex)
                    {
                        item.SetResultStatus(QueueItemResult.FileAccessViolation);
                        return false;
                    }
                    FileHash = StringUtilities.ByteArrayToHex(md5);
                }

                item.SetFileHash(FileHash);

                return true;
            }
            else
            {
                item.SetResultStatus(QueueItemResult.FileNotFound);
            }

            return false;
        }
예제 #4
0
 /// <summary>
 /// Adds each QueueItem in the QueueItem array to the queue. Automatically skips duplicate entries.
 /// </summary>
 /// <param name="item">Array of QueueItems to add.</param>
 public void Add(QueueItem[] items)
 {
     foreach (QueueItem qi in items)
     {
         Add(qi);
     }
 }
예제 #5
0
        /// <summary>
        /// Adds the QueueItem to the queue. Automatically skips duplicate entries.
        /// </summary>
        /// <param name="item">QueueItem to add.</param>
        public void Add(QueueItem item)
        {
            if (item == null)
                throw new ArgumentNullException("item");

            // De-dupe
            foreach (QueueItem qi in _ItemQueue)
            {
                if (qi.File.FullName == item.File.FullName)
                {
                    return;
                }
            }

            _ItemQueue.Enqueue(item);
            _TotalRecords++;
        }
예제 #6
0
 public void Add(QueueItem qi)
 {
 }
예제 #7
0
        private QueueItem[] LoadFile()
        {
            ArrayList TempList = new ArrayList();

            // Read the file
            bool FoundOne = false;
            using (StreamReader sr = new StreamReader(_File.FullName))
            {
                string FileLine;
                while ((FileLine = sr.ReadLine()) != null)
                {
                    Match m = null;
                    if (_FileType == HashType.CRC)
                    {
                        m = CRCMatch.Match(FileLine);
                    }
                    else if (_FileType == HashType.MD5)
                    {
                        m = MD5Match.Match(FileLine);
                    }

                    if (FileLine.StartsWith(";"))
                    {
                        if (FileLine.StartsWith("; "))
                        {
                            _Comments += FileLine.Substring(2).TrimEnd(null) + Environment.NewLine;
                        }
                        else
                        {
                            _Comments += FileLine.Substring(1).TrimEnd(null) + Environment.NewLine;
                        }
                        continue;
                    }

                    if (m != null && m.Success)
                    {
                        TempList.Add(new QueueItem(new FileInfo(Path.Combine(_File.Directory.FullName, m.Groups["file"].Value.Trim())), m.Groups["hash"].Value, QueueItemAction.TestHash, _FileType));
                        FoundOne = true;
                    }
                }
            }

            if (_Comments.EndsWith(Environment.NewLine))
            {
            _Comments = _Comments.Substring(0, _Comments.Length - Environment.NewLine.Length);
            }

            if (FoundOne)
            {
                QueueItem[] items = new QueueItem[TempList.Count];
                for (int i = 0; i < TempList.Count; i++)
                {
                    items[i] = (QueueItem)TempList[i];
                }
                return items;
            }
            else
            {
                return new QueueItem[0];
            }
        }
예제 #8
0
 /// <summary>
 /// Reads all valid lines from the SFV file. Returning an array of QueueItems
 /// </summary>
 /// <returns>QueueItem array containing the SFV data.</returns>
 public QueueItem[] ReadAll()
 {
     QueueItem[] items = new QueueItem[_Items.Count];
     for (int i = 0; i < _Items.Count; i++)
     {
         items[i] = (QueueItem)_Items[i];
     }
     return items;
 }
예제 #9
0
        /// <summary>
        /// Adds the QueueItem to the queue. Automatically skips duplicate entries.
        /// </summary>
        /// <param name="item">QueueItem to add.</param>
        public void Add(QueueItem item)
        {
            if (item == null)
                throw new ArgumentNullException("item");

            // De-dupe
            foreach (QueueItem qi in _Items)
            {
                if (qi.File.FullName == item.File.FullName)
                {
                    return;
                }
            }

            _Items.Add(item);
        }
예제 #10
0
        private void AddQueueItemToListView(QueueItem qi)
        {
            string Status;
            int ImageIndex;

            switch (qi.Results)
            {
                case QueueItemResult.HashMatch:
                    Status = "File OK";
                    ImageIndex = 0;
                    break;
                case QueueItemResult.HashMisMatch:
                    Status = "File Corrupt";
                    ImageIndex = 1;
                    if (Properties.Settings.Default.EnableSounds)
                    {
                        SystemSounds.Exclamation.Play();
                    }
                    if (Properties.Settings.Default.RenameBadFiles && qi.File.Extension.ToLower() != ".corrupt")
                    {
                        if (qi.File.Exists)
                        {
                            qi.File.MoveTo(qi.File.FullName + ".corrupt");
                        }
                        Status += " (File Renamed)";
                    }
                    if (Properties.Settings.Default.DeleteBadFiles)
                    {
                        if (qi.File.Exists)
                        {
                            if (Properties.Settings.Default.UseRecycleBinWhenDeleting)
                            {
                                RecycleBin.SendToRecycleBin(qi.File.FullName + "\0");
                            }
                            else
                            {
                                qi.File.Delete();
                            }
                        }
                        Status += " (File Deleted)";
                    }
                    break;
                case QueueItemResult.NoTeshHash:
                    Status = "No Checksum Found";
                    ImageIndex = 2;
                    break;
                case QueueItemResult.HashGenerated:
                    Status = qi.Type.ToString() + " Generated";
                    ImageIndex = 3;
                    break;
                case QueueItemResult.HashAppended:
                    Status = qi.Type.ToString() + " Appended";
                    ImageIndex = 3;
                    break;
                case QueueItemResult.HashAppendSkipped:
                    Status = qi.Type.ToString() + " already in name";
                    ImageIndex = 4;
                    break;
                case QueueItemResult.HashAppendFailed:
                    Status = "Could not rename file";
                    ImageIndex = 4;
                    break;
                case QueueItemResult.FileNotFound:
                    Status = "File Not Found";
                    ImageIndex = 4;
                    break;
                case QueueItemResult.FileAccessViolation:
                    Status = "File Access Violation (Could not open file for read)";
                    ImageIndex = 4;
                    break;

                default:
                    Status = "Unknown";
                    ImageIndex = 2;
                    break;
            }

            ArrayList TempArray = new ArrayList();

            foreach (ColumnHeader ch in listView1.Columns)
            {
                if (ch.Text == "File Name")
                    TempArray.Add(qi.File.Name);
                else if (ch.Text == "File Hash")
                    TempArray.Add((!String.IsNullOrEmpty(qi.FileHash) ? qi.FileHash : "n/a"));
                else if (ch.Text == "File Size")
                    TempArray.Add((qi.File.Exists ? StringUtilities.GetFileSizeAsString(qi.File.Length) : "n/a"));
                else if (ch.Text == "Status")
                    TempArray.Add(Status);
                else if (ch.Text == "Path")
                    TempArray.Add(qi.File.Directory.FullName);
            }

            string[] ListItemData = (string[])TempArray.ToArray(typeof(string));
            ListViewItem ListItem = new ListViewItem(ListItemData);
            ListItem.ImageIndex = ImageIndex;
            ListItem.ToolTipText = qi.File.Name + " - " + Status;
            ListItem.Tag = qi;
            if (qi.Results == QueueItemResult.HashMisMatch)
            {
                ListItem.ForeColor = Color.Red;
            }
            this.listView1.Items.Add(ListItem);

            // Alternating row colors on even rows
            if (Properties.Settings.Default.AlternateRowColors && ListItem.Index % 2 != 0)
            {
                ListItem.BackColor = Properties.Settings.Default.AlternateRowColor;
            }

            if (Properties.Settings.Default.AutomaticallyScrollFileList)
            {
                ListItem.EnsureVisible();
            }
        }