Installer: Make IsDirWritable() remove all its temporary dirs / files
[msysgit.git] / share / WinGit / helpers.inc.iss
bloba02a7ca43042cb7e82baa67eaa186a560863e204
1 [Code]
3 // Copies a NULL-terminated array of characters to a string.
4 function ArrayToString(Chars:array of Char):String;
5 var
6 Len,i:Longint;
7 begin
8 Len:=GetArrayLength(Chars);
9 SetLength(Result,Len);
11 i:=0;
12 while (i<Len) and (Chars[i]<>#0) do begin
13 Result[i+1]:=Chars[i];
14 Inc(i);
15 end;
17 SetLength(Result,i);
18 end;
20 // Copies a string to a NULL-terminated array of characters.
21 function StringToArray(Str:String):array of Char;
22 var
23 Len,i:Longint;
24 begin
25 Len:=Length(Str);
26 SetArrayLength(Result,Len+1);
28 i:=0;
29 while i<Len do begin
30 Result[i]:=Str[i+1];
31 Inc(i);
32 end;
34 Result[i]:=#0;
35 end;
37 // Returns the path to the common or user shell folder as specified in "Param".
38 function GetShellFolder(Param:string):string;
39 begin
40 if IsAdminLoggedOn then begin
41 Param:='{common'+Param+'}';
42 end else begin
43 Param:='{user'+Param+'}';
44 end;
45 Result:=ExpandConstant(Param);
46 end;
48 // As IsComponentSelected() is not supported during uninstall, this work-around
49 // simply checks the Registry. This is unreliable if the user runs the installer
50 // twice, the first time selecting the component, the second deselecting it.
51 function IsComponentInstalled(Component:String):Boolean;
52 var
53 UninstallKey,UninstallValue:String;
54 Value:String;
55 begin
56 Result:=False;
58 UninstallKey:='SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{#APP_NAME}_is1';
59 UninstallValue:='Inno Setup: Selected Components';
61 if RegQueryStringValue(HKEY_LOCAL_MACHINE,UninstallKey,UninstallValue,Value) then begin
62 Result:=(Pos(Component,Value)>0);
63 end;
64 end;
66 // Checks whether the specified directory can be created and written to
67 // by creating all intermediate directories and a temporary file.
68 function IsDirWritable(DirName:String):Boolean;
69 var
70 AbsoluteDir,FirstExistingDir,FirstCreatedDir,FileName:String;
71 begin
72 Result:=True;
74 AbsoluteDir:=ExpandFileName(DirName);
76 FirstExistingDir:=AbsoluteDir;
77 while not DirExists(FirstExistingDir) do begin
78 FirstCreatedDir:=FirstExistingDir;
79 FirstExistingDir:=ExtractFileDir(FirstExistingDir);
80 end;
81 Log('Line {#__LINE__}: First directory in hierarchy that already exists is "' + FirstExistingDir + '".')
83 if Length(FirstCreatedDir)>0 then begin
84 Log('Line {#__LINE__}: First directory in hierarchy needs to be created is "' + FirstCreatedDir + '".')
86 if ForceDirectories(DirName) then begin
87 FileName:=GenerateUniqueName(DirName,'.txt');
88 Log('Line {#__LINE__}: Trying to write to temporary file "' + Filename + '".')
90 if SaveStringToFile(FileName,'This file is writable.',False) then begin
91 if not DeleteFile(FileName) then begin
92 Result:=False;
93 end;
94 end else begin
95 Result:=False;
96 end;
97 end else begin
98 Result:=False;
99 end;
101 if not DelTree(FirstCreatedDir,True,False,True) then begin
102 Result:=False;
103 end;
104 end;
105 end;