Example #1
0
        /// <summary>
        /// Runs the verifier
        /// </summary>
        public static bool Run(Args args)
        {
            Contract.Requires(args != null);
            Contract.Requires(!args.HelpRequested);

            Contract.Assume(args.FileContentTablePath != null); // Should be provable from the invariant on Args

            DateTime now = DateTime.UtcNow;

            using (ConfigureConsoleLogging(now))
            {
                var          pt = new PathTable();
                AbsolutePath path;
                Contract.Assert(args.FileContentTablePath != null, "Implied when help was not requested");
                if (!AbsolutePath.TryCreate(pt, Path.GetFullPath(args.FileContentTablePath), out path))
                {
                    Console.Error.WriteLine(Resources.Bad_table_path, args.FileContentTablePath);
                    return(false);
                }

                Console.WriteLine(Resources.Verifying_path, path.ToString(pt));

                FileContentTable tableToVerify = TryLoadFileContentTable(pt, path).Result;
                if (tableToVerify == null)
                {
                    // Note the error has already been logged via TryLoadFileContentTable
                    return(false);
                }

                if (!FileContentTableAccessorFactory.TryCreate(out IFileContentTableAccessor accessor, out string error))
                {
                    Console.Error.WriteLine(error);
                    return(false);
                }

                Stopwatch sw = Stopwatch.StartNew();
                List <FileContentTableDiagnosticExtensions.IncorrectFileContentEntry> incorrectEntries = null;

                using (accessor)
                {
                    incorrectEntries = tableToVerify.FindIncorrectEntries(accessor);
                }

                sw.Stop();

                foreach (FileContentTableDiagnosticExtensions.IncorrectFileContentEntry incorrectEntry in incorrectEntries)
                {
                    Console.Error.WriteLine(
                        Resources.Incorrect_entry,
                        incorrectEntry.Path,
                        incorrectEntry.ExpectedHash.ToHex(),
                        incorrectEntry.ActualHash.ToHex(),
                        incorrectEntry.Usn);
                }

                Console.WriteLine(Resources.Verification_summary, tableToVerify.Count, incorrectEntries.Count, sw.Elapsed);

                return(incorrectEntries.Count == 0);
            }
        }
        private static void VerifyTable(FileContentTable table)
        {
            List <FileContentTableDiagnosticExtensions.IncorrectFileContentEntry> incorrect = null;

            XAssert.IsTrue(FileContentTableAccessorFactory.TryCreate(out IFileContentTableAccessor accessor, out string error), error);

            using (accessor)
            {
                XAssert.IsNotNull(accessor);
                incorrect = table.FindIncorrectEntries(accessor);
            }

            if (incorrect.Count > 0)
            {
                string incorrectSummary = string.Join(
                    Environment.NewLine,
                    incorrect.Select(
                        i => string.Format("\tPath {0} (expected {1} ; actual {2})", i.Path, i.ExpectedHash.ToHex(), i.ActualHash.ToHex())));
                XAssert.Fail("Found incorrect file content table entires in post-validation:\n{0}", incorrectSummary);
            }
        }