Ruby: FileUtils.cp truncates file; FileUtils.mv it does not?

本小妞迷上赌 提交于 2020-02-04 05:03:21

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6459553/ruby-fileutils-cp-truncates-file-fileutils-mv-it-does-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!