#!/usr/bin/env bash
+set -u
+
# Test that src and dest trees are as identical as bup is capable of
# making them. For now, use rsync -niaHAX ...
-set -e
-set -o pipefail
-
usage() {
cat <<EOF
Usage: compare-trees [-h] [-c] [-x] SOURCE DEST
verify_content=" --checksum"
-while getopts "hc" OPTION
+while getopts "hcx" OPTION
do
case "$OPTION" in
h) usage; exit 0;;
esac
done
-shift $(($OPTIND - 1))
+shift $(($OPTIND - 1)) || exit $?
if ! test $# -eq 2
then
src="$1"
dest="$2"
-tmpfile="$(mktemp)"
-trap "rm -rf '${tmpfile}'" EXIT
+tmpfile="$(mktemp /tmp/bup-test-XXXXXXX)" || exit $?
+trap "rm -rf '$tmpfile'" EXIT || exit $?
rsync_opts="-niaH$verify_content --delete"
-if rsync --version | grep -q xattrs; then
- rsync_opts="$rsync_opts -AX"
+rsync_version=$(rsync --version)
+if [[ ! "$rsync_version" =~ "ACLs" ]] || [[ "$rsync_version" =~ "no ACLs" ]]; then
+ echo "Not comparing ACLs (not supported by available rsync)" 1>&2
else
- echo "Not comparing xattrs/acls (unsupported by available rsync)." 1>&2
+ case $OSTYPE in
+ cygwin|darwin|netbsd)
+ echo "Not comparing ACLs (not yet supported on $OSTYPE)" 1>&2
+ ;;
+ *)
+ rsync_opts="$rsync_opts -A"
+ ;;
+ esac
fi
-rsync $rsync_opts "$src" "$dest" > "${tmpfile}"
+xattrs_available=''
+if [[ ! "$rsync_version" =~ "xattrs" ]] || [[ "$rsync_version" =~ "no xattrs" ]]; then
+ echo "Not comparing xattrs (not supported by available rsync)" 1>&2
+else
+ xattrs_available=yes
+fi
+
+# Even in dry-run mode, rsync may fail if -X is specified and the
+# filesystems don't support xattrs.
+
+if test "$xattrs_available"; then
+ rsync $rsync_opts -X "$src" "$dest" > "$tmpfile"
+ if test $? -ne 0; then
+ # Try again without -X
+ rsync $rsync_opts "$src" "$dest" > "$tmpfile" || exit $?
+ fi
+else
+ rsync $rsync_opts "$src" "$dest" > "$tmpfile" || exit $?
+fi
-if test $(wc -l < "${tmpfile}") != 0; then
- echo "Differences between $src and $dest"
- cat "${tmpfile}"
+if test $(wc -l < "$tmpfile") != 0; then
+ echo "Differences between $src and $dest" 1>&2
+ cat "$tmpfile"
exit 1
fi