gnupload: Support option -h as alias of --help.
[gnulib.git] / tests / test-copy-file.sh
blob98da280efb9c473170bdc1c3dc0b8ee74c5ce2b5
1 #!/bin/sh
3 # Show all commands when run with environment variable VERBOSE=yes.
4 test -z "$VERBOSE" || set -x
6 # func_tmpdir
7 # creates a temporary directory.
8 # Sets variable
9 # - tmp pathname of freshly created temporary directory
10 func_tmpdir ()
12 # Use the environment variable TMPDIR, falling back to /tmp. This allows
13 # users to specify a different temporary directory, for example, if their
14 # /tmp is filled up or too small.
15 : ${TMPDIR=/tmp}
17 # Use the mktemp program if available. If not available, hide the error
18 # message.
19 tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
20 test -n "$tmp" && test -d "$tmp"
21 } ||
23 # Use a simple mkdir command. It is guaranteed to fail if the directory
24 # already exists. $RANDOM is bash specific and expands to empty in shells
25 # other than bash, ksh and zsh. Its use does not increase security;
26 # rather, it minimizes the probability of failure in a very cluttered /tmp
27 # directory.
28 tmp=$TMPDIR/gl$$-$RANDOM
29 (umask 077 && mkdir "$tmp")
30 } ||
32 echo "$0: cannot create a temporary directory in $TMPDIR" >&2
33 exit 1
37 func_tmpdir
38 builddir=`pwd`
39 cd "$builddir" ||
41 echo "$0: cannot determine build directory (unreadable parent dir?)" >&2
42 exit 1
44 # Switch to a temporary directory, to increase the likelihood that ACLs are
45 # supported on the current file system. (/tmp is usually locally mounted,
46 # whereas the build dir is sometimes NFS-mounted.)
47 ( cd "$tmp"
49 # Prepare tmpfile0.
50 rm -f tmpfile[0-9] tmpaclout[0-2]
51 echo "Simple contents" > tmpfile0
52 chmod 600 tmpfile0
54 # Classification of the platform according to the programs available for
55 # manipulating ACLs.
56 # Possible values are:
57 # linux, cygwin, freebsd, solaris, hpux, hpuxjfs, osf1, aix, macosx, irix, none.
58 # TODO: Support also native Windows platforms (mingw).
59 acl_flavor=none
60 if (getfacl tmpfile0 >/dev/null) 2>/dev/null; then
61 # Platforms with the getfacl and setfacl programs.
62 # Linux, FreeBSD, Solaris, Cygwin.
63 if (setfacl --help >/dev/null) 2>/dev/null; then
64 # Linux, Cygwin.
65 if (LC_ALL=C setfacl --help | grep ' --set-file' >/dev/null) 2>/dev/null; then
66 # Linux.
67 acl_flavor=linux
68 else
69 acl_flavor=cygwin
71 else
72 # FreeBSD, Solaris.
73 if (LC_ALL=C setfacl 2>&1 | grep '\-x entries' >/dev/null) 2>/dev/null; then
74 # FreeBSD.
75 acl_flavor=freebsd
76 else
77 # Solaris.
78 acl_flavor=solaris
81 else
82 if (lsacl / >/dev/null) 2>/dev/null; then
83 # Platforms with the lsacl and chacl programs.
84 # HP-UX, sometimes also IRIX.
85 if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
86 # HP-UX 11.11 or newer.
87 acl_flavor=hpuxjfs
88 else
89 # HP-UX 11.00.
90 acl_flavor=hpux
92 else
93 if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
94 # Tru64, NonStop Kernel.
95 if (getacl -m tmpfile0 >/dev/null) 2>/dev/null; then
96 # Tru64.
97 acl_flavor=osf1
98 else
99 # NonStop Kernel.
100 acl_flavor=nsk
102 else
103 if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
104 # AIX.
105 acl_flavor=aix
106 else
107 if (fsaclctl -v >/dev/null) 2>/dev/null; then
108 # Mac OS X.
109 acl_flavor=macosx
110 else
111 if test -f /sbin/chacl; then
112 # IRIX.
113 acl_flavor=irix
121 # Define a function to test for the same ACLs, from the point of view of
122 # the programs.
123 # func_test_same_acls file1 file2
124 case $acl_flavor in
125 linux | cygwin | freebsd | solaris)
126 func_test_same_acls ()
128 getfacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
129 getfacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
130 cmp tmpaclout1 tmpaclout2 > /dev/null
133 hpux)
134 func_test_same_acls ()
136 lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
137 lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
138 cmp tmpaclout1 tmpaclout2 > /dev/null
141 hpuxjfs)
142 func_test_same_acls ()
144 { lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
145 lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
146 cmp tmpaclout1 tmpaclout2 > /dev/null
147 } &&
148 { getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
149 getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
150 cmp tmpaclout1 tmpaclout2 > /dev/null
154 osf1 | nsk)
155 func_test_same_acls ()
157 getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
158 getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
159 cmp tmpaclout1 tmpaclout2 > /dev/null
162 aix)
163 func_test_same_acls ()
165 aclget "$1" > tmpaclout1
166 aclget "$2" > tmpaclout2
167 cmp tmpaclout1 tmpaclout2 > /dev/null
170 macosx)
171 func_test_same_acls ()
173 /bin/ls -le "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
174 /bin/ls -le "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
175 cmp tmpaclout1 tmpaclout2 > /dev/null
178 irix)
179 func_test_same_acls ()
181 /bin/ls -lD "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
182 /bin/ls -lD "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
183 cmp tmpaclout1 tmpaclout2 > /dev/null
186 none)
187 func_test_same_acls ()
192 esac
194 # func_test_copy file1 file2
195 # copies file1 to file2 and verifies the permissions and ACLs are the same
196 # on both.
197 func_test_copy ()
199 "$builddir"/test-copy-file${EXEEXT} "$1" "$2" || exit 1
200 if test "$USE_ACL" != 0; then
201 "$builddir"/test-sameacls${EXEEXT} "$1" "$2" || exit 1
202 func_test_same_acls "$1" "$2" || exit 1
206 func_test_copy tmpfile0 tmpfile1
208 if test "$USE_ACL" != 0 && test $acl_flavor != none; then
209 # A POSIX compliant 'id' program.
210 if test -f /usr/xpg4/bin/id; then
211 ID=/usr/xpg4/bin/id
212 else
213 ID=id
215 # Use a user and group id different from the current one, to avoid
216 # redundant/ambiguous ACLs.
217 myuid=`$ID -u`
218 mygid=`$ID -g`
219 auid=1
220 if test "$auid" = "$myuid"; then auid=2; fi
221 agid=1
222 if test "$agid" = "$mygid"; then agid=2; fi
224 case $acl_flavor in
225 linux | freebsd | solaris)
227 # Set an ACL for a user.
228 setfacl -m user:$auid:1 tmpfile0
230 func_test_copy tmpfile0 tmpfile2
232 # Set an ACL for a group.
233 setfacl -m group:$agid:4 tmpfile0
235 func_test_copy tmpfile0 tmpfile3
237 # Set an ACL for other.
238 case $acl_flavor in
239 freebsd) setfacl -m other::4 tmpfile0 ;;
240 solaris) chmod o+r tmpfile0 ;;
241 *) setfacl -m other:4 tmpfile0 ;;
242 esac
244 func_test_copy tmpfile0 tmpfile4
246 # Remove the ACL for the user.
247 case $acl_flavor in
248 linux) setfacl -x user:$auid tmpfile0 ;;
249 freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
250 *) setfacl -d user:$auid:1 tmpfile0 ;;
251 esac
253 func_test_copy tmpfile0 tmpfile5
255 # Remove the ACL for other.
256 case $acl_flavor in
257 linux | solaris) ;; # impossible
258 freebsd) setfacl -x other::4 tmpfile0 ;;
259 *) setfacl -d other:4 tmpfile0 ;;
260 esac
262 func_test_copy tmpfile0 tmpfile6
264 # Remove the ACL for the group.
265 case $acl_flavor in
266 linux) setfacl -x group:$agid tmpfile0 ;;
267 freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
268 *) setfacl -d group:$agid:4 tmpfile0 ;;
269 esac
271 func_test_copy tmpfile0 tmpfile7
273 # Delete all optional ACLs.
274 case $acl_flavor in
275 linux | freebsd)
276 setfacl -m user:$auid:1 tmpfile0
277 setfacl -b tmpfile0
280 setfacl -s user::6,group::0,other:0 tmpfile0 ;;
281 esac
283 func_test_copy tmpfile0 tmpfile8
285 # Copy ACLs from a file that has no ACLs.
286 echo > tmpfile9
287 chmod a+x tmpfile9
288 case $acl_flavor in
289 linux) getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
290 freebsd) ;;
291 *) getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
292 esac
293 rm -f tmpfile9
295 func_test_copy tmpfile0 tmpfile9
299 cygwin)
301 # Set an ACL for a group.
302 setfacl -m group:0:1 tmpfile0
304 func_test_copy tmpfile0 tmpfile2
306 # Set an ACL for other.
307 setfacl -m other:4 tmpfile0
309 func_test_copy tmpfile0 tmpfile4
311 # Remove the ACL for the group.
312 setfacl -d group:0 tmpfile0
314 func_test_copy tmpfile0 tmpfile5
316 # Remove the ACL for other.
317 setfacl -d other:4 tmpfile0
319 func_test_copy tmpfile0 tmpfile6
321 # Delete all optional ACLs.
322 setfacl -s user::6,group::0,other:0 tmpfile0
324 func_test_copy tmpfile0 tmpfile8
326 # Copy ACLs from a file that has no ACLs.
327 echo > tmpfile9
328 chmod a+x tmpfile9
329 getfacl tmpfile9 | setfacl -f - tmpfile0
330 rm -f tmpfile9
332 func_test_copy tmpfile0 tmpfile9
336 hpux)
338 # Set an ACL for a user.
339 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
340 chacl -r "${orig}($auid.%,--x)" tmpfile0
342 func_test_copy tmpfile0 tmpfile2
344 # Set an ACL for a group.
345 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
346 chacl -r "${orig}(%.$agid,r--)" tmpfile0
348 func_test_copy tmpfile0 tmpfile3
350 # Set an ACL for other.
351 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
352 chacl -r "${orig}(%.%,r--)" tmpfile0
354 func_test_copy tmpfile0 tmpfile4
356 # Remove the ACL for the user.
357 chacl -d "($auid.%,--x)" tmpfile0
359 func_test_copy tmpfile0 tmpfile5
361 # Remove the ACL for the group.
362 chacl -d "(%.$agid,r--)" tmpfile0
364 func_test_copy tmpfile0 tmpfile6
366 # Delete all optional ACLs.
367 chacl -z tmpfile0
369 func_test_copy tmpfile0 tmpfile8
371 # Copy ACLs from a file that has no ACLs.
372 echo > tmpfile9
373 chmod a+x tmpfile9
374 orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
375 rm -f tmpfile9
376 chacl -r "${orig}" tmpfile0
378 func_test_copy tmpfile0 tmpfile9
382 hpuxjfs)
384 # Set an ACL for a user.
385 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
386 chacl -r "${orig}($auid.%,--x)" tmpfile0 \
387 || setacl -m user:$auid:1 tmpfile0
389 func_test_copy tmpfile0 tmpfile2
391 # Set an ACL for a group.
392 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
393 chacl -r "${orig}(%.$agid,r--)" tmpfile0 \
394 || setacl -m group:$agid:4 tmpfile0
396 func_test_copy tmpfile0 tmpfile3
398 # Set an ACL for other.
399 orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
400 chacl -r "${orig}(%.%,r--)" tmpfile0 \
401 || setacl -m other:4 tmpfile0
403 func_test_copy tmpfile0 tmpfile4
405 # Remove the ACL for the user.
406 chacl -d "($auid.%,--x)" tmpfile0 \
407 || setacl -d user:$auid tmpfile0
409 func_test_copy tmpfile0 tmpfile5
411 # Remove the ACL for the group.
412 chacl -d "(%.$agid,r--)" tmpfile0 \
413 || setacl -d group:$agid tmpfile0
415 func_test_copy tmpfile0 tmpfile6
417 # Delete all optional ACLs.
418 chacl -z tmpfile0 \
419 || { setacl -m user:$auid:1 tmpfile0
420 setacl -s user::6,group::0,class:7,other:0 tmpfile0
423 func_test_copy tmpfile0 tmpfile8
425 # Copy ACLs from a file that has no ACLs.
426 echo > tmpfile9
427 chmod a+x tmpfile9
428 orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
429 getacl tmpfile9 > tmpaclout0
430 rm -f tmpfile9
431 chacl -r "${orig}" tmpfile0 \
432 || setacl -f tmpaclout0 tmpfile0
434 func_test_copy tmpfile0 tmpfile9
438 osf1)
440 # Set an ACL for a user.
441 setacl -u user:$auid:1 tmpfile0
443 func_test_copy tmpfile0 tmpfile2
445 # Set an ACL for a group.
446 setacl -u group:$agid:4 tmpfile0
448 func_test_copy tmpfile0 tmpfile3
450 # Set an ACL for other.
451 setacl -u other::4 tmpfile0
453 func_test_copy tmpfile0 tmpfile4
455 # Remove the ACL for the user.
456 setacl -x user:$auid:1 tmpfile0
458 func_test_copy tmpfile0 tmpfile5
460 if false; then # would give an error "can't set ACL: Invalid argument"
461 # Remove the ACL for other.
462 setacl -x other::4 tmpfile0
464 func_test_copy tmpfile0 tmpfile6
467 # Remove the ACL for the group.
468 setacl -x group:$agid:4 tmpfile0
470 func_test_copy tmpfile0 tmpfile7
472 # Delete all optional ACLs.
473 setacl -u user:$auid:1 tmpfile0
474 setacl -b tmpfile0
476 func_test_copy tmpfile0 tmpfile8
478 # Copy ACLs from a file that has no ACLs.
479 echo > tmpfile9
480 chmod a+x tmpfile9
481 getacl tmpfile9 > tmpaclout0
482 setacl -b -U tmpaclout0 tmpfile0
483 rm -f tmpfile9
485 func_test_copy tmpfile0 tmpfile9
489 nsk)
491 # Set an ACL for a user.
492 setacl -m user:$auid:1 tmpfile0
494 func_test_copy tmpfile0 tmpfile2
496 # Set an ACL for a group.
497 setacl -m group:$agid:4 tmpfile0
499 func_test_copy tmpfile0 tmpfile3
501 # Set an ACL for other.
502 setacl -m other:4 tmpfile0
504 func_test_copy tmpfile0 tmpfile4
506 # Remove the ACL for the user.
507 setacl -d user:$auid tmpfile0
509 func_test_copy tmpfile0 tmpfile5
511 # Remove the ACL for the group.
512 setacl -d group:$agid tmpfile0
514 func_test_copy tmpfile0 tmpfile6
516 # Delete all optional ACLs.
517 setacl -m user:$auid:1 tmpfile0
518 setacl -s user::6,group::0,class:7,other:0 tmpfile0
520 func_test_copy tmpfile0 tmpfile8
522 # Copy ACLs from a file that has no ACLs.
523 echo > tmpfile9
524 chmod a+x tmpfile9
525 getacl tmpfile9 > tmpaclout0
526 setacl -f tmpaclout0 tmpfile0
527 rm -f tmpfile9
529 func_test_copy tmpfile0 tmpfile9
533 aix)
535 # Set an ACL for a user.
536 { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo " permit --x u:$auid"; } | aclput tmpfile0
538 func_test_copy tmpfile0 tmpfile2
540 # Set an ACL for a group.
541 { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo " permit r-- g:$agid"; } | aclput tmpfile0
543 func_test_copy tmpfile0 tmpfile3
545 # Set an ACL for other.
546 chmod o+r tmpfile0
548 func_test_copy tmpfile0 tmpfile4
550 # Remove the ACL for the user.
551 aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
553 func_test_copy tmpfile0 tmpfile5
555 # Remove the ACL for the group.
556 aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
558 func_test_copy tmpfile0 tmpfile7
560 # Delete all optional ACLs.
561 aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
563 func_test_copy tmpfile0 tmpfile8
565 # Copy ACLs from a file that has no ACLs.
566 echo > tmpfile9
567 chmod a+x tmpfile9
568 aclget tmpfile9 | aclput tmpfile0
569 rm -f tmpfile9
571 func_test_copy tmpfile0 tmpfile9
575 macosx)
577 # Set an ACL for a user.
578 /bin/chmod +a "user:daemon allow execute" tmpfile0
580 func_test_copy tmpfile0 tmpfile2
582 # Set an ACL for a group.
583 /bin/chmod +a "group:daemon allow read" tmpfile0
585 func_test_copy tmpfile0 tmpfile3
587 # Set an ACL for other.
588 chmod o+r tmpfile0
590 func_test_copy tmpfile0 tmpfile4
592 # Remove the ACL for the user.
593 /bin/chmod -a "user:daemon allow execute" tmpfile0
595 func_test_copy tmpfile0 tmpfile5
597 # Remove the ACL for the group.
598 /bin/chmod -a "group:daemon allow read" tmpfile0
600 func_test_copy tmpfile0 tmpfile7
602 # Delete all optional ACLs.
603 /bin/chmod -N tmpfile0
605 func_test_copy tmpfile0 tmpfile8
607 # Copy ACLs from a file that has no ACLs.
608 echo > tmpfile9
609 chmod a+x tmpfile9
610 { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
611 rm -f tmpfile9
613 func_test_copy tmpfile0 tmpfile9
617 irix)
619 # Set an ACL for a user.
620 /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
622 func_test_copy tmpfile0 tmpfile2
624 # Set an ACL for a group.
625 /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
627 func_test_copy tmpfile0 tmpfile3
629 # Set an ACL for other.
630 /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
632 func_test_copy tmpfile0 tmpfile4
634 # Remove the ACL for the user.
635 /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
637 func_test_copy tmpfile0 tmpfile5
639 # Remove the ACL for the group.
640 /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
642 func_test_copy tmpfile0 tmpfile7
646 esac
649 rm -f tmpfile[0-9] tmpaclout[0-2]
650 ) || exit 1
652 rm -rf "$tmp"
653 exit 0