コード例 #1
0
        private void InstallFile(SourceFile sourceFile, CancellationToken cancellationToken)
        {
            DebugLogger.Log(string.Format("Installing file {0}", sourceFile.Name));

            if (!sourceFile.Exists())
            {
                throw new InstallerException(string.Format("Cannot find file {0} in content package.", sourceFile.Name));
            }

            string destinationFilePath = _localData.Path.PathCombine(sourceFile.Name);

            DirectoryOperations.CreateParentDirectory(destinationFilePath, cancellationToken);

            if (File.Exists(destinationFilePath))
            {
                DebugLogger.LogFormat("Destination file {0} already exists, removing it.", destinationFilePath);
                FileOperations.Delete(destinationFilePath, cancellationToken);
            }

            FileOperations.Move(sourceFile.FullPath, destinationFilePath, cancellationToken);
            _localMetaData.RegisterEntry(sourceFile.Name, _versionId);
        }
コード例 #2
0
        public override void Execute(CancellationToken cancellationToken)
        {
            base.Execute(cancellationToken);

            Checks.FileExists(_packagePath);
            Assert.IsTrue(_localMetaData.GetRegisteredEntries().Length == 0,
                          "Cannot install content if previous version is still present.");

            if (_versionContentSummary.CompressionMethod == "pack1")
            {
                Assert.IsTrue(File.Exists(_packageMetaPath),
                              "Compression method is pack1, but meta file does not exist");

                DebugLogger.Log("Parsing package meta file");
                _pack1Meta = Pack1Meta.ParseFromFile(_packageMetaPath);
                DebugLogger.Log("Package meta file parsed succesfully");
            }

            DebugLogger.Log("Installing content.");

            TemporaryDirectory.ExecuteIn(_packagePath + ".temp_unpack_" + Path.GetRandomFileName(), (packageDir) => {
                DebugLogger.LogVariable(packageDir.Path, "packageDirPath");

                DebugLogger.Log("Unarchiving package.");

                string usedSuffix;
                IUnarchiver unarchiver = CreateUnrachiver(packageDir.Path, out usedSuffix);

                _unarchivePackageStatus.IsActive.Value    = true;
                _unarchivePackageStatus.Description.Value = "Unarchiving package...";
                _unarchivePackageStatus.Progress.Value    = 0.0;

                unarchiver.UnarchiveProgressChanged += (name, isFile, entry, amount, entryProgress) =>
                {
                    var entryMinProgress = (entry - 1) / (double)amount;
                    var entryMaxProgress = entry / (double)amount;

                    _unarchivePackageStatus.Progress.Value = entryMinProgress + (entryMaxProgress - entryMinProgress) * entryProgress;

                    _unarchivePackageStatus.Description.Value = string.Format("Unarchiving package ({0}/{1})...", entry, amount);
                };

                // Allow to unpack with errors. This allows to install content even on corrupted hard drives, and attempt to fix these later
                unarchiver.ContinueOnError = true;

                unarchiver.Unarchive(cancellationToken);
                NeedRepair = unarchiver.HasErrors;

                _unarchivePackageStatus.Progress.Value = 1.0;
                _unarchivePackageStatus.IsActive.Value = false;

                DebugLogger.Log("Moving files.");

                _copyFilesStatus.IsActive.Value    = true;
                _copyFilesStatus.Description.Value = "Installing...";
                _copyFilesStatus.Progress.Value    = 0.0;

                for (int i = 0; i < _versionContentSummary.Files.Length; i++)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var sourceFile = new SourceFile(_versionContentSummary.Files[i].Path, packageDir.Path, usedSuffix);

                    if (unarchiver.HasErrors && !sourceFile.Exists()) // allow unexistent file only if does not have errors
                    {
                        DebugLogger.LogWarning("Skipping unexisting file because I've been expecting unpacking errors: " + sourceFile.Name);
                    }
                    else
                    {
                        InstallFile(sourceFile, cancellationToken);
                    }

                    _copyFilesStatus.Progress.Value    = (i + 1) / (double)_versionContentSummary.Files.Length;
                    _copyFilesStatus.Description.Value = string.Format("Installing ({0}/{1})...", i + 1, _versionContentSummary.Files.Length);
                }

                _copyFilesStatus.Progress.Value = 1.0;
                _copyFilesStatus.IsActive.Value = false;
            });
        }