xref: /openbsd/regress/usr.bin/rsync/lib.sh (revision b6cae74b)
1#! /bin/sh
2
3set -u
4set -e
5
6# Library of functions.
7# Intended to be sourced by scripts (or interactive shells if you want).
8
9genfile_stdout_16m ()
10{
11    seq -f%015g 1048576
12}
13genfile_stdout_1m ()
14{
15    seq -f%015g 65536
16}
17genfile ()
18{
19    #touch "$1"
20    genfile_stdout_1m > "$1"
21}
22
23# makes a directory path and optionally a file in it.
24# if you want the last element to be a directory, add / at the end
25mkdirfile ()
26{
27    case "$1" in
28        '') error that cannot work;;
29        */) mkdir -p "$1";;
30        */*) mkdir -p "${1%/*}"; genfile "$1";;
31        *) genfile "$1";;
32    esac
33}
34
35mkdirsymlink ()
36{
37    (
38        mkdir -p "$1"
39        cd "$1"
40        ln -sf "$2" "$3"
41    )
42}
43
44# make a first interesting tree
45generate_tree_1 ()
46{
47    mkdirfile foo/bar/baz/one.txt
48    mkdirfile foo/bar/baz/one2.txt
49    mkdirfile 'foo/bar/baz/  two.txt'
50    mkdirfile 'foo/bar/baz/two  2.txt'
51    mkdirfile 'foo/bar/baz/two3.txt  '
52    mkdirsymlink foo/baz/ ../bar/baz/one.txt three.txt
53    mkdirfile one/two/three/four.txt
54    mkdirfile foo/five/one/two/five/blah.txt
55    mkdirfile foo/one/two/five/blah.txt
56}
57
58# a frontend for find
59# first argument is a dir to chdir to
60findme ()
61{
62    if [ $# -lt 2 ] ; then
63        echo usage: different 1>&2
64        return 1
65    fi
66    (
67        cd "$1" ; shift
68        # Remove unstable fields:
69        #    1: inode
70        #    2: size in blocks
71        # 8-10: last modification time
72        find "$@" -ls |
73        sed -e 's/^[[:space:]]*//' -e 's/[[:space:]][[:space:]]*/ /g' |
74        cut -d ' ' -f 3-7,11- |
75        sort
76    )
77}
78
79# compare two trees.  This will later be modular to pick between:
80# - diff
81# - find . -print0 | sort --zero-terminated | xargs -0 tar fc foo.tar
82# - mtree
83compare_trees ()
84{
85    if [ $# -ne 2 ] ; then
86        echo usage: different 1>&2
87        return 1
88    fi
89    # files_and_permissions
90    findme "$1" . > find1
91    findme "$2" . > find2
92    diff -u find[12]
93    # file contents
94    diff -ru "$1" "$2"
95}
96