Browse Source

* fix for DirectoryExists - proper handling of root directory

git-svn-id: trunk@18186 -
Tomas Hajny 14 years ago
parent
commit
867ad94115
3 changed files with 49 additions and 47 deletions
  1. 28 33
      rtl/emx/sysutils.pp
  2. 6 10
      rtl/go32v2/sysutils.pp
  3. 15 4
      rtl/os2/sysutils.pp

+ 28 - 33
rtl/emx/sysutils.pp

@@ -578,22 +578,14 @@ begin
 end;
 end;
 
 
 
 
-function FileExists (const FileName: string): boolean; assembler;
-asm
-{$IFDEF REGCALL}
- mov edx, eax
-{$ELSE REGCALL}
- mov edx, FileName
-{$ENDIF REGCALL}
- mov ax, 4300h
- call syscall
- mov eax, 0
- jc @FExistsEnd
- test cx, 18h
- jnz @FExistsEnd
- inc eax
-@FExistsEnd:
-end {['eax', 'ecx', 'edx']};
+function FileExists (const FileName: string): boolean;
+begin
+  if Directory = '' then
+   Result := false
+  else
+   Result := FileGetAttr (ExpandFileName (Directory)) and
+                                                     faDirectory = faDirectory;
+end;
 
 
 
 
 type    TRec = record
 type    TRec = record
@@ -970,29 +962,32 @@ begin
 end;
 end;
 
 
 
 
-{$ASMMODE INTEL}
-function DirectoryExists (const Directory: string): boolean; assembler;
-asm
-{$IFDEF REGCALL}
- mov edx, eax
-{$ELSE REGCALL}
- mov edx, Directory
-{$ENDIF REGCALL}
- mov ax, 4300h
- call syscall
- mov eax, 0
- jc @FExistsEnd
- test cx, 10h
- jz @FExistsEnd
- inc eax
-@FExistsEnd:
-end {['eax', 'ecx', 'edx']};
+function DirectoryExists (const Directory: string): boolean;
+var
+  L: longint;
+begin
+  if Directory = '' then
+   Result := false
+  else
+   begin
+    if (Directory [Length (Directory)] in AllowDirectorySeparators) and
+                                              (Length (Directory) > 1) and
+(* Do not remove '\' after ':' (root directory of a drive) 
+   or in '\\' (invalid path, possibly broken UNC path). *)
+      not (Directory [Length (Directory) - 1] in AllowDriveSeparators + AllowDirectorySeparators) then
+     L := FileGetAttr (ExpandFileName (Copy (Directory, 1, Length (Directory) - 1)))
+    else
+     L := FileGetAttr (ExpandFileName (Directory));
+    Result := (L > 0) and (L and faDirectory = faDirectory);
+   end;
+end;
 
 
 
 
 {****************************************************************************
 {****************************************************************************
                               Time Functions
                               Time Functions
 ****************************************************************************}
 ****************************************************************************}
 
 
+{$ASMMODE INTEL}
 procedure GetLocalTime (var SystemTime: TSystemTime); assembler;
 procedure GetLocalTime (var SystemTime: TSystemTime); assembler;
 asm
 asm
 (* Expects the default record alignment (word)!!! *)
 (* Expects the default record alignment (word)!!! *)

+ 6 - 10
rtl/go32v2/sysutils.pp

@@ -302,7 +302,6 @@ end;
 
 
 Function DirectoryExists (Const Directory : String) : Boolean;
 Function DirectoryExists (Const Directory : String) : Boolean;
 Var
 Var
-  Sr : Searchrec;
   Dir : String;
   Dir : String;
   drive : byte;
   drive : byte;
   StoredIORes : longint;
   StoredIORes : longint;
@@ -334,16 +333,13 @@ begin
 {$ifdef OPT_I}
 {$ifdef OPT_I}
   {$I+}
   {$I+}
 {$endif}
 {$endif}
-  if (length(dir)>1) and (dir[length(dir)] in ['/','\']) then
+  if (Length (Dir) > 1) and
+    (Dir [Length (Dir)] in AllowDirectorySeparators) and
+(* Do not remove '\' after ':' (root directory of a drive) 
+   or in '\\' (invalid path, possibly broken UNC path). *)
+     not (Dir [Length (Dir - 1)] in (AllowDriveSeparators + AllowDirectorySeparators)) then
     dir:=copy(dir,1,length(dir)-1);
     dir:=copy(dir,1,length(dir)-1);
-  DOS.FindFirst(Dir,$3f,sr);
-  if DosError = 0 then
-   begin
-     Result:=(sr.attr and $10)=$10;
-     Dos.FindClose(sr);
-   end
-  else
-   Result:=false;
+  Result := FileGetAttr (Dir) and faDirectory = faDirectory;
 end;
 end;
 
 
 
 

+ 15 - 4
rtl/os2/sysutils.pp

@@ -817,11 +817,22 @@ end;
 
 
 function DirectoryExists (const Directory: string): boolean;
 function DirectoryExists (const Directory: string): boolean;
 var
 var
-  SR: TSearchRec;
+  L: longint;
 begin
 begin
-  DirectoryExists := (FindFirst (Directory, faAnyFile, SR) = 0) and
-                                                (SR.Attr and faDirectory <> 0);
-  FindClose(SR);
+  if Directory = '' then
+   Result := false
+  else
+   begin
+    if (Directory [Length (Directory)] in AllowDirectorySeparators) and
+                                              (Length (Directory) > 1) and
+(* Do not remove '\' after ':' (root directory of a drive) 
+   or in '\\' (invalid path, possibly broken UNC path). *)
+      not (Directory [Length (Directory) - 1] in AllowDriveSeparators + AllowDirectorySeparators) then
+     L := FileGetAttr (ExpandFileName (Copy (Directory, 1, Length (Directory) - 1)))
+    else
+     L := FileGetAttr (ExpandFileName (Directory));
+    Result := (L > 0) and (L and faDirectory = faDirectory);
+   end;
 end;
 end;
 
 
 {****************************************************************************
 {****************************************************************************