Installer: Make IsDirWritable() remove all its temporary dirs / files
authorSebastian Schuberth <sschuberth@gmail.com>
Sun, 21 Oct 2012 08:55:10 +0000 (21 10:55 +0200)
committerSebastian Schuberth <sschuberth@gmail.com>
Sun, 21 Oct 2012 08:55:10 +0000 (21 10:55 +0200)
This fixes msysgit issue #66.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
share/WinGit/helpers.inc.iss

index 97dd90f..a02a7ca 100644 (file)
@@ -63,24 +63,43 @@ begin
     end;\r
 end;\r
 \r
-// Checks whether the specified directory can be created and written to.\r
-// Note that the created dummy file is not explicitly deleted here, so that\r
-// needs to be done as part of the uninstall process.\r
+// Checks whether the specified directory can be created and written to\r
+// by creating all intermediate directories and a temporary file.\r
 function IsDirWritable(DirName:String):Boolean;\r
 var\r
-    FileName:String;\r
+    AbsoluteDir,FirstExistingDir,FirstCreatedDir,FileName:String;\r
 begin\r
-    Result:=False;\r
+    Result:=True;\r
+\r
+    AbsoluteDir:=ExpandFileName(DirName);\r
 \r
-    if not ForceDirectories(DirName) then begin\r
-        Exit;\r
+    FirstExistingDir:=AbsoluteDir;\r
+    while not DirExists(FirstExistingDir) do begin\r
+        FirstCreatedDir:=FirstExistingDir;\r
+        FirstExistingDir:=ExtractFileDir(FirstExistingDir);\r
     end;\r
+    Log('Line {#__LINE__}: First directory in hierarchy that already exists is "' + FirstExistingDir + '".')\r
 \r
-    FileName:=DirName+'\setup.ini';\r
+    if Length(FirstCreatedDir)>0 then begin\r
+        Log('Line {#__LINE__}: First directory in hierarchy needs to be created is "' + FirstCreatedDir + '".')\r
 \r
-    if not SetIniBool('Dummy','Writable',true,FileName) then begin\r
-        Exit;\r
-    end;\r
+        if ForceDirectories(DirName) then begin\r
+            FileName:=GenerateUniqueName(DirName,'.txt');\r
+            Log('Line {#__LINE__}: Trying to write to temporary file "' + Filename + '".')\r
 \r
-    Result:=GetIniBool('Dummy','Writable',false,FileName);\r
+            if SaveStringToFile(FileName,'This file is writable.',False) then begin\r
+                if not DeleteFile(FileName) then begin\r
+                    Result:=False;\r
+                end;\r
+            end else begin\r
+                Result:=False;\r
+            end;\r
+        end else begin\r
+            Result:=False;\r
+        end;\r
+\r
+        if not DelTree(FirstCreatedDir,True,False,True) then begin\r
+            Result:=False;\r
+        end;\r
+    end;\r
 end;\r