コード例 #1
0
        /// <summary>
        /// Verify command line arguments
        /// </summary>
        /// <returns>True if arguments are verified</returns>
        public static bool VerifyArguments()
        {
            //
            // Command Line: action
            //
            if (_action == ProgramAction.Invalid)
            {
                Usage("You must either run the -update or -verify action");
                return(false);
            }

            //
            // Command Line: match
            //
            if (_matchPattern.Contains("*") || _matchPattern.Contains("?"))
            {
                // if the match is a path, split into path and file match
                if (_matchPattern.Contains(@"\"))
                {
                    _basePath     = _matchPattern.Substring(0, _matchPattern.LastIndexOf(@"\", StringComparison.OrdinalIgnoreCase));
                    _matchPattern = _matchPattern.Substring(_matchPattern.LastIndexOf(@"\", StringComparison.OrdinalIgnoreCase) + 1);
                }

                _matchType = MatchType.Wildcard;
            }
            else if (FileUtils.ExistsLong(_matchPattern))
            {
                _matchType = MatchType.File;
            }
            else if (FileUtils.DirectoryExistsLong(_matchPattern))
            {
                _matchType = MatchType.Directory;
            }
            else
            {
                Usage("File or directory does not exist: " + _matchPattern);
                return(false);
            }

            //
            // Command Line: recursion
            //

            // means we're doing directory match
            if (_recurse)
            {
                _matchType = MatchType.Directory;
            }

            //
            // Command Line: XML db
            //
            //  verify we can open it first
            if (String.IsNullOrEmpty(_xmlFileName))
            {
                Usage("Must specify the XML DB file");
                return(false);
            }

            FileStream file;

            if (_action == ProgramAction.Update)
            {
                try
                {
                    // only complain if the file exists but we can't read it
                    if (FileUtils.ExistsLong(_xmlFileName))
                    {
                        file = File.OpenRead(_xmlFileName);
                        file.Close();
                    }
                }
                catch (IOException)
                {
                    Console.WriteLine("Cannot open XML DB: {0}", _xmlFileName);
                    return(false);
                }
            }
            else if (_action == ProgramAction.Verify)
            {
                // complain if the file doesn't exist or we can't read it
                try
                {
                    file = File.OpenRead(_xmlFileName);
                    file.Close();
                }
                catch (IOException)
                {
                    Console.WriteLine("Cannot open XML DB: {0}", _xmlFileName);
                    return(false);
                }
            }

            //
            // -basePath
            //
            _basePath = _basePath.TrimEnd('\\');
            if (!FileUtils.DirectoryExistsLong(_basePath))
            {
                Console.WriteLine("Base path {0} does not exist", _basePath);
                return(false);
            }

            FileInfo basePathInfo = new FileInfo(_basePath);

            _basePath = basePathInfo.FullName;

            return(true);
        }
コード例 #2
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 (!FileUtils.DirectoryExistsLong(_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 (!FileUtils.ExistsLong(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));
        }