Sign in Join

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

Mailing lists / fpc-pascal / 2007-08

Target path of a SymLink file

6 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Graeme Geldenhuys

Hi,

Is there a function in SysUtils or some other place in RTL that I can use to extract the target path of a symlink file? I'm using FindFirst/FindNext. The sr.Attrib and faSymLink is True, but I can't (in a easy way) see how I can actually find out where it points to. I'm thinking x-platform here.

Something like ExtractSymLinkPath(). :) Thinking of other available functions like ExpandUNCFileName() or ExtractFilePath() or ExtractRelativePath() etc..

I have the following code that works, but then I need to wrap it in IFDEF's so it doesn't execute under Windows. I don't want IFDEF's in my code! I'm hoping RTL has something already and just returns a blank string for Windows, just like the faSymLink which means nothing under Windows, yet is available.

    var
      info : _stat;
    .....
    fullname := FDirectoryName + e.Name;
    if lstat(PChar(fullname),info) = 0 then
    begin
      e.IsLink := ((sr.Mode and $F000) = $A000);
      if e.IsLink then
      begin
        SetLength(e.LinkTarget, MAX_PATH);  // length was 256
        r := libc.readlink(PChar(fullname),@(e.LinkTarget[1]),
sizeof(e.LinkTarget));
        if r > 0 then
          SetLength(e.LinkTarget, r)
        else
          e.LinkTarget := '';
        libc.stat(PChar(fullname), info);
      end;

Graeme.

Reply by email

Vincent Snijders
to Graeme Geldenhuys
Graeme Geldenhuys wrote 17 lines
Hi, Is there a function in SysUtils or some other place in RTL that I can use to extract the target path of a symlink file? I'm using FindFirst/FindNext. The sr.Attrib and faSymLink is True, but I can't (in a easy way) see how I can actually find out where it points to. I'm thinking x-platform here. Something like ExtractSymLinkPath(). :) Thinking of other available functions like ExpandUNCFileName() or ExtractFilePath() or ExtractRelativePath() etc.. I have the following code that works, but then I need to wrap it in IFDEF's so it doesn't execute under Windows. I don't want IFDEF's in my code! I'm hoping RTL has something already and just returns a blank string for Windows, just like the faSymLink which means nothing under Windows, yet is available.

If you cannot find it in the RTL, consider submitting ReadAllLinks from the LCL's fileutil unit to the FPC team. At least it doesn't depend on libc (directly).

Vincent

Reply by email

Graeme Geldenhuys
to Vincent Snijders
Vincent Snijders wrote
If you cannot find it in the RTL, consider submitting ReadAllLinks from the LCL's fileutil unit to the FPC team. At least it doesn't depend on libc (directly).

Thanks Vincent. I'll go have a look.

In the mean time I grep searched all the FPC src and couldn't find such a function. For now I implemented some $I included file trickery to avoid IFDEF's in my code.

Lets go see what LCL did.. :)

Regards,
- Graeme -

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 10 lines
On 15/08/07, Vincent Snijders wrote: > > If you cannot find it in the RTL, consider submitting ReadAllLinks from the LCL's > fileutil unit to the FPC team. At least it doesn't depend on libc (directly). Thanks Vincent. I'll go have a look. In the mean time I grep searched all the FPC src and couldn't find such a function. For now I implemented some $I included file trickery to avoid IFDEF's in my code.

You must have missed

fpReadLink

in baseunix.

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote 7 lines
> In the mean time I grep searched all the FPC src and couldn't find > such a function. For now I implemented some $I included file trickery > to avoid IFDEF's in my code. You must have missed fpReadLink

My search string had the string 'sym' in it. :-(

Graeme.

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 10 lines
On 15/08/07, Michael Van Canneyt wrote: > > In the mean time I grep searched all the FPC src and couldn't find > > such a function. For now I implemented some $I included file trickery > > to avoid IFDEF's in my code. > > You must have missed > > fpReadLink My search string had the string 'sym' in it. :-(

You are too impatient. These electrons have a long way to walk to get your mail to the server, from server to me, and then all the way back with my answer. On top of that, I live up north, so they have to walk uphill all the time.

If you were an electron, you'd get pretty tired too.

;-)

Michael.

Reply by email