How to acess ctime, mtime, … of a symbolic link?
Asked Answered
L

3

6

On unix symlinks are pointers to another file. Not only the file but also the symlink has a ctime, mtime, …. I know the symlinks time can be accessed, as ls displays it. If I use one of ruby's File#ctime, File#mtime, …, I always get the attribute of the file the symlink is pointing to, not of the symlink. How can I read this values in ruby? If this is not possible in ruby, tell me how to do it in C. I would write my own c extension in that case.

Lobotomy answered 5/1, 2010 at 22:56 Comment(0)
R
10

Use File#lstat(). Example:

# This is a dummy symlink; there's no file named "foo".
ln -s foo bar

# Run irb.
irb(main):001:0> File.lstat("bar")
=> #<File::Stat dev=0x801, ino=90113, mode=0120777, nlink=1, uid=1000, gid=1000, rdev=0x0, size=3, blksize=4096, blocks=0, atime=2010-01-05 17:59:06 -0500, mtime=2010-01-05 17:59:05 -0500, ctime=2010-01-05 17:59:05 -0500>

# Get the mtime of the link.
irb(main):002:0> File.lstat("bar").mtime
=> 2010-01-05 17:59:05 -0500
Rune answered 5/1, 2010 at 23:2 Comment(0)
T
1

lstat() can do it in C; not sure if there's a Ruby equivalent.

Torrent answered 5/1, 2010 at 23:0 Comment(1)
There seems to be a File.lstat in ruby, which does the same.Lobotomy
R
1

There're not only the attributes of the symlink and the attributes of the final target, but also, if the symlink is itself to another symlink, one or more intermediate steps; to get all the attributes, you'd need to do lstats in a readlink loop.

Reginiaregiomontanus answered 6/1, 2010 at 1:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.