简体   繁体   English

如何在ruby脚本中复制损坏的符号链接

[英]how to copy broken symlinks in ruby script

I want to copy all the contents from one directory to another (including broken symlinks) in my Ruby script. 我想在我的Ruby脚本中将所有内容从一个目录复制到另一个目录(包括损坏的符号链接)。 I am using FileUtils.cp_r 'src/.', 'dest' but it is complaining about the broken symlinks. 我正在使用FileUtils.cp_r 'src/.', 'dest'但它抱怨符号链接损坏。 Can someone please help me with this? 有人可以帮我吗? It is a show-stopper for me right now. 现在对我来说,这是个表演秀。

FileUtils.cp_r internally copies the src folder recursively to dest . FileUtils.cp_r内部将src文件夹递归复制到dest When it finds a symlink, it will create a symlink using File#symlink method (Refer line 1369 of fileutils.rb ). 找到符号链接后,它将使用File#symlink方法创建符号File#symlink (请参考fileutils.rb的第1369行)。

The documentation of File#symlink states that: File#symlink的文档指出:

Creates a symbolic link called new_name for the existing file old_name. 现有文件 old_name创建一个名为new_name的符号链接。 Raises a NotImplemented exception on platforms that do not support symbolic links. 在不支持符号链接的平台上引发NotImplemented异常。

So, it seems that it may not be possible to use FileUtils.cp_r to copy directories if the one of the symlinks in it is broken and pointing to a non-existing file. 因此,似乎如果FileUtils.cp_r中的其中一个符号链接中断并指向一个不存在的文件,则可能无法使用FileUtils.cp_r复制目录。

Workaround 解决方法

You can execute shell command cp -r command from your ruby script, it may not be platform-independent code and may not be easy to debug, but it will help you to get around the given scenario which you consider to be a show-stopper . 您可以从ruby脚本中执行shell命令cp -r命令,它可能不是平台无关的代码,并且可能不容易调试,但是它将帮助您解决给定的场景(您认为它是个制止者)

src = "/path/to/src/dir"
dest = "/path/to/dest/dir"

`cp -r #{src} #{dest}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM