Print this page
XXX Remove nawk(1)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/test/zfs-tests/tests/functional/acl/acl_common.kshlib
+++ new/usr/src/test/zfs-tests/tests/functional/acl/acl_common.kshlib
1 1 #
2 2 # CDDL HEADER START
3 3 #
4 4 # The contents of this file are subject to the terms of the
5 5 # Common Development and Distribution License (the "License").
6 6 # You may not use this file except in compliance with the License.
7 7 #
8 8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 # or http://www.opensolaris.org/os/licensing.
10 10 # See the License for the specific language governing permissions
11 11 # and limitations under the License.
12 12 #
13 13 # When distributing Covered Code, include this CDDL HEADER in each
14 14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 # If applicable, add the following below this CDDL HEADER, with the
16 16 # fields enclosed by brackets "[]" replaced with your own identifying
17 17 # information: Portions Copyright [yyyy] [name of copyright owner]
18 18 #
19 19 # CDDL HEADER END
20 20 #
21 21
22 22 #
23 23 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 24 # Use is subject to license terms.
25 25 #
26 26
27 27 . $STF_SUITE/tests/functional/acl/acl.cfg
28 28 . $STF_SUITE/include/libtest.shlib
29 29
30 30 #
31 31 # Get the given file/directory access mode
32 32 #
33 33 # $1 object -- file or directroy
34 34 #
35 35 function get_mode #<obj>
36 36 {
37 37 typeset obj=$1
38 38 if (( ${#obj} == 0 )); then
39 39 return 1
40 40 fi
41 41
42 42 $LS -ld $obj | $AWK '{print $1}'
43 43 }
44 44
45 45 #
46 46 # Get the given file/directory ACL
↓ open down ↓ |
46 lines elided |
↑ open up ↑ |
47 47 #
48 48 # $1 object -- file or directroy
49 49 #
50 50 function get_acl #<obj>
51 51 {
52 52 typeset obj=$1
53 53 if (( ${#obj} == 0 )); then
54 54 return 1
55 55 fi
56 56
57 - $LS -vd $obj | $NAWK '(NR != 1) {print $0}'
57 + $LS -vd $obj | $AWK '(NR != 1) {print $0}'
58 58 }
59 59
60 60 #
61 61 # Get the given file/directory ACL
62 62 #
63 63 # $1 object -- file or directroy
64 64 #
65 65 function get_compact_acl #<obj>
66 66 {
67 67 typeset obj=$1
68 68 if (( ${#obj} == 0 )); then
69 69 return 1
70 70 fi
71 71
72 - $LS -Vd $obj | $NAWK '(NR != 1) {print $0}'
72 + $LS -Vd $obj | $AWK '(NR != 1) {print $0}'
73 73 }
74 74
75 75 #
76 76 # Check the given two files/directories have the same ACLs
77 77 #
78 78 # Return 0, if source object acl is equal to target object acl.
79 79 #
80 80 # $1 source object
81 81 # $2 target object
82 82 #
83 83 function compare_acls #<src> <tgt>
84 84 {
85 85 typeset src=$1
86 86 typeset tgt=$2
87 87
88 88 (( ${#src} == 0 || ${#tgt} == 0 )) && return 1
89 89 [[ $src == $tgt ]] && return 0
90 90
91 91 typeset tmpsrc=/tmp/compare_acls.src.$$
92 92 typeset tmptgt=/tmp/compare_acls.tgt.$$
93 93
94 94 get_acl $src > $tmpsrc
95 95 get_acl $tgt > $tmptgt
96 96 typeset -i ret=0
97 97 $DIFF $tmpsrc $tmptgt > /dev/null 2>&1
98 98 ret=$?
99 99 $RM -f $tmpsrc $tmptgt
100 100
101 101 if (( ret != 0 )); then
102 102 return $ret
103 103 fi
104 104
105 105 get_compact_acl $src > $tmpsrc
106 106 get_compact_acl $tgt > $tmptgt
107 107 $DIFF $tmpsrc $tmptgt > /dev/null 2>&1
108 108 ret=$?
109 109 $RM -f $tmpsrc $tmptgt
110 110
111 111 return $ret
112 112 }
113 113
114 114 #
115 115 # Check that the given two objects have the same modes.
116 116 # Return 0, if their modes are equal with each other. Otherwise, return 1.
117 117 #
118 118 # $1 source object
119 119 # $2 target object
120 120 #
121 121 function compare_modes #<src> <tgt>
122 122 {
123 123 typeset src=$1
124 124 typeset tgt=$2
125 125 typeset -i i=0
126 126 set -A mode
127 127
128 128 (( ${#src} == 0 || ${#tgt} == 0 )) && return 1
129 129 [[ $src == $tgt ]] && return 0
130 130
131 131 typeset obj
132 132 for obj in $src $tgt
133 133 do
134 134 mode[i]=$(get_mode $obj)
135 135
136 136 (( i = i + 1 ))
137 137 done
138 138
139 139 [[ ${mode[0]} != ${mode[1]} ]] && return 1
140 140
141 141 return 0
142 142 }
143 143
144 144 #
145 145 # Check that the given two objects have the same xattrs.
146 146 # Return 0, if their xattrs are equal with each other. Otherwise, return 1.
147 147 #
148 148 # $1 source object
149 149 # $2 target object
150 150 #
151 151 function compare_xattrs #<src> <tgt>
152 152 {
153 153 typeset src=$1
154 154 typeset tgt=$2
155 155
156 156 (( ${#src} == 0 || ${#tgt} == 0 )) && return 1
157 157 [[ $src == $tgt ]] && return 0
158 158
159 159 typeset tmpsrc=/tmp/compare_xattrs.src.$$
160 160 typeset tmptgt=/tmp/compare_xattrs.tgt.$$
161 161
162 162 get_xattr $src > $tmpsrc
163 163 get_xattr $tgt > $tmptgt
164 164 typeset -i ret=0
165 165 $DIFF $tmpsrc $tmptgt > /dev/null 2>&1
166 166 ret=$?
167 167 $RM -f $tmpsrc $tmptgt
168 168
169 169 return $ret
170 170 }
171 171
172 172 #
173 173 # Check '+' is set for a given file/directory with 'ls [-l]' command
174 174 #
175 175 # $1 object -- file or directory.
176 176 #
177 177 function plus_sign_check_l #<obj>
178 178 {
179 179 typeset obj=$1
180 180 if (( ${#obj} == 0 )); then
181 181 return 1
182 182 fi
183 183
184 184 $LS -ld $obj | $AWK '{print $1}' | $GREP "+\>" > /dev/null
185 185
186 186 return $?
187 187 }
188 188
189 189 #
190 190 # Check '+' is set for a given file/directory with 'ls [-v]' command
↓ open down ↓ |
108 lines elided |
↑ open up ↑ |
191 191 #
192 192 # $1 object -- file or directory.
193 193 #
194 194 function plus_sign_check_v #<obj>
195 195 {
196 196 typeset obj=$1
197 197 if (( ${#obj} == 0 )); then
198 198 return 1
199 199 fi
200 200
201 - $LS -vd $obj | $NAWK '(NR == 1) {print $1}' | $GREP "+\>" > /dev/null
201 + $LS -vd $obj | $AWK '(NR == 1) {print $1}' | $GREP "+\>" > /dev/null
202 202
203 203 return $?
204 204 }
205 205
206 206 #
207 207 # A wrapper function of c program
208 208 #
209 209 # $1 legal login name
210 210 # $2-n commands and options
211 211 #
212 212 function chgusr_exec #<login_name> <commands> [...]
213 213 {
214 214 $CHG_USR_EXEC $@
215 215 return $?
216 216 }
217 217
218 218 #
219 219 # Export the current user for the following usr_exec operating.
220 220 #
221 221 # $1 legal login name
222 222 #
223 223 function set_cur_usr #<login_name>
224 224 {
225 225 export ZFS_ACL_CUR_USER=$1
226 226 }
227 227
228 228 #
229 229 # Run commands by $ZFS_ACL_CUR_USER
230 230 #
231 231 # $1-n commands and options
232 232 #
233 233 function usr_exec #<commands> [...]
234 234 {
235 235 $CHG_USR_EXEC "$ZFS_ACL_CUR_USER" $@
236 236 return $?
237 237 }
238 238
239 239 #
240 240 # Count how many ACEs for the speficied file or directory.
↓ open down ↓ |
29 lines elided |
↑ open up ↑ |
241 241 #
242 242 # $1 file or directroy name
243 243 #
244 244 function count_ACE #<file or dir name>
245 245 {
246 246 if [[ ! -e $1 ]]; then
247 247 log_note "Need input file or directroy name."
248 248 return 1
249 249 fi
250 250
251 - $LS -vd $1 | $NAWK 'BEGIN {count=0}
251 + $LS -vd $1 | $AWK 'BEGIN {count=0}
252 252 (NR != 1)&&(/[0-9]:/) {count++}
253 253 END {print count}'
254 254
255 255 return 0
256 256 }
257 257
258 258 #
259 259 # Get specified number ACE content of specified file or directory.
260 260 #
261 261 # $1 file or directory name
262 262 # $2 specified number
263 263 #
264 264 function get_ACE #<file or dir name> <specified number> <verbose|compact>
265 265 {
266 266 if [[ ! -e $1 || $2 -ge $(count_ACE $1) ]]; then
267 267 return 1
268 268 fi
269 269
270 270 typeset file=$1
271 271 typeset -i num=$2
272 272 typeset format=${3:-verbose}
273 273 typeset -i next_num=-1
274 274
275 275 typeset tmpfile=/tmp/tmp_get_ACE.$$
276 276 typeset line=""
277 277 typeset args
278 278
279 279 case $format in
280 280 verbose) args="-vd"
281 281 ;;
282 282 compact) args="-Vd"
283 283 ;;
284 284 *) log_fail "Invalid parameter as ($format), " \
285 285 "only verbose|compact is supported."
286 286 ;;
287 287 esac
288 288
289 289 $LS $args $file > $tmpfile
290 290 (( $? != 0 )) && log_fail "FAIL: $LS $args $file > $tmpfile"
291 291 while read line; do
292 292 [[ -z $line ]] && continue
293 293 if [[ $args == -vd ]]; then
294 294 if [[ $line == "$num":* ]]; then
295 295 (( next_num = num + 1 ))
296 296 fi
297 297 if [[ $line == "$next_num":* ]]; then
298 298 break
299 299 fi
300 300 if (( next_num != -1 )); then
301 301 print -n $line
302 302 fi
303 303 else
304 304 if (( next_num == num )); then
305 305 print -n $line
306 306 fi
307 307 (( next_num += 1 ))
308 308 fi
309 309 done < $tmpfile
310 310
311 311 $RM -f $tmpfile
312 312 (( $? != 0 )) && log_fail "FAIL: $RM -f $tmpfile"
313 313 }
314 314
315 315 #
316 316 # Cleanup exist user/group.
317 317 #
318 318 function cleanup_user_group
319 319 {
320 320 del_user $ZFS_ACL_ADMIN
321 321
322 322 del_user $ZFS_ACL_STAFF1
323 323 del_user $ZFS_ACL_STAFF2
324 324 del_group $ZFS_ACL_STAFF_GROUP
325 325
326 326 del_user $ZFS_ACL_OTHER1
327 327 del_user $ZFS_ACL_OTHER2
328 328 del_group $ZFS_ACL_OTHER_GROUP
329 329
330 330 return 0
331 331 }
332 332
333 333 #
334 334 # Clean up testfile and test directory
335 335 #
336 336 function cleanup
337 337 {
338 338 if [[ -d $TESTDIR ]]; then
339 339 cd $TESTDIR
340 340 $RM -rf $TESTDIR/*
341 341 fi
342 342 }
343 343
344 344 #
345 345 # According to specified access or acl_spec, do relevant operating by using the
346 346 # specified user.
347 347 #
348 348 # $1 specified user
349 349 # $2 node
350 350 # $3 acl_spec or access
351 351 #
352 352 function rwx_node #user node acl_spec|access
353 353 {
354 354 typeset user=$1
355 355 typeset node=$2
356 356 typeset acl_spec=$3
357 357
358 358 if [[ $user == "" || $node == "" || $acl_spec == "" ]]; then
359 359 log_note "node or acl_spec are not defined."
360 360 return 1
361 361 fi
362 362
363 363 if [[ -d $node ]]; then
364 364 case $acl_spec in
365 365 *:read_data:*|read_data)
366 366 chgusr_exec $user $LS -l $node > /dev/null 2>&1
367 367 return $? ;;
368 368 *:write_data:*|write_data)
369 369 if [[ -f ${node}/tmpfile ]]; then
370 370 log_must $RM -f ${node}/tmpfile
371 371 fi
372 372 chgusr_exec $user $TOUCH ${node}/tmpfile > \
373 373 /dev/null 2>&1
374 374 return $? ;;
375 375 *"execute:"*|execute)
376 376 chgusr_exec $user $FIND $node > /dev/null 2>&1
377 377 return $? ;;
378 378 esac
379 379 else
380 380 case $acl_spec in
381 381 *:read_data:*|read_data)
382 382 chgusr_exec $user $CAT $node > /dev/null 2>&1
383 383 return $? ;;
384 384 *:write_data:*|write_data)
385 385 chgusr_exec $user $DD if=/usr/bin/ls of=$node > \
386 386 /dev/null 2>&1
387 387 return $? ;;
388 388 *"execute:"*|execute)
389 389 ZFS_ACL_ERR_STR=$(chgusr_exec $user $node 2>&1)
390 390 return $? ;;
391 391 esac
392 392 fi
393 393 }
394 394
395 395 #
396 396 # Get the given file/directory xattr
397 397 #
398 398 # $1 object -- file or directroy
399 399 #
400 400 function get_xattr #<obj>
401 401 {
402 402 typeset obj=$1
403 403 typeset xattr
404 404 if (( ${#obj} == 0 )); then
405 405 return 1
406 406 fi
407 407
408 408 for xattr in `$RUNAT $obj $LS | \
409 409 /usr/xpg4/bin/egrep -v -e SUNWattr_ro -e SUNWattr_rw` ; do
410 410 $RUNAT $obj $SUM $xattr
411 411 done
412 412 }
413 413
414 414 #
415 415 # Get the owner of a file/directory
416 416 #
417 417 function get_owner #node
418 418 {
419 419 typeset node=$1
420 420 typeset value
421 421
422 422 if [[ -z $node ]]; then
423 423 log_fail "node are not defined."
424 424 fi
425 425
426 426 if [[ -d $node ]]; then
427 427 value=$($LS -dl $node | $AWK '{print $3}')
428 428 elif [[ -e $node ]]; then
429 429 value=$($LS -l $node | $AWK '{print $3}')
430 430 fi
431 431
432 432 $ECHO $value
433 433 }
434 434
435 435 #
436 436 # Get the group of a file/directory
437 437 #
438 438 function get_group #node
439 439 {
440 440 typeset node=$1
441 441 typeset value
442 442
443 443 if [[ -z $node ]]; then
444 444 log_fail "node are not defined."
445 445 fi
446 446
447 447 if [[ -d $node ]]; then
448 448 value=$($LS -dl $node | $AWK '{print $4}')
449 449 elif [[ -e $node ]]; then
450 450 value=$($LS -l $node | $AWK '{print $4}')
451 451 fi
452 452
453 453 $ECHO $value
454 454 }
455 455
456 456
457 457 #
458 458 # Get the group name that a UID belongs to
459 459 #
460 460 function get_user_group #uid
461 461 {
462 462 typeset uid=$1
463 463 typeset value
464 464
465 465 if [[ -z $uid ]]; then
466 466 log_fail "UID not defined."
467 467 fi
468 468
469 469 value=$(id $uid)
470 470
471 471 if [[ $? -eq 0 ]]; then
472 472 value=${value##*\(}
473 473 value=${value%%\)*}
474 474 $ECHO $value
475 475 else
476 476 log_fail "Invalid UID (uid)."
477 477 fi
478 478 }
479 479
480 480 #
481 481 # Get the specified item of the specified string
482 482 #
483 483 # $1: Item number, count from 0.
484 484 # $2-n: strings
485 485 #
486 486 function getitem
487 487 {
488 488 typeset -i n=$1
489 489 shift
490 490
491 491 (( n += 1 ))
492 492 eval echo \${$n}
493 493 }
494 494
495 495 #
496 496 # This function calculate the specified directory files checksum and write
497 497 # to the specified array.
498 498 #
499 499 # $1 directory in which the files will be cksum.
500 500 # $2 file array name which was used to store file cksum information.
501 501 # $3 attribute array name which was used to store attribute information.
502 502 #
503 503 function cksum_files #<dir> <file_array_name> <attribute_array_name>
504 504 {
505 505 typeset dir=$1
506 506 typeset farr_name=$2
507 507 typeset aarr_name=$3
508 508
509 509 [[ ! -d $dir ]] && return
510 510 typeset oldpwd=$PWD
511 511 cd $dir
512 512 typeset files=$($LS file*)
513 513
514 514 typeset -i i=0
515 515 typeset -i n=0
516 516 while (( i < NUM_FILE )); do
517 517 typeset f=$(getitem $i $files)
518 518 eval $farr_name[$i]=\$\(\$CKSUM $f\)
519 519
520 520 typeset -i j=0
521 521 while (( j < NUM_ATTR )); do
522 522 eval $aarr_name[$n]=\$\(\$RUNAT \$f \$CKSUM \
523 523 attribute.$j\)
524 524
525 525 (( j += 1 ))
526 526 (( n += 1 ))
527 527 done
528 528
529 529 (( i += 1 ))
530 530 done
531 531
532 532 cd $oldpwd
533 533 }
534 534
535 535 #
536 536 # This function compare two cksum results array.
537 537 #
538 538 # $1 The array name which stored the cksum before operation.
539 539 # $2 The array name which stored the cksum after operation.
540 540 #
541 541 function compare_cksum #<array1> <array2>
542 542 {
543 543 typeset before=$1
544 544 typeset after=$2
545 545 eval typeset -i count=\${#$before[@]}
546 546
547 547 typeset -i i=0
548 548 while (( i < count )); do
549 549 eval typeset var1=\${$before[$i]}
550 550 eval typeset var2=\${$after[$i]}
551 551
552 552 if [[ $var1 != $var2 ]]; then
553 553 return 1
554 554 fi
555 555
556 556 (( i += 1 ))
557 557 done
558 558
559 559 return 0
560 560 }
561 561
562 562 #
563 563 # This function calculate all the files cksum information in current directory
564 564 # and output them to the specified file.
565 565 #
566 566 # $1 directory from which the files will be cksum.
567 567 # $2 cksum output file
568 568 #
569 569 function record_cksum #<outfile>
570 570 {
571 571 typeset dir=$1
572 572 typeset outfile=$2
573 573
574 574 [[ ! -d ${outfile%/*} ]] && usr_exec $MKDIR -p ${outfile%/*}
575 575
576 576 usr_exec cd $dir ; $FIND . -depth -type f -exec cksum {} \\\; | \
577 577 $SORT > $outfile
578 578 usr_exec cd $dir ; $FIND . -depth -type f -xattr -exec runat {} \
579 579 cksum attribute* \\\; | $SORT >> $outfile
580 580 }
581 581
582 582 #
583 583 # The function create_files creates the directories and files that the script
584 584 # will operate on to test extended attribute functionality.
585 585 #
586 586 # $1 The base directory in which to create directories and files.
587 587 #
588 588 function create_files #<directory>
589 589 {
590 590 typeset basedir=$1
591 591
592 592 [[ ! -d $basedir ]] && usr_exec $MKDIR -m 777 $basedir
593 593 [[ ! -d $RES_DIR ]] && usr_exec $MKDIR -m 777 $RES_DIR
594 594 [[ ! -d $INI_DIR ]] && usr_exec $MKDIR -m 777 $INI_DIR
595 595 [[ ! -d $TST_DIR ]] && usr_exec $MKDIR -m 777 $TST_DIR
596 596 [[ ! -d $TMP_DIR ]] && usr_exec $MKDIR -m 777 $TMP_DIR
597 597
598 598 #
599 599 # Create the original file and its attribute files.
600 600 #
601 601 [[ ! -a $RES_DIR/file ]] && \
602 602 usr_exec $FILE_WRITE -o create -f $RES_DIR/file \
603 603 -b 1024 -d 0 -c 1
604 604 [[ ! -a $RES_DIR/attribute ]] && \
605 605 usr_exec $CP $RES_DIR/file $RES_DIR/attribute
606 606
607 607 typeset oldpwd=$PWD
608 608 cd $INI_DIR
609 609
610 610 typeset -i i=0
611 611 while (( i < NUM_FILE )); do
612 612 typeset dstfile=$INI_DIR/file.$$.$i
613 613 usr_exec $CP $RES_DIR/file $dstfile
614 614
615 615 typeset -i j=0
616 616 while (( j < NUM_ATTR )); do
617 617 usr_exec $RUNAT $dstfile \
618 618 $CP $RES_DIR/attribute ./attribute.$j
619 619 (( j += 1 ))
620 620 done
621 621
622 622 (( i += 1 ))
623 623 done
624 624
625 625 cd $oldpwd
626 626 }
↓ open down ↓ |
365 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX