如何在 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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Perl 中,您可以使用 readlink() 函数来查找符号链接的目标。
您还可以使用
lstat()
函数读取符号链接的权限(与stat()
不同,后者将读取符号链接指向的文件的详细信息)。实际上,如果没有额外的帮助,就无法在新符号链接上设置所有权,因为 Perl 不会公开
lchown()
系统调用。 为此,您可以使用 CPAN 中的 PerlLchown
模块。假设有足够的权限(注意:未经检查的代码)
您无需担心符号链接的权限 - 它们始终显示为
-rwxrwxrwx
In Perl you can use the
readlink()
function to find out the destination of a symlink.You can also use the
lstat()
function to read the permissions of the symlink (as opposed tostat()
which will read the details of the file pointed to by the symlink).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 PerlLchown
module from CPAN.Assuming sufficient permissions (nb: unchecked code)
You don't need to worry about the permissions on the symlink - they always appear as
-rwxrwxrwx
模块 File::Copy::Recursive 负责处理的。 默认情况下,它将复制符号链接并尝试保留所有权。
The module File::Copy::Recursive takes care of that. By default it will copy symlinks and try to preserve ownership.
根据您关于绝对目标路径与相对目标路径、目录层次结构等的用例,使用 link 创建指向您要复制的符号链接的硬链接可能也已经足够了。 Perl 在许多平台上也支持创建硬链接,不需要额外的包。
Depending on your use case regarding absolute vs. relative target paths, dir hierarchy etc., using link to create a hardlink to the symlink you want to copy might be enough already as well. No extra packages needed and creating hardlinks is supported by Perl on many platforms as well.