Linux – How does one atomically change a symlink to a directory in busybox

busyboxdirectorylinuxsymlink

I am trying to (as close as possibly) atomically change a symlink. I've tried:

ln -sf other_dir existing_symlink

That just put the new symlink in the directory that existing_symlink pointed to.

ln -sf other_dir new_symlink
mv -f new_symlink existing_symlink

That did the same thing: it moved the symlink into the directory.

cp -s other_dir existing_symlink

It refuses because it's a directory.

I've read that mv -T is was made for this, but busybox doesn't have the -T flag.

Best Answer

I don't see how you can get atomic operation. The man page for symlink(2) says it gives EEXIST if the target already exists. If the kernel doesn't support atomic operation, your userland limitations are irrelevant.

I also don't see how mv -T helps, even if you have it. Try it on a regular Linux box, one with GNU mv:

$ mkdir a b
$ ln -s a z
$ mv -T b z
mv: cannot overwrite non-directory `z' with directory `b'

I think you're going to have to do this in two steps: remove the old symlink and recreate it.

Related Question