]> arthur.barton.de Git - bup.git/blob - dev/compare-trees
index: restore print format of octal mode and gitmode
[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   --times
19   --no-times
20     Check or don't check timestamps (checking is the default)
21 EOF
22 }
23
24 verify_content=" --checksum"
25 verify_times=' --times'
26
27 while test $# -gt 0; do
28     case "$1" in
29         -h) usage; exit 0;;
30         -c) verify_content=" --checksum"; shift;;
31         -x) verify_content=""; shift;;
32         --times) verify_times=' --times'; shift;;
33         --no-times) verify_times=''; shift;;
34         -*) usage 1>&2; exit 2;;
35         [^-]*) break;;
36     esac
37 done
38
39 if ! test $# -eq 2
40 then
41     usage 1>&2
42     exit 2
43 fi
44
45 src="$1"
46 dest="$2"
47
48 tmpfile="$(mktemp /tmp/bup-test-XXXXXXX)" || exit $?
49 trap "rm -rf '$tmpfile'" EXIT || exit $?
50
51 rsync_opts="-rlpgoD" # --archive, without --times
52 rsync_opts="$rsync_opts -niH --delete"
53 rsync_opts="$rsync_opts$verify_content"
54 rsync_opts="$rsync_opts$verify_times"
55
56 rsync_version=$(rsync --version)
57 if [[ ! "$rsync_version" =~ "ACLs" ]] || [[ "$rsync_version" =~ "no ACLs" ]]; then
58     echo "Not comparing ACLs (not supported by available rsync)" 1>&2
59 else
60     case $OSTYPE in
61         cygwin|darwin|netbsd)
62             echo "Not comparing ACLs (not yet supported on $OSTYPE)" 1>&2
63             ;;
64         *)
65             rsync_opts="$rsync_opts -A"
66             ;;
67     esac
68 fi
69
70 xattrs_available=''
71 if [[ ! "$rsync_version" =~ "xattrs" ]] || [[ "$rsync_version" =~ "no xattrs" ]]; then
72     echo "Not comparing xattrs (not supported by available rsync)" 1>&2
73 else
74     xattrs_available=yes
75 fi
76
77 # Even in dry-run mode, rsync may fail if -X is specified and the
78 # filesystems don't support xattrs.
79
80 if test "$xattrs_available"; then
81     rsync $rsync_opts -X "$src" "$dest" > "$tmpfile"
82     if test $? -ne 0; then
83         # Try again without -X
84         rsync $rsync_opts "$src" "$dest" > "$tmpfile" || exit $?
85     fi
86 else
87     rsync $rsync_opts "$src" "$dest" > "$tmpfile" || exit $?
88 fi
89
90 if test $(wc -l < "$tmpfile") != 0; then
91     echo "Differences between $src and $dest" 1>&2
92     cat "$tmpfile"
93     exit 1
94 fi
95
96 exit 0