Beispiel #1
0
        public InstallStatus GetInstallStatus()
        {
            var fileNames = LocalMetaData.GetRegisteredEntries();

            if (fileNames.Length == 0)
            {
                return(InstallStatus.NotInstalled);
            }

            int installedVersion = LocalMetaData.GetEntryVersionId(fileNames[0]);

            foreach (string fileName in fileNames)
            {
                string path = LocalDirectory.Path.PathCombine(fileName);
                if (!File.Exists(path))
                {
                    DebugLogger.LogWarning("File in metadata, but not found on disk: " + fileName + ", search path: " +
                                           path);
                    return(InstallStatus.Broken);
                }

                int fileVersion = LocalMetaData.GetEntryVersionId(fileName);
                if (fileVersion != installedVersion)
                {
                    DebugLogger.LogWarning("File " + fileName + " installed version is " + fileVersion +
                                           " but expected " + installedVersion);
                    return(InstallStatus.Broken);
                }
            }

            return(InstallStatus.Installed);
        }
        private FileIntegrity CheckFile(AppContentSummaryFile file)
        {
            if (!File.Exists(_localDirectory.Path.PathCombine(file.Path)))
            {
                return(new FileIntegrity(file.Path, FileIntegrityStatus.MissingData));
            }

            if (!_localMetaData.IsEntryRegistered(file.Path))
            {
                return(new FileIntegrity(file.Path, FileIntegrityStatus.MissingMetaData));
            }

            if (_localMetaData.GetEntryVersionId(file.Path) != _versionId)
            {
                return(new FileIntegrity(file.Path, FileIntegrityStatus.InvalidVersion));
            }

            string hash = HashCalculator.ComputeFileHash(_localDirectory.Path.PathCombine(file.Path));

            if (hash != file.Hash)
            {
                return(new FileIntegrity(file.Path, FileIntegrityStatus.InvalidHash));
            }

            // TODO: Check file size (always).

            return(new FileIntegrity(file.Path, FileIntegrityStatus.Ok));
        }
        private FileIntegrity CheckFile(AppContentSummaryFile file)
        {
            Action onVerificationFailed = () =>
            {
                PatcherStatistics.DispatchSendEvent(PatcherStatistics.Event.FileVerificationFailed, new PatcherStatistics.OptionalParams
                {
                    FileName = file.Path,
                    Size     = file.Size,
                });
            };

            string localPath = _localDirectory.Path.PathCombine(file.Path);

            if (!File.Exists(localPath))
            {
                onVerificationFailed();
                return(new FileIntegrity(file.Path, FileIntegrityStatus.MissingData));
            }

            if (!_localMetaData.IsEntryRegistered(file.Path))
            {
                onVerificationFailed();
                return(new FileIntegrity(file.Path, FileIntegrityStatus.MissingMetaData));
            }

            int actualVersionId = _localMetaData.GetEntryVersionId(file.Path);

            if (actualVersionId != _versionId)
            {
                onVerificationFailed();
                return(FileIntegrity.InvalidVersion(_versionId, actualVersionId, file.Path));
            }

            if (_isCheckingSize)
            {
                long actualSize = new FileInfo(localPath).Length;
                if (actualSize != file.Size)
                {
                    onVerificationFailed();
                    return(FileIntegrity.InvalidSize(file.Size, actualSize, file.Path));
                }
            }

            if (_isCheckingHash)
            {
                string actualFileHash = HashCalculator.ComputeFileHash(localPath);
                if (actualFileHash != file.Hash)
                {
                    onVerificationFailed();
                    return(FileIntegrity.InvalidHash(file.Hash, actualFileHash, file.Path));
                }
            }

            return(new FileIntegrity(file.Path, FileIntegrityStatus.Ok));
        }
Beispiel #4
0
        private void PatchFile(string fileName, string packageDirPath)
        {
            string filePath = _localData.Path.PathCombine(fileName);

            if (!File.Exists(filePath))
            {
                throw new InstallerException(string.Format("Couldn't patch file <{0}> - file doesn't exists.", fileName));
            }
            if (!_localMetaData.IsEntryRegistered(fileName))
            {
                throw new InstallerException(string.Format("Couldn't patch file <{0}> - file is not registered in meta data.", fileName));
            }

            int fileVersion = _localMetaData.GetEntryVersionId(fileName);

            if (fileVersion != _versionId - 1)
            {
                throw new InstallerException(string.Format(
                                                 "Couldn't patch file <{0}> - expected file with previous version ({1}) but the file version is {2}.",
                                                 fileName, _versionId - 1, fileVersion));
            }

            string newFilePath = _temporaryData.GetUniquePath();

            try
            {
                var filePatcher = new FilePatcher(filePath,
                                                  Path.Combine(packageDirPath, fileName), newFilePath);
                filePatcher.Patch();

                FileOperations.Copy(newFilePath, filePath, true);
            }
            finally
            {
                if (File.Exists(newFilePath))
                {
                    FileOperations.Delete(newFilePath);
                }
            }
        }
        private FileIntegrity CheckFile(AppContentSummaryFile file)
        {
            string localPath = _localDirectory.Path.PathCombine(file.Path);

            if (!File.Exists(localPath))
            {
                return(new FileIntegrity(file.Path, FileIntegrityStatus.MissingData));
            }

            if (!_localMetaData.IsEntryRegistered(file.Path))
            {
                return(new FileIntegrity(file.Path, FileIntegrityStatus.MissingMetaData));
            }

            if (_localMetaData.GetEntryVersionId(file.Path) != _versionId)
            {
                return(new FileIntegrity(file.Path, FileIntegrityStatus.InvalidVersion));
            }

            if (_isCheckingSize)
            {
                long size = new FileInfo(localPath).Length;
                if (size != file.Size)
                {
                    return(new FileIntegrity(file.Path, FileIntegrityStatus.InvalidSize));
                }
            }

            if (_isCheckingHash)
            {
                string hash = HashCalculator.ComputeFileHash(localPath);
                if (hash != file.Hash)
                {
                    return(new FileIntegrity(file.Path, FileIntegrityStatus.InvalidHash));
                }
            }

            return(new FileIntegrity(file.Path, FileIntegrityStatus.Ok));
        }
Beispiel #6
0
        private void PatchFile(
            string fileName, string packageDirPath, string suffix,
            TemporaryDirectory tempDiffDir, CancellationToken cancellationToken)
        {
            _logger.LogDebug(string.Format("Processing patch file entry {0}", fileName));

            var filePath = _localData.Path.PathCombine(fileName);

            _logger.LogTrace("filePath = " + filePath);

            if (!File.Exists(filePath))
            {
                throw new MissingLocalDataFileException(
                          string.Format("Couldn't patch file {0} because it doesn't exists in local data.", fileName));
            }

            var fileVersion = _localMetaData.GetEntryVersionId(fileName);

            _logger.LogTrace("fileVersion = " + fileVersion);

            if (fileVersion != _versionId - 1)
            {
                throw new InvalidLocalDataFileVersionException(string.Format(
                                                                   "Couldn't patch file {0} because expected file version to be ({1}) but it's {2}.",
                                                                   fileName, _versionId - 1, fileVersion));
            }

            _logger.LogDebug("Checking whether patching file content is necessary...");
            if (IsPatchingFileContentNecessary(fileName))
            {
                _logger.LogDebug("Patching is necessary. Generating new file with patched content...");

                var sourceDeltaFilePath = Path.Combine(packageDirPath, fileName + suffix);
                _logger.LogTrace("sourceDeltaFilePath = " + sourceDeltaFilePath);

                if (!File.Exists(sourceDeltaFilePath))
                {
                    throw new MissingFileFromPackageException(string.Format("Cannot find delta file {0} in diff package.",
                                                                            fileName));
                }

                var newFilePath = tempDiffDir.GetUniquePath();
                _logger.LogTrace("newFilePath = " + newFilePath);

                var filePatcher = new FilePatcher(filePath, sourceDeltaFilePath, newFilePath);
                filePatcher.Patch();

                _logger.LogDebug("New file generated. Deleting old file in local data...");
                FileOperations.Delete(filePath, cancellationToken);

                _logger.LogDebug("Old file deleted. Moving new file to local data...");
                FileOperations.Move(newFilePath, filePath, cancellationToken);

                _logger.LogDebug("New file moved.");
            }
            else
            {
                _logger.LogDebug("Patching is not necessary. File content is the same as in previous version.");
            }

            _localMetaData.RegisterEntry(fileName, _versionId);

            _logger.LogDebug("Patch file entry processed.");
        }