开发者

How to copy a file in creation without destroying it in Unix/Linux

I 开发者_开发问答have a file under creation in a long process. To my understanding copying that file to another file would kill the creation process. Is there away to overcome this problem?


No, reading from a file that is currently being written to will not disrupt that writing process. However, the copy you take will be incomplete, as obviously you can't copy parts that haven't been written yet. Only the first file will get subsequent writes, not the copy, so if you want the entire file, wait until the writing is complete.


Unless the second process uses a file descriptor associated with the same open file description as the process creating the file, there will be no problem. If the file descriptor does share the same open file description, then the read process will move the file position (as will the write process), leading to confusion. However, that requires the processes to have a common ancestor that opened the file (for example, the copier is forked by the creator). This is unlikely to be the case, I think, from your description.

Your copying process can do as tail -f does and read up to the first EOF, then pause and read again when there is more data available, repeating until told to stop, or it can determine that the file is complete. If it has no way of telling when the file is complete, then you'll have to kill it (interrupt, or worse).


Depending on what exactly the semantics of your operation should be, links may be what you need. Check out the behaviour of symbolic and hard links. One of the two may be exactly what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜