We always add a slash now that path is cleaned.
[rsync.git] / testsuite / xattrs.test
1 #!/bin/sh
2
3 # This program is distributable under the terms of the GNU GPL (see
4 # COPYING).
5
6 # Test that rsync handles basic xattr preservation.
7
8 . $suitedir/rsync.fns
9 lnkdir="$tmpdir/lnk"
10
11 $RSYNC --version | grep "[, ] xattrs" >/dev/null || test_skipped "Rsync is configured without xattr support"
12
13 case "$HOST_OS" in
14 darwin*)
15     xset() {
16         xnam="$1"
17         xval="$2"
18         shift 2
19         xattr -s "$xnam" "$xval" "${@}"
20     }
21     xls() {
22         xattr -l "${@}" | sed "s/^[ $tab_ch]*//"
23     }
24     RSYNC_PREFIX='rsync'
25     RUSR='rsync.nonuser'
26     ;;
27 solaris*)
28     xset() {
29         xnam="$1"
30         xval="$2"
31         shift 2
32         for fn in "${@}"; do
33             runat "$fn" "$SHELL_PATH" <<EOF
34 echo "${xval}" > "${xnam}"
35 EOF
36         done
37     }
38     xls() {
39         for fn in "${@}"; do
40             runat "$fn" "$SHELL_PATH" <<EOF
41 for x in *; do echo "\$x=\`cat \$x\`"; done
42 EOF
43        done
44     }
45     RSYNC_PREFIX='rsync'
46     RUSR='rsync.nonuser'
47     ;;
48 freebsd*)
49     xset() {
50         xnam="$1"
51         xval="$2"
52         shift 2
53         setextattr -h user "$xnam" "$xval" "${@}"
54     }
55     xls() {
56         for f in "${@}"; do lsextattr -q -h user "$f" | tr '[[:space:]]' '\n' | sort | xargs -I % getextattr -h user % "$f"; done
57     }
58     RSYNC_PREFIX='rsync'
59     RUSR='rsync'
60     ;;
61 *)
62     xset() {
63         xnam="$1"
64         xval="$2"
65         shift 2
66         setfattr -n "$xnam" -v "$xval" "${@}"
67     }
68     xls() {
69         getfattr -d "${@}"
70     }
71     RSYNC_PREFIX='user.rsync'
72     RUSR='user.rsync'
73     ;;
74 esac
75
76 makepath "$lnkdir" "$fromdir/foo/bar"
77 echo now >"$fromdir/file0"
78 echo something >"$fromdir/file1"
79 echo else >"$fromdir/file2"
80 echo deep >"$fromdir/foo/file3"
81 echo normal >"$fromdir/file4"
82 echo deeper >"$fromdir/foo/bar/file5"
83
84 makepath "$chkdir/foo"
85 echo wow >"$chkdir/file1"
86 cp_touch "$fromdir/foo/file3" "$chkdir/foo"
87
88 dirs='foo foo/bar'
89 files='file0 file1 file2 foo/file3 file4 foo/bar/file5'
90
91 uid_gid=`"$TOOLDIR/tls" "$fromdir/foo" | sed 's/^.* \([0-9][0-9]*\)\.\([0-9][0-9]*\) .*/\1:\2/'`
92
93 cd "$fromdir"
94
95 xset user.foo foo file0 2>/dev/null || test_skipped "Unable to set an xattr"
96 xset user.bar bar file0
97
98 xset user.short 'this is short' file1
99 xset user.long 'this is a long attribute that will be truncated in the initial data send' file1
100 xset user.good 'this is good' file1
101 xset user.nice 'this is nice' file1
102
103 xset user.foo foo file2
104 xset user.bar bar file2
105 xset user.long 'a long attribute for our new file that tests to ensure that this works' file2
106
107 xset user.dir1 'need to test directory xattrs too' foo
108 xset user.dir2 'another xattr' foo
109 xset user.dir3 'this is one last one for the moment' foo
110
111 xset user.dir4 'another dir test' foo/bar
112 xset user.dir5 'one last one' foo/bar
113
114 xset user.foo 'new foo' foo/file3 foo/bar/file5
115 xset user.bar 'new bar' foo/file3 foo/bar/file5
116 xset user.long 'this is also a long attribute that will be truncated in the initial data send' foo/file3 foo/bar/file5
117 xset $RUSR.equal 'this long attribute should remain the same and not need to be transferred' foo/file3 foo/bar/file5
118
119 xset user.dir0 'old extra value' "$chkdir/foo"
120 xset user.dir1 'old dir value' "$chkdir/foo"
121
122 xset user.short 'old short' "$chkdir/file1"
123 xset user.extra 'remove me' "$chkdir/file1"
124
125 xset user.foo 'old foo' "$chkdir/foo/file3"
126 xset $RUSR.equal 'this long attribute should remain the same and not need to be transferred' "$chkdir/foo/file3"
127
128 case $0 in
129 *hlink*)
130     ln foo/bar/file5 foo/bar/file6 || test_skipped "Can't create hardlink"
131     files="$files foo/bar/file6"
132     dashH='-H'
133     altDest='--link-dest'
134     ;;
135 *)
136     dashH=''
137     altDest='--copy-dest'
138     ;;
139 esac
140
141 xls $dirs $files >"$scratchdir/xattrs.txt"
142
143 XFILT='-f-x_system.* -f-x_security.*'
144
145 # OK, let's try a simple xattr copy.
146 checkit "$RSYNC -avX $XFILT $dashH --super . '$chkdir/'" "$fromdir" "$chkdir"
147
148 cd "$chkdir"
149 xls $dirs $files | diff $diffopt "$scratchdir/xattrs.txt" -
150
151 cd "$fromdir"
152
153 if [ -n "$dashH" ]; then
154     for fn in $files; do
155         name=`basename $fn`
156         ln $fn ../lnk/$name
157     done
158 fi
159
160 checkit "$RSYNC -aiX $XFILT $dashH --super $altDest=../chk . ../to" "$fromdir" "$todir"
161
162 cd "$todir"
163 xls $dirs $files | diff $diffopt "$scratchdir/xattrs.txt" -
164
165 [ -n "$dashH" ] && rm -rf "$lnkdir"
166
167 cd "$fromdir"
168 rm -rf "$todir"
169
170 xset user.nice 'this is nice, but different' file1
171
172 xls $dirs $files >"$scratchdir/xattrs.txt"
173
174 checkit "$RSYNC -aiX $XFILT $dashH --fake-super --link-dest=../chk . ../to" "$chkdir" "$todir"
175
176 cd "$todir"
177 xls $dirs $files | diff $diffopt "$scratchdir/xattrs.txt" -
178
179 sed -n -e '/^[^d ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *1 /p' "$scratchdir/ls-to" >"$scratchdir/ls-diff-all"
180 grep -F -v './file1' "$scratchdir/ls-diff-all" >"$scratchdir/ls-diff" || :
181 if [ -s "$scratchdir/ls-diff" ]; then
182     echo "Missing hard links on:"
183     cat "$scratchdir/ls-diff"
184     exit 1
185 fi
186 if [ ! -s "$scratchdir/ls-diff-all" ]; then
187     echo "Too many hard links on file1!"
188     exit 1
189 fi
190
191 cd "$chkdir"
192 chmod go-rwx . $dirs $files
193
194 xset user.nice 'this is nice, but different' file1
195 xset $RSYNC_PREFIX.%stat "40000 0,0 $uid_gid" $dirs
196 xset $RSYNC_PREFIX.%stat "100000 0,0 $uid_gid" $files
197
198 xls $dirs $files >"$scratchdir/xattrs.txt"
199
200 cd "$fromdir"
201 rm -rf "$todir"
202
203 # When run by a non-root tester, this checks if no-user-perm files/dirs can be copied.
204 checkit "$RSYNC -aiX $XFILT $dashH --fake-super --chmod=a= . ../to" "$chkdir" "$todir" # 2>"$scratchdir/errors.txt"
205
206 cd "$todir"
207 xls $dirs $files | diff $diffopt "$scratchdir/xattrs.txt" -
208
209 cd "$fromdir"
210 rm -rf "$todir" "$chkdir"
211
212 $RSYNC -aX file1 file2
213 $RSYNC -aX file1 file2 ../chk/
214 $RSYNC -aX --del ../chk/ .
215 $RSYNC -aX file1 ../lnk/
216 [ -n "$dashH" ] && ln "$chkdir/file1" ../lnk/extra-link
217
218 xls file1 file2 >"$scratchdir/xattrs.txt"
219
220 checkit "$RSYNC -aiiX $XFILT $dashH $altDest=../lnk . ../to" "$chkdir" "$todir"
221
222 [ -n "$dashH" ] && rm ../lnk/extra-link
223
224 cd "$todir"
225 xls file1 file2 | diff $diffopt "$scratchdir/xattrs.txt" -
226
227 cd "$fromdir"
228 rm "$todir/file2"
229
230 echo extra >file1
231 $RSYNC -aX . ../chk/
232
233 checkit "$RSYNC -aiiX $XFILT . ../to" "$chkdir" "$todir"
234
235 cd "$todir"
236 xls file1 file2 | diff $diffopt "$scratchdir/xattrs.txt" -
237
238 # The script would have aborted on error, so getting here means we've won.
239 exit 0