Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@janmoesen janmoesen commented Nov 1, 2011

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

This comment has been minimized.

Copy link
Owner Author

@janmoesen janmoesen commented Nov 2, 2011

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