PrePAN

Sign in to PrePAN

File::Symlink::Atomic provides atomicity when giving a symlink a new target

Good

Synopsis

use File::Symlink::Atomic;   # imports replacement symlink
symlink "target", "name1";   # easy peasy
symlink "bullseye", "name1"; # now atomic

Description

Actually creating a symlink is not problematic, but making an existing one point at a new target may not be atomic on your system. For example, on Linus, the system does unlink and then symlink. In between, no symlink exists. If something goes wrong, you're left with nothing.

In your shell, you probably want to do something like:

mkdir old-target new target # Create your targets
ln -s old-target link       # Create your initial symlink
# ln -sf new-target link    # NOT atomic!
ln -s new-target link-tmp && mv -Tf link-tmp link

Moving the symlink to the new name makes it atomic, because under the hood, the mv command does rename, which is guaranteed to be atomic by POSIX.

File::Symlink::Atomic attempts to do the same thing in Perl what the command shown above does for your shell.

Comments

Please sign up to post a review.