How to detect symlinks?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to detect symlinks?

Rena
How with Lua/LFS can I tell if a file is a symlink? LFS has
symlinkattributes, but that only gives information about the link
itself, with no apparent way to tell if the file is actually a link or
not. (Given a non-symlink, it behaves identically to lfs.attributes.)
Can io.open be told not to follow links?

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: How to detect symlinks?

Jeff Pohlmeyer
On Sun, Apr 29, 2012 at 11:35 PM, Rena <[hidden email]> wrote:

> How with Lua/LFS can I tell if a file is a symlink? LFS has
> symlinkattributes, but that only gives information about the link
> itself, with no apparent way to tell if the file is actually a link or
> not. (Given a non-symlink, it behaves identically to lfs.attributes.)


I think this might work:

function IsSymLink(filename)
  local a = lfs.attributes(filename)
  local s = lfs.symlinkattributes(filename)
  return a and s and ( a.dev ~= s.dev or a.ino ~= s.ino )
end

 - Jeff

Reply | Threaded
Open this post in threaded view
|

Re: How to detect symlinks?

Rena
On Sun, Apr 29, 2012 at 23:57, Jeff Pohlmeyer <[hidden email]> wrote:

> On Sun, Apr 29, 2012 at 11:35 PM, Rena <[hidden email]> wrote:
>
>> How with Lua/LFS can I tell if a file is a symlink? LFS has
>> symlinkattributes, but that only gives information about the link
>> itself, with no apparent way to tell if the file is actually a link or
>> not. (Given a non-symlink, it behaves identically to lfs.attributes.)
>
>
> I think this might work:
>
> function IsSymLink(filename)
>  local a = lfs.attributes(filename)
>  local s = lfs.symlinkattributes(filename)
>  return a and s and ( a.dev ~= s.dev or a.ino ~= s.ino )
> end
>
>  - Jeff
>

Thanks, that appears to work. Still is there a way to open the link
itself instead of the file it points to?

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: How to detect symlinks?

Jeff Pohlmeyer
On Mon, Apr 30, 2012 at 1:04 AM, Rena <[hidden email]> wrote:

> Thanks, that appears to work.

It also looks like the "mode" string for symlinkattributes will return
"link" for symbolic
links, so that would probably be simpler (and more self-documenting.)


> Still is there a way to open the link itself instead of the file it points to?

Just curious, why would you want to do that?

 - Jeff

Reply | Threaded
Open this post in threaded view
|

Re: How to detect symlinks?

Rena
On Mon, Apr 30, 2012 at 00:11, Jeff Pohlmeyer <[hidden email]> wrote:
> On Mon, Apr 30, 2012 at 1:04 AM, Rena <[hidden email]> wrote:
>
>> Thanks, that appears to work.
>
> It also looks like the "mode" string for symlinkattributes will return
> "link" for symbolic
> links, so that would probably be simpler (and more self-documenting.)
>
Ah, did I miss that or is it not documented?

>
>> Still is there a way to open the link itself instead of the file it points to?
>
> Just curious, why would you want to do that?
>
>  - Jeff
>
I'm iterating a filesystem recursively, comparing backups to the
original files to verify integrity. Following links means potentially
getting stuck in a loop (as I found the hard way). The backups are
made with rsync, preserving links, so I think it should be enough to
verify the links themselves (i.e. ensure that the link points to the
correct file).

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: How to detect symlinks?

Jeff Pohlmeyer
On Mon, Apr 30, 2012 at 1:16 AM, Rena <[hidden email]> wrote:
> On Mon, Apr 30, 2012 at 00:11, Jeff Pohlmeyer <[hidden email]> wrote:

>> It also looks like the "mode" string for symlinkattributes will
>> return "link" for symbolic links

> Ah, did I miss that or is it not documented?

It's more-or-less documented, but the docs really don't explain if the
string "link" refers to hard or symbolic links, or that it only works
with symlinkattributes.


> I'm iterating a filesystem recursively, comparing backups to the
> original files to verify integrity.

I usually just use "diff -rq" for that.


 - Jeff

Reply | Threaded
Open this post in threaded view
|

Re: How to detect symlinks?

Miles Bader-2
Jeff Pohlmeyer <[hidden email]> writes:
> It's more-or-less documented, but the docs really don't explain if the
> string "link" refers to hard or symbolic links, or that it only works
> with symlinkattributes.

With unix-style hardlinks, it's literally impossible to distinguish a
"hardlink" from the linked-to object, so it _must_ refer to
symlinks...  :]

-miles

--
In New York, most people don't have cars, so if you want to kill a person, you
have to take the subway to their house.  And sometimes on the way, the train
is delayed and you get impatient, so you have to kill someone on the subway.
  [George Carlin]

Reply | Threaded
Open this post in threaded view
|

Re: How to detect symlinks?

Tony Finch
In reply to this post by Rena
Rena <[hidden email]> wrote:
>
> Still is there a way to open the link itself instead of the file it
> points to?

Sounds like you want readlink() but it looks like LuaFileSystem doesn't
include a binding for that system call.

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Wight, Portland: South 6 to gale 8, occasionally severe gale 9 at first in
Portland, backing east 5, then becoming cyclonic 5 to 7, perhaps gale 8 later.
Rough or very rough, becoming moderate. Rain or thundery showers. Good,
occasionally poor.

Reply | Threaded
Open this post in threaded view
|

Re: How to detect symlinks?

steve donovan
On Mon, Apr 30, 2012 at 12:03 PM, Tony Finch <[hidden email]> wrote:
> Sounds like you want readlink() but it looks like LuaFileSystem doesn't
> include a binding for that system call.

luaposix's posix.readlink does exactly what you need then; it only
returns non-nil for actual symbolic links.

steve d.