From 952ae50d80b5d0002a1d7715e35fd2fffcc0c2e1 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Mon, 7 Jun 2010 17:04:05 +0200 Subject: [PATCH] Installer: Move the code to replace in-use files to a separate function Signed-off-by: Sebastian Schuberth --- share/WinGit/install.iss | 35 ++-------------------- share/WinGit/modules.inc.iss | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 33 deletions(-) diff --git a/share/WinGit/install.iss b/share/WinGit/install.iss index 476c4c93..86e03c47 100644 --- a/share/WinGit/install.iss +++ b/share/WinGit/install.iss @@ -992,39 +992,8 @@ begin DeleteContextMenuEntries; FileName:=AppDir+'\git-cheetah\git_shell_ext.dll'; - TempName:=GenerateUniqueName(ExtractFilePath(FileName),'.git_shell_ext'); - - // Try to delete any previously renamed old shell extension files. - DelTree(ExtractFilePath(FileName)+'\*.git_shell_ext',False,True,False); - - if FileExists(FileName) then begin - if not UnregisterServer(Is64BitInstallMode,FileName,False) then begin - Msg:='Line {#emit __LINE__}: Unable to unregister "'+FileName+'".'; - MsgBox(Msg,mbError,MB_OK); - Log(Msg); - // This is not a critical error, the user can probably fix it manually, - // so we continue. - end; - - if (not DeleteFile(FileName)) and (not RenameFile(FileName,TempName)) then begin - Msg:='Line {#emit __LINE__}: Unable to delete or rename "'+FileName+'".'; - MsgBox(Msg,mbError,MB_OK); - Log(Msg); - // This is pretty much critical, but will be catched below when - // renaming the new shell extension file fails. - end; - end; - - if not RenameFile(FileName+'.new',FileName) then begin - Msg:='Line {#emit __LINE__}: Unable to install git-cheetah. Please do it manually by renaming' + #13 + #13 + - '"'+FileName+'.new" to "'+FileName+'"' + #13 + #13 + - 'and registering the shell extension by typing' + #13 + #13 + - '"regsvr32 '+ExtractFileName(FileName)+'"' + #13 + #13 + - 'at the command prompt in the git-cheetah directory.'; - MsgBox(Msg,mbError,MB_OK); - Log(Msg); - end else begin - RegisterServer(Is64BitInstallMode,FileName,False); + if not ReplaceInUseFile(FileName,FileName+'.new',True) then begin + Log('Line {#emit __LINE__}: Replacing file "'+FileName+'" failed.'); end; end; end; diff --git a/share/WinGit/modules.inc.iss b/share/WinGit/modules.inc.iss index 6dc90cbd..5033db74 100644 --- a/share/WinGit/modules.inc.iss +++ b/share/WinGit/modules.inc.iss @@ -539,3 +539,72 @@ begin Result:=FindProcessesUsingModule_WinVista(Module,Processes); end; end; + +{ + Helper code +} + +// Tries to replace an in-use file, e.g. a registered shell extension, by +// renaming it and then renaming the new file to the original name. Optionally, +// performs (un-)registering via regsvr32. +function ReplaceInUseFile(CurFile,NewFile:String;Register:Boolean):Boolean; +var + CurFilePath,CurFileName,NewFileName:String; + CurFileStem,CurFileTemp:String; + UnregisterFailed,RenameFailed:Boolean; + Msg:String; +begin + Result:=False; + + // Note that CurFile may not exist, in which case NewFile is just renamed. + if not FileExists(NewFile) then begin + Exit; + end; + + CurFilePath:=ExtractFilePath(CurFile); + CurFileName:=ExtractFileName(CurFile); + NewFileName:=ExtractFileName(NewFile); + + // Get the file name without extension or period and use that as a suffix + // for the temporary file. + CurFileStem:=ChangeFileExt(CurFileName,''); + CurFileTemp:=GenerateUniqueName(CurFilePath,'.'+CurFileStem); + + // Clean-up by trying to delete any previously renamed temporary files. + DelTree(CurFilePath+'\*.'+CurFileStem,False,True,False); + + UnregisterFailed:=False; + RenameFailed:=False; + + if FileExists(CurFile) then begin + if Register and (not UnregisterServer(Is64BitInstallMode,CurFile,False)) then begin + UnregisterFailed:=True; + end; + + if (not DeleteFile(CurFile)) and (not RenameFile(CurFile,CurFileTemp)) then begin + RenameFailed:=True; + end; + end; + + if not RenameFile(NewFile,CurFile) then begin + Msg:='Unable to install a new version of "'+CurFileName+'". ' + + 'Please finish the installation manually by following theses steps on the command line:' + #13 + #13; + if FileExists(CurFile) then begin + if UnregisterFailed then begin + Msg := Msg + '- run "regsvr32 /u ' + CurFileName + '",' + #13; + end; + if RenameFailed then begin + Msg := Msg + '- rename "' + CurFileName + '" to something else,' + #13; + end; + end; + Msg := Msg + '- rename "' + NewFileName + '" to "' + CurFileName + '",' + #13; + Msg := Msg + '- run "regsvr32 ' + CurFileName + '".'; + + MsgBox(Msg,mbError,MB_OK); + end else begin + if Register then begin + RegisterServer(Is64BitInstallMode,CurFile,False); + end; + Result:=True; + end; +end; -- 2.11.4.GIT