Example #1
0
        public PKGStatus CheckInstallationStatus(PKGRunningPackageData Package, PKGInstallState state)
        {
            State = state;
            Package.SetInstallPath(InstallPath);
            Package.ErrorText = "";

            PKGStatus p = CheckReq(Package);

            if (p != PKGStatus.Success)
            {
                Package.ErrorText = "Not needed";
            }

            return(p);
        }
Example #2
0
        public bool InstallPackage(string Filename, List <byte[]> CerCertificates, InstallMode Mode, bool ZipIsMetaOnly,
                                   out string ErrorText, out PKGStatus res, out PKGRecieptData Reciept, string OtherDLL = "")
        {
            ErrorText = "";
            Reciept   = null;
            PKGRunningPackageData RunningPKG;
            PKGRecieptData        RunningReciept = new PKGRecieptData();

            RunningReciept.HeaderID = "FoxRecieptScriptV1";
            res = PKGStatus.Unknown;

            if (CerCertificates == null)
            {
                CerCertificates = new List <byte[]>();
            }
            CerCertificates.Add(Resources.Vulpes_Main);

            if (InitPackageZipFile(Filename, CerCertificates, ZipIsMetaOnly, out ErrorText, out RunningPKG, OtherDLL) == false)
            {
                return(false);
            }

            RunningPKG.ApplicationPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            if (RunningPKG.ApplicationPath.EndsWith("\\") == false)
            {
                RunningPKG.ApplicationPath += "\\";
            }

            using (FileStream ZipFile = File.Open(Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (ZipArchive ZipArch = new ZipArchive(ZipFile, ZipArchiveMode.Read, true, Encoding.UTF8))
                {
                    RunningReciept.scriptsource = RunningPKG.scriptsource;
                    RunningReciept.PackageID    = RunningPKG.PackageID;
                    RunningReciept.InstalledOn  = DateTime.Now;
                    RunningReciept.Description  = RunningPKG.Description;

                    if (Mode == InstallMode.TestPackageOnly || Mode == InstallMode.ApplyUserSettingsTest)
                    {
                        return(true);
                    }

                    if (ZipIsMetaOnly == true)
                    {
                        if (Mode == InstallMode.Install || Mode == InstallMode.Update)
                        {
                            Debug.WriteLine("Cannot install/update a Meta-Package");
                            ErrorText = "Cannot install/update a Meta-Package";
                            return(false);
                        }
                    }

                    try
                    {
                        UpdateText("Running pre install script");

                        PKGInstallState inst = PKGInstallState.NotSet;
                        switch (Mode)
                        {
                        case InstallMode.Install:
                            inst = PKGInstallState.Install; break;

                        case InstallMode.Test:
                            inst = PKGInstallState.Test; break;

                        case InstallMode.TestPackageOnly:
                            inst = PKGInstallState.Test; break;

                        case InstallMode.Update:
                            inst = PKGInstallState.Update; break;

                        case InstallMode.UpdateTest:
                            inst = PKGInstallState.UpdateTest; break;
                        }

                        res = RunningPKG.script.CheckInstallationStatus(RunningPKG, inst);
                        if (res == PKGStatus.DependencyFailed)
                        {
                            RequiredDependencies = RunningPKG.script.GetDependencies(RunningPKG);
                            ErrorText            = RunningPKG.ErrorText;
                            return(false);
                        }
                        if (res == PKGStatus.NotNeeded)
                        {
                            ErrorText = RunningPKG.ErrorText;
                            return(true);  //no installation
                        }
                        if (res == PKGStatus.Failed)
                        {
                            ErrorText = RunningPKG.ErrorText;
                            return(false);
                        }

                        if (Mode == InstallMode.Test || Mode == InstallMode.UpdateTest)
                        {
                            ErrorText = "";
                            res       = PKGStatus.Success;
                            return(true);
                        }

                        res = RunningPKG.script.PreInstall(RunningPKG);
                        if (res != PKGStatus.Success)
                        {
                            ErrorText = RunningPKG.ErrorText;
                            return(false);
                        }
                    }
                    catch (Exception ee)
                    {
                        Debug.WriteLine(ee.ToString());
                        ErrorText = "Script Error:\r\n" + ee.ToString();
                        return(false);
                    }

                    RunningReciept.CreatedFolders = new List <string>();
                    RunningReciept.InstalledFiles = new List <string>();

                    int lasterr;
                    CommonUtilities.ResetLastError();
                    List <string> DeleteFiles = new List <string>();

                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        Alphaleonis.Win32.Filesystem.KernelTransaction kt = new Alphaleonis.Win32.Filesystem.KernelTransaction();
                        lasterr = Marshal.GetLastWin32Error();
                        if (lasterr != 0)
                        {
                            ErrorText = "Cannot start transaction 0x" + lasterr.ToString("X");
                            return(false);
                        }

                        foreach (PKGFile file in RunningPKG.Files)
                        {
                            string fldr = Environment.ExpandEnvironmentVariables(file.FolderName);
                            if (fldr.EndsWith("\\") == false)
                            {
                                fldr += "\\";
                            }
                            string fullfilename = fldr + file.FileName;
                            string renamedfile  = fldr + "FOX-" + CommonUtilities.NewGUID + ".PENDING";

                            bool RestartMove = false;

                            try
                            {
                                if (Alphaleonis.Win32.Filesystem.File.ExistsTransacted(kt, fullfilename) == true)
                                {
                                    try
                                    {
                                        Alphaleonis.Win32.Filesystem.File.MoveTransacted(kt, fullfilename, renamedfile);

                                        lasterr = Marshal.GetLastWin32Error();
                                        if (lasterr != 0)
                                        {
                                            string SubErr;
                                            RollbackFiles(RunningPKG, out SubErr);
                                            kt.Rollback();
                                            ErrorText = "Cannot move file " + fullfilename + " => " + renamedfile + " 0x" + lasterr.ToString("X") + "\r\n" + SubErr;
                                            return(false);
                                        }
                                        DeleteFiles.Add(renamedfile);
                                    }
                                    catch
                                    {
                                        RestartMove = true;
                                        string tmp = fullfilename;
                                        fullfilename = renamedfile;
                                        renamedfile  = tmp;
                                    }
                                }
                            }
                            catch (Exception ee)
                            {
                                Debug.WriteLine(ee.ToString());

                                UpdateText("Rolling back");

                                string SubErr;
                                RollbackFiles(RunningPKG, out SubErr);
                                kt.Rollback();
                                ErrorText = "Move Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                                return(false);
                            }

                            UpdateText("Installing " + fullfilename);
                            try
                            {
                                string mkdir = Path.GetDirectoryName(fldr);
                                if (Alphaleonis.Win32.Filesystem.Directory.ExistsTransacted(kt, mkdir) == false)
                                {
                                    try
                                    {
                                        Alphaleonis.Win32.Filesystem.Directory.CreateDirectoryTransacted(kt, mkdir);
                                        RunningReciept.CreatedFolders.Add(fldr);

                                        //lasterr = Marshal.GetLastWin32Error();
                                        //if (lasterr != 0)
                                        //{
                                        //    string SubErr;
                                        //    RollbackFiles(RunningPKG, out SubErr);
                                        //    kt.Rollback();
                                        //    ErrorText = "Cannot create folder " + mkdir + " 0x" + lasterr.ToString("X") + "\r\n" + SubErr;
                                        //    return (false);
                                        //}
                                    }
                                    catch (Exception ee)
                                    {
                                        Debug.WriteLine(ee.ToString());
                                    }
                                }
                            }
                            catch (Exception ee)
                            {
                                Debug.WriteLine(ee.ToString());
                            }

                            CommonUtilities.ResetLastError();
                            try
                            {
                                if (OutputToFile(kt, file.SrcFile, fullfilename, ZipArch, out ErrorText) == false)
                                {
                                    UpdateText("Rolling back");

                                    string SubErr;
                                    RollbackFiles(RunningPKG, out SubErr);
                                    kt.Rollback();
                                    ErrorText += "\r\n" + SubErr;
                                    return(false);
                                }
                            }
                            catch (Exception ee)
                            {
                                Debug.WriteLine(ee.ToString());

                                UpdateText("Rolling back");

                                string SubErr;
                                RollbackFiles(RunningPKG, out SubErr);
                                kt.Rollback();
                                ErrorText = "File Output Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                                return(false);
                            }

                            RunningReciept.InstalledFiles.Add(RestartMove == true ? renamedfile : fullfilename);

                            if (RestartMove == true)
                            {
                                try
                                {
                                    Alphaleonis.Win32.Filesystem.File.CopyMoveCore(false, kt, renamedfile, null, true, null, Alphaleonis.Win32.Filesystem.MoveOptions.DelayUntilReboot, null, null, null, Alphaleonis.Win32.Filesystem.PathFormat.RelativePath);
                                    Alphaleonis.Win32.Filesystem.File.CopyMoveCore(false, kt, fullfilename, renamedfile, true, null, Alphaleonis.Win32.Filesystem.MoveOptions.DelayUntilReboot, null, null, null, Alphaleonis.Win32.Filesystem.PathFormat.RelativePath);

                                    lasterr = Marshal.GetLastWin32Error();
                                    if (lasterr != 0)
                                    {
                                        string SubErr;
                                        RollbackFiles(RunningPKG, out SubErr);
                                        kt.Rollback();
                                        ErrorText = "Cannot pending-move file " + fullfilename + " => " + renamedfile + " 0x" + lasterr.ToString("X") + "\r\n" + SubErr;
                                        return(false);
                                    }
                                }
                                catch (Exception ee)
                                {
                                    Debug.WriteLine(ee.ToString());

                                    UpdateText("Rolling back");

                                    string SubErr;
                                    RollbackFiles(RunningPKG, out SubErr);
                                    kt.Rollback();
                                    ErrorText = "Restart Move Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                                    return(false);
                                }
                            }
                        }

                        try
                        {
                            res = RunningPKG.script.PostInstall(RunningPKG);
                            if (res != PKGStatus.Success)
                            {
                                ErrorText = "PostInstall failed.";
                                UpdateText("Rolling back");
                                RollbackFiles(RunningPKG, out ErrorText);
                                kt.Rollback();
                                return(false);
                            }
                        }
                        catch (Exception ee)
                        {
                            Debug.WriteLine(ee.ToString());

                            UpdateText("Rolling back");

                            string SubErr;
                            RollbackFiles(RunningPKG, out SubErr);
                            kt.Rollback();
                            ErrorText = "PostInstall Script Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                            return(false);
                        }

                        try
                        {
                            kt.Commit();
                        }
                        catch (Exception ee)
                        {
                            Debug.WriteLine(ee.ToString());

                            UpdateText("Rolling back");

                            string SubErr;
                            RollbackFiles(RunningPKG, out SubErr);
                            kt.Rollback();
                            ErrorText = "Commit Error:\r\n" + ee.ToString() + "\r\n" + SubErr;
                            return(false);
                        }
                    }

                    if (RunningPKG.NoReciept == false)
                    {
                        Reciept = RunningReciept;
                    }
                    else
                    {
                        Reciept = null;
                    }

                    //Clean up mess here
                    foreach (string file in DeleteFiles)
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch
                        {
                            CommonUtilities.PendingMove(file, null);
                        }
                    }

                    res = PKGStatus.Success;
                }
            }
            return(true);
        }