rsync.fns 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #! /bin/sh
  2. # Copyright (C) 2001 by Martin Pool <mbp@samba.org>
  3. # General-purpose test functions for rsync.
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version
  6. # 2 as published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful, but
  9. # WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. # Lesser General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU Lesser General Public
  14. # License along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. tmpdir="$scratchdir"
  17. fromdir="$tmpdir/from"
  18. todir="$tmpdir/to"
  19. chkdir="$tmpdir/chk"
  20. # For itemized output:
  21. all_plus='+++++++++'
  22. allspace=' '
  23. dots='.....' # trailing dots after changes
  24. tab_ch=' ' # a single tab character
  25. # Berkley's nice.
  26. PATH="$PATH:/usr/ucb"
  27. if diff -u "$srcdir/testsuite/rsync.fns" "$srcdir/testsuite/rsync.fns" >/dev/null 2>&1; then
  28. diffopt="-u"
  29. else
  30. diffopt="-c"
  31. fi
  32. HOME="$scratchdir"
  33. export HOME
  34. runtest() {
  35. echo $ECHO_N "Test $1: $ECHO_C"
  36. if eval "$2"
  37. then
  38. echo "$ECHO_T done."
  39. return 0
  40. else
  41. echo "$ECHO_T failed!"
  42. return 1
  43. fi
  44. }
  45. set_cp_destdir() {
  46. while test $# -gt 1; do
  47. shift
  48. done
  49. destdir="$1"
  50. }
  51. # Perform a "cp -p", making sure that timestamps are really the same,
  52. # even if the copy rounded microsecond times on the destination file.
  53. cp_touch() {
  54. cp -p "${@}" || test_fail "cp -p failed"
  55. if test $# -gt 2 -o -d "$2"; then
  56. set_cp_destdir "${@}" # sets destdir var
  57. while test $# -gt 1; do
  58. destname="$destdir/`basename $1`"
  59. touch -r "$destname" "$1" "$destname"
  60. shift
  61. done
  62. else
  63. touch -r "$2" "$1" "$2"
  64. fi
  65. }
  66. # Call this if you want to filter out verbose messages (-v or -vv) from
  67. # the output of an rsync run (whittling the output down to just the file
  68. # messages). This isn't needed if you use -i without -v.
  69. filter_outfile() {
  70. sed -e '/^building file list /d' \
  71. -e '/^sending incremental file list/d' \
  72. -e '/^created directory /d' \
  73. -e '/^done$/d' \
  74. -e '/ --whole-file$/d' \
  75. -e '/^total: /d' \
  76. -e '/^client charset: /d' \
  77. -e '/^server charset: /d' \
  78. -e '/^$/,$d' \
  79. <"$outfile" >"$outfile.new"
  80. mv "$outfile.new" "$outfile"
  81. }
  82. printmsg() {
  83. echo "$1"
  84. }
  85. rsync_ls_lR() {
  86. find "$@" -print | sort | sed 's/ /\\ /g' | xargs "$TOOLDIR/tls" $TLS_ARGS
  87. }
  88. get_testuid() {
  89. id 2>/dev/null | sed 's/^[^0-9]*\([0-9][0-9]*\).*/\1/'
  90. }
  91. check_perms() {
  92. perms=`"$TOOLDIR/tls" "$1" | sed 's/^[-d]\(.........\).*/\1/'`
  93. if test $perms = $2; then
  94. return 0
  95. fi
  96. echo "permissions: $perms on $1"
  97. echo "should be: $2"
  98. test_fail "failed test $3"
  99. }
  100. rsync_getgroups() {
  101. "$TOOLDIR/getgroups"
  102. }
  103. ####################
  104. # Build test directories $todir and $fromdir, with $fromdir full of files.
  105. hands_setup() {
  106. # Clean before creation
  107. rm -rf "$fromdir"
  108. rm -rf "$todir"
  109. [ -d "$tmpdir" ] || mkdir "$tmpdir"
  110. [ -d "$fromdir" ] || mkdir "$fromdir"
  111. [ -d "$todir" ] || mkdir "$todir"
  112. # On some BSD systems, the umask affects the mode of created
  113. # symlinks, even though the mode apparently has no effect on how
  114. # the links behave in the future, and it cannot be changed using
  115. # chmod! rsync always sets its umask to 000 so that it can
  116. # accurately recreate permissions, but this script is probably run
  117. # with a different umask.
  118. # This causes a little problem that "ls -l" of the two will not be
  119. # the same. So, we need to set our umask before doing any creations.
  120. # set up test data
  121. touch "$fromdir/empty"
  122. mkdir "$fromdir/emptydir"
  123. # a hundred lines of text or so
  124. rsync_ls_lR "$srcdir" > "$fromdir/filelist"
  125. echo $ECHO_N "This file has no trailing lf$ECHO_C" > "$fromdir/nolf"
  126. umask 0
  127. ln -s nolf "$fromdir/nolf-symlink"
  128. umask 022
  129. cat "$srcdir"/*.c > "$fromdir/text"
  130. mkdir "$fromdir/dir"
  131. cp "$fromdir/text" "$fromdir/dir"
  132. mkdir "$fromdir/dir/subdir"
  133. echo some data > "$fromdir/dir/subdir/foobar.baz"
  134. mkdir "$fromdir/dir/subdir/subsubdir"
  135. if [ -r /etc ]; then
  136. ls -ltr /etc > "$fromdir/dir/subdir/subsubdir/etc-ltr-list"
  137. else
  138. ls -ltr / > "$fromdir/dir/subdir/subsubdir/etc-ltr-list"
  139. fi
  140. mkdir "$fromdir/dir/subdir/subsubdir2"
  141. if [ -r /bin ]; then
  142. ls -lt /bin > "$fromdir/dir/subdir/subsubdir2/bin-lt-list"
  143. else
  144. ls -lt / > "$fromdir/dir/subdir/subsubdir2/bin-lt-list"
  145. fi
  146. # echo testing head:
  147. # ls -lR "$srcdir" | head -10 || echo failed
  148. }
  149. ####################
  150. # Many machines do not have "mkdir -p", so we have to build up long paths.
  151. # How boring.
  152. makepath() {
  153. for p in "${@}"; do
  154. (echo " makepath $p"
  155. # Absolut Unix.
  156. if echo $p | grep '^/' >/dev/null
  157. then
  158. cd /
  159. fi
  160. # This will break if $p contains a space.
  161. for c in `echo $p | tr '/' ' '`
  162. do
  163. if [ -d "$c" ] || mkdir "$c"
  164. then
  165. cd "$c" || return $?
  166. else
  167. echo "failed to create $c" >&2; return $?
  168. fi
  169. done)
  170. done
  171. }
  172. ###########################
  173. # Run a test (in '$1') then compare directories $2 and $3 to see if
  174. # there are any difference. If there are, explain them.
  175. # So normally basically $1 should be an rsync command, and $2 and $3
  176. # the source and destination directories. This is only good when you
  177. # expect to transfer the whole directory exactly as is. If some files
  178. # should be excluded, you might need to use something else.
  179. checkit() {
  180. failed=
  181. # We can just write everything to stdout/stderr, because the
  182. # wrapper hides it unless there is a problem.
  183. echo "Running: \"$1\""
  184. eval "$1"
  185. status=$?
  186. if [ $status != 0 ]; then
  187. failed="$failed status=$status"
  188. fi
  189. echo "-------------"
  190. echo "check how the directory listings compare with diff:"
  191. echo ""
  192. ( cd "$2" && rsync_ls_lR . ) > "$tmpdir/ls-from"
  193. ( cd "$3" && rsync_ls_lR . ) > "$tmpdir/ls-to"
  194. diff $diffopt "$tmpdir/ls-from" "$tmpdir/ls-to" || failed="$failed dir-diff"
  195. echo "-------------"
  196. echo "check how the files compare with diff:"
  197. echo ""
  198. if [ "x$4" != x ]; then
  199. echo " === Skipping (as directed) ==="
  200. else
  201. diff -r $diffopt "$2" "$3" || failed="$failed file-diff"
  202. fi
  203. echo "-------------"
  204. if [ -z "$failed" ] ; then
  205. return 0
  206. fi
  207. echo "Failed: $failed"
  208. return 1
  209. }
  210. build_rsyncd_conf() {
  211. # Build an appropriate configuration file
  212. conf="$scratchdir/test-rsyncd.conf"
  213. echo "building configuration $conf"
  214. port=2612
  215. pidfile="$scratchdir/rsyncd.pid"
  216. logfile="$scratchdir/rsyncd.log"
  217. hostname=`uname -n`
  218. cat >"$conf" <<EOF
  219. # rsyncd configuration file autogenerated by $0
  220. pid file = $pidfile
  221. use chroot = no
  222. munge symlinks = no
  223. hosts allow = localhost 127.0.0.0/24 192.168.0.0/16 10.0.0.0/8 $hostname
  224. log file = $logfile
  225. log format = %i %h [%a] %m (%u) %l %f%L
  226. transfer logging = yes
  227. exclude = ? foobar.baz
  228. max verbosity = 9
  229. uid = 0
  230. gid = 0
  231. [test-from]
  232. path = $fromdir
  233. read only = yes
  234. comment = r/o
  235. [test-to]
  236. path = $todir
  237. read only = no
  238. comment = r/w
  239. [test-scratch]
  240. path = $scratchdir
  241. read only = no
  242. [test-hidden]
  243. path = $fromdir
  244. list = no
  245. EOF
  246. # Build a helper script to ignore exit code 23
  247. ignore23="$scratchdir/ignore23"
  248. echo "building help script $ignore23"
  249. cat >"$ignore23" <<'EOT'
  250. if "${@}"; then
  251. exit
  252. fi
  253. ret=$?
  254. if test $ret = 23; then
  255. exit
  256. fi
  257. exit $ret
  258. EOT
  259. chmod +x "$ignore23"
  260. }
  261. build_symlinks() {
  262. mkdir "$fromdir"
  263. date >"$fromdir/referent"
  264. ln -s referent "$fromdir/relative"
  265. ln -s "$fromdir/referent" "$fromdir/absolute"
  266. ln -s nonexistent "$fromdir/dangling"
  267. ln -s "$srcdir/rsync.c" "$fromdir/unsafe"
  268. }
  269. test_fail() {
  270. echo "$@" >&2
  271. exit 1
  272. }
  273. test_skipped() {
  274. echo "$@" >&2
  275. echo "$@" > "$tmpdir/whyskipped"
  276. exit 77
  277. }
  278. # It failed, but we expected that. don't dump out error logs,
  279. # because most users won't want to see them. But do leave
  280. # the working directory around.
  281. test_xfail() {
  282. echo "$@" >&2
  283. exit 78
  284. }
  285. # Determine what shell command will appropriately test for links.
  286. ln -s foo "$scratchdir/testlink"
  287. for cmd in test /bin/test /usr/bin/test /usr/ucb/bin/test /usr/ucb/test
  288. do
  289. for switch in -h -L
  290. do
  291. if $cmd $switch "$scratchdir/testlink" 2>/dev/null
  292. then
  293. # how nice
  294. TEST_SYMLINK_CMD="$cmd $switch"
  295. # i wonder if break 2 is portable?
  296. break 2
  297. fi
  298. done
  299. done
  300. # ok, now get rid of it
  301. rm "$scratchdir/testlink"
  302. if [ "x$TEST_SYMLINK_CMD" = 'x' ]
  303. then
  304. test_fail "Couldn't determine how to test for symlinks"
  305. else
  306. echo "Testing for symlinks using '$TEST_SYMLINK_CMD'"
  307. fi
  308. # Test whether something is a link, allowing for shell peculiarities
  309. is_a_link() {
  310. # note the variable contains the first option and therefore is not quoted
  311. $TEST_SYMLINK_CMD "$1"
  312. }
  313. # We need to set the umask to be reproducible. Note also that when we
  314. # do some daemon tests as root, we will setuid() and therefore the
  315. # directory has to be writable by the nobody user in some cases. The
  316. # best thing is probably to explicitly chmod those directories after
  317. # creation.
  318. umask 022