Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What is the effect of copying a file say fileA.big (900mb) from location B to locationC. If during that cp operation, say 35% through the process, fileA.big is appended with new information and grows from 900mb to 930mb.

What is the result of the end copy (i.e. fileA.big at locationC)?

What if the copy is about 70% through, and the original file is updated but this time truncated to 400mb (i.e. the progress of the copy is beyond the truncation point), what is the result of the end copy?

Referring to a linux OS on an ext3/ext4 filesystem. No volume shadow magic etc.. Just plain old cp. Curiousity sparked by copying live couchdb files for backup, but more interested in general scenarios rather than specific use case.

share|improve this question
    
Thanks for asking this one. My 'knowledge' was mostly a guess... until now. –  Tshepang Feb 5 '11 at 9:46
add comment

2 Answers

up vote 8 down vote accepted

If fileA.big is grown during the copy, the copy will include the data that was appended.

If the file is truncated shorter than where the copy is currently at, the copy will abort right where its at and the destination file will contain what was copied up to the time it aborted.

share|improve this answer
add comment

Patrick has it more or less correct, but here's why. The way you copy a file under UNIX works like this:

  1. Try to read some (more) bytes from fileA.
  2. If we failed to get bytes because we're at (or past) the end of the file, we're done; quit.
  3. Otherwise, write the bytes to fileB and loop back to step 1.

Knowing that, and knowing it's as simple as that, lets us see some corner cases.

As soon as we find the end of the file, the copy is done. So let's say our file is growing during the copy, but is growing more slowly than we're copying it. The copy program will keep going past the original file size, because by the time it gets there, there is more to the file. But at some point, it catches up with the end of the file, and it knows it's at the end because it can't read any more bytes right now. So it quits right there, even if the file is about to grow further.

If the file is truncated, the copy program says "Whoa, I'm past the end of the file!" and quits.

And if pieces of the file are being updated at random by, say, a database program :-), then your copy is going to be some mix of old and new data, because the data is not all copied at the same time. The result will probably be a corrupt copy, which is why it's not generally a good idea to make copies of live databases.

(That said, I'm not familiar with CouchDB, and it's possible to design a database to be resistant to this sort of corruption. But best to be absolutely sure.)

share|improve this answer
    
Good explanation. BTW, this has always surprised me why this is possible under UNIX-like OSes without getting the typical error message known from Windows ("Can't access file - file in use") You could not even play an MP3 file that was already deleted while playing it. Under Unix, you can (surprisingly) - with no problems at all. I guess UNIX-based OSes always work with backup copies of the files, so this is feasible. –  syntaxerror Sep 26 '13 at 16:31
1  
Actually, being able to read a deleted file comes from a different UNIX feature: under UNIX, files and filenames are different things. When you delete a file, what you are really doing is deleting a named "link" to the file. When a program opens a file, that also counts as a link. The system will delete the file itself only when it has no links left. –  Jander Sep 26 '13 at 23:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.