]> arthur.barton.de Git - bup.git/blob - dev/compare-trees
compare-trees: drop getopt to allow long opts and fix exits
[bup.git] / dev / compare-trees
1 #!/usr/bin/env bash
2
3 set -euo pipefail
4
5 # Test that src and dest trees are as identical as bup is capable of
6 # making them.  For now, use rsync -niaHAX ...
7
8 usage() {
9 cat <<EOF
10 Usage: compare-trees [-h] [-c] [-x] SOURCE DEST
11 OPTIONS:
12   -h
13     Display help
14   -c
15     Check file content (default)
16   -x
17     Don't check file content (rely on size/timestamps, etc.)
18 EOF
19 }
20
21 verify_content=" --checksum"
22
23 while test $# -gt 0; do
24     case "$1" in
25         -h) usage; exit 0;;
26         -c) verify_content=" --checksum"; shift;;
27         -x) verify_content=""; shift;;
28         -*) usage 1>&2; exit 2;;
29         [^-]*) break;;
30     esac
31 done
32
33 if ! test $# -eq 2
34 then
35     usage 1>&2
36     exit 2
37 fi
38
39 src="$1"
40 dest="$2"
41
42 tmpfile="$(mktemp /tmp/bup-test-XXXXXXX)" || exit $?
43 trap "rm -rf '$tmpfile'" EXIT || exit $?
44
45 rsync_opts="-niaH$verify_content --delete"
46
47 rsync_version=$(rsync --version)
48 if [[ ! "$rsync_version" =~ "ACLs" ]] || [[ "$rsync_version" =~ "no ACLs" ]]; then
49     echo "Not comparing ACLs (not supported by available rsync)" 1>&2
50 else
51     case $OSTYPE in
52         cygwin|darwin|netbsd)
53             echo "Not comparing ACLs (not yet supported on $OSTYPE)" 1>&2
54             ;;
55         *)
56             rsync_opts="$rsync_opts -A"
57             ;;
58     esac
59 fi
60
61 xattrs_available=''
62 if [[ ! "$rsync_version" =~ "xattrs" ]] || [[ "$rsync_version" =~ "no xattrs" ]]; then
63     echo "Not comparing xattrs (not supported by available rsync)" 1>&2
64 else
65     xattrs_available=yes
66 fi
67
68 # Even in dry-run mode, rsync may fail if -X is specified and the
69 # filesystems don't support xattrs.
70
71 if test "$xattrs_available"; then
72     rsync $rsync_opts -X "$src" "$dest" > "$tmpfile"
73     if test $? -ne 0; then
74         # Try again without -X
75         rsync $rsync_opts "$src" "$dest" > "$tmpfile" || exit $?
76     fi
77 else
78     rsync $rsync_opts "$src" "$dest" > "$tmpfile" || exit $?
79 fi
80
81 if test $(wc -l < "$tmpfile") != 0; then
82     echo "Differences between $src and $dest" 1>&2
83     cat "$tmpfile"
84     exit 1
85 fi
86
87 exit 0