Created
November 1, 2011 22:14
-
-
Save janmoesen/1332110 to your computer and use it in GitHub Desktop.
Atomic symlink switcheroo test script for GNU mv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Make a temporary directory for our little test. | |
mkdir linkage.tmp || exit $?; | |
cd linkage.tmp; | |
# Create two directories and a symlink to the first. | |
rm -rvf v1 v2 current; | |
mkdir v1 v2; | |
ln -s v1 current; | |
ls -AlF; | |
function toggle { | |
# Create a symlink to the directory that is not symlinked. | |
[ "$(readlink 'current')" = 'v1' ] && target=v2 || target=v1; | |
ln -vs "$target" "$target.symlink"; | |
# Now atomically overwrite the "current" symlink with the new symlink. | |
mv -Tvf "$target.symlink" current; # strace shows this indeed uses rename() | |
} | |
# Toggle twice, to show that everything works as expected. | |
toggle; | |
ls -AlF; | |
toggle; | |
ls -AlF; | |
# Clean up. | |
cd ..; | |
rm -rf linkage.tmp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also see @pixelb's File Replacement on UNIX.