+ tmpdir="$(WVPASS wvmktempdir)" || exit $?
+ export BUP_DIR="$tmpdir/bup"
+ WVPASS setup-test-tree
+ WVPASS cd "$tmpdir"
+ WVPASS test-src-save-restore
+
+ # Test a deeper subdir/ to make sure top-level non-dir metadata is
+ # restored correctly. We need at least one dir and one non-dir at
+ # the "top-level".
+ WVPASS test -d src/var/cmd
+ WVPASS test -f src/var/cmd/save-cmd.py
+ WVPASS rm -rf "$BUP_DIR"
+ WVPASS bup init
+ WVPASS touch -t 201111111111 src-restore # Make sure the top won't match.
+ WVPASS bup index src
+ WVPASS bup save -t -n src src
+ WVPASS force-delete src-restore
+ WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/var/."
+ WVPASS touch -t 201211111111 src-restore # Make sure the top won't match.
+ # Check that the only difference is the top dir.
+ WVFAIL $TOP/t/compare-trees -c src/var/ src-restore/ > tmp-compare-trees
+ WVPASSEQ $(cat tmp-compare-trees | wc -l) 1
+ # The number of rsync status characters varies, so accept any
+ # number of trailing dots. For example OS X native rsync produces
+ # 9, but Homebrew's produces 12, while on other platforms, 11 is
+ # common.
+ expected_diff_rx='^\.d\.\.t\.\.\.(\.)+ \./$'
+ if ! grep -qE "$expected_diff_rx" tmp-compare-trees; then
+ echo -n 'tmp-compare-trees: ' 1>&2
+ cat tmp-compare-trees 1>&2
+ fi
+ WVPASS grep -qE "$expected_diff_rx" tmp-compare-trees
+ WVPASS rm -r "$tmpdir"
+) || exit $?
+
+# Test that we pull the index (not filesystem) metadata for any
+# unchanged files whenever we're saving other files in a given
+# directory.
+WVSTART 'metadata save/restore (using index metadata)'
+(
+ tmpdir="$(WVPASS wvmktempdir)" || exit $?
+ export BUP_DIR="$tmpdir/bup"
+ WVPASS setup-test-tree
+ WVPASS cd "$tmpdir"
+
+ # ...for now -- might be a problem with hardlink restores that was
+ # causing noise wrt this test.
+ WVPASS rm -rf src/hardlink*
+
+ # Pause here to keep the filesystem changes far enough away from
+ # the first index run that bup won't cap their index timestamps
+ # (see "bup help index" for more information). Without this
+ # sleep, the compare-trees test below "Bup should *not* pick up
+ # these metadata..." may fail.
+ WVPASS sleep 1
+
+ WVPASS rm -rf "$BUP_DIR"
+ WVPASS bup init
+ WVPASS bup index src
+ WVPASS bup save -t -n src src
+
+ WVPASS force-delete src-restore-1
+ WVPASS mkdir src-restore-1
+ WVPASS bup restore -C src-restore-1 "/src/latest$(pwd)/"
+ WVPASS test -d src-restore-1/src
+ WVPASS "$TOP/t/compare-trees" -c src/ src-restore-1/src/
+
+ WVPASS echo "blarg" > src/volatile/1
+ WVPASS cp -pP src/volatile/1 src-restore-1/src/volatile/
+ WVPASS bup index src
+
+ # Bup should *not* pick up these metadata changes.
+ WVPASS touch src/volatile/2
+
+ WVPASS bup save -t -n src src
+
+ WVPASS force-delete src-restore-2
+ WVPASS mkdir src-restore-2
+ WVPASS bup restore -C src-restore-2 "/src/latest$(pwd)/"
+ WVPASS test -d src-restore-2/src
+ WVPASS "$TOP/t/compare-trees" -c src-restore-1/src/ src-restore-2/src/
+
+ WVPASS rm -r "$tmpdir"
+
+) || exit $?
+
+
+setup-hardlink-test()
+{
+ WVPASS rm -rf "$tmpdir/src" "$BUP_DIR"
+ WVPASS bup init
+ WVPASS mkdir "$tmpdir/src"
+}
+
+hardlink-test-run-restore()
+{
+ WVPASS force-delete src-restore
+ WVPASS mkdir src-restore
+ WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
+ WVPASS test -d src-restore/src
+}
+
+# Test hardlinks more carefully.
+WVSTART 'metadata save/restore (hardlinks)'
+(
+ tmpdir="$(WVPASS wvmktempdir)" || exit $?
+ export BUP_DIR="$tmpdir/bup"
+
+ WVPASS setup-hardlink-test
+ WVPASS cd "$tmpdir"
+
+ # Test trivial case - single hardlink.
+ (
+ WVPASS cd src
+ WVPASS touch hardlink-target
+ WVPASS ln hardlink-target hardlink-1
+ ) || exit $?
+ WVPASS bup index src
+ WVPASS bup save -t -n src src
+ WVPASS hardlink-test-run-restore
+ WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
+
+ # Test the case where the hardlink hasn't changed, but the tree
+ # needs to be saved again. i.e. the save-cmd.py "if hashvalid:"
+ # case.
+ (
+ WVPASS cd src
+ WVPASS echo whatever > something-new
+ ) || exit $?
+ WVPASS bup index src
+ WVPASS bup save -t -n src src
+ WVPASS hardlink-test-run-restore
+ WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
+
+ # Test hardlink changes between index runs.
+ #
+ WVPASS setup-hardlink-test
+ WVPASS cd src
+ WVPASS touch hardlink-target-a
+ WVPASS touch hardlink-target-b
+ WVPASS ln hardlink-target-a hardlink-b-1
+ WVPASS ln hardlink-target-a hardlink-a-1
+ WVPASS cd ..
+ WVPASS bup index -vv src
+ WVPASS rm src/hardlink-b-1
+ WVPASS ln src/hardlink-target-b src/hardlink-b-1
+ WVPASS bup index -vv src
+ WVPASS bup save -t -n src src
+ WVPASS hardlink-test-run-restore
+ WVPASS echo ./src/hardlink-a-1 > hardlink-sets.expected
+ WVPASS echo ./src/hardlink-target-a >> hardlink-sets.expected
+ WVPASS echo >> hardlink-sets.expected
+ WVPASS echo ./src/hardlink-b-1 >> hardlink-sets.expected
+ WVPASS echo ./src/hardlink-target-b >> hardlink-sets.expected
+ (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
+ || exit $?
+ WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
+
+ # Test hardlink changes between index and save -- hardlink set [a
+ # b c d] changes to [a b] [c d]. At least right now bup should
+ # notice and recreate the latter.
+ WVPASS setup-hardlink-test
+ WVPASS cd "$tmpdir"/src
+ WVPASS touch a
+ WVPASS ln a b
+ WVPASS ln a c
+ WVPASS ln a d
+ WVPASS cd ..
+ WVPASS bup index -vv src
+ WVPASS rm src/c src/d
+ WVPASS touch src/c
+ WVPASS ln src/c src/d
+ WVPASS bup save -t -n src src
+ WVPASS hardlink-test-run-restore
+ WVPASS echo ./src/a > hardlink-sets.expected
+ WVPASS echo ./src/b >> hardlink-sets.expected
+ WVPASS echo >> hardlink-sets.expected
+ WVPASS echo ./src/c >> hardlink-sets.expected
+ WVPASS echo ./src/d >> hardlink-sets.expected
+ (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
+ || exit $?
+ WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
+
+ # Test that we don't link outside restore tree.
+ WVPASS setup-hardlink-test
+ WVPASS cd "$tmpdir"
+ WVPASS mkdir src/a src/b
+ WVPASS touch src/a/1
+ WVPASS ln src/a/1 src/b/1
+ WVPASS bup index -vv src
+ WVPASS bup save -t -n src src
+ WVPASS force-delete src-restore
+ WVPASS mkdir src-restore
+ WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/a/"
+ WVPASS test -e src-restore/1
+ WVPASS echo -n > hardlink-sets.expected
+ (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
+ || exit $?
+ WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
+
+ # Test that we do link within separate sub-trees.
+ WVPASS setup-hardlink-test
+ WVPASS cd "$tmpdir"
+ WVPASS mkdir src/a src/b
+ WVPASS touch src/a/1
+ WVPASS ln src/a/1 src/b/1
+ WVPASS bup index -vv src/a src/b
+ WVPASS bup save -t -n src src/a src/b
+ WVPASS hardlink-test-run-restore
+ WVPASS echo ./src/a/1 > hardlink-sets.expected
+ WVPASS echo ./src/b/1 >> hardlink-sets.expected
+ (WVPASS cd src-restore; WVPASS hardlink-sets .) > hardlink-sets.restored \
+ || exit $?
+ WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
+
+ WVPASS rm -r "$tmpdir"
+
+) || exit $?