Ruby: FileUtils.cp truncates file; FileUtils.mv it

2019-07-18 22:36发布

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条回答
男人必须洒脱
2楼-- · 2019-07-18 23:04

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.

查看更多
登录 后发表回答