Recent

Author Topic: How to know if a file/folder is a Symbolic Link (Mac OS X/Unix)  (Read 10647 times)

sinfoni

  • Jr. Member
  • **
  • Posts: 56
Hi all,

I have a function scanning the whole hard drive of a Mac. It works recursively through all directories. Some files are symbolic links pointing on the directory which contains it, that is:

/dirA/dirB/SymbolicLink where SymbolicLink is pointing to dirA.

So, the function enters in an infinite loop.

A symbolic link has a special attribute "L" that I can see in a terminal window. Is there any way in lazarus to read this attriubte ? (for a folder, I use faDirectory).

thanks in advance,
André.   

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: How to know if a file/folder is a Symbolic Link (Mac OS X/Unix)
« Reply #1 on: June 29, 2010, 05:26:30 am »
(for a folder, I use faDirectory).
try "faSymLink" for Symbolic Links

sinfoni

  • Jr. Member
  • **
  • Posts: 56
Re: How to know if a file/folder is a Symbolic Link (Mac OS X/Unix)
« Reply #2 on: June 29, 2010, 06:36:57 am »
thanks a lot  :D

sinfoni

  • Jr. Member
  • **
  • Posts: 56
Re: How to know if a file/folder is a Symbolic Link (Mac OS X/Unix)
« Reply #3 on: June 30, 2010, 12:20:04 am »
Damn, it doesn't work !

For example in /Developer/Platforms/MacOSX.platform/ I have these entries
drwxrwxr-x  3 root  admin    102 18 mai  2009 Developer
-rw-rw-r--  1 root  admin    984 24 sep  2009 Info.plist
-rw-rw-r--  1 root  admin  38149 24 sep  2009 MacOSX.icns
-rw-rw-r--  1 root  admin    485 24 sep  2009 ResourceRules.plist
-rw-rw-r--  1 root  admin    402 24 sep  2009 version.plist

Developer is NOT a symbolic link but my code see it as a symbolic link.

Here is my code. What is wrong ?

Code: [Select]
Procedure ListAllFichiers(chemin: AnsiString; niveau : Tstrings);
  Type
  TUnixFindData = Record
    NamePos    : LongInt;     {to track which search this is}
    DirPtr     : Pointer;     {directory pointer for reading directory}
    SearchSpec : String;
    SearchType : Byte;        {0=normal, 1=open will close, 2=only 1 file}
    SearchAttr : Byte;        {attribute we are searching for}
  End;
  PUnixFindData = ^TUnixFindData;
Var S:TSearchRec;
    vc_ext: Ansistring;
    F: TextFile;
    vi_i, vi_numdir, vi_numfile: Integer;
    vl_next, vl_ext: boolean;

  I: Integer;
   UnixFindData : PUnixFindData;
Begin
    vi_numdir := 0;
    vi_numfile := 0;
    Chemin:=IncludeTrailingPathDelimiter(chemin);
   
    If FindFirst(Chemin+'*',faAnyFile,S) = 0   Then Begin
      Repeat
        UnixFindData:=PUnixFindData(S.FindHandle);

           // Don't scan  . and .. !
           If (S.Name<>'.')And(s.Name<>'..') then Begin
               // Folder ? => recursive call
               If (S.Attr And faDirectory)<>0 Then begin
                 // Ignore symbolic links
                  If ((((UnixFindData^.searchattr)) and faSymlink) <> 0) Then begin
                      trace(S.name); // write entry in a debug log file
                  end
                  else begin // scan it
                     inc(vi_numdir);
                     niveau[niveau.count - 1] := IntToStr(vi_numdir);
                     list(chemin[1], niveau,'R,' + S.Name); // Write entry in a file
                     niveau.Append('1');
                     ListAllFichiers(Chemin+S.Name, niveau);
                     niveau.Delete(niveau.count - 1);
                  end;
               end
               // It is a file
               Else begin
                    list(chemin[1], niveau,'F,' + S.Name); // Write entry in a file
               end;
        end;
      // Recherche du suivant
      Until FindNext(S)<>0;
      FindClose(S);
    End;
End;

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: How to know if a file/folder is a Symbolic Link (Mac OS X/Unix)
« Reply #4 on: June 30, 2010, 12:47:28 am »
1) add faSymLink to the search mask
Code: [Select]
FindFirst(Chemin+'*',faAnyFile or faSymLink,S)

2) check for S.Attr for the file (dir) flags, not (UnixFindData^.searchattr)

Code: [Select]
If (s.Attr and faSymlink) <> 0 Then begin
  trace(S.name); // write entry in a debug log file

3) check for faSymLink first, because faDirectory and faSymLink are not set together.
« Last Edit: June 30, 2010, 12:49:45 am by skalogryz »

sinfoni

  • Jr. Member
  • **
  • Posts: 56
Re: How to know if a file/folder is a Symbolic Link (Mac OS X/Unix)
« Reply #5 on: June 30, 2010, 01:11:42 am »
Seems to work better. thanks angain.
André.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: How to know if a file/folder is a Symbolic Link (Mac OS X/Unix)
« Reply #6 on: June 30, 2010, 01:28:10 am »
Seems to work better. thanks angain.

It seems you've found rtl bug. Reported: http://bugs.freepascal.org/view.php?id=16817

 

TinyPortal © 2005-2018