예제 #1
0
        private IEnumerable <PackageFragmentValidationResult> LoadPackageFragmentInstallerBinaries()
        {
            string binariesDirectory = Path.Combine(this.PackageInstallationDirectory, PackageSystemSettings.BinariesDirectoryName);

            if (!C1Directory.Exists(binariesDirectory))
            {
                yield break;
            }

            foreach (string filename in C1Directory.GetFiles(binariesDirectory))
            {
                string newFilename = Path.Combine(this.TempDirectory, Path.GetFileName(filename));
                C1File.Copy(filename, newFilename);

                Log.LogVerbose("PackageUninstaller", "Loading package uninstaller fragment assembly '{0}'", newFilename);

                Exception exception = null;
                try
                {
                    PackageAssemblyHandler.AddAssembly(newFilename);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }

                if (exception != null)
                {
                    yield return(new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal, exception));
                }
            }
        }
예제 #2
0
        /// <exclude />
        public override void Uninstall()
        {
            Verify.IsNotNull(_filesToDelete as object ?? _filesToCopy, "{0} has not been validated", this.GetType().Name);

            foreach (string filename in _filesToDelete)
            {
                Log.LogVerbose(LogTitle, "Uninstalling the file '{0}'", filename);

                FileUtils.Delete(filename);
            }

            foreach (var fileToCopy in _filesToCopy)
            {
                string targetFile = fileToCopy.Item2;

                Log.LogVerbose(LogTitle, "Restoring file from a backup copy'{0}'", targetFile);

                if ((C1File.GetAttributes(targetFile) & FileAttributes.ReadOnly) > 0)
                {
                    FileUtils.RemoveReadOnly(targetFile);
                }

                C1File.Copy(fileToCopy.Item1, targetFile, true);
            }
        }
        /// <exclude />
        public object Deserialize(string serializedObject)
        {
            Dictionary <string, string> dic = StringConversionServices.ParseKeyValueCollection(serializedObject);

            string zipFilename             = StringConversionServices.DeserializeValueString(dic["ZipFileName"]);
            string packageInstallDirectory = StringConversionServices.DeserializeValueString(dic["PackageInstallDirectory"]);
            bool   hasBeenValidated        = StringConversionServices.DeserializeValueBool(dic["HasBeenValidated"]);

            string originalPackageInstallDirectory = null;
            string serializedValue;

            if (dic.TryGetValue("OriginalPackageInstallDirectory", out serializedValue))
            {
                originalPackageInstallDirectory = StringConversionServices.DeserializeValueString(serializedValue);
            }

            if (C1File.Exists(zipFilename))
            {
                XElement installContent;
                XmlHelper.LoadInstallXml(zipFilename, out installContent);

                PackageInformation packageInformation;
                PackageManager.ValidatePackageInformation(installContent, out packageInformation);

                string packageZipFilename = Path.Combine(packageInstallDirectory, Path.GetFileName(zipFilename));
                C1File.Copy(zipFilename, packageZipFilename, true);

                var packageInstaller = new PackageInstaller(new PackageInstallerUninstallerFactory(), packageZipFilename, packageInstallDirectory, TempDirectoryFacade.CreateTempDirectory(), packageInformation);

                var packageManagerInstallProcess = new PackageManagerInstallProcess(
                    packageInstaller,
                    packageInformation.SystemLockingType,
                    zipFilename,
                    packageInstallDirectory,
                    packageInformation.Name,
                    packageInformation.Version,
                    packageInformation.Id,
                    originalPackageInstallDirectory);

                if (hasBeenValidated)
                {
                    packageManagerInstallProcess.Validate();
                }

                return(packageManagerInstallProcess);
            }

            return(new PackageManagerInstallProcess(new List <PackageFragmentValidationResult>(), null));;
        }
        /// <exclude />
        public static void SaveConfigurationSection(string sectionName, ConfigurationSection configurationSection)
        {
            using (GlobalInitializerFacade.CoreLockScope)
            {
                lock (_lock)
                {
                    string tempFilePath = ConfigurationServices.TempRandomConfigFilePath;
                    C1File.Copy(FileConfigurationSourcePath, tempFilePath);
                    FileConfigurationParameter configurationParameter = new FileConfigurationParameter(tempFilePath);
                    ConfigurationServices.ConfigurationSource.Add(configurationParameter, sectionName, configurationSection);
                    // Kill monitoring of file changes:
                    //                    FileConfigurationSource.ResetImplementation(ConfigurationServices.FileConfigurationSourcePath, false);
                    C1File.Copy(tempFilePath, ConfigurationServices.FileConfigurationSourcePath, true);
                    DeleteTempConfigurationFile(tempFilePath);

                    _configurationSource = new FileConfigurationSource(ConfigurationServices.FileConfigurationSourcePath);
                    // Kill monitoring of file changes:
                    //                    FileConfigurationSource.ResetImplementation(ConfigurationServices.FileConfigurationSourcePath, false);
                }
            }
        }
예제 #5
0
        /// <exclude />
        public override IEnumerable <XElement> Install()
        {
            Verify.IsNotNull(_filesToCopy, "{0} has not been validated", this.GetType().Name);

            var dependencies     = new List <Pair <string, Version> >();
            var asmBindingsToAdd = new List <AssemblyBindingInfo>();

            var fileElements = new List <XElement>();

            foreach (FileToCopy fileToCopy in _filesToCopy)
            {
                Log.LogVerbose(LogTitle, "Installing the file '{0}' to the target filename '{1}'",
                               fileToCopy.SourceFilename, fileToCopy.TargetFilePath);

                // Extracting the dll file so version can be checked
                string tempFileName = Path.Combine(InstallerContext.TempDirectory, Path.GetRandomFileName());
                this.InstallerContext.ZipFileSystem.WriteFileToDisk(fileToCopy.SourceFilename, tempFileName);

                // Checking for dll version here:
                var sourceFileVersionInfo = FileVersionInfo.GetVersionInfo(tempFileName);
                var sourceFileVersion     = GetDllProductVersion(tempFileName);

                dependencies.Add(new Pair <string, Version>(fileToCopy.TargetRelativeFilePath, sourceFileVersion));

                string targetDirectory = Path.GetDirectoryName(fileToCopy.TargetFilePath);
                if (!Directory.Exists(targetDirectory))
                {
                    Directory.CreateDirectory(targetDirectory);
                }

                string backupFileName = null;

                bool addAssemblyBinding = fileToCopy.AddAssemblyBinding;

                if (C1File.Exists(fileToCopy.TargetFilePath) && fileToCopy.Overwrite)
                {
                    var existingFileVersion = GetDllProductVersion(fileToCopy.TargetFilePath);

                    if (existingFileVersion == sourceFileVersion)
                    {
                        Log.LogInformation(LogTitle,
                                           "Skipping installation for file '{0}' version '{1}'. A file with the same version already exists.",
                                           fileToCopy.TargetRelativeFilePath, sourceFileVersion);
                        continue;
                    }

                    if (existingFileVersion > sourceFileVersion)
                    {
                        Log.LogInformation(LogTitle,
                                           "Skipping installation for file '{0}' version '{1}', as a file with a newer version '{2}' already exists.",
                                           fileToCopy.TargetRelativeFilePath, sourceFileVersion, existingFileVersion);
                        continue;
                    }

                    addAssemblyBinding = true;
                    if ((C1File.GetAttributes(fileToCopy.TargetFilePath) & FileAttributes.ReadOnly) > 0)
                    {
                        FileUtils.RemoveReadOnly(fileToCopy.TargetFilePath);
                    }

                    if (InstallerContext.PackageInformation.CanBeUninstalled)
                    {
                        backupFileName = GetBackupFileName(fileToCopy.TargetFilePath);

                        string backupFilesFolder = this.InstallerContext.PackageDirectory + "\\FileBackup";

                        C1Directory.CreateDirectory(backupFilesFolder);

                        C1File.Copy(fileToCopy.TargetFilePath, backupFilesFolder + "\\" + backupFileName);
                    }

                    Log.LogInformation(LogTitle, "Overwriting existing file '{0}' version '{2}', new version is '{1}'",
                                       fileToCopy.TargetRelativeFilePath, sourceFileVersion, existingFileVersion);
                }

                if (addAssemblyBinding)
                {
                    asmBindingsToAdd.Add(new AssemblyBindingInfo
                    {
                        FilePath        = fileToCopy.TargetFilePath,
                        FileVersionInfo = sourceFileVersionInfo,
                        VersionInfo     = sourceFileVersion
                    });
                }


                File.Delete(fileToCopy.TargetFilePath);
                File.Move(tempFileName, fileToCopy.TargetFilePath);

                var fileElement = new XElement("File",
                                               new XAttribute("filename", fileToCopy.TargetRelativeFilePath),
                                               new XAttribute("version", sourceFileVersion));

                if (backupFileName != null)
                {
                    fileElement.Add(new XAttribute("backupFile", backupFileName));
                }

                fileElements.Add(fileElement);
            }

            UpdateBindingRedirects(asmBindingsToAdd);

            yield return(new XElement("Files", fileElements));
        }
예제 #6
0
        /// <exclude />
        public override IEnumerable <XElement> Install()
        {
            Verify.IsNotNull(_filesToCopy, "{0} has not been validated", this.GetType().Name);

            foreach (string directoryToDelete in _directoriesToDelete)
            {
                Directory.Delete(directoryToDelete, true);
            }

            var fileElements = new List <XElement>();

            foreach (FileToCopy fileToCopy in _filesToCopy)
            {
                Log.LogVerbose(LogTitle, "Installing the file '{0}' to the target filename '{1}'", fileToCopy.SourceFilename, fileToCopy.TargetFilePath);

                string targetDirectory = Path.GetDirectoryName(fileToCopy.TargetFilePath);
                if (!Directory.Exists(targetDirectory))
                {
                    Directory.CreateDirectory(targetDirectory);
                }

                string backupFileName = null;

                if (C1File.Exists(fileToCopy.TargetFilePath) && fileToCopy.Overwrite)
                {
                    if ((C1File.GetAttributes(fileToCopy.TargetFilePath) & FileAttributes.ReadOnly) > 0)
                    {
                        FileUtils.RemoveReadOnly(fileToCopy.TargetFilePath);
                    }

                    if (InstallerContext.PackageInformation.CanBeUninstalled)
                    {
                        backupFileName = GetBackupFileName(fileToCopy.TargetFilePath);

                        string backupFilesFolder = this.InstallerContext.PackageDirectory + "\\FileBackup";

                        C1Directory.CreateDirectory(backupFilesFolder);

                        C1File.Copy(fileToCopy.TargetFilePath, backupFilesFolder + "\\" + backupFileName);
                    }
                }

                this.InstallerContext.ZipFileSystem.WriteFileToDisk(fileToCopy.SourceFilename, fileToCopy.TargetFilePath);

                // Searching for static IData interfaces
                string targetFilePath = fileToCopy.TargetFilePath;

                if (targetFilePath.StartsWith(Path.Combine(PathUtil.BaseDirectory, "Bin"), StringComparison.InvariantCultureIgnoreCase) &&
                    targetFilePath.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
                {
                    string fileName = Path.GetFileName(targetFilePath);

                    if (!DllsNotToLoad.Any(fileName.StartsWith))
                    {
                        Assembly assembly;

                        try
                        {
                            assembly = Assembly.LoadFrom(targetFilePath);
                        }
                        catch (Exception)
                        {
                            continue;
                        }

                        DataTypeTypesManager.AddNewAssembly(assembly, false);
                    }
                }

                var fileElement = new XElement("File", new XAttribute("filename", fileToCopy.TargetRelativeFilePath));

                if (backupFileName != null)
                {
                    fileElement.Add(new XAttribute("backupFile", backupFileName));
                }

                fileElements.Add(fileElement);
            }

            yield return(new XElement("Files", fileElements));
        }
예제 #7
0
        private IEnumerable <PackageFragmentValidationResult> LoadPackageFragmentInstallerBinaries(XElement packageFragmentInstallerBinariesElement)
        {
            var binaryElements = packageFragmentInstallerBinariesElement.Elements(XmlUtils.GetXName(PackageSystemSettings.XmlNamespace,
                                                                                                    PackageSystemSettings.PackageFragmentInstallerBinariesAddElementName)).ToList();

            if (!binaryElements.Any())
            {
                return(new PackageFragmentValidationResult[0]);
            }

            string binariesDirectory = Path.Combine(this.PackageInstallDirectory, PackageSystemSettings.BinariesDirectoryName);

            if (!C1Directory.Exists(binariesDirectory))
            {
                C1Directory.CreateDirectory(binariesDirectory);
            }

            var result = new List <PackageFragmentValidationResult>();

            foreach (XElement element in binaryElements)
            {
                XAttribute pathAttribute = element.Attribute(PackageSystemSettings.PathAttributeName);

                string sourceFilename = pathAttribute.Value;
                string targetFilename = Path.Combine(binariesDirectory, Path.GetFileName(sourceFilename));

                ZipFileSystem zipFileSystem = new ZipFileSystem(this.ZipFilename);
                if (!zipFileSystem.ContainsFile(sourceFilename))
                {
                    result.AddFatal($"The file '{sourceFilename}' is missing from the zip file");
                    continue;
                }

                // Extracting dll to package temp folder
                if (C1File.Exists(targetFilename))
                {
                    bool success = false;
                    try
                    {
                        FileUtils.Delete(targetFilename);
                        success = true;
                    }
                    catch (UnauthorizedAccessException) {}

                    if (!success)
                    {
                        result.AddFatal($"Access denied to file '{targetFilename}'");
                        continue;
                    }
                }

                zipFileSystem.WriteFileToDisk(sourceFilename, targetFilename);

                string newTargetFilename = Path.Combine(this.TempDirectory, Path.GetFileName(targetFilename));
                C1File.Copy(targetFilename, newTargetFilename);

                Log.LogVerbose("PackageInstaller", "Loading package uninstaller fragment assembly '{0}'", newTargetFilename);

                PackageAssemblyHandler.AddAssembly(newTargetFilename);
            }

            return(result);
        }
예제 #8
0
        /// <exclude />
        public static PackageManagerInstallProcess Install(Stream zipFileStream, bool isLocalInstall, string packageServerAddress)
        {
            if (!isLocalInstall && string.IsNullOrEmpty(packageServerAddress))
            {
                throw new ArgumentException("Non local install needs a packageServerAddress");
            }

            string zipFilename = null;

            try
            {
                PackageFragmentValidationResult packageFragmentValidationResult = SaveZipFile(zipFileStream, out zipFilename);
                if (packageFragmentValidationResult != null)
                {
                    return(new PackageManagerInstallProcess(new List <PackageFragmentValidationResult> {
                        packageFragmentValidationResult
                    }, null));
                }

                XElement installContent;
                packageFragmentValidationResult = XmlHelper.LoadInstallXml(zipFilename, out installContent);
                if (packageFragmentValidationResult != null)
                {
                    return(new PackageManagerInstallProcess(new List <PackageFragmentValidationResult> {
                        packageFragmentValidationResult
                    }, zipFilename));
                }

                PackageInformation packageInformation;
                packageFragmentValidationResult = ValidatePackageInformation(installContent, out packageInformation);
                if (packageFragmentValidationResult != null)
                {
                    return(new PackageManagerInstallProcess(new List <PackageFragmentValidationResult> {
                        packageFragmentValidationResult
                    }, zipFilename));
                }

                if (RuntimeInformation.ProductVersion < packageInformation.MinCompositeVersionSupported ||
                    RuntimeInformation.ProductVersion > packageInformation.MaxCompositeVersionSupported)
                {
                    return(new PackageManagerInstallProcess(new List <PackageFragmentValidationResult>
                    {
                        new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal,
                                                            Texts.PackageManager_CompositeVersionMisMatch(
                                                                RuntimeInformation.ProductVersion,
                                                                packageInformation.MinCompositeVersionSupported,
                                                                packageInformation.MaxCompositeVersionSupported))
                    }, zipFilename));
                }

                bool updatingInstalledPackage = false;
                if (IsInstalled(packageInformation.Id))
                {
                    string currentVersionString = GetCurrentVersion(packageInformation.Id);

                    Version currentVersion = new Version(currentVersionString);
                    Version newVersion     = new Version(packageInformation.Version);

                    if (newVersion <= currentVersion)
                    {
                        string validationError = newVersion == currentVersion
                                    ? Texts.PackageManager_PackageAlreadyInstalled
                                    : Texts.PackageManager_NewerVersionInstalled;

                        return(new PackageManagerInstallProcess(
                                   new List <PackageFragmentValidationResult>
                        {
                            new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal, validationError)
                        }, zipFilename));
                    }

                    updatingInstalledPackage = true;
                }

                string originalInstallDirectory = null;
                string packageInstallDirectory  = CreatePackageDirectoryName(packageInformation);

                if (updatingInstalledPackage)
                {
                    originalInstallDirectory = packageInstallDirectory;
                    packageInstallDirectory += "-" + packageInformation.Version;
                }

                C1Directory.CreateDirectory(packageInstallDirectory);

                string packageZipFilename = Path.Combine(packageInstallDirectory, Path.GetFileName(zipFilename));
                C1File.Copy(zipFilename, packageZipFilename, true);

                string username = "******";
                if (UserValidationFacade.IsLoggedIn())
                {
                    username = UserValidationFacade.GetUsername();
                }

                var      doc = new XDocument();
                XElement packageInfoElement = new XElement(XmlUtils.GetXName(PackageSystemSettings.XmlNamespace, PackageSystemSettings.PackageInfoElementName));
                doc.Add(packageInfoElement);
                packageInfoElement.Add(
                    new XAttribute(PackageSystemSettings.PackageInfo_NameAttributeName, packageInformation.Name),
                    new XAttribute(PackageSystemSettings.PackageInfo_GroupNameAttributeName, packageInformation.GroupName),
                    new XAttribute(PackageSystemSettings.PackageInfo_VersionAttributeName, packageInformation.Version),
                    new XAttribute(PackageSystemSettings.PackageInfo_AuthorAttributeName, packageInformation.Author),
                    new XAttribute(PackageSystemSettings.PackageInfo_WebsiteAttributeName, packageInformation.Website),
                    new XAttribute(PackageSystemSettings.PackageInfo_DescriptionAttributeName, packageInformation.Description),
                    new XAttribute(PackageSystemSettings.PackageInfo_InstallDateAttributeName, DateTime.Now),
                    new XAttribute(PackageSystemSettings.PackageInfo_InstalledByAttributeName, username),
                    new XAttribute(PackageSystemSettings.PackageInfo_IsLocalInstalledAttributeName, isLocalInstall),
                    new XAttribute(PackageSystemSettings.PackageInfo_CanBeUninstalledAttributeName, packageInformation.CanBeUninstalled),
                    new XAttribute(PackageSystemSettings.PackageInfo_FlushOnCompletionAttributeName, packageInformation.FlushOnCompletion),
                    new XAttribute(PackageSystemSettings.PackageInfo_ReloadConsoleOnCompletionAttributeName, packageInformation.ReloadConsoleOnCompletion),
                    new XAttribute(PackageSystemSettings.PackageInfo_SystemLockingAttributeName, packageInformation.SystemLockingType.Serialize()));

                if (!string.IsNullOrEmpty(packageServerAddress))
                {
                    packageInfoElement.Add(new XAttribute(PackageSystemSettings.PackageInfo_PackageServerAddressAttributeName, packageServerAddress));
                }

                string infoFilename = Path.Combine(packageInstallDirectory, PackageSystemSettings.PackageInformationFilename);
                doc.SaveToFile(infoFilename);

                var packageInstaller = new PackageInstaller(new PackageInstallerUninstallerFactory(), packageZipFilename, packageInstallDirectory, TempDirectoryFacade.CreateTempDirectory(), packageInformation);

                return(new PackageManagerInstallProcess(
                           packageInstaller,
                           packageInformation.SystemLockingType,
                           zipFilename,
                           packageInstallDirectory,
                           packageInformation.Name,
                           packageInformation.Version,
                           packageInformation.Id,
                           originalInstallDirectory));
            }
            catch (Exception ex)
            {
                return(new PackageManagerInstallProcess(new List <PackageFragmentValidationResult>
                {
                    new PackageFragmentValidationResult(PackageFragmentValidationResultType.Fatal, ex)
                }, zipFilename));
            }
        }