RFC: alias tar="tar --backup" ?

Micah Cowan micahcowan at ubuntu.com
Fri May 18 03:16:17 UTC 2007

Soren Hansen wrote:
> On Thu, May 17, 2007 at 06:03:18PM -0700, Micah Cowan wrote:
>>> A completely different approach could be that the calls that
>>> actually write to a file check that the file does not exist. You
>>> could activate this with a system-wide flag, but I strongly suspect
>>> that this would be more work than the few words it took for me to
>>> write the idea.
>> The call of which you speak (open()) already does this, unless you
>> tell it that you specifically wish to overwrite files (which tar
>> does).
> That's not quite right.
> Usually, you'll just pass the O_CREAT and O_TRUNC flags to open(2) in
> order to just open the file (creating it if it doesn't exist already),
> while you have to explicitly give it the O_EXCL flag to bail out if the
> file already exists.

True, my bad: you do have to explicitly ask it not to overwrite. I was
thinking of O_TRUNC for some reason, but of course, without O_TRUNC it
will still overwrite the existing file (just without truncating it first).

> Passing --backup to tar makes it check if it should
> make a backup of the file, does so, and then proceeds to open the file
> with (O_WRONLY | O_BINARY | O_CREAT | O_TRUNC) unless you pass it '-k',
> in which case O_TRUNC is replaced with O_EXCL.

Ouch, so it does. That's broken (race condition). OTOH, I've racked my
brain to come up with an alternate solution, and they all have race
conditions (or else copy to a backup instead of move, which may not be
an improvement over the race condition), so maybe it's not broken.

Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: <https://lists.ubuntu.com/archives/ubuntu-devel-discuss/attachments/20070517/ea6163f5/attachment.sig>

More information about the Ubuntu-devel-discuss mailing list