Exemplo n.º 1
0
        private static List <FeactureInstallTO> GetFeactureScriptDataBase(Session session, List <string> listFeactureNames)
        {
            string sFileName = string.Empty;

            try
            {
                List <FeactureInstallTO> listF;

                string sLevel = session["INSTALLLEVEL"];
                listF = new List <FeactureInstallTO>();
                FeactureInstallTO f;
                int i;

                string sQuery = "SELECT `Feature`.`Feature`, `Feature`.`Title`, `Feature`.`Display`, " +
                                " `Component`.`Directory_`, `File`.`FileName` " +
                                " FROM `FeatureComponents`, `Feature`, `Component`, `File` " +
                                " WHERE `FeatureComponents`.`Feature_` = `Feature`.`Feature` " +
                                " AND `FeatureComponents`.`Component_` = `Component`.`Component` " +
                                " AND `File`.`Component_` = `Component`.`Component` " +
                                " AND `Feature`.`RuntimeLevel` > 0 AND `Feature`.`Level` > 0 " +
                                // " AND `Feature`.`Level` <= " + sLevel +
                                " ORDER BY `Feature`.`Display`";
                using (Microsoft.Deployment.WindowsInstaller.View v = session.Database.OpenView(sQuery))
                {
                    if (v != null)
                    {
                        v.Execute();

                        //var str = string.Empty;
                        //foreach (var fea in listFeactureNames)
                        //    str += fea.ToString();

                        //MessageBox.Show(str);
                        //str = string.Empty ;

                        //var frm = new FeatureList( session,v,listFeactureNames);
                        //frm.ShowDialog();

                        for (Record r = v.Fetch(); r != null; r = v.Fetch())
                        {
                            if (listFeactureNames.Contains(r.GetString("Feature")) && r.GetString("FileName").ToUpper().EndsWith(".SQL"))
                            {
                                i = r.GetString("FileName").IndexOf("|");
                                if (i > 0)
                                {
                                    sFileName = r.GetString("FileName").Substring(i + 1);
                                }
                                else
                                {
                                    sFileName = r.GetString("FileName");
                                }

                                f = new FeactureInstallTO()
                                {
                                    Feature       = r.GetString("Feature"),
                                    Title         = r.GetString("Title"),
                                    DisplayOrder  = r.GetInteger("Display"),
                                    FileName      = sFileName,
                                    DirectoryPath = session.GetTargetPath(r.GetString("Directory_"))
                                };
                                listF.Add(f);
                            }
                            r.Dispose();
                        }
                    }
                }
                return(listF);
            }
            catch (Exception ex)
            {
                InstallUtilities.WriteLogInstall(session, "Fail to Read Feature Script", ex, true);
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Exports all database tables, streams, and summary information to archive files.
        /// </summary>
        /// <param name="directoryPath">Path to the directory where archive files will be created</param>
        /// <exception cref="FileNotFoundException">the directory path is invalid</exception>
        /// <exception cref="InvalidHandleException">the Database handle is invalid</exception>
        /// <remarks><p>
        /// The directory will be created if it does not already exist.
        /// </p><p>
        /// Win32 MSI API:
        /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msidatabaseexport.asp">MsiDatabaseExport</a>
        /// </p></remarks>
        public void ExportAll(string directoryPath)
        {
            if (String.IsNullOrEmpty(directoryPath))
            {
                throw new ArgumentNullException("directoryPath");
            }

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            this.Export("_SummaryInformation", Path.Combine(directoryPath, "_SummaryInformation.idt"));

            using (View view = this.OpenView("SELECT `Name` FROM `_Tables`"))
            {
                view.Execute();

                foreach (Record rec in view)
                {
                    using (rec)
                    {
                        string table = (string)rec[1];

                        this.Export(table, Path.Combine(directoryPath, table + ".idt"));
                    }
                }
            }

            if (!Directory.Exists(Path.Combine(directoryPath, "_Streams")))
            {
                Directory.CreateDirectory(Path.Combine(directoryPath, "_Streams"));
            }

            using (View view = this.OpenView("SELECT `Name`, `Data` FROM `_Streams`"))
            {
                view.Execute();

                foreach (Record rec in view)
                {
                    using (rec)
                    {
                        string stream = (string)rec[1];
                        if (stream.EndsWith("SummaryInformation", StringComparison.Ordinal))
                        {
                            continue;
                        }

                        int i = stream.IndexOf('.');
                        if (i >= 0)
                        {
                            if (File.Exists(Path.Combine(
                                                directoryPath,
                                                Path.Combine(stream.Substring(0, i), stream.Substring(i + 1) + ".ibd"))))
                            {
                                continue;
                            }
                        }
                        rec.GetStream(2, Path.Combine(directoryPath, Path.Combine("_Streams", stream)));
                    }
                }
            }
        }
Exemplo n.º 3
0
        private object[][] GetComponentRegistryRows(string productCode, string componentCode, Session session, bool includeComponent)
        {
            ArrayList rows          = new ArrayList();
            string    componentPath = new ComponentInstallation(componentCode, productCode).Path;

            string componentKey = (string)session.Database.ExecuteScalar(
                "SELECT `Component` FROM `Component` WHERE `ComponentId` = '{0}'", componentCode);

            if (componentKey == null)
            {
                return(null);
            }
            int attributes = Convert.ToInt32(session.Database.ExecuteScalar(
                                                 "SELECT `Attributes` FROM `Component` WHERE `Component` = '{0}'", componentKey));
            bool registryKeyPath = (attributes & (int)ComponentAttributes.RegistryKeyPath) != 0;

            if (!registryKeyPath && componentPath.Length > 0)
            {
                componentPath = Path.GetDirectoryName(componentPath);
            }
            string keyPath = (string)session.Database.ExecuteScalar(
                "SELECT `KeyPath` FROM `Component` WHERE `Component` = '{0}'", componentKey);

            using (View view = session.Database.OpenView("SELECT `Registry`, `Root`, `Key`, `Name`, " +
                                                         "`Value` FROM `Registry` WHERE `Component_` = '{0}'", componentKey))
            {
                view.Execute();

                foreach (Record rec in view)
                {
                    using (rec)
                    {
                        string regName = (string)rec["Name"];
                        if (regName == "-")
                        {
                            continue;                                 // Don't list deleted keys
                        }
                        string regTableKey = (string)rec["Registry"];
                        bool   isKey       = registryKeyPath && keyPath == regTableKey;
                        string regPath     = this.GetRegistryPath(session, (RegistryRoot)Convert.ToInt32(rec["Root"]),
                                                                  (string)rec["Key"], (string)rec["Name"]);

                        string dbValue;
                        using (Record formatRec = new Record(0))
                        {
                            formatRec[0] = rec["Value"];
                            dbValue      = session.FormatRecord(formatRec);
                        }

                        string installedValue = this.GetRegistryValue(regPath);
                        bool   exists         = installedValue != null;
                        if (!exists)
                        {
                            installedValue = "";
                        }
                        bool match = installedValue == dbValue;

                        object[] row;
                        if (includeComponent)
                        {
                            row = new object[] { isKey, regTableKey, regPath, exists, dbValue, installedValue, match, componentCode }
                        }
                        ;
                        else
                        {
                            row = new object[] { isKey, regTableKey, regPath, exists, dbValue, installedValue, match }
                        };
                        rows.Add(row);
                    }
                }
            }

            return((object[][])rows.ToArray(typeof(object[])));
        }
Exemplo n.º 4
0
        private object[][] GetComponentFilesRows(string productCode, string componentCode, Session session, bool includeComponent)
        {
            ArrayList rows          = new ArrayList();
            string    componentPath = new ComponentInstallation(componentCode, productCode).Path;

            string componentKey = (string)session.Database.ExecuteScalar(
                "SELECT `Component` FROM `Component` WHERE `ComponentId` = '{0}'", componentCode);

            if (componentKey == null)
            {
                return(null);
            }
            int attributes = Convert.ToInt32(session.Database.ExecuteScalar(
                                                 "SELECT `Attributes` FROM `Component` WHERE `Component` = '{0}'", componentKey));
            bool registryKeyPath = (attributes & (int)ComponentAttributes.RegistryKeyPath) != 0;

            if (!registryKeyPath && componentPath.Length > 0)
            {
                componentPath = Path.GetDirectoryName(componentPath);
            }
            string keyPath = (string)session.Database.ExecuteScalar(
                "SELECT `KeyPath` FROM `Component` WHERE `Component` = '{0}'", componentKey);

            using (View view = session.Database.OpenView("SELECT `File`, `FileName`, `Version`, `Language`, " +
                                                         "`Attributes` FROM `File` WHERE `Component_` = '{0}'", componentKey))
            {
                view.Execute();

                foreach (Record rec in view)
                {
                    using (rec)
                    {
                        string fileKey = (string)rec["File"];
                        bool   isKey   = !registryKeyPath && keyPath == fileKey;

                        string dbVersion     = (string)rec["Version"];
                        bool   versionedFile = dbVersion.Length != 0;
                        if (versionedFile)
                        {
                            string language = (string)rec["Language"];
                            if (language.Length > 0)
                            {
                                dbVersion = dbVersion + " (" + language + ")";
                            }
                        }
                        else if (session.Database.Tables.Contains("MsiFileHash"))
                        {
                            IList <int> hash = session.Database.ExecuteIntegerQuery("SELECT `HashPart1`, `HashPart2`, " +
                                                                                    "`HashPart3`, `HashPart4` FROM `MsiFileHash` WHERE `File_` = '{0}'", fileKey);
                            if (hash != null && hash.Count == 4)
                            {
                                dbVersion = this.GetFileHashString(hash);
                            }
                        }

                        string filePath         = GetLongFileName((string)rec["FileName"]);
                        bool   exists           = false;
                        bool   installedMatch   = false;
                        string installedVersion = "";
                        if (!registryKeyPath && componentPath.Length > 0)
                        {
                            filePath = Path.Combine(componentPath, filePath);

                            if (File.Exists(filePath))
                            {
                                exists = true;
                                if (versionedFile)
                                {
                                    installedVersion = Installer.GetFileVersion(filePath);
                                    string language = Installer.GetFileLanguage(filePath);
                                    if (language.Length > 0)
                                    {
                                        installedVersion = installedVersion + " (" + language + ")";
                                    }
                                }
                                else
                                {
                                    int[] hash = new int[4];
                                    Installer.GetFileHash(filePath, hash);
                                    installedVersion = this.GetFileHashString(hash);
                                }
                                installedMatch = installedVersion == dbVersion;
                            }
                        }

                        object[] row;
                        if (includeComponent)
                        {
                            row = new object[] { isKey, fileKey, filePath, exists, dbVersion, installedVersion, installedMatch, componentCode }
                        }
                        ;
                        else
                        {
                            row = new object[] { isKey, fileKey, filePath, exists, dbVersion, installedVersion, installedMatch }
                        };
                        rows.Add(row);
                    }
                }
            }

            return((object[][])rows.ToArray(typeof(object[])));
        }
Exemplo n.º 5
0
        private static ActionResult ScriptsImmediate(Session session, int elevated, string deferredProperty)
        {
            Database db = session.Database;

            if (!db.Tables.Contains("PowerShellScripts"))
            {
                return(ActionResult.Success);
            }

            try
            {
                List <ScriptActionData> scripts = new List <ScriptActionData>();
                using (View view = db.OpenView(string.Format("SELECT `Id`, `Script`, `IgnoreErrors` FROM `PowerShellScripts` WHERE `Elevated` = {0} ORDER BY `Order`", elevated)))
                {
                    view.Execute();

                    Record row;
                    while ((row = view.Fetch()) != null)
                    {
                        string script = Encoding.Unicode.GetString(Convert.FromBase64String(row["Script"].ToString()));
                        script = session.Format(script);
                        script = Convert.ToBase64String(Encoding.Unicode.GetBytes(script));

                        ScriptActionData data = new ScriptActionData()
                        {
                            Id           = row["Id"].ToString(),
                            Script       = script,
                            IgnoreErrors = row["IgnoreErrors"].ToString() == "1"
                        };

                        scripts.Add(data);
                        session.Log("Adding {0} to CustomActionData", data.Id);
                    }
                }

                XmlSerializer srlz = new XmlSerializer(scripts.GetType());
                using (StringWriter sw = new StringWriter())
                {
                    srlz.Serialize(sw, scripts);
                    session[deferredProperty] = sw.ToString();
                }

                // Tell the installer to increase the value of the final total
                // length of the progress bar by the total number of ticks in
                // the custom action.
                MessageResult iResult;
                using (var hProgressRec = new Record(2))
                {
                    hProgressRec[1] = 3;
                    hProgressRec[2] = TotalTicks;
                    iResult         = session.Message(InstallMessage.Progress, hProgressRec);
                }

                if (iResult == MessageResult.Cancel)
                {
                    return(ActionResult.UserExit);
                }

                return(ActionResult.Success);
            }
            catch (Exception ex)
            {
                session.Log(ex.ToString());
                return(ActionResult.Failure);
            }
            finally
            {
                db.Close();
            }
        }
Exemplo n.º 6
0
        private static ActionResult FilesImmediate(Session session, int elevated, string deferredProperty)
        {
            Database db = session.Database;

            const string tableName = "PowerShellFiles";

            if (!db.Tables.Contains(tableName))
            {
                return(ActionResult.Success);
            }

            try
            {
                XDocument doc;
                using (View view = db.OpenView(string.Format("SELECT `Id`, `File`, `Arguments`, `IgnoreErrors` FROM `{0}` WHERE `Elevated` = {1} ORDER BY `Order`", tableName, elevated)))
                {
                    view.Execute();

                    doc = new XDocument(new XDeclaration("1.0", "utf-16", "yes"), new XElement("r"));

                    foreach (Record row in view)
                    {
                        var args         = session.Format(row["Arguments"].ToString());
                        var IgnoreErrors = session.Format(row["IgnoreErrors"].ToString());

                        session.Log("args '{0}'", args);

                        doc.Root.Add(new XElement("d", new XAttribute("Id", row["Id"]),
                                                  new XAttribute("file", session.Format(row["File"].ToString())), new XAttribute("args", args), new XAttribute("IgnoreErrors", IgnoreErrors)));
                    }
                }

                var cad = new CustomActionData {
                    { "xml", doc.ToString() }
                };

                session[deferredProperty] = cad.ToString();

                // Tell the installer to increase the value of the final total
                // length of the progress bar by the total number of ticks in
                // the custom action.
                MessageResult iResult;
                using (var hProgressRec = new Record(2))
                {
                    hProgressRec[1] = 3;
                    hProgressRec[2] = TotalTicks;
                    iResult         = session.Message(InstallMessage.Progress, hProgressRec);
                }

                if (iResult == MessageResult.Cancel)
                {
                    return(ActionResult.UserExit);
                }

                return(ActionResult.Success);
            }
            catch (Exception ex)
            {
                session.Log(ex.Message);
                return(ActionResult.Failure);
            }
            finally
            {
                db.Close();
            }
        }
        /// <summary>
        ///     Returns data from the MSI tables: `FeatureComponents`, `Feature`, `Component`, `File`.
        ///     We filter to just get the files that end in (*.SQL).
        /// </summary>
        /// <param name="session">Windows Installer Session.</param>
        /// <param name="listFeatureNames">List list with names to be installed.</param>
        /// <returns>Returns scripts database files to be installed.</returns>
        private static List <FeatureInstallTO> GetFeatureScriptDataBase(Session session, List <string> listFeatureNames)
        {
            try
            {
                List <FeatureInstallTO> listFeatures;

                string sLevel = session["INSTALLLEVEL"];
                listFeatures = new List <FeatureInstallTO>();
                FeatureInstallTO f;
                int    i;
                string sFileName;
                string sQuery = "SELECT `Feature`.`Feature`, `Feature`.`Title`, `Feature`.`Display`, " +
                                " `Component`.`Directory_`, `File`.`FileName` " +
                                " FROM `FeatureComponents`, `Feature`, `Component`, `File` " +
                                " WHERE `FeatureComponents`.`Feature_` = `Feature`.`Feature` " +
                                " AND `FeatureComponents`.`Component_` = `Component`.`Component` " +
                                " AND `File`.`Component_` = `Component`.`Component` " +
                                " AND `Feature`.`RuntimeLevel` > 0 AND `Feature`.`Level` > 0 " +
                                // " AND `Feature`.`Level` <= " + sLevel +
                                " ORDER BY `Feature`.`Display`";
                using (View v = session.Database.OpenView(sQuery))
                {
                    if (v != null)
                    {
                        v.Execute();
                        for (Record r = v.Fetch(); r != null; r = v.Fetch())
                        {
                            if (listFeatureNames.Contains(r.GetString("Feature")) &&
                                r.GetString("FileName").ToUpper().EndsWith(".SQL"))
                            {
                                i = r.GetString("FileName").IndexOf("|", StringComparison.Ordinal);
                                if (i > 0)
                                {
                                    sFileName = r.GetString("FileName").Substring(i + 1);
                                }
                                else
                                {
                                    sFileName = r.GetString("FileName");
                                }

                                f = new FeatureInstallTO
                                {
                                    Feature       = r.GetString("Feature"),
                                    Title         = r.GetString("Title"),
                                    DisplayOrder  = r.GetInteger("Display"),
                                    FileName      = sFileName,
                                    DirectoryPath = session.GetTargetPath(r.GetString("Directory_"))
                                };
                                listFeatures.Add(f);
                            }
                            r.Dispose();
                        }
                    }
                }

                return(listFeatures);
            }
            catch (Exception ex)
            {
                InstallUtilities.WriteLogInstall(session, "Exception, ReadFeactureScriptDataBase", ex, true);
                throw;
            }
        }