On 2016-11-04, Hongyi Zhao <
hongy...@gmail.com> wrote:
> Hi all,
>
> I've a huge file which is being created by some commands / cripts, say mv/
> cp and etc., I want to lock the file to avoiding it been read/written
> till the file is completely created.
>
> How to achieve this job?
The easiest thing is to use a temporary file which has a properly
"anonymized" name.
If the file being created is a name like /tmp/tmp.uEqAKULVJ7
or whatever, then only a maliciously crafted program (running
as the same user or root) will interfere.
The legitimate programs which are looking for "outputfile"
will not see anything until your script executes
mv /tmp/tmp.uEqAKULVJ7 outputfile
the file is then "published".
You probably don't even need a fancy name in /tmp; just any
name that the other programs aren't looking for, like
"outputfile.inprogress" which gets renamed to "outputfile"
on completion.