Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-02

SysUtils.FileExists bug under Go32v2

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Iván Montes

Hi,

I think I've found a bug on the FileExists function (sysutils) under Go32v2, when I want to check if a directory exists.

FileExists( 'c:\winnt' ) will return False when compiled for Go32 but true for Win32.
I guess that the Win32 version is doing rigth, since a directory is part of the file system.

I've checked a recent CVS copy of the sources and I found this

[go32v2/sysutls.pp]

Function FileExists (Const FileName : String) : Boolean;
var Handle: longint;
begin
  //!!   This can be done quicker, need to find out how
  Result := (OpenFile(FileName, Handle, ofRead, faOpen) = 0);
  if Handle  0 then
    FileClose(Handle);
end;

this could be changed for example to:

Function FileExists (Const filename :String) :Boolean;
begin
   //FileGetAttrib will return -1 if the file was not found
   //but I don't know if it will return -1 if the file exists but it's open
   result := boolean( FileGetAttr( filename ) XOR -1 );
end;

hope this helps,
Ivan

Reply by email

Anton Tichawa
to Iván Montes

Hello, Ivan!

Show quoted text 6 lines
Function FileExists (Const filename :String) :Boolean; begin //FileGetAttrib will return -1 if the file was not found //but I don't know if it will return -1 if the file exists but it's open result := boolean( FileGetAttr( filename ) XOR -1 ); end;

I'll have a look at that ..

Anton Tichawa.

Reply by email

Anton Tichawa
to Iván Montes: I think I've found a bug on the FileExists function (sysutils) under Go32v2, when I want…

Hello!

The following should work under 32-bit windows-compatible os's:

***
Function FileExists (Const filename :String) :Boolean;
var
  S: TSEARCHREC;
begin
  R := FindFirst(Filename, faAnyFile, S);
  Result := R = 0;
  if R = 0 then FindClose(S);
end;
***

Status: Untested.

Caution: The circumstances under which FindClose should be called are not clearly formulated by the original manufacturer. My procedure is just a proposal. I saw a system hanging several seconds or maybe even crashing when I called, or missed to call, FindClose. The fact that the condition R = 0 has to be tested twice, shows the poor quality of the API.

BTW: How and where can I obtain the original microsoft 32-bit windows API help files, and what names do they have? I thing *.chm or so?

HTH

Anton Tichawa.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Reply by email

Iván Montes

Hi,

I think I've found a bug on the FileExists function (sysutils) under Go32v2, when I want to check if a directory exists.

FileExists( 'c:\winnt' ) will return False when compiled for Go32 but true for Win32.
I guess that the Win32 version is doing rigth, since a directory is part of the file system.

I've checked a recent CVS copy of the sources and I found this

[go32v2/sysutls.pp]

Function FileExists (Const FileName : String) : Boolean;
var Handle: longint;
begin
  //!!   This can be done quicker, need to find out how
  Result := (OpenFile(FileName, Handle, ofRead, faOpen) = 0);
  if Handle  0 then
    FileClose(Handle);
end;

this could be changed for example to:

Function FileExists (Const filename :String) :Boolean;
begin
   //FileGetAttrib will return -1 if the file was not found
   //but I don't know if it will return -1 if the file exists but it's open
by another program
   result := boolean( FileGetAttr( filename ) XOR -1 );
end;

hope this helps,
Ivan

Reply by email