Skip to content

Instantly share code, notes, and snippets.

@janmoesen
Created November 1, 2011 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janmoesen/1332110 to your computer and use it in GitHub Desktop.
Save janmoesen/1332110 to your computer and use it in GitHub Desktop.
Atomic symlink switcheroo test script for GNU mv
#!/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
@janmoesen
Copy link
Author

See also: How does one atomically change a symlink to a directory in busybox? — no idea on how to do this on OS X without a custom binary (or GNU mv).

@janmoesen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment