如何在 Perl 中复制符号链接? [英] How do I copy symbolic links in Perl?

查看:45
本文介绍了如何在 Perl 中复制符号链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Perl 程序中复制符号链接(而不是它指向的文件),同时保留所有符号链接属性(例如所有者和权限)?

How do I copy a symbolic link (and not the file it points to) in a Perl program while preserving all of the symbolic link attributes (such as owner and permissions)?

推荐答案

在 Perl 中,您可以使用 readlink() 函数来找出符号链接的目的地.

In Perl you can use the readlink() function to find out the destination of a symlink.

您还可以使用 lstat() 函数来读取符号链接的权限(与 stat() 相反,它将读取指向的文件的详细信息通过符号链接).

You can also use the lstat() function to read the permissions of the symlink (as opposed to stat() which will read the details of the file pointed to by the symlink).

实际上,如果没有额外的帮助,就无法在新符号链接上设置所有权,因为 Perl 不公开 lchown() 系统调用.为此,您可以使用 CPAN 中的 Perl Lchown 模块.

Actually setting the ownership on the new symlink can't be done without extra help as Perl doesn't expose the lchown() system call. For that you can use the Perl Lchown module from CPAN.

假设有足够的权限(注意:未经检查的代码)

Assuming sufficient permissions (nb: unchecked code)

 use Lchown;
 my $old_link = 'path to the symlink';
 my $new_link = 'path to the copy';

 my $dst = readlink($old_link);
 my @stat = lstat($old_link);

 symlink $dst, $new_link;
 lchown $stat[4], $stat[5], $new_link;  # set UID and GID from the lstat() results

您无需担心符号链接的权限 - 它们始终显示为 -rwxrwxrwx

You don't need to worry about the permissions on the symlink - they always appear as -rwxrwxrwx

这篇关于如何在 Perl 中复制符号链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆