如何访问ctime、mtime、…符号链接?

发布于 2024-08-17 02:20:27 字数 229 浏览 5 评论 0原文

在 UNIX 上,符号链接是指向另一个文件的指针。不仅文件而且符号链接都有 ctime、mtime、...。我知道可以访问符号链接时间,正如 ls 显示的那样。如果我使用 ruby​​ 的 File#ctime 、 File#mtime 等之一,我总是获得符号链接指向的文件的属性,而不是符号链接的属性。我怎样才能在红宝石中读取这个值?如果这在 ruby​​ 中不可能,请告诉我如何在 C 中做到这一点。在这种情况下,我会编写自己的 c 扩展。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昔日梦未散 2024-08-24 02:20:27

使用File#lstat()。例子:

# 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

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
笑梦风尘 2024-08-24 02:20:27

lstat() 可以用 C 实现;不确定是否有 Ruby 等效项。

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

耳钉梦 2024-08-24 02:20:27

不仅有符号链接的属性和最终目标的属性,而且如果符号链接本身是另一个符号链接,则还有一个或多个中间步骤;要获取所有属性,您需要在 readlink 循环中执行 lstats。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文