private Boolean ApplyPetraPatch(String APatchFile) { Boolean ReturnValue; String TempPath; string[] directories; String[] files; ReturnValue = false; TLogging.Log("installing patch " + APatchFile); // create temp directory TempPath = Path.GetFullPath(Path.GetTempPath() + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(APatchFile)); if (Directory.Exists(TempPath)) { // remove old temp directory: for testing patches, otherwise we are using the old version Directory.Delete(TempPath, true); } Directory.CreateDirectory(TempPath); // extract the tar patch file into the temp directory PackTools.Unzip(TempPath, APatchFile); // make sure that PetraClient has been stopped if (PatchApplication.ProcessStillRunning(FBinPath + Path.DirectorySeparatorChar + "PetraClient.exe")) { TLogging.Log("Wait for PetraClient to stop..."); while (PatchApplication.ProcessStillRunning(FBinPath + Path.DirectorySeparatorChar + "PetraClient.exe")) { TLogging.Log("Wait for PetraClient to stop..."); } } try { // apply the patch TLogging.Log("applying patch, please wait"); // go through all directories in patch, all files directories = System.IO.Directory.GetDirectories(TempPath); foreach (string dir in directories) { ApplyPatchRecursively(TempPath, dir); } // rename the manually patched .dll and .exe files in net-patches directory, // since they should be part of the official patch in bin directory // go through bin/netpatches (or sapatches or remotepatches) directories = System.IO.Directory.GetDirectories(FBinPath, "*patches"); foreach (string dir in directories) { // delete the file.old if it already exists, if there is a file without .old // that way we prevent the deletion of nrr (non routine request) files that have gone to the wrong directory files = System.IO.Directory.GetFiles(dir, "*.old"); foreach (string filename in files) { if (System.IO.File.Exists(filename.Substring(0, filename.Length - 4))) { System.IO.File.Delete(filename); } else { // restore the .dll file (nrr) System.IO.File.Move(filename, filename.Substring(0, filename.Length - 4)); } } // rename .dll file to file.dll.old files = System.IO.Directory.GetFiles(directories[0], "*.dll"); foreach (string filename in files) { System.IO.File.Move(filename, filename + ".old"); } // rename .exe file to file.exe.old files = System.IO.Directory.GetFiles(directories[0], "*.exe"); foreach (string filename in files) { System.IO.File.Move(filename, filename + ".old"); } } // delete the .dll.orig files, because we have applied the new version for all files // those files have been created manually when we give out special dll files (not recommended) files = System.IO.Directory.GetFiles(FBinPath, "*.orig"); foreach (string filename in files) { if (System.IO.File.Exists(filename.Substring(0, filename.Length - 5))) { System.IO.File.Delete(filename); } } TLogging.Log("Patch " + APatchFile + " has been applied successfully!"); ReturnValue = true; } catch (Exception e) { TLogging.Log("Patch could not be installed: " + e.Message); TLogging.Log(e.ToString()); TLogging.Log(e.StackTrace); TLogging.Log("Restoring the situation before the patch..."); // if unsuccessful, restore the previous situation; // go through all directories in patch, all files directories = System.IO.Directory.GetDirectories(TempPath); foreach (string dir in directories) { UndoPatchRecursively(TempPath, dir); } TLogging.Log("Please contact your IT support and send the file " + TLogWriter.GetLogFileName()); } // clean up the safety backup files of the patch directories = System.IO.Directory.GetDirectories(TempPath); foreach (string dir in directories) { CleanupPatchRecursively(TempPath, dir); } // delete temp directory recursively (and the backup zip files) Directory.Delete(TempPath, true); return(ReturnValue); }
/// <summary> /// static run function /// </summary> public static bool Run() { try { // check command line new TAppSettingsManager(false); string TempPath = TAppSettingsManager.GetValue("OpenPetra.PathTemp"); new TLogging(TempPath + Path.DirectorySeparatorChar + "PetraPatch.log"); // Catalog.Init(); String action = TAppSettingsManager.GetValue("action", false); if (action.Equals("create")) { PatchCreation.CreateDiff(TempPath, TAppSettingsManager.GetValue("deliverypath"), TAppSettingsManager.GetValue("appname"), TAppSettingsManager.GetValue("zipname"), TAppSettingsManager.GetValue("oldversion"), TAppSettingsManager.GetValue("newversion")); } else if (action.Equals("preparePatch")) { // to be called before installing the patch; // will only copy the files if there is a new patch available TPetraPatchTools patchTools = new TPetraPatchTools( TAppSettingsManager.GetValue("OpenPetra.Path"), TAppSettingsManager.GetValue("OpenPetra.Path") + Path.DirectorySeparatorChar + "bin" + TPatchTools.OPENPETRA_VERSIONPREFIX, TempPath, "", TAppSettingsManager.GetValue("OpenPetra.Path.Patches"), ""); if (patchTools.CheckForRecentPatch()) { patchTools.CopyLatestPatchProgram(TAppSettingsManager.GetValue("OpenPetra.PathTemp")); } else { System.Console.WriteLine(Catalog.GetString("There is no new patch to be installed.")); return(false); } } else if (action.Equals("patchRemote")) { // basically the same as patchFiles, but will use the status window. PatchApplication.PatchRemoteInstallation(); } else if (action.Equals("patchFiles")) { // need to call first preparePatch; // and then run the patch from TmpPatchPath so that the files can be overwritten // this will patch the application files TPetraPatchTools patchTools = new TPetraPatchTools(TAppSettingsManager.GetValue("OpenPetra.Path"), TAppSettingsManager.GetValue("OpenPetra.Path") + Path.DirectorySeparatorChar + "bin" + TPatchTools.OPENPETRA_VERSIONPREFIX, TempPath, TAppSettingsManager.GetValue("OpenPetra.Path.Dat"), TAppSettingsManager.GetValue("OpenPetra.Path.Patches"), ""); if (patchTools.CheckForRecentPatch()) { if (!patchTools.PatchTheFiles()) { System.Console.WriteLine(Catalog.GetString("There was a problem installing the patch.")); return(false); } } else { if ((!patchTools.GetCurrentPatchVersion().Equals(patchTools.GetLatestPatchVersion()))) { System.Console.WriteLine(Catalog.GetString( "You don't have all patches that are necessary for patching to the latest patch.")); return(false); } else { System.Console.WriteLine(Catalog.GetString("There is no new patch to be installed.")); return(false); } } } else if (action.Equals("patchDatabase")) { // this should only be called after patchFiles; // this will possibly change the database structure, and modify database content TPetraPatchTools patchTools = new TPetraPatchTools(TAppSettingsManager.GetValue("OpenPetra.Path"), TAppSettingsManager.GetValue("OpenPetra.Path") + Path.DirectorySeparatorChar + "bin" + TPatchTools.OPENPETRA_VERSIONPREFIX, TempPath, "", TAppSettingsManager.GetValue("DBPatches.Path"), ""); patchTools.RunDBPatches(); } else { System.Console.WriteLine( "patch creation: patchtool -action:create -OpenPetra.PathTemp:u:/tmp/patch -deliverypath:u:/delivery -oldversion:0.0.8-0 -newversion:0.0.10-0"); System.Console.WriteLine( "patch application: patchtool -action:patchFiles -C:\"C:\\Program Files (x86)\\OpenPetra\\etc30\\PetraClientRemote.config\" -OpenPetra.Path:\"C:\\Program Files (x86)\\OpenPetra\""); //-OpenPetra.PathTemp:u:/tmp/patch -diffzip:u:/tmp/patch/Patch2.2.3-5_2.2.4-3.zip -apppath:c:/Programme/OpenPetra.org -datpath:c:/Programme/OpenPetra.org/data30"); System.Console.WriteLine( "patch application: patchtool -action:patchRemote -C:\"C:\\Program Files (x86)\\OpenPetra\\etc30\\PetraClientRemote.config\" -OpenPetra.Path:\"C:\\Program Files (x86)\\OpenPetra\""); return(false); } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); TLogging.Log(e.Message, TLoggingType.ToLogfile); TLogging.Log(e.StackTrace, TLoggingType.ToLogfile); return(false); } return(true); }