bug-tar
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-tar] tar-wish: -k avoid error messages


From: Helmut Waitzmann
Subject: Re: [Bug-tar] tar-wish: -k avoid error messages
Date: Tue, 15 Feb 2005 17:38:27 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Yotam Medini <address@hidden> writes:

>It would be nicer if the check for pre-existing file, would occur 
>_before_ trying to open. Something like:
>
>
>     if (
>         (old_files_option == KEEP_OLD_FILES) &&     
>         (stat(file_name,&dumbuf) == 0)
>        )
>     {
>        if (verbose_option) { WARN("keeping old %s\n", file_name); }
>     }
>     else
>     {
>         fd = open (file_name, openflag, mode);
>         ....
>     }
>

No, that's unsafe:  If you stat before opening the file, then you
get a race condition between tar and some other process creating the
file just after tar has statted but before has opened the file.

To be safe, tar must open the file exclusively, so the open will fail if
the file already exists:

open (file_name, openflag | O_EXCL, mode);

On NFS file systems, even this is broken, as can be read in the open(2)
manual page:

 O_EXCL
        When used with O_CREAT, if the file  already  exists  it  is  an
        error  and  the open will fail. In this context, a symbolic link
        exists, regardless of where its points to.  O_EXCL is broken  on
        NFS file systems, programs which rely on it for performing lock-
        ing tasks will contain a race condition.  The solution for  per-
        forming  atomic  file  locking  using  a lockfile is to create a
        unique file on the same fs  (e.g.,  incorporating  hostname  and
        pid),  use  link(2)  to  make  a link to the lockfile. If link()
        returns 0, the lock is successful.  Otherwise,  use  stat(2)  on
        the  unique  file to check if its link count has increased to 2,
        in which case the lock is also successful.

-- 
Wenn Sie mir E-Mail schreiben, stellen |  When writing me e-mail, please
Sie bitte vor meine E-Mail-Adresse     |  precede my e-mail address with
meinen Vor- und Nachnamen, etwa so:    |  my full name, like
Helmut Waitzmann <address@hidden>, (Helmut Waitzmann) address@hidden




reply via email to

[Prev in Thread] Current Thread [Next in Thread]