Ruby:FileUtils.cp截断文件; FileUtils.mv它不? [英] Ruby: FileUtils.cp truncates file; FileUtils.mv it does not?

查看:252
本文介绍了Ruby:FileUtils.cp截断文件; FileUtils.mv它不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我这是很奇怪的...我无法弄清楚为什么这样做的生活。我已经得到了一个充满各种CoffeeScript,SASS,HTML和XML文件的文件夹。
  2. 我有一个 Ruby脚本,将所有脚本全部编译,并将其缩小为一个主XML文件(用于iGoogle Gadget开发)。

    • 这个脚本使用 trollop 命令行参数(我只在下面阐明我的代码)。 >

  3. 我希望这个脚本将这个文件从创建它的当前目录复制到它将运行的目标目录。 b $ b

到目前为止,构建/编译/缩小步骤就像魔术一样运行。 #b
$ b

 #!/ usr / bin / ruby​​ 
...
if opts [:deploy_local]
FileUtils.cp'build.xml','/ path / to / destination /'
将复制的#{写入的文件名}复制到#{output_destination}。如果opts [:verbose]
end

当复制文件时,目标文件是通过它的方式截断3/4。源文件就好了。但是,移动文件就像一个魅力,出于某种奇怪的原因。

  FileUtils.mv' build.xml','/ path / to / destination /'

如果我只是做一个系统副本,它也会被截断。

  system(cp build.xml / path / to / destination )

FWIW,我从 zsh运行这个脚本而不是 bash 。在这两种情况下(复制和移动),源文件和目标文件都没有被其他进程使用。

有人可以解释这个奇怪的行为吗? $ b

解决方案

一些事情:


  • 移动到相同的磁盘卷?如果是这样,那么,是的,凸轮关于原子性的评论是绝对正确的。操作系统可能只是在移动过程中搞乱inode表,而不是写出数据。如果你正在移动卷之间的数据,那么它不会那么简单。

  • 你试过传递

     :verbose =>真正的

    到FileUtils.cp命令?这可能会给出关于失败的诊断。


This is weird… and I can't figure out for the life of me why it's doing it this way.

  1. I've got a folder full of various CoffeeScript, SASS, HTML, and XML files.
  2. I've got a Ruby script that's taking them all, compiling them, and minifying them into one master XML file (it's for iGoogle Gadget development).
    • This script takes command line args using trollop (I only state this to clarify my code below).
  3. I want this script to copy this file from the current directory where it's created to a destination directory where it will be run.

So far, the building/compiling/minifying step runs like magic. It's #3 that's borked to Twilight Zone-level.

#!/usr/bin/ruby
…
if opts[:deploy_local]
  FileUtils.cp 'build.xml', '/path/to/destination/'
  puts "Copied #{written_file_name} to #{output_destination}." if opts[:verbose]
end

When this copies the file, the destination file is truncated about 3/4 of the way through it. The source file is just fine. However, moving the file works like a charm, for some strange reason.

  FileUtils.mv 'build.xml', '/path/to/destination/'

To add another level of weirdness, if I just do a system copy, it also gets truncated.

  system("cp build.xml /path/to/destination")

FWIW, I'm running this script from zsh and not bash. In both instances (copying and moving) the source and destination files are not in use by any other process.

Can anybody explain this freaky behavior?

解决方案

A few things:

  • Are you moving to the same disk volume? If so, then, yeah, cam's comment about atomicity is definitely true; the OS is probably just messing with the inode table during a move, as opposed to writing out the data. IF you're moving the data between volumes, then it wouldn't be so simple.

  • Have you tried passing

    :verbose => true
    

    to the FileUtils.cp command? That might give a diagnostic about the failure.

这篇关于Ruby:FileUtils.cp截断文件; FileUtils.mv它不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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