Пример #1
0
        /// <summary>
        /// Calculates the file's checksum and adds it to the Database
        /// </summary>
        /// <param name="fileName">File to add</param>
        /// <param name="basePath">Base path</param>
        /// <param name="pathType">Path type</param>
        /// <param name="checksumType">Checksum type</param>
        /// <returns>Checksum of the file</returns>
        public string AddFile(string fileName, string basePath, PathType pathType, ChecksumType checksumType)
        {
            _hasChanges = true;

            // load file checksum
            FileChecksum fc = new FileChecksum(fileName, basePath, pathType, checksumType);

            // add to database
            _fileLookup.Add(fc.ResolvedFileName, fc);

            // return the checksum
            return(fc.Checksum);
        }
Пример #2
0
        /// <summary>
        /// Updates a file's checksum
        /// </summary>
        /// <param name="fileName">File to update</param>
        /// <param name="basePath">Base path</param>
        /// <param name="pathType">Path type</param>
        /// <param name="checksumType">Checksum type</param>
        /// <returns>True if the file was updated</returns>
        public bool UpdateFile(string fileName, string basePath, PathType pathType, ChecksumType checksumType)
        {
            // load file checksum
            FileChecksum fc = new FileChecksum(fileName, basePath, pathType, checksumType);

            if (_fileLookup.ContainsKey(fc.ResolvedFileName))
            {
                // determine if the checksums are different
                if (_fileLookup[fc.ResolvedFileName].Checksum != fc.Checksum)
                {
                    _fileLookup[fc.ResolvedFileName] = fc;

                    _hasChanges = true;

                    return(true);
                }
            }

            return(false);
        }
Пример #3
0
        /// <summary>
        /// Verify the checksums of files listed in the Database
        /// </summary>
        /// <param name="ignoreChecksum">Ignore checksum changes</param>
        /// <param name="ignoreMissing">Ignore missing files</param>
        /// <param name="showNew">Show new files</param>
        /// <returns>Number of bad files found, or -1 for other error</returns>
        public VerifyChecksumsResult VerifyChecksums(bool ignoreChecksum, bool ignoreMissing, bool showNew)
        {
            if (_filesToCheck == null)
            {
                throw new InvalidOperationException("You must scan files before verifying checksums");
            }

            if (!Directory.Exists(_basePath))
            {
                return(new VerifyChecksumsResult(false, null));
            }

            _reporter.VerifyingChecksums(_db.FileCount());

            // keep track of files we checked
            Hashtable checkedFiles = new Hashtable();

            // list of bad files
            _badFiles = new List <BadFile>();

            // verify the checksums of all of the files in the DB
            int fileChecked = 0;

            // save then cd to basepath
            string previousDirectory = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(_basePath);

            foreach (FileChecksum fc in _db.Files)
            {
                fileChecked++;

                // display progress
                _reporter.VerifyingFile(fileChecked, _db.FileCount(), fc.ResolvedFileName);

                // keep trace of files we looked at
                checkedFiles.Add(fc.ResolvedFileName, 1);

                // determine if the file is missing from the dist
                if (!File.Exists(fc.FilePath))
                {
                    if (!ignoreMissing)
                    {
                        _badFiles.Add(new BadFile()
                        {
                            FileName    = fc.ResolvedFileName,
                            BadFileType = BadFileType.Missing
                        });
                    }

                    continue;
                }

                if (!ignoreChecksum)
                {
                    // verify the checksum from the disk
                    FileChecksum fileChecksum = new FileChecksum(fc.FilePath, _basePath, _pathType, _checksumType);

                    // if the checksum is bad, add bad files
                    if (fc.Checksum != fileChecksum.Checksum)
                    {
                        _badFiles.Add(new BadFile()
                        {
                            FileName         = fc.ResolvedFileName,
                            BadFileType      = BadFileType.DifferentChecksum,
                            ChecksumDisk     = fileChecksum.Checksum,
                            ChecksumDatabase = fc.Checksum
                        });

                        continue;
                    }
                }
            }

            // notify the user of new files
            if (showNew)
            {
                foreach (string file in _filesToCheck)
                {
                    // change file name via PathType
                    string fileName = FileChecksum.GetFileName(file, _basePath, _pathType);

                    if (!checkedFiles.ContainsKey(fileName))
                    {
                        _badFiles.Add(new BadFile()
                        {
                            FileName    = fileName,
                            BadFileType = BadFileType.New,
                        });
                    }
                }
            }

            ReadOnlyCollection <BadFile> badFiles = _badFiles.AsReadOnly();

            _reporter.VerifyingChecksumsCompleted(badFiles);

            // cd back to previous directory
            Directory.SetCurrentDirectory(previousDirectory);

            return(new VerifyChecksumsResult(badFiles.Count == 0, badFiles));
        }
Пример #4
0
        /// <summary>
        /// Updates checksums and finds new files
        /// </summary>
        /// <param name="ignoreNew">Ignore new files</param>
        /// <param name="removeMissing">Remove missing files</param>
        /// <param name="pretend">Don't save changes to the database</param>
        /// <returns>True on success</returns>
        public UpdateChecksumsResult UpdateChecksums(bool ignoreNew, bool removeMissing, bool pretend)
        {
            int filesUpdated = 0;

            if (_filesToCheck == null)
            {
                throw new InvalidOperationException("You must scan files before updating checksums");
            }

            _reporter.UpdatingChecksums(_db.FileCount());

            // if adding new files, find those missing in the XML DB
            if (!ignoreNew)
            {
                foreach (string fileName in _filesToCheck)
                {
                    string fileNameToCheck = FileChecksum.GetFileName(fileName, _basePath, _pathType);

                    if (!_db.HasFile(fileNameToCheck))
                    {
                        _reporter.AddingFile(fileNameToCheck);

                        // add file to database
                        string checksum = _db.AddFile(fileName, _basePath, _pathType, _checksumType);

                        _reporter.AddingFileCompleted(fileNameToCheck, checksum);

                        filesUpdated++;
                    }
                }
            }

            // determine if there are any files in the DB but not on the disk
            if (removeMissing)
            {
                List <string> filesToRemove = new List <string>();

                foreach (FileChecksum fc in _db.Files)
                {
                    if (!File.Exists(fc.FilePath))
                    {
                        // build a list of files to remove first
                        filesToRemove.Add(fc.ResolvedFileName);
                        _reporter.RemovedFile(fc.FilePath);

                        filesUpdated++;
                    }
                }

                // now actually remove the files
                foreach (string fileToRemove in filesToRemove)
                {
                    _db.RemoveFile(fileToRemove);
                }
            }

            // write out XML unless we're in pretend mode
            if (!pretend)
            {
                if (_db.HasChanges())
                {
                    _reporter.WritingDatabase(_xmlDatabasePath);

                    _db.Write();

                    _reporter.WritingDatabaseCompleted();
                }
                else
                {
                    _reporter.UpdatingChecksumsCompleted(false);
                }
            }

            return(new UpdateChecksumsResult(true, filesUpdated));
        }
Пример #5
0
 /// <summary>
 /// Initializes the FileChecksum class after being loaded from XML
 /// </summary>
 /// <param name="basePath">Base path</param>
 /// <param name="pathType">Path type</param>
 public void InitFromXml(string basePath, PathType pathType)
 {
     FilePath = FileChecksum.GetFilePath(ResolvedFileName, basePath, pathType);
 }
Пример #6
0
        /// <summary>
        /// Calculates the file's checksum and adds it to the Database
        /// </summary>
        /// <param name="fileName">File to add</param>
        /// <param name="basePath">Base path</param>
        /// <param name="pathType">Path type</param>
        /// <param name="checksumType">Checksum type</param>
        /// <returns>Checksum of the file</returns>
        public string AddFile(string fileName, string basePath, PathType pathType, ChecksumType checksumType)
        {
            _hasChanges = true;

            // load file checksum
            FileChecksum fc = new FileChecksum(fileName, basePath, pathType, checksumType);

            // add to database
            _fileLookup.Add(fc.ResolvedFileName, fc);

            // return the checksum
            return fc.Checksum;
        }
Пример #7
0
        /// <summary>
        /// Verify the checksums of files listed in the Database
        /// </summary>
        /// <param name="ignoreChecksum">Ignore checksum changes</param>
        /// <param name="ignoreMissing">Ignore missing files</param>
        /// <param name="showNew">Show new files</param>
        /// <returns>Number of bad files found, or -1 for other error</returns>
        public VerifyChecksumsResult VerifyChecksums(bool ignoreChecksum, bool ignoreMissing, bool showNew)
        {
            if (_filesToCheck == null)
            {
                throw new InvalidOperationException("You must scan files before verifying checksums");
            }

            if (!Directory.Exists(_basePath))
            {
                return new VerifyChecksumsResult(false, null);
            }

            _reporter.VerifyingChecksums(_db.FileCount());

            // keep track of files we checked
            Hashtable checkedFiles = new Hashtable();

            // list of bad files
            _badFiles = new List<BadFile>();

            // verify the checksums of all of the files in the DB
            int fileChecked = 0;

            // save then cd to basepath
            string previousDirectory = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(_basePath);

            foreach (FileChecksum fc in _db.Files)
            {
                fileChecked++;

                // display progress
                _reporter.VerifyingFile(fileChecked, _db.FileCount(), fc.ResolvedFileName);

                // keep trace of files we looked at
                checkedFiles.Add(fc.ResolvedFileName, 1);

                // determine if the file is missing from the dist
                if (!File.Exists(fc.FilePath))
                {
                    if (!ignoreMissing)
                    {
                        _badFiles.Add(new BadFile()
                        {
                            FileName = fc.ResolvedFileName,
                            BadFileType = BadFileType.Missing
                        });
                    }

                    continue;
                }

                if (!ignoreChecksum)
                {
                    // verify the checksum from the disk
                    FileChecksum fileChecksum = new FileChecksum(fc.FilePath, _basePath, _pathType, _checksumType);

                    // if the checksum is bad, add bad files
                    if (fc.Checksum != fileChecksum.Checksum)
                    {
                         _badFiles.Add(new BadFile()
                         {
                            FileName = fc.ResolvedFileName,
                            BadFileType = BadFileType.DifferentChecksum,
                            ChecksumDisk = fileChecksum.Checksum,
                            ChecksumDatabase = fc.Checksum
                        });

                        continue;
                    }
                }
            }

            // notify the user of new files
            if (showNew)
            {
                foreach (string file in _filesToCheck)
                {
                    // change file name via PathType
                    string fileName = FileChecksum.GetFileName(file, _basePath, _pathType);

                    if (!checkedFiles.ContainsKey(fileName))
                    {
                        _badFiles.Add(new BadFile()
                        {
                            FileName = fileName,
                            BadFileType = BadFileType.New,
                        });
                    }
                }
            }

            ReadOnlyCollection<BadFile> badFiles = _badFiles.AsReadOnly();

            _reporter.VerifyingChecksumsCompleted(badFiles);

            // cd back to previous directory
            Directory.SetCurrentDirectory(previousDirectory);

            return new VerifyChecksumsResult(badFiles.Count == 0, badFiles);
        }