Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi directoryexists function odd behaviour for network mapped units

In delphi XE, when I call SysUtils DirectoryExists function with the following input

'Y:\blabla\'

where Y is a network mapped unit, it correctly returns false because blabla doesnt exist.

But when I call with the following input

'Y:\blabla\Y:\bla'

it returns true.

Documentation is poor, and I've not found anywhere on the internets people with the same problem

maybe someone here already had this problem, or know what is happening?

like image 508
dmd_anfini Avatar asked Mar 01 '13 20:03

dmd_anfini


1 Answers

It seems a bug in the implementation of the DirectoryExists function.

This is the relevant code of this function

function DirectoryExists(const Directory: string; FollowLink: Boolean = True): Boolean;
{$IFDEF MSWINDOWS}
var
  Code: Cardinal;
  Handle: THandle;
  LastError: Cardinal;
begin
  Result := False;
  Code := GetFileAttributes(PChar(Directory));

  if Code <> INVALID_FILE_ATTRIBUTES then
  begin
    ...
    //more code
    ...
  end
  else
  begin
    LastError := GetLastError;
    Result := (LastError <> ERROR_FILE_NOT_FOUND) and
      (LastError <> ERROR_PATH_NOT_FOUND) and
      (LastError <> ERROR_INVALID_NAME) and
      (LastError <> ERROR_BAD_NETPATH);
  end;
end;
{$ENDIF MSWINDOWS}

As you see if the GetFileAttributes function call fails, the result of the GetLastError method is compared against a set of possible values. but in your case passing a invalid path will return a ERROR_BAD_PATHNAME (161) code, so the function returns True.

like image 131
RRUZ Avatar answered Sep 22 '22 13:09

RRUZ