[fpc-pascal] FPC docs about FindFirst

Michael Van Canneyt michael at freepascal.org
Tue Sep 24 09:15:30 CEST 2019



On Tue, 24 Sep 2019, Anthony Walter wrote:

> Related,
>
> It would seem on Linux that FindFirst, FindNext or TSearchRec that none of
> them can properly detect a symbolic link. For example, if you wanted
> enumerate files and folder, and there exists a symbolic link inside one
> folder to some parent of said folder, it's not possible to detect leading
> to endless recursion. faSymLink currently isn't picked up. I have to use
> code like this:
>
> uses
>  BaseUnix;
>
> function IsSymLink(const Path: string): Boolean;
> var
>  Stat: TStat;
> begin
>  fpLstat(PChar(Path), Stat);
>  Result := fpS_ISLNK(Stat.st_mode);
> end;
>
> Which slows down the search a bit.

It works here. Did you specify faDirectory together with faSymlink ?

If you don't, directories will not get picked up since the directory
attribute is included in the resulting info (as windows does this too).

I.e. this won't work:

uses sysutils;

var
   info : TSearchrec;

begin
   if findfirst('*',faSymlink,Info)=0 then
     try
     repeat
       if (info.attr and faSymlink)<>0 then
         Writeln('symlink :',info.name)
     until findnext(info)<>0;
     finally
       findclose(info);
     end; 
end.

It will only pick up symlinked files.

But

uses sysutils;

var
   info : TSearchrec;

begin
   if findfirst('*',faSymlink or faDirectory,Info)=0 then
     try
     repeat
       if (info.attr and faSymlink)<>0 then
         Writeln('symlink :',info.name)
     until findnext(info)<>0;
     finally
       findclose(info);
     end;
end.

will report directories.

Tested in trunk and 3.0.4

Michael.


More information about the fpc-pascal mailing list