bug-coreutils
[Top][All Lists]
Advanced

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

mv -f may remove the destination file


From: Urs Thuermann
Subject: mv -f may remove the destination file
Date: 05 May 2005 09:27:40 +0200
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

When strace'ing GNU mv from fileutils-4.1 and from coreutils-5.2.1, I
see that mv checks with lstat(2) for the existance of the destination
file before calling rename(2) which would unlink the destination file:

    $ strace mv foo bar
    ...
    umask(0)                            = 022
    stat64("bar", 0xbffffa38)           = -1 ENOENT (No such file or directory)
    brk(0x8053000)                      = 0x8053000
    lstat64("foo", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
    lstat64("bar", 0xbffff904)          = -1 ENOENT (No such file or directory)
    rename("foo", "bar")                = 0
    _exit(0)                            = ?
    
This check, however, is not sufficient as a file named bar could be
created between the calls to lstat(2) and rename(2).  If the source is
not a directory, a better solution, suggested in comp.unix.internals,
is to use link(2)

    if (link(old, new) == 0)
        unlink(old);

urs




reply via email to

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