public override void Context()
            {
                CommandExecutor.initialize_with(new Lazy <IFileSystem>(() => fileSystem.Object), () => process.Object);

                service = new AutomaticUninstallerService(packageInfoService.Object, fileSystem.Object, registryService.Object, commandExecutor.Object);
                service.WaitForCleanup          = false;
                config.Features.AutoUninstaller = true;
                config.PromptForConfirmation    = false;
                package.Setup(p => p.Id).Returns("regular");
                package.Setup(p => p.Version).Returns(new SemanticVersion("1.2.0"));
                packageResult      = new PackageResult(package.Object, null);
                packageInformation = new ChocolateyPackageInformation(package.Object);
                registryKeys.Add(
                    new RegistryApplicationKey
                {
                    DisplayName       = expectedDisplayName,
                    InstallLocation   = @"C:\Program Files (x86)\WinDirStat",
                    UninstallString   = originalUninstallString,
                    HasQuietUninstall = true,
                    KeyPath           = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WinDirStat",
                    InstallerType     = installerType.InstallerType,
                });
                packageInformation.RegistrySnapshot = new Registry("123", registryKeys);
                packageInfoService.Setup(s => s.get_package_information(package.Object)).Returns(packageInformation);
                packageResults.GetOrAdd("regular", packageResult);

                fileSystem.Setup(f => f.directory_exists(registryKeys.FirstOrDefault().InstallLocation)).Returns(true);
                registryService.Setup(r => r.installer_value_exists(registryKeys.FirstOrDefault().KeyPath, ApplicationParameters.RegistryValueInstallLocation)).Returns(true);
                fileSystem.Setup(f => f.get_full_path(expectedUninstallString)).Returns(expectedUninstallString);
                fileSystem.Setup(x => x.file_exists(expectedUninstallString)).Returns(true);

                var field = typeof(ApplicationParameters).GetField("AllowPrompts");

                field.SetValue(null, false);
            }
        public ChocolateyPackageInformation get_package_information(IPackage package)
        {
            var packageInformation = new ChocolateyPackageInformation(package);

            if (package == null)
            {
                this.Log().Debug("No package information as package is null.");
                return(packageInformation);
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));

            if (!_fileSystem.directory_exists(pkgStorePath))
            {
                return(packageInformation);
            }

            string registrySnapshotFile = _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE);

            if (_fileSystem.file_exists(registrySnapshotFile))
            {
                packageInformation.RegistrySnapshot = _registryService.read_from_file(registrySnapshotFile);
            }

            packageInformation.HasSilentUninstall = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE));
            packageInformation.IsSideBySide       = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            packageInformation.IsPinned           = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));

            return(packageInformation);
        }
        public void save_package_information(ChocolateyPackageInformation packageInformation)
        {
            _fileSystem.create_directory_if_not_exists(ApplicationParameters.ChocolateyPackageInfoStoreLocation);
            _fileSystem.ensure_file_attribute_set(ApplicationParameters.ChocolateyPackageInfoStoreLocation, FileAttributes.Hidden);

            if (packageInformation.Package == null)
            {
                this.Log().Debug("No package information to save as package is null.");
                return;
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(packageInformation.Package.Id, packageInformation.Package.Version.to_string()));

            _fileSystem.create_directory_if_not_exists(pkgStorePath);

            if (packageInformation.RegistrySnapshot != null)
            {
                _registryService.save_to_file(packageInformation.RegistrySnapshot, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
            }

            if (packageInformation.FilesSnapshot != null)
            {
                _filesService.save_to_file(packageInformation.FilesSnapshot, _fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
            }

            if (!string.IsNullOrWhiteSpace(packageInformation.Arguments))
            {
                var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);
                if (_fileSystem.file_exists(argsFile))
                {
                    _fileSystem.delete_file(argsFile);
                }
                _fileSystem.write_file(argsFile, packageInformation.Arguments);
            }

            if (packageInformation.HasSilentUninstall)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE), string.Empty, Encoding.ASCII);
            }
            if (packageInformation.IsSideBySide)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE), string.Empty, Encoding.ASCII);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            }

            if (packageInformation.IsPinned)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE), string.Empty, Encoding.ASCII);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            }
        }
Example #4
0
 public override void Context()
 {
     base.Context();
     package.Setup(x => x.Id).Returns("bob");
     packageInfo = new ChocolateyPackageInformation(package.Object);
     packageInfo.FilesSnapshot = new PackageFiles();
     packageFiles = new PackageFiles();
     fileSystem.Setup(x => x.directory_exists(It.IsAny <string>())).Returns(true);
 }
Example #5
0
        /// <summary>
        /// Gets package installed location
        /// </summary>
        public static string GetPackageLocation(this IPackage package, ChocolateyPackageInformation pkgInfo = null)
        {
            if (package is RegistryPackage regp && regp.RegistryKey != null)
            {
                return(regp.RegistryKey.InstallLocation);
            }

            var isSideBySide = pkgInfo != null && pkgInfo.IsSideBySide;
            var installDir   = Path.Combine(InstallContext.Instance.PackagesLocation,
                                            "{0}{1}".format_with(package.Id, isSideBySide ? "." + package.Version.to_string() : string.Empty));

            return(installDir);
        }
        public ChocolateyPackageInformation get_package_information(IPackage package)
        {
            var packageInformation = new ChocolateyPackageInformation(package);

            if (package == null)
            {
                this.Log().Debug("No package information as package is null.");
                return(packageInformation);
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));

            if (!_fileSystem.directory_exists(pkgStorePath))
            {
                return(packageInformation);
            }

            FaultTolerance.try_catch_with_logging_exception(
                () =>
            {
                packageInformation.RegistrySnapshot = _registryService.read_from_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
            },
                "Unable to read registry snapshot file for {0} (located at {1})".format_with(package.Id, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE)),
                throwError: false,
                logWarningInsteadOfError: true
                );

            FaultTolerance.try_catch_with_logging_exception(
                () =>
            {
                packageInformation.FilesSnapshot = _filesService.read_from_file(_fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
            },
                "Unable to read files snapshot file",
                throwError: false,
                logWarningInsteadOfError: true
                );

            packageInformation.HasSilentUninstall = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE));
            packageInformation.IsSideBySide       = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            packageInformation.IsPinned           = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);

            if (_fileSystem.file_exists(argsFile))
            {
                packageInformation.Arguments = _fileSystem.read_file(argsFile);
            }

            return(packageInformation);
        }
        public void save_package_information(ChocolateyPackageInformation packageInformation)
        {
            _fileSystem.create_directory_if_not_exists(ApplicationParameters.ChocolateyPackageInfoStoreLocation);
            _fileSystem.ensure_file_attribute_set(ApplicationParameters.ChocolateyPackageInfoStoreLocation, FileAttributes.Hidden);

            if (packageInformation.Package == null)
            {
                this.Log().Debug("No package information to save as package is null.");
                return;
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(packageInformation.Package.Id, packageInformation.Package.Version.to_string()));
            _fileSystem.create_directory_if_not_exists(pkgStorePath);

            if (packageInformation.RegistrySnapshot != null)
            {
                _registryService.save_to_file(packageInformation.RegistrySnapshot, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
            }

            if (packageInformation.FilesSnapshot != null)
            {
                _filesService.save_to_file(packageInformation.FilesSnapshot, _fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
            }

            if (packageInformation.HasSilentUninstall)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE), string.Empty, Encoding.ASCII);
            }
            if (packageInformation.IsSideBySide)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE), string.Empty, Encoding.ASCII);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            }

            if (packageInformation.IsPinned)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE), string.Empty, Encoding.ASCII);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            }
        }
        public ChocolateyPackageInformation get_package_information(IPackage package)
        {
            var packageInformation = new ChocolateyPackageInformation(package);
            if (package == null)
            {
                this.Log().Debug("No package information as package is null.");
                return packageInformation;
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));
            if (!_fileSystem.directory_exists(pkgStorePath))
            {
                return packageInformation;
            }

            FaultTolerance.try_catch_with_logging_exception(
                () =>
                    {
                        packageInformation.RegistrySnapshot = _registryService.read_from_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
                    },
                    "Unable to read registry snapshot file for {0} (located at {1})".format_with(package.Id, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE)),
                    throwError: false,
                    logWarningInsteadOfError: true
                 );

            FaultTolerance.try_catch_with_logging_exception(
                () =>
                    {
                        packageInformation.FilesSnapshot = _filesService.read_from_file(_fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
                    },
                    "Unable to read files snapshot file",
                    throwError: false,
                    logWarningInsteadOfError: true
                 );

            packageInformation.HasSilentUninstall = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE));
            packageInformation.IsSideBySide = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            packageInformation.IsPinned = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);
            if (_fileSystem.file_exists(argsFile)) packageInformation.Arguments = _fileSystem.read_file(argsFile);

            return packageInformation;
        }
        public void save_package_information(ChocolateyPackageInformation packageInformation)
        {
            _fileSystem.create_directory_if_not_exists(ApplicationParameters.ChocolateyPackageInfoStoreLocation);
            _fileSystem.ensure_file_attribute_set(ApplicationParameters.ChocolateyPackageInfoStoreLocation, FileAttributes.Hidden);

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(packageInformation.Package.Id, packageInformation.Package.Version.to_string()));

            _fileSystem.create_directory_if_not_exists(pkgStorePath);

            if (packageInformation.RegistrySnapshot != null)
            {
                _registryService.save_to_file(packageInformation.RegistrySnapshot, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
            }

            if (packageInformation.HasSilentUninstall)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE), string.Empty, Encoding.ASCII);
            }
            if (packageInformation.IsSideBySide)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE), string.Empty, Encoding.ASCII);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            }

            if (packageInformation.IsPinned)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE), string.Empty, Encoding.ASCII);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            }
        }
Example #10
0
        public void backup_changed_files(string packageInstallPath, ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
        {
            if (packageInfo == null || packageInfo.Package == null) return;

            var version = packageInfo.Package.Version.to_string();

            if (packageInfo.FilesSnapshot == null || packageInfo.FilesSnapshot.Files.Count == 0)
            {
                var configFiles = _fileSystem.get_files(packageInstallPath, ApplicationParameters.ConfigFileExtensions, SearchOption.AllDirectories);
                foreach (var file in configFiles.or_empty_list_if_null())
                {
                    var backupName = "{0}.{1}".format_with(_fileSystem.get_file_name(file), version);

                    FaultTolerance.try_catch_with_logging_exception(
                        () => _fileSystem.copy_file(file, _fileSystem.combine_paths(_fileSystem.get_directory_name(file), backupName), overwriteExisting: true),
                        "Error backing up configuration file");
                }
            }
            else
            {
                var currentFiles = _filesService.capture_package_files(packageInstallPath, config);
                foreach (var currentFile in currentFiles.Files.or_empty_list_if_null())
                {
                    var installedFile = packageInfo.FilesSnapshot.Files.FirstOrDefault(x => x.Path.is_equal_to(currentFile.Path));
                    if (installedFile != null)
                    {
                        if (!currentFile.Checksum.is_equal_to(installedFile.Checksum))
                        {
                            var backupName = "{0}.{1}".format_with(_fileSystem.get_file_name(currentFile.Path), version);
                            FaultTolerance.try_catch_with_logging_exception(
                                () => _fileSystem.copy_file(currentFile.Path, _fileSystem.combine_paths(_fileSystem.get_directory_name(currentFile.Path), backupName), overwriteExisting: true),
                                "Error backing up changed file");
                        }
                    }
                }
            }
        }
Example #11
0
        public void rename_legacy_package_version(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
        {
            if (pkgInfo != null && pkgInfo.IsSideBySide) return;

            var installDirectory = _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id);
            if (!_fileSystem.directory_exists(installDirectory))
            {
                // if the folder has a version on it, we need to rename the folder first.
                var pathResolver = new ChocolateyPackagePathResolver(NugetCommon.GetNuGetFileSystem(config, _nugetLogger), useSideBySidePaths: true);
                installDirectory = pathResolver.GetInstallPath(installedPackage);
                if (_fileSystem.directory_exists(installDirectory))
                {
                    FaultTolerance.try_catch_with_logging_exception(
                        () => _fileSystem.move_directory(installDirectory, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id)),
                        "Error during old package rename");
                }
            }
        }
Example #12
0
        public void remove_installation_files(IPackage removedPackage, ChocolateyPackageInformation pkgInfo)
        {
            var isSideBySide = pkgInfo != null && pkgInfo.IsSideBySide;
            var installDir = _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, "{0}{1}".format_with(removedPackage.Id, isSideBySide ? "." + removedPackage.Version.to_string() : string.Empty));

            if (_fileSystem.directory_exists(installDir) && pkgInfo != null && pkgInfo.FilesSnapshot != null)
            {
                foreach (var file in _fileSystem.get_files(installDir, "*.*", SearchOption.AllDirectories).or_empty_list_if_null())
                {
                    var fileSnapshot = pkgInfo.FilesSnapshot.Files.FirstOrDefault(f => f.Path.is_equal_to(file));
                    if (fileSnapshot == null) continue;

                    if (fileSnapshot.Checksum == _filesService.get_package_file(file).Checksum)
                    {
                        FaultTolerance.try_catch_with_logging_exception(
                            () => _fileSystem.delete_file(file),
                            "Error deleting file");
                    }

                    if (fileSnapshot.Checksum == ApplicationParameters.HashProviderFileLocked)
                    {
                        this.Log().Warn(()=> "Snapshot for '{0}' was attempted when file was locked.{1} Please inspect and manually remove file{1} at '{2}'".format_with(_fileSystem.get_file_name(file), Environment.NewLine, _fileSystem.get_directory_name(file)));
                    }
                }
            }

            if (_fileSystem.directory_exists(installDir) && !_fileSystem.get_files(installDir, "*.*", SearchOption.AllDirectories).or_empty_list_if_null().Any())
            {
                _fileSystem.delete_directory_if_exists(installDir, recursive: true);
            }
        }
Example #13
0
        public void backup_existing_version(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation packageInfo)
        {
            _fileSystem.create_directory_if_not_exists(ApplicationParameters.PackageBackupLocation);

            var pathResolver = NugetCommon.GetPathResolver(config, NugetCommon.GetNuGetFileSystem(config, _nugetLogger));
            var pkgInstallPath = pathResolver.GetInstallPath(installedPackage);
            if (!_fileSystem.directory_exists(pkgInstallPath))
            {
                var chocoPathResolver = pathResolver as ChocolateyPackagePathResolver;
                if (chocoPathResolver != null)
                {
                    chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;
                    pkgInstallPath = chocoPathResolver.GetInstallPath(installedPackage);
                }
            }

            if (_fileSystem.directory_exists(pkgInstallPath))
            {
                this.Log().Debug("Backing up existing {0} prior to upgrade.".format_with(installedPackage.Id));

                var backupLocation = pkgInstallPath.Replace(ApplicationParameters.PackagesLocation, ApplicationParameters.PackageBackupLocation);

                var errored = false;
                try
                {
                    _fileSystem.move_directory(pkgInstallPath, backupLocation);
                }
                catch (Exception ex)
                {
                    errored = true;
                    this.Log().Error("Error during backup (move phase):{0} {1}".format_with(Environment.NewLine, ex.Message));
                }
                finally
                {
                    try
                    {
                        _fileSystem.copy_directory(backupLocation, pkgInstallPath, overwriteExisting: true);
                    }
                    catch (Exception ex)
                    {
                        errored = true;
                        this.Log().Error("Error during backup (reset phase):{0} {1}".format_with(Environment.NewLine, ex.Message));
                    }
                }

                backup_changed_files(pkgInstallPath, config, packageInfo);

                if (errored)
                {
                    this.Log().Warn(ChocolateyLoggers.Important,
                                    @"There was an error accessing files. This could mean there is a
             process locking the folder or files. Please make sure nothing is
             running that would lock the files or folders in this directory prior
             to upgrade. If the package fails to upgrade, this is likely the cause.");
                }
            }
        }
Example #14
0
        /// <summary>
        /// Remove the shimgen director files from the package.
        /// These are .gui/.ignore files that may have been created during the installation 
        /// process and won't be pulled by the nuget package replacement.
        /// This usually happens when package maintainers haven't been very good about how 
        /// they create the files in the past (not using force with new-item typically throws 
        /// an error if the file exists).
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="installedPackage">The installed package.</param>
        /// <param name="pkgInfo">The package information.</param>
        private void remove_shim_directors(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
        {
            var pathResolver = NugetCommon.GetPathResolver(config, NugetCommon.GetNuGetFileSystem(config, _nugetLogger));
            var pkgInstallPath = pathResolver.GetInstallPath(installedPackage);
            if (!_fileSystem.directory_exists(pkgInstallPath))
            {
                var chocoPathResolver = pathResolver as ChocolateyPackagePathResolver;
                if (chocoPathResolver != null)
                {
                    chocoPathResolver.UseSideBySidePaths = !chocoPathResolver.UseSideBySidePaths;
                    pkgInstallPath = chocoPathResolver.GetInstallPath(installedPackage);
                }
            }

            if (_fileSystem.directory_exists(pkgInstallPath))
            {
                var shimDirectorFiles = _fileSystem.get_files(pkgInstallPath, ApplicationParameters.ShimDirectorFileExtensions, SearchOption.AllDirectories);
                foreach (var file in shimDirectorFiles.or_empty_list_if_null())
                {
                    FaultTolerance.try_catch_with_logging_exception(
                        () => _fileSystem.delete_file(file),
                        "Error deleting shim director file");
                }
            }
        }
Example #15
0
        public void save_package_information(ChocolateyPackageInformation packageInformation)
        {
            _fileSystem.create_directory_if_not_exists(ApplicationParameters.ChocolateyPackageInfoStoreLocation);
            _fileSystem.ensure_file_attribute_set(ApplicationParameters.ChocolateyPackageInfoStoreLocation, FileAttributes.Hidden);

            if (packageInformation.Package == null)
            {
                this.Log().Debug("No package information to save as package is null.");
                return;
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(packageInformation.Package.Id, packageInformation.Package.Version.to_string()));

            _fileSystem.create_directory_if_not_exists(pkgStorePath);

            if (packageInformation.RegistrySnapshot != null)
            {
                _registryService.save_to_file(packageInformation.RegistrySnapshot, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
            }

            if (packageInformation.FilesSnapshot != null)
            {
                FaultTolerance.try_catch_with_logging_exception(
                    () =>
                {
                    _filesService.save_to_file(packageInformation.FilesSnapshot, _fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
                },
                    "Unable to save files snapshot",
                    throwError: false,
                    logWarningInsteadOfError: true
                    );
            }

            if (!string.IsNullOrWhiteSpace(packageInformation.Arguments))
            {
                var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);
                if (_fileSystem.file_exists(argsFile))
                {
                    _fileSystem.delete_file(argsFile);
                }
                _fileSystem.write_file(argsFile, packageInformation.Arguments);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, ARGS_FILE));
            }

            if (!string.IsNullOrWhiteSpace(packageInformation.ExtraInformation))
            {
                var extraFile = _fileSystem.combine_paths(pkgStorePath, EXTRA_FILE);
                if (_fileSystem.file_exists(extraFile))
                {
                    _fileSystem.delete_file(extraFile);
                }
                _fileSystem.write_file(extraFile, packageInformation.ExtraInformation);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, EXTRA_FILE));
            }

            if (packageInformation.VersionOverride != null)
            {
                var versionOverrideFile = _fileSystem.combine_paths(pkgStorePath, VERSION_OVERRIDE_FILE);
                if (_fileSystem.file_exists(versionOverrideFile))
                {
                    _fileSystem.delete_file(versionOverrideFile);
                }
                _fileSystem.write_file(versionOverrideFile, packageInformation.VersionOverride.to_string());
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, VERSION_OVERRIDE_FILE));
            }

            if (packageInformation.HasSilentUninstall)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE), string.Empty, Encoding.ASCII);
            }
            if (packageInformation.IsSideBySide)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE), string.Empty, Encoding.ASCII);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            }

            if (packageInformation.IsPinned)
            {
                _fileSystem.write_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE), string.Empty, Encoding.ASCII);
            }
            else
            {
                _fileSystem.delete_file(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            }
        }
Example #16
0
        public void ensure_package_files_have_compatible_attributes(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
        {
            var installDirectory = get_install_directory(config, installedPackage);
            if (!_fileSystem.directory_exists(installDirectory)) return;

            _filesService.ensure_compatible_file_attributes(installDirectory, config);
        }
Example #17
0
        /// <summary>
        /// Remove the shimgen director files from the package.
        /// These are .gui/.ignore files that may have been created during the installation 
        /// process and won't be pulled by the nuget package replacement.
        /// This usually happens when package maintainers haven't been very good about how 
        /// they create the files in the past (not using force with new-item typically throws 
        /// an error if the file exists).
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="installedPackage">The installed package.</param>
        /// <param name="pkgInfo">The package information.</param>
        private void remove_shim_directors(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
        {
            var pkgInstallPath = get_install_directory(config, installedPackage);

            if (_fileSystem.directory_exists(pkgInstallPath))
            {
                var shimDirectorFiles = _fileSystem.get_files(pkgInstallPath, ApplicationParameters.ShimDirectorFileExtensions, SearchOption.AllDirectories);
                foreach (var file in shimDirectorFiles.or_empty_list_if_null())
                {
                    FaultTolerance.try_catch_with_logging_exception(
                        () => _fileSystem.delete_file(file),
                        "Error deleting shim director file");
                }
            }
        }
Example #18
0
        public void rename_legacy_package_version(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
        {
            if (pkgInfo != null && pkgInfo.IsSideBySide)
            {
                return;
            }

            var installDirectory = _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id);

            if (!_fileSystem.directory_exists(installDirectory))
            {
                // if the folder has a version on it, we need to rename the folder first.
                var pathResolver = new ChocolateyPackagePathResolver(NugetCommon.GetNuGetFileSystem(config, _nugetLogger), useSideBySidePaths: true);
                installDirectory = pathResolver.GetInstallPath(installedPackage);
                if (_fileSystem.directory_exists(installDirectory))
                {
                    FaultTolerance.try_catch_with_logging_exception(
                        () => _fileSystem.move_directory(installDirectory, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id)),
                        "Error during old package rename");
                }
            }
        }
Example #19
0
        public void ensure_package_files_have_compatible_attributes(ChocolateyConfiguration config, IPackage installedPackage, ChocolateyPackageInformation pkgInfo)
        {
            var installDirectory = _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, installedPackage.Id);
            if (!_fileSystem.directory_exists(installDirectory))
            {
                var pathResolver = new ChocolateyPackagePathResolver(NugetCommon.GetNuGetFileSystem(config, _nugetLogger), useSideBySidePaths: true);
                installDirectory = pathResolver.GetInstallPath(installedPackage);
                if (!_fileSystem.directory_exists(installDirectory)) return;
            }

            _filesService.ensure_compatible_file_attributes(installDirectory, config);
        }
Example #20
0
        public void TestBackupRemove()
        {
            InstallContext.Instance.RootLocation = Path.Combine("c:", "choco");
            string filePath     = Path.Combine("c:", "choco", "lib", "somelib", "license.txt");
            string fileConfPath = Path.Combine("c:", "choco", "lib", "somelib", "myconf.xml");
            string filePath2    = Path.Combine("c:", "choco", "lib", "somelib", "license2.txt");

            RegistryPackage package = new RegistryPackage();

            package.Id      = "Bob";
            package.Version = new SemanticVersion(1, 2, 3, 4);
            var packageInfo = new ChocolateyPackageInformation(package);
            var packageFile = new PackageFile {
                Path = filePath, Checksum = "1234"
            };
            var packageFiles = new PackageFiles()
            {
                Files = new List <PackageFile>
                {
                    packageFile
                }
            };

            packageInfo.FilesSnapshot = packageFiles;

            var fileSystemFiles = new [] { filePath };
            var config          = new ChocolateyConfiguration();

            fileSystem.Setup(x => x.get_files(It.IsAny <string>(), It.IsAny <string>(), SearchOption.AllDirectories)).Returns(fileSystemFiles);
            filesService.Setup(x => x.capture_package_files(It.IsAny <string>(), config)).Returns(packageFiles);

            using (new LogScope("should_ignore_an_unchanged_file"))
            {
                service.backup_changed_files(filePath, config, packageInfo);
            }

            var updatedPackageFiles = new PackageFiles()
            {
                Files = new List <PackageFile>
                {
                    new PackageFile {
                        Path = filePath, Checksum = "4321"
                    }
                }
            };

            using (new LogScope("should_backup_a_changed_file"))
            {
                filesService.Setup(x => x.capture_package_files(It.IsAny <string>(), config)).Returns(updatedPackageFiles);
                service.backup_changed_files(filePath, config, packageInfo);
            }

            using (new LogScope("if_snapshot_not_available_should_backup_files_in_folder")) //new case
            {
                var confFiles = new[] { fileConfPath };
                fileSystem.Setup(x => x.get_files(It.IsAny <string>(), It.IsAny <string[]>(), SearchOption.AllDirectories)).Returns(confFiles);
                packageInfo.FilesSnapshot = null;
                service.backup_changed_files(filePath, config, packageInfo);
            }

            using (new LogScope("should_do_nothing_if_the_directory_no_longer_exists"))
            {
                fileSystem.Setup(x => x.directory_exists(It.IsAny <string>())).Returns(false);
                filesService.Setup(x => x.get_package_file(It.IsAny <string>())).Returns(packageFile);
                service.remove_installation_files(package, packageInfo);
            }

            using (new LogScope("should_remove_an_unchanged_file"))
            {
                fileSystem.Setup(x => x.directory_exists(It.IsAny <string>())).Returns(true);
                fileSystem.Setup(x => x.get_files(It.IsAny <string>(), It.IsAny <string>(), SearchOption.AllDirectories)).Returns(fileSystemFiles);
                fileSystem.Setup(x => x.file_exists(filePath)).Returns(true);
                service.remove_installation_files(package, packageInfo);
            }

            using (new LogScope("should_not_delete_a_changed_file"))
            {
                filesService.Setup(x => x.get_package_file(It.IsAny <string>())).Returns(updatedPackageFiles.Files[0]);
                service.remove_installation_files(package, packageInfo);
            }

            using (new LogScope("should_not_delete_an_unfound_file"))
            {
                var packageFileNotInOriginal = new PackageFile {
                    Path = filePath2, Checksum = "4321"
                };
                filesService.Setup(x => x.get_package_file(It.IsAny <string>())).Returns(packageFileNotInOriginal);
                service.remove_installation_files(package, packageInfo);
            }

            using (new LogScope("generated_package_should_be_in_current_directory"))
            {
                fileSystem.Setup(x => x.get_current_directory()).Returns("c:/choco");
                service.pack_noop(config);
            }

            using (new LogScope("generated_package_should_be_in_specified_directory"))
            {
                config.OutputDirectory = "c:/packages";
                service.pack_noop(config);
            }
        }
Example #21
0
        public ChocolateyPackageInformation get_package_information(IPackage package)
        {
            var packageInformation = new ChocolateyPackageInformation(package);

            if (package == null)
            {
                this.Log().Debug("No package information as package is null.");
                return(packageInformation);
            }

            var pkgStorePath = _fileSystem.combine_paths(ApplicationParameters.ChocolateyPackageInfoStoreLocation, "{0}.{1}".format_with(package.Id, package.Version.to_string()));

            if (!_fileSystem.directory_exists(pkgStorePath))
            {
                return(packageInformation);
            }

            var deserializationErrorMessage = @"
A corrupt .registry file exists at {0}.
 Open this file in a text editor, and remove/escape any characters that
 are regarded as illegal within XML strings not surrounded by CData. 
 These are typically the characters &, `<`, and `>`. Again, this
 is an XML document, so you will see many < and > characters, so just
 focus exclusively in the string values not surrounded by CData. Once 
 these have been corrected, rename the .registry.bad file to .registry.
 Once saved, try running the same Chocolatey command that was just 
 executed, so verify problem is fixed.
 NOTE: It will not be possible to rename the file in Windows Explorer.
 Instead, you can use the following PowerShell command:
 Move-Item .\.registry.bad .\.registry
".format_with(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE));

            try
            {
                if (_fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE)))
                {
                    this.Log().Warn(deserializationErrorMessage);
                }
                else
                {
                    packageInformation.RegistrySnapshot = _registryService.read_from_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
                }
            }
            catch (Exception)
            {
                FaultTolerance.try_catch_with_logging_exception(
                    () =>
                {
                    this.Log().Warn(deserializationErrorMessage);

                    // rename the bad registry file so that it isn't processed again
                    _fileSystem.move_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE), _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE));
                },
                    "Unable to read registry snapshot file for {0} (located at {1})".format_with(package.Id, _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE)),
                    throwError: false,
                    logWarningInsteadOfError: true,
                    isSilent: true
                    );
            }

            FaultTolerance.try_catch_with_logging_exception(
                () =>
            {
                packageInformation.FilesSnapshot = _filesService.read_from_file(_fileSystem.combine_paths(pkgStorePath, FILES_SNAPSHOT_FILE));
            },
                "Unable to read files snapshot file",
                throwError: false,
                logWarningInsteadOfError: true,
                isSilent: true
                );

            packageInformation.HasSilentUninstall = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SILENT_UNINSTALLER_FILE));
            packageInformation.IsSideBySide       = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, SIDE_BY_SIDE_FILE));
            packageInformation.IsPinned           = _fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, PIN_FILE));
            var argsFile = _fileSystem.combine_paths(pkgStorePath, ARGS_FILE);

            if (_fileSystem.file_exists(argsFile))
            {
                packageInformation.Arguments = _fileSystem.read_file(argsFile);
            }
            var extraInfoFile = _fileSystem.combine_paths(pkgStorePath, EXTRA_FILE);

            if (_fileSystem.file_exists(extraInfoFile))
            {
                packageInformation.ExtraInformation = _fileSystem.read_file(extraInfoFile);
            }

            var versionOverrideFile = _fileSystem.combine_paths(pkgStorePath, VERSION_OVERRIDE_FILE);

            if (_fileSystem.file_exists(versionOverrideFile))
            {
                FaultTolerance.try_catch_with_logging_exception(
                    () =>
                {
                    packageInformation.VersionOverride = new SemanticVersion(_fileSystem.read_file(versionOverrideFile).trim_safe());
                },
                    "Unable to read version override file",
                    throwError: false,
                    logWarningInsteadOfError: true
                    );
            }

            return(packageInformation);
        }
Example #22
0
        /// <summary>
        /// NuGet will happily report a package has been uninstalled, even if it doesn't always remove the nupkg.
        /// Ensure that the package is deleted or throw an error.
        /// </summary>
        /// <param name="removedPackage">The installed package.</param>
        /// <param name="pkgInfo">The package information.</param>
        private void ensure_nupkg_is_removed(IPackage removedPackage, ChocolateyPackageInformation pkgInfo)
        {
            var isSideBySide = pkgInfo != null && pkgInfo.IsSideBySide;

            var nupkgFile = "{0}{1}.nupkg".format_with(removedPackage.Id, isSideBySide ? "." + removedPackage.Version.to_string() : string.Empty);
            var installDir = _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, "{0}{1}".format_with(removedPackage.Id, isSideBySide ? "." + removedPackage.Version.to_string() : string.Empty));
            var nupkg = _fileSystem.combine_paths(installDir, nupkgFile);

            FaultTolerance.try_catch_with_logging_exception(
                () => _fileSystem.delete_file(nupkg),
                "Error deleting nupkg file",
                throwError: true);
        }