Merge commit 'stable/20110609'
[gnulib.git] / tests / test-copy-file.sh
1 #!/bin/sh
2
3 # Show all commands when run with environment variable VERBOSE=yes.
4 test -z "$VERBOSE" || set -x
5
6 # func_tmpdir
7 # creates a temporary directory.
8 # Sets variable
9 # - tmp             pathname of freshly created temporary directory
10 func_tmpdir ()
11 {
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}
16   {
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   } ||
22   {
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   } ||
31   {
32     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
33     exit 1
34   }
35 }
36
37 func_tmpdir
38 builddir=`pwd`
39 cd "$builddir" ||
40   {
41     echo "$0: cannot determine build directory (unreadable parent dir?)" >&2
42     exit 1
43   }
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"
48
49   # Prepare tmpfile0.
50   rm -f tmpfile[0-9] tmpaclout[0-2]
51   echo "Simple contents" > tmpfile0
52   chmod 600 tmpfile0
53
54   # Classification of the platform according to the programs available for
55   # manipulating ACLs.
56   # Possible values are:
57   #   linux, cygwin, freebsd, solaris, hpux, osf1, aix, macosx, irix, none.
58   # TODO: Support also native Win32 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
70       fi
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
79       fi
80     fi
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       acl_flavor=hpux
86     else
87       if (getacl tmpfile0 >/dev/null) 2>/dev/null; then
88         # Tru64, NonStop Kernel.
89         if (getacl -m tmpfile0 >/dev/null) 2>/dev/null; then
90           # Tru64.
91           acl_flavor=osf1
92         else
93           # NonStop Kernel.
94           acl_flavor=nsk
95         fi
96       else
97         if (aclget tmpfile0 >/dev/null) 2>/dev/null; then
98           # AIX.
99           acl_flavor=aix
100         else
101           if (fsaclctl -v >/dev/null) 2>/dev/null; then
102             # MacOS X.
103             acl_flavor=macosx
104           else
105             if test -f /sbin/chacl; then
106               # IRIX.
107               acl_flavor=irix
108             fi
109           fi
110         fi
111       fi
112     fi
113   fi
114
115   # Define a function to test for the same ACLs, from the point of view of
116   # the programs.
117   # func_test_same_acls file1 file2
118   case $acl_flavor in
119     linux | cygwin | freebsd | solaris)
120       func_test_same_acls ()
121       {
122         getfacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
123         getfacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
124         cmp tmpaclout1 tmpaclout2 > /dev/null
125       }
126       ;;
127     hpux)
128       func_test_same_acls ()
129       {
130         lsacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
131         lsacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
132         cmp tmpaclout1 tmpaclout2 > /dev/null
133       }
134       ;;
135     osf1 | nsk)
136       func_test_same_acls ()
137       {
138         getacl "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
139         getacl "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
140         cmp tmpaclout1 tmpaclout2 > /dev/null
141       }
142       ;;
143     aix)
144       func_test_same_acls ()
145       {
146         aclget "$1" > tmpaclout1
147         aclget "$2" > tmpaclout2
148         cmp tmpaclout1 tmpaclout2 > /dev/null
149       }
150       ;;
151     macosx)
152       func_test_same_acls ()
153       {
154         /bin/ls -le "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
155         /bin/ls -le "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
156         cmp tmpaclout1 tmpaclout2 > /dev/null
157       }
158       ;;
159     irix)
160       func_test_same_acls ()
161       {
162         /bin/ls -lD "$1" | sed -e "s/$1/FILENAME/g" > tmpaclout1
163         /bin/ls -lD "$2" | sed -e "s/$2/FILENAME/g" > tmpaclout2
164         cmp tmpaclout1 tmpaclout2 > /dev/null
165       }
166       ;;
167     none)
168       func_test_same_acls ()
169       {
170         :
171       }
172       ;;
173   esac
174
175   # func_test_copy file1 file2
176   # copies file1 to file2 and verifies the permissions and ACLs are the same
177   # on both.
178   func_test_copy ()
179   {
180     "$builddir"/test-copy-file${EXEEXT} "$1" "$2" || exit 1
181     if test "$USE_ACL" != 0; then
182       "$builddir"/test-sameacls${EXEEXT} "$1" "$2" || exit 1
183       func_test_same_acls                "$1" "$2" || exit 1
184     fi
185   }
186
187   func_test_copy tmpfile0 tmpfile1
188
189   if test "$USE_ACL" != 0 && test $acl_flavor != none; then
190     # A POSIX compliant 'id' program.
191     if test -f /usr/xpg4/bin/id; then
192       ID=/usr/xpg4/bin/id
193     else
194       ID=id
195     fi
196     # Use a user and group id different from the current one, to avoid
197     # redundant/ambiguous ACLs.
198     myuid=`$ID -u`
199     mygid=`$ID -g`
200     auid=1
201     if test "$auid" = "$myuid"; then auid=2; fi
202     agid=1
203     if test "$agid" = "$mygid"; then agid=2; fi
204
205     case $acl_flavor in
206       linux | freebsd | solaris)
207
208         # Set an ACL for a user.
209         setfacl -m user:$auid:1 tmpfile0
210
211         func_test_copy tmpfile0 tmpfile2
212
213         # Set an ACL for a group.
214         setfacl -m group:$agid:4 tmpfile0
215
216         func_test_copy tmpfile0 tmpfile3
217
218         # Set an ACL for other.
219         case $acl_flavor in
220           freebsd) setfacl -m other::4 tmpfile0 ;;
221           solaris) chmod o+r tmpfile0 ;;
222           *)       setfacl -m other:4 tmpfile0 ;;
223         esac
224
225         func_test_copy tmpfile0 tmpfile4
226
227         # Remove the ACL for the user.
228         case $acl_flavor in
229           linux)   setfacl -x user:$auid tmpfile0 ;;
230           freebsd) setfacl -x user:$auid:1 tmpfile0 ;;
231           *)       setfacl -d user:$auid:1 tmpfile0 ;;
232         esac
233
234         func_test_copy tmpfile0 tmpfile5
235
236         # Remove the ACL for other.
237         case $acl_flavor in
238           linux | solaris) ;; # impossible
239           freebsd) setfacl -x other::4 tmpfile0 ;;
240           *)       setfacl -d other:4 tmpfile0 ;;
241         esac
242
243         func_test_copy tmpfile0 tmpfile6
244
245         # Remove the ACL for the group.
246         case $acl_flavor in
247           linux)   setfacl -x group:$agid tmpfile0 ;;
248           freebsd) setfacl -x group:$agid:4 tmpfile0 ;;
249           *)       setfacl -d group:$agid:4 tmpfile0 ;;
250         esac
251
252         func_test_copy tmpfile0 tmpfile7
253
254         # Delete all optional ACLs.
255         case $acl_flavor in
256           linux | freebsd)
257             setfacl -m user:$auid:1 tmpfile0
258             setfacl -b tmpfile0
259             ;;
260           *)
261             setfacl -s user::6,group::0,other:0 tmpfile0 ;;
262         esac
263
264         func_test_copy tmpfile0 tmpfile8
265
266         # Copy ACLs from a file that has no ACLs.
267         echo > tmpfile9
268         chmod a+x tmpfile9
269         case $acl_flavor in
270           linux)   getfacl tmpfile9 | setfacl --set-file=- tmpfile0 ;;
271           freebsd) ;;
272           *)       getfacl tmpfile9 | setfacl -f - tmpfile0 ;;
273         esac
274         rm -f tmpfile9
275
276         func_test_copy tmpfile0 tmpfile9
277
278         ;;
279
280       cygwin)
281
282         # Set an ACL for a group.
283         setfacl -m group:0:1 tmpfile0
284
285         func_test_copy tmpfile0 tmpfile2
286
287         # Set an ACL for other.
288         setfacl -m other:4 tmpfile0
289
290         func_test_copy tmpfile0 tmpfile4
291
292         # Remove the ACL for the group.
293         setfacl -d group:0 tmpfile0
294
295         func_test_copy tmpfile0 tmpfile5
296
297         # Remove the ACL for other.
298         setfacl -d other:4 tmpfile0
299
300         func_test_copy tmpfile0 tmpfile6
301
302         # Delete all optional ACLs.
303         setfacl -s user::6,group::0,other:0 tmpfile0
304
305         func_test_copy tmpfile0 tmpfile8
306
307         # Copy ACLs from a file that has no ACLs.
308         echo > tmpfile9
309         chmod a+x tmpfile9
310         getfacl tmpfile9 | setfacl -f - tmpfile0
311         rm -f tmpfile9
312
313         func_test_copy tmpfile0 tmpfile9
314
315         ;;
316
317       hpux)
318
319         # Set an ACL for a user.
320         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
321         chacl -r "${orig}($auid.%,--x)" tmpfile0
322
323         func_test_copy tmpfile0 tmpfile2
324
325         # Set an ACL for a group.
326         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
327         chacl -r "${orig}(%.$agid,r--)" tmpfile0
328
329         func_test_copy tmpfile0 tmpfile3
330
331         # Set an ACL for other.
332         orig=`lsacl tmpfile0 | sed -e 's/ tmpfile0$//'`
333         chacl -r "${orig}(%.%,r--)" tmpfile0
334
335         func_test_copy tmpfile0 tmpfile4
336
337         # Remove the ACL for the user.
338         chacl -d "($auid.%,--x)" tmpfile0
339
340         func_test_copy tmpfile0 tmpfile5
341
342         # Remove the ACL for the group.
343         chacl -d "(%.$agid,r--)" tmpfile0
344
345         func_test_copy tmpfile0 tmpfile6
346
347         # Delete all optional ACLs.
348         chacl -z tmpfile0
349
350         func_test_copy tmpfile0 tmpfile8
351
352         # Copy ACLs from a file that has no ACLs.
353         echo > tmpfile9
354         chmod a+x tmpfile9
355         orig=`lsacl tmpfile9 | sed -e 's/ tmpfile9$//'`
356         rm -f tmpfile9
357         chacl -r "${orig}" tmpfile0
358
359         func_test_copy tmpfile0 tmpfile9
360
361         ;;
362
363       osf1)
364
365         # Set an ACL for a user.
366         setacl -u user:$auid:1 tmpfile0
367
368         func_test_copy tmpfile0 tmpfile2
369
370         # Set an ACL for a group.
371         setacl -u group:$agid:4 tmpfile0
372
373         func_test_copy tmpfile0 tmpfile3
374
375         # Set an ACL for other.
376         setacl -u other::4 tmpfile0
377
378         func_test_copy tmpfile0 tmpfile4
379
380         # Remove the ACL for the user.
381         setacl -x user:$auid:1 tmpfile0
382
383         func_test_copy tmpfile0 tmpfile5
384
385         if false; then # would give an error "can't set ACL: Invalid argument"
386           # Remove the ACL for other.
387           setacl -x other::4 tmpfile0
388
389           func_test_copy tmpfile0 tmpfile6
390         fi
391
392         # Remove the ACL for the group.
393         setacl -x group:$agid:4 tmpfile0
394
395         func_test_copy tmpfile0 tmpfile7
396
397         # Delete all optional ACLs.
398         setacl -u user:$auid:1 tmpfile0
399         setacl -b tmpfile0
400
401         func_test_copy tmpfile0 tmpfile8
402
403         # Copy ACLs from a file that has no ACLs.
404         echo > tmpfile9
405         chmod a+x tmpfile9
406         getacl tmpfile9 > tmpaclout0
407         setacl -b -U tmpaclout0 tmpfile0
408         rm -f tmpfile9
409
410         func_test_copy tmpfile0 tmpfile9
411
412         ;;
413
414       nsk)
415
416         # Set an ACL for a user.
417         setacl -m user:$auid:1 tmpfile0
418
419         func_test_copy tmpfile0 tmpfile2
420
421         # Set an ACL for a group.
422         setacl -m group:$agid:4 tmpfile0
423
424         func_test_copy tmpfile0 tmpfile3
425
426         # Set an ACL for other.
427         setacl -m other:4 tmpfile0
428
429         func_test_copy tmpfile0 tmpfile4
430
431         # Remove the ACL for the user.
432         setacl -d user:$auid tmpfile0
433
434         func_test_copy tmpfile0 tmpfile5
435
436         # Remove the ACL for the group.
437         setacl -d group:$agid tmpfile0
438
439         func_test_copy tmpfile0 tmpfile6
440
441         # Delete all optional ACLs.
442         setacl -m user:$auid:1 tmpfile0
443         setacl -s user::6,group::0,class:7,other:0 tmpfile0
444
445         func_test_copy tmpfile0 tmpfile8
446
447         # Copy ACLs from a file that has no ACLs.
448         echo > tmpfile9
449         chmod a+x tmpfile9
450         getacl tmpfile9 > tmpaclout0
451         setacl -f tmpaclout0 tmpfile0
452         rm -f tmpfile9
453
454         func_test_copy tmpfile0 tmpfile9
455
456         ;;
457
458       aix)
459
460         # Set an ACL for a user.
461         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit --x u:$auid"; } | aclput tmpfile0
462
463         func_test_copy tmpfile0 tmpfile2
464
465         # Set an ACL for a group.
466         { aclget tmpfile0 | sed -e 's/disabled$/enabled/'; echo "        permit r-- g:$agid"; } | aclput tmpfile0
467
468         func_test_copy tmpfile0 tmpfile3
469
470         # Set an ACL for other.
471         chmod o+r tmpfile0
472
473         func_test_copy tmpfile0 tmpfile4
474
475         # Remove the ACL for the user.
476         aclget tmpfile0 | grep -v ' u:[^ ]*$' | aclput tmpfile0
477
478         func_test_copy tmpfile0 tmpfile5
479
480         # Remove the ACL for the group.
481         aclget tmpfile0 | grep -v ' g:[^ ]*$' | aclput tmpfile0
482
483         func_test_copy tmpfile0 tmpfile7
484
485         # Delete all optional ACLs.
486         aclget tmpfile0 | sed -e 's/enabled$/disabled/' | sed -e '/disabled$/q' | aclput tmpfile0
487
488         func_test_copy tmpfile0 tmpfile8
489
490         # Copy ACLs from a file that has no ACLs.
491         echo > tmpfile9
492         chmod a+x tmpfile9
493         aclget tmpfile9 | aclput tmpfile0
494         rm -f tmpfile9
495
496         func_test_copy tmpfile0 tmpfile9
497
498         ;;
499
500       macosx)
501
502         # Set an ACL for a user.
503         /bin/chmod +a "user:daemon allow execute" tmpfile0
504
505         func_test_copy tmpfile0 tmpfile2
506
507         # Set an ACL for a group.
508         /bin/chmod +a "group:daemon allow read" tmpfile0
509
510         func_test_copy tmpfile0 tmpfile3
511
512         # Set an ACL for other.
513         chmod o+r tmpfile0
514
515         func_test_copy tmpfile0 tmpfile4
516
517         # Remove the ACL for the user.
518         /bin/chmod -a "user:daemon allow execute" tmpfile0
519
520         func_test_copy tmpfile0 tmpfile5
521
522         # Remove the ACL for the group.
523         /bin/chmod -a "group:daemon allow read" tmpfile0
524
525         func_test_copy tmpfile0 tmpfile7
526
527         # Delete all optional ACLs.
528         /bin/chmod -N tmpfile0
529
530         func_test_copy tmpfile0 tmpfile8
531
532         # Copy ACLs from a file that has no ACLs.
533         echo > tmpfile9
534         chmod a+x tmpfile9
535         { /bin/ls -le tmpfile9 | sed -n -e 's/^ [0-9][0-9]*: //p'; echo; } | /bin/chmod -E tmpfile0
536         rm -f tmpfile9
537
538         func_test_copy tmpfile0 tmpfile9
539
540         ;;
541
542       irix)
543
544         # Set an ACL for a user.
545         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x tmpfile0
546
547         func_test_copy tmpfile0 tmpfile2
548
549         # Set an ACL for a group.
550         /sbin/chacl user::rw-,group::---,other::---,user:$auid:--x,group:$agid:r-- tmpfile0
551
552         func_test_copy tmpfile0 tmpfile3
553
554         # Set an ACL for other.
555         /sbin/chacl user::rw-,group::---,user:$auid:--x,group:$agid:r--,other::r-- tmpfile0
556
557         func_test_copy tmpfile0 tmpfile4
558
559         # Remove the ACL for the user.
560         /sbin/chacl user::rw-,group::---,group:$agid:r--,other::r-- tmpfile0
561
562         func_test_copy tmpfile0 tmpfile5
563
564         # Remove the ACL for the group.
565         /sbin/chacl user::rw-,group::---,other::r-- tmpfile0
566
567         func_test_copy tmpfile0 tmpfile7
568
569         ;;
570
571     esac
572   fi
573
574   rm -f tmpfile[0-9] tmpaclout[0-2]
575 ) || exit 1
576
577 rm -rf "$tmp"
578 exit 0