// returns true if there was no integrity errors, false if there was and repair was performed
        private bool PerformInternal(PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken)
        {
            int installedVersionId = Context.App.GetInstalledVersionId();

            VersionIntegrity results = CheckIntegrity(cancellationToken, installedVersionId);
            var filesNeedFixing      = FilesNeedFixing(results);

            if (filesNeedFixing.Count() == 0)
            {
                DebugLogger.Log("No missing or invalid size files.");
                return(true);
            }

            // need to collect some data about the application to calculate the repair cost and make decisions

            int latestVersionId = Context.App.GetLatestVersionId(true, cancellationToken);

            AppContentSummary installedVersionContentSummary
                = Context.App.RemoteMetaData.GetContentSummary(installedVersionId, cancellationToken);

            AppContentSummary latestVersionContentSummary
                = Context.App.RemoteMetaData.GetContentSummary(latestVersionId, cancellationToken);

            bool isNewVersionAvailable = installedVersionId < latestVersionId;

            long contentSize = isNewVersionAvailable
                ? latestVersionContentSummary.Files.Sum(f => f.Size)
                : installedVersionContentSummary.Files.Sum(f => f.Size);

            double repairCost = CalculateRepairCost(installedVersionContentSummary, filesNeedFixing);

            // increasing repair costs that reinstallation will be done for 1/3 of the content size
            repairCost *= IncreaseRepairCost;


            if (_lowestVersionWithContentId > installedVersionId)
            {
                DebugLogger.Log(
                    "Repair is impossible because lowest version with content id is "
                    + _lowestVersionWithContentId +
                    " and currently installed version id is "
                    + installedVersionId +
                    ". Reinstalling.");

                ReinstallContent(cancellationToken);
            }
            else if (repairCost < contentSize)
            {
                DebugLogger.Log(string.Format("Repair cost {0} is smaller than content cost {1}, repairing...", repairCost, contentSize));
                IAppUpdaterStrategy repairStrategy = _strategyResolver.Create(StrategyType.Repair, Context);
                repairStrategy.Update(cancellationToken);
            }
            else
            {
                DebugLogger.Log(string.Format("Content cost {0} is smaller than repair {1}. Reinstalling.", contentSize, repairCost));
                ReinstallContent(cancellationToken);
            }

            return(false);
        }
Exemplo n.º 2
0
        public CheckDiskSpaceCommand(AppContentSummary contentSummary, string localDirectoryPath)
        {
            Checks.ArgumentNotNull(localDirectoryPath, "localDirectoryPath");

            _contentSummary     = contentSummary;
            _localDirectoryPath = localDirectoryPath;
        }
Exemplo n.º 3
0
        public override void Prepare([NotNull] UpdaterStatus status, CancellationToken cancellationToken)
        {
            if (status == null)
            {
                throw new ArgumentNullException("status");
            }

            try
            {
                _logger.LogDebug("Preparing diff installation...");

                base.Prepare(status, cancellationToken);

                _localData.PrepareForWriting();

                _previousContentSummary = _remoteMetaData.GetContentSummary(_versionId - 1, cancellationToken);
                _contentSummary         = _remoteMetaData.GetContentSummary(_versionId, cancellationToken);
                _diffSummary            = _remoteMetaData.GetDiffSummary(_versionId, cancellationToken);

                double unarchivePackageWeight = StatusWeightHelper.GetUnarchivePackageWeight(_diffSummary.Size);
                _logger.LogTrace("unarchivePackageWeight = " + unarchivePackageWeight);
                _unarchivePackageStatusReporter = new OperationStatus
                {
                    Weight = { Value = unarchivePackageWeight }
                };
                status.RegisterOperation(_unarchivePackageStatusReporter);

                double addFilesWeight = StatusWeightHelper.GetAddDiffFilesWeight(_diffSummary);
                _logger.LogTrace("addFilesWeight = " + addFilesWeight);
                _addFilesStatusReporter = new OperationStatus
                {
                    Weight = { Value = addFilesWeight }
                };
                status.RegisterOperation(_addFilesStatusReporter);

                double modifiedFilesWeight = StatusWeightHelper.GetModifyDiffFilesWeight(_diffSummary);
                _logger.LogTrace("modifiedFilesWeight = " + modifiedFilesWeight);
                _modifiedFilesStatusReporter = new OperationStatus
                {
                    Weight = { Value = modifiedFilesWeight }
                };
                status.RegisterOperation(_modifiedFilesStatusReporter);

                double removeFilesWeight = StatusWeightHelper.GetRemoveDiffFilesWeight(_diffSummary);
                _logger.LogTrace("removeFilesWeight = " + removeFilesWeight);
                _removeFilesStatusReporter = new OperationStatus
                {
                    Weight = { Value = removeFilesWeight }
                };
                status.RegisterOperation(_removeFilesStatusReporter);

                _logger.LogDebug("Diff installation prepared.");
            }
            catch (Exception e)
            {
                _logger.LogError("Failed to prepare diff installation.", e);
                throw;
            }
        }
        public CheckVersionIntegrityCommand(int versionId, AppContentSummary versionSummary, ILocalDirectory localDirectory, ILocalMetaData localMetaData)
        {
            Checks.ArgumentValidVersionId(versionId, "versionId");
            // TODO: Validate the content summary.
            Checks.ArgumentNotNull(localDirectory, "localDirectory");
            Checks.ArgumentNotNull(localMetaData, "localMetaData");


            DebugLogger.LogConstructor();
            DebugLogger.LogVariable(versionId, "versionId");

            _versionId      = versionId;
            _versionSummary = versionSummary;
            _localDirectory = localDirectory;
            _localMetaData  = localMetaData;
        }
        public CheckVersionIntegrityCommand(int versionId, AppContentSummary versionSummary,
                                            ILocalDirectory localDirectory, ILocalMetaData localMetaData, bool isCheckingHash, bool isCheckingSize)
        {
            Checks.ArgumentValidVersionId(versionId, "versionId");
            Checks.ArgumentNotNull(versionSummary, "versionSummary");
            Checks.ArgumentNotNull(localDirectory, "localDirectory");
            Checks.ArgumentNotNull(localMetaData, "localMetaData");

            DebugLogger.LogConstructor();
            DebugLogger.LogVariable(versionId, "versionId");

            _versionId      = versionId;
            _versionSummary = versionSummary;
            _localDirectory = localDirectory;
            _localMetaData  = localMetaData;
            _isCheckingSize = isCheckingSize;
            _isCheckingHash = isCheckingHash;
        }
        public InstallContentCommand(string packagePath, string packageMetaPath, string packagePassword, int versionId,
                                     AppContentSummary versionContentSummary, ILocalDirectory localData, ILocalMetaData localMetaData)
        {
            Checks.ArgumentValidVersionId(versionId, "versionId");
            // TODO: Validate the content summary.
            Checks.ArgumentNotNull(localData, "localData");
            Checks.ArgumentNotNull(localMetaData, "localMetaData");

            DebugLogger.LogConstructor();
            DebugLogger.LogVariable(packagePath, "packagePath");
            DebugLogger.LogVariable(versionId, "versionId");

            _packagePath           = packagePath;
            _packageMetaPath       = packageMetaPath;
            _packagePassword       = packagePassword;
            _versionId             = versionId;
            _versionContentSummary = versionContentSummary;
            _localData             = localData;
            _localMetaData         = localMetaData;
        }
        public ICheckDiskSpace CreateCheckDiskSpaceCommandForContent(int versionId, AppUpdaterContext context)
        {
            AppContentSummary contentSummary = context.App.RemoteMetaData.GetContentSummary(versionId);

            return(new CheckDiskSpaceCommand(contentSummary, context.App.LocalDirectory.Path));
        }
 public static double GetCopyContentFilesWeight(AppContentSummary summary)
 {
     return(BytesToWeight(summary.Size) * 0.01);
 }
 public static double GetCheckVersionIntegrityWeight(AppContentSummary summary)
 {
     return(BytesToWeight(summary.Size) * 0.05);
 }
Exemplo n.º 10
0
 private long CalculateRepairCost(AppContentSummary contentSummary, IEnumerable <FileIntegrity> filesToRepair)
 {
     return(filesToRepair
            .Select(f => contentSummary.Files.FirstOrDefault(e => e.Path == f.FileName))
            .Sum(f => f.Size));
 }
        private void PreUpdate(PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken)
        {
            DebugLogger.Log("Pre update integrity check");

            var commandFactory = new AppUpdaterCommandFactory();

            int installedVersionId         = Context.App.GetInstalledVersionId();
            int latestVersionId            = Context.App.GetLatestVersionId();
            int lowestVersionWithContentId = Context.App.GetLowestVersionWithContentId();

            if (lowestVersionWithContentId > installedVersionId)
            {
                DebugLogger.Log(
                    "Repair is impossible because lowest version with content id is "
                    + lowestVersionWithContentId +
                    " and currently installed version id is "
                    + installedVersionId +
                    ". Uninstalling to prepare for content strategy.");

                IUninstallCommand uninstall = commandFactory.CreateUninstallCommand(Context);
                uninstall.Prepare(_status);
                uninstall.Execute(cancellationToken);
                return;
            }

            AppContentSummary installedVersionContentSummary
                = Context.App.RemoteMetaData.GetContentSummary(installedVersionId);

            AppContentSummary latestVersionContentSummary
                = Context.App.RemoteMetaData.GetContentSummary(latestVersionId);

            bool isNewVersionAvailable = installedVersionId < latestVersionId;

            long contentSize = isNewVersionAvailable
                ? latestVersionContentSummary.Size
                : installedVersionContentSummary.Size;

            ICheckVersionIntegrityCommand checkIntegrity = commandFactory
                                                           .CreateCheckVersionIntegrityCommand(
                versionId: installedVersionId,
                context: Context,
                isCheckingHash: false,
                isCheckingSize: true);

            checkIntegrity.Prepare(_status);
            checkIntegrity.Execute(cancellationToken);

            var missingFiles = checkIntegrity.Results.Files
                               .Where(f => f.Status == FileIntegrityStatus.MissingData);

            int missingFilesCount = missingFiles.Count();

            var invalidSizeFiles = checkIntegrity.Results.Files
                                   .Where(f => f.Status == FileIntegrityStatus.InvalidSize);

            int invalidSizeFilesCount = invalidSizeFiles.Count();

            if (missingFilesCount + invalidSizeFilesCount == 0)
            {
                DebugLogger.Log("No missing or invalid size files.");
                return;
            }

            double repairCost = CalculateRepairCost(installedVersionContentSummary, missingFiles.Concat(invalidSizeFiles));

            if (repairCost < contentSize)
            {
                DebugLogger.Log(string.Format("Repair cost {0} is smaller than content cost {1}, repairing...", repairCost, contentSize));
                IAppUpdaterStrategy repairStrategy = _strategyResolver.Create(StrategyType.Repair, Context);
                repairStrategy.Update(cancellationToken);
            }
            else
            {
                DebugLogger.Log("Content cost is smaller than repair. Uninstalling to prepare for content strategy.");
                IUninstallCommand uninstall = commandFactory.CreateUninstallCommand(Context);
                uninstall.Prepare(_status);
                uninstall.Execute(cancellationToken);
            }
        }