Forum > General
How to know if a file/folder is a Symbolic Link (Mac OS X/Unix)
sinfoni:
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:
--- Quote from: sinfoni on June 29, 2010, 05:23:25 am ---(for a folder, I use faDirectory).
--- End quote ---
try "faSymLink" for Symbolic Links
sinfoni:
thanks a lot :D
sinfoni:
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: ---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;
--- End code ---
skalogryz:
1) add faSymLink to the search mask
--- Code: ---FindFirst(Chemin+'*',faAnyFile or faSymLink,S)
--- End code ---
2) check for S.Attr for the file (dir) flags, not (UnixFindData^.searchattr)
--- Code: ---If (s.Attr and faSymlink) <> 0 Then begin
trace(S.name); // write entry in a debug log file
--- End code ---
3) check for faSymLink first, because faDirectory and faSymLink are not set together.
Navigation
[0] Message Index
[#] Next page