protected override void ExecuteInternal(CmdExecutionContext context, CancellationToken token)
        {
            InitializeProgress();

            String sourceFullPath = Path.Combine(context.InstallerTargetDirectory, Command.SourcePath);

            if (!File.Exists(sourceFullPath))
            {
                throw new CmdExecutionFailedException("A file given by the sourcePath doesn't exist.",
                                                      String.Format(Properties.Resources.CopyFileErrorParamMsg, Command.SourcePath));
            }

            String targetFullPath = Path.Combine(context.InstallerTargetDirectory, Command.TargetPath);

            if (!PathUtilities.IsValidPath(targetFullPath))
            {
                throw new CmdExecutionFailedException("A given targetPath is not a valid path.",
                                                      String.Format(Properties.Resources.CopyFileErrorParamMsg, Command.SourcePath));
            }

            PathUtilities.CreateDirectoryIfNotExist(Path.GetDirectoryName(targetFullPath));

            FileUtilities.CopyFileEx(sourceFullPath, targetFullPath, token);

            SetMaxProgress();
        }
Beispiel #2
0
        /// <summary>
        /// Creates a backup directory for the given file basing on the free space required to backup the given file.
        /// </summary>
        /// <param name="file"></param>
        /// <exception cref="System.IO.IOException"></exception>
        /// <returns></returns>
        protected String CreateBackupDirForFile(String file)
        {
            long fileSize = GetSizeInBytes(file);

            var tempDirPath  = Path.GetTempPath();
            var tempDirDrive = new DriveInfo(tempDirPath);

            if (tempDirDrive.AvailableFreeSpace > fileSize)
            {
                var backupDirPath = Path.Combine(tempDirPath, backupStoreName);

                PathUtilities.CreateDirectoryIfNotExist(backupDirPath);
                //Create path if not exist
                return(backupDirPath);
            }
            else
            {
                var fixedDrives = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Fixed);
                foreach (var drive in fixedDrives)
                {
                    if (drive.AvailableFreeSpace > fileSize)
                    {
                        var backupDirPath = Path.Combine(drive.Name, backupStoreName);
                        //Create path if not exist
                        PathUtilities.CreateDirectoryIfNotExist(backupDirPath);
                        var backupDir = new DirectoryInfo(backupDirPath);
                        //backupDir.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
                        //hidden //<---------------------------------------------------------------------------------------

                        return(backupDirPath);
                    }
                }
            }

            throw new IOException("Not enough disk space for the backup");
        }
        //Ten async i await przed Task.Run mo¿na wyrzuciæ, ale nie wiem jak to wp³ynie na mozliwosci, ponoæ tak lepiej dla wydajnoœci
        private async Task RunInstallAsync()
        {
            await Task.Run(() =>
            {
                CurrentMessage = WargameModInstaller.Properties.Resources.InstallerInitializing;

                //Czy to powinno byæ w task, czy wy¿ej...
                PathUtilities.CreateDirectoryIfNotExist(InstallLocation);

                var configFilePath = ConfigFileLocator.GetConfigFilePath();

                IEnumerable <ICmdGroup> commandGroups = null;
                if (ComponentsToInstall != null)
                {
                    commandGroups = installCommandsReader.ReadGroups(configFilePath, ComponentsToInstall);
                }
                else
                {
                    commandGroups = installCommandsReader.ReadGroups(configFilePath);
                }
                var commandGroupsExecutors = CreateCommandGroupsExecutors(commandGroups);

                var progressProvidingExecutors = GetProgressProvidingExecutors(commandGroupsExecutors);
                RegisterProgressProviders(progressProvidingExecutors);

                if (isBackupEnabled)
                {
                    //Zapamietac liste dla iteracyjnego restore?
                    var backupTargetsList = commandGroups
                                            .SelectMany(group => group.Commands)
                                            .OfType <IHasTarget>()
                                            .Select(c => c.TargetPath);

                    //The InstallerService takes control over the backup porogress notification.
                    //Tak wogole to total steps powinno byæ obliczone przed rozpoczeciem operacji zwiekszajacych progrss, zeby unikn¹æ skakania paska progressu.
                    TotalSteps     = backupTargetsList.Count();
                    CurrentStep    = 0;
                    CurrentMessage = WargameModInstaller.Properties.Resources.InstallerBackupingFiles;

                    foreach (var backupTarget in backupTargetsList)
                    {
                        var fileToBackupPath = Path.Combine(InstallLocation, backupTarget);
                        if (File.Exists(fileToBackupPath))
                        {
                            backupService.Backup(fileToBackupPath, cancellationTokenSource.Token);
                            backupedFiles.Add(fileToBackupPath);
                        }

                        CurrentStep++;

                        //Check for the cancellation
                        cancellationTokenSource.Token.ThrowIfCancellationRequested();
                    }

                    //From now, if the installation is interrupted, the installer restores backuped files.
                    hasBackupCompleted = true;
                }

                var executionContext = CreateCmdExecutionContext();
                foreach (var executor in commandGroupsExecutors)
                {
                    executor.Execute(executionContext, cancellationTokenSource.Token);

                    //Check for the cancellation
                    cancellationTokenSource.Token.ThrowIfCancellationRequested();
                }

                progressManager.SetProgressMax();
            }, cancellationTokenSource.Token);
        }