cp: make -a option preserve xattrs, but with reduced diagnostics
[platform/upstream/coreutils.git] / tests / misc / xattr
1 #!/bin/sh
2 # Ensure that cp --preserve=xattr, cp --preserve=all and mv preserve extended
3 # attributes and install does not preserve extended attributes.
4 # cp -a should preserve xattr, error diagnostics should not be displayed
5
6 # Copyright (C) 2009 Free Software Foundation, Inc.
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 if test "$VERBOSE" = yes; then
22   set -x
23   cp --version
24   mv --version
25   ginstall --version
26 fi
27
28 . $srcdir/test-lib.sh
29
30 # Skip this test if cp was built without xattr support:
31 touch src dest || framework_failure
32 cp --preserve=xattr -n src dest 2>/dev/null \
33   || skip_test_ "coreutils built without xattr support"
34
35 # this code was taken from test mv/backup-is-src
36 cleanup_() { rm -rf "$other_partition_tmpdir"; }
37 . "$abs_srcdir/other-fs-tmpdir"
38 b_other="$other_partition_tmpdir/b"
39 rm -f $b_other || framework_failure
40
41 # testing xattr name-value pair
42 xattr_name="user.foo"
43 xattr_value="bar"
44 xattr_pair="$xattr_name=\"$xattr_value\""
45
46 # create new file and check its xattrs
47 touch a || framework_failure
48 getfattr -d a >out_a || skip_test_ "failed to get xattr of file"
49 grep -F "$xattr_pair" out_a >/dev/null && framework_failure
50
51 # try to set user xattr on file
52 setfattr -n "$xattr_name" -v "$xattr_value" a >out_a \
53   || skip_test_ "failed to set xattr of file"
54 getfattr -d a >out_a || skip_test_ "failed to get xattr of file"
55 grep -F "$xattr_pair" out_a >/dev/null \
56   || skip_test_ "failed to set xattr of file"
57
58 fail=0
59
60 # cp should not preserve xattr by default
61 cp a b || fail=1
62 getfattr -d b >out_b || skip_test_ "failed to get xattr of file"
63 grep -F "$xattr_pair" out_b >/dev/null && fail=1
64
65 # test if --preserve=xattr option works
66 cp --preserve=xattr a b || fail=1
67 getfattr -d b >out_b || skip_test_ "failed to get xattr of file"
68 grep -F "$xattr_pair" out_b >/dev/null || fail=1
69
70 #test if --preserve=all option works
71 cp --preserve=all a c || fail=1
72 getfattr -d c >out_c || skip_test_ "failed to get xattr of file"
73 grep -F "$xattr_pair" out_c >/dev/null || fail=1
74
75 #test if -a option works without any diagnostics
76 cp -a a d 2>err && test -s err && fail=1
77 getfattr -d d >out_d || skip_test_ "failed to get xattr of file"
78 grep -F "$xattr_pair" out_d >/dev/null || fail=1
79
80 rm b || framework_failure
81
82 # install should never preserve xattr
83 ginstall a b || fail=1
84 getfattr -d b >out_b || skip_test_ "failed to get xattr of file"
85 grep -F "$xattr_pair" out_b >/dev/null && fail=1
86
87 # mv should preserve xattr when renaming within a file system.
88 # This is implicitly done by rename () and doesn't need explicit
89 # xattr support in mv.
90 mv a b || fail=1
91 getfattr -d b >out_b || skip_test_ "failed to get xattr of file"
92 grep -F "$xattr_pair" out_b >/dev/null || cat >&2 <<EOF
93 =================================================================
94 $0: WARNING!!!
95 rename () does not preserve extended attributes
96 =================================================================
97 EOF
98
99 # try to set user xattr on file on other partition
100 test_mv=1
101 touch $b_other || framework_failure
102 setfattr -n "$xattr_name" -v "$xattr_value" $b_other >out_a 2>/dev/null \
103   || test_mv=0
104 getfattr -d $b_other >out_b 2>/dev/null || test_mv=0
105 grep -F "$xattr_pair" out_b >/dev/null || test_mv=0
106 rm -f $b_other || framework_failure
107
108 if test $test_mv -eq 1; then
109   # mv should preserve xattr when copying content from one partition to another
110   mv b $b_other || fail=1
111   getfattr -d $b_other >out_b 2>/dev/null || skip_test_ "failed to get xattr of file"
112   grep -F "$xattr_pair" out_b >/dev/null || fail=1
113 else
114   cat >&2 <<EOF
115 =================================================================
116 $0: WARNING!!!
117 failed to set xattr of file $b_other
118 =================================================================
119 EOF
120 fi
121
122 Exit $fail