1 ########################################################################
3 # This software is part of the ast package #
4 # Copyright (c) 1994-2016 AT&T Intellectual Property #
5 # and is licensed under the #
6 # Eclipse Public License, Version 1.0 #
7 # by AT&T Intellectual Property #
9 # A copy of the License is available at #
10 # http://www.eclipse.org/org/documents/epl-v10.html #
11 # (with md5 checksum b35adb5213ca9657e911e9befb180842) #
13 # Information and Software Systems Research #
17 # Glenn Fowler <glenn.s.fowler@gmail.com> #
19 ########################################################################
20 ########################################################################
22 # This software is part of the ast package #
23 # Copyright (c) 1994-2012 AT&T Intellectual Property #
24 # and is licensed under the #
25 # Eclipse Public License, Version 1.0 #
26 # by AT&T Intellectual Property #
28 # A copy of the License is available at #
29 # http://www.eclipse.org/org/documents/epl-v10.html #
30 # (with md5 checksum b35adb5213ca9657e911e9befb180842) #
32 # Information and Software Systems Research #
36 # Glenn Fowler <glenn.s.fowler@gmail.com> #
38 ########################################################################
39 : replicate directory hierarchies
42 case `(getopts '[-][123:xyz]' opt --xyz; echo 0$opt) 2>/dev/null` in
43 0123) ARGV0
="-a $COMMAND"
46 @(#)$Id: ditto (AT&T Labs Research) 2012-10-25 $
49 [+NAME?ditto - replicate directory hierarchies]
50 [+DESCRIPTION?\bditto\b replicates the \asource\a directory hierarchy
51 to the \adestination\a directory hierarchy. Both \asource\a and
52 \adestination\a may be of the form
53 [\auser\a@]][\ahost\a:]][\adirectory\a]]. At least one of
54 \ahost\a: or \adirectory\a must be specified. The current user is used
55 if \auser@\a is omitted, the local host is used if \ahost\a: is
56 omitted, and the user home directory is used if \adirectory\a is
58 [+?Remote hosts and files are accessed via \bssh\b(1) or \brsh\b(1). \bksh\b(1),
59 \bpax\b(1), and \btw\b(1) must be installed on the local and remote hosts.]
60 [+?For each source file \bditto\b does one of these actions:]{
61 [+chmod|chown?change the mode and/or ownership of the destination
62 file to match the source]
63 [+copy?copy the source file to the destination]
64 [+delete?delete the destination file]
65 [+skip?the destination file is not changed]
67 [+?The source and destination hierarchies are generated by \btw\b(1) with
68 the \b--logical\b option. An \b--expr\b option may
69 be specified to prune the search. The \btw\b searches are relative to
70 the \asource\a and \adestination\a directories.]
71 [c:checksum?Copy if the \btw\b(1) 32x4 checksum mismatches.]
72 [d:delete?Delete \adestination\a files that are not in the \asource\a.]
73 [e:expr?\btw\b(1) select expression.]:[tw-expression]
74 [m!:mode?Preserve file mode.]
75 [n:show?Show the operations but do not exectute.]
76 [o:owner?Preserve file user and group ownership.]
77 [p:physical?Generate source and destination hierarchies by \btw\b(1) with
78 the \b--physical\b option.]
79 [r:remote?The remote access protocol; either \bssh\b or
80 \brsh\b.]:[protocol:=ssh]
81 [u:update?Copy only if the \asource\a file is newer than the
82 \adestination\a file.]
83 [v:verbose?Trace the operations as they are executed.]
84 [D:debug?Enable the debug trace.]
88 [+SEE ALSO?\brdist\b(1), \brsync\b(1), \brsh\b(1), \bssh\b(1), \btw\b(1)]
92 USAGE
="de:[tw-expression]mnouvD source destination"
99 getopts $ARGV0 "$USAGE" OPT
'-?'
103 parse
() # id user@host:dir
105 typeset id dir user
host
108 (( debug ||
! exec )) && print
-r $id $dir
131 eval ${id}_user
='$user'
132 eval ${id}_host
='$host'
133 eval ${id}_dir
='$dir'
138 typeset
-A chown
chmod
139 typeset tw
cp rm link
140 integer ntw
=0 ncp
=0 nrm
=0 nlink
=0 n
142 typeset src_user src_host src_path src_type src_uid src_gid src_perm src_sum
143 typeset dst_user dst_host dst_path dst_type dst_uid dst_gid dst_perm dst_sum
144 integer src_size src_mtime src_eof
145 integer dst_size dst_mtime dst_eof
147 integer debug
=0 delete
=0 exec=1 mode
=1 owner
=0 update
=0 verbose
=0 logical
149 typeset remote
=ssh trace
150 typeset checksum
='"-"' pax
="pax"
151 typeset paxreadflags
="" paxwriteflags
="--write --format=tgz --nosummary"
157 tw
[ntw
++]=--ignore-errors
158 tw
[ntw
++]=--expr=sort:name
162 while getopts $ARGV0 "$USAGE" OPT
164 c
) checksum
=checksum
;;
166 e
) tw
[ntw
++]=--expr=\"$OPTARG\" ;;
168 n
) exec=0 verbose
=1 ;;
170 p
) tw
[logical
]=--physical ;;
182 tw
[ntw
++]=--expr=\''action:printf("%d\t%d\t%s\t%s\t%s\t%-.1s\t%o\t%s\t%s\n", size, mtime, '$checksum', uid, gid, mode, perm, path, symlink);'\'
185 paxreadflags
="$paxreadflags --read"
189 paxreadflags
="$paxreadflags --verbose"
192 # start the source and destination path list generators
197 # the |& command may exit before the exec &p
198 # the print sync + read delays the |& until the exec &p finishes
201 then ($remote $src_user $src_host "{ test ! -f .profile || . ./.profile ;} && cd $src_dir && read && ${tw[*]}") 2>&1 |
&
202 else (cd $src_dir && read && eval "${tw[@]}") 2>&1 |
&
209 then ($remote $dst_user $dst_host "{ test ! -f .profile || . ./.profile ;} && cd $dst_dir && read && ${tw[*]}") 2>&1 |
&
210 else (cd $dst_dir && read && eval "${tw[@]}") 2>&1 |
&
216 # scan through the sorted path lists
226 src_path
='' src_eof
=0
227 dst_path
='' dst_eof
=0
232 # get the next source path
234 if [[ ! $src_path ]] && (( ! src_eof
))
236 if read -r -u5 text src_mtime src_sum src_uid src_gid src_type src_perm src_path src_link
238 if [[ $text != +([[:digit
:]]) ]]
240 print
-u2 $COMMAND: source: "'$text'"
248 elif (( src_size
==0 ))
257 # get the next destination path
259 if [[ ! $dst_path ]] && (( ! dst_eof
))
261 if read -r -u6 text dst_mtime dst_sum dst_uid dst_gid dst_type dst_perm dst_path dst_link
263 if [[ $text != +([[:digit
:]]) ]]
265 print
-u2 $COMMAND: destination
: $text
273 elif (( dst_size
==0 ))
282 # determine the { cp rm chmod chown } ops
286 [[ $src_path ]] && print
-r -u2 -f $
': src %8s %10s %s %s %s %s %3s %s\n' $src_size $src_mtime $src_sum $src_uid $src_gid $src_type $src_perm "$src_path"
287 [[ $dst_path ]] && print
-r -u2 -f $
': dst %8s %10s %s %s %s %s %3s %s\n' $dst_size $dst_mtime $dst_sum $dst_uid $dst_gid $dst_type $dst_perm "$dst_path"
289 if [[ $src_path == $dst_path ]]
291 if [[ $src_type != $dst_type ]]
294 if [[ $dst_path != $dst_skip ]]
296 if [[ $dst_type == d
]]
298 dst_skip
="$dst_path/*"
299 print
-r rm -r "'$dst_path'"
302 print
-r rm "'$dst_path'"
306 if [[ $src_type == l
]]
307 then if [[ $src_link != $dst_link ]]
310 if [[ $src_path != $src_skip ]]
313 print
-r cp "'$src_path'"
316 elif [[ $src_type != d
]] && { (( update
&& src_mtime
> dst_mtime
)) ||
(( ! update
)) && { (( src_size
!= dst_size
)) ||
[[ $src_sum != $dst_sum ]] ;} ;}
318 if [[ $src_path != .
]]
321 if [[ $src_path != $src_skip ]]
324 print
-r cp "'$src_path'"
328 if (( owner
)) && [[ $src_uid != $dst_uid ||
$src_gid != $dst_gid ]]
330 chown
[$src_uid.
$src_gid]="${chown[$src_uid.$src_gid]} '$src_path'"
331 if [[ $src_path != $src_skip ]]
334 print
-r chown
$src_uid.
$src_gid "'$src_path'"
336 if (( (src_perm
& 07000) || mode
&& src_perm
!= dst_perm
))
338 chmod[$src_perm]="${chmod[$src_perm]} '$src_path'"
339 if [[ $src_path != $src_skip ]]
342 print
-r chmod $src_perm "'$src_path'"
345 elif (( mode
&& src_perm
!= dst_perm
))
347 chmod[$src_perm]="${chmod[$src_perm]} '$src_path'"
348 if [[ $src_path != $src_skip ]]
351 print
-r chmod $src_perm "'$src_path'"
357 elif [[ ! $dst_path ||
$src_path && $src_path < $dst_path ]]
359 if [[ $src_path != .
]]
362 if [[ $src_path != $src_skip ]]
364 if [[ $src_type == d
]]
366 src_skip
="$src_path/*"
367 print
-r cp -r "'$src_path'"
370 print
-r cp "'$src_path'"
380 if [[ $dst_path != $dst_skip ]]
382 if [[ $dst_type == d
]]
384 dst_skip
="$dst_path/*"
385 print
-r rm -r "'$dst_path'"
388 print
-r rm "'$dst_path'"
399 # generate, transfer and execute the { rm chown chmod } script
401 if (( ${#rm[@]} || ${#chmod[@]} || ${#chown[@]} ))
408 print
-nr -- cd "'$dst_dir'"
420 for i
in ${!chown[@]}
423 for j
in ${chown[$i]}
429 print
-nr -- chown
$i
434 for i
in ${!chmod[@]}
437 for j
in ${chmod[$i]}
443 print
-nr -- chmod $i
455 $remote $dst_user $dst_host sh
462 # generate, transfer and read back the { cp } tarball
468 print
-r -f $
'%s\n' "${cp[@]}" |
473 $remote $dst_user $dst_host "{ test ! -f .profile || { DITTO_OPTIONS=dst=1; . ./.profile ;} ;} && { test -d \"$dst_dir\" || mkdir -p \"$dst_dir\" ;} && cd \"$dst_dir\" && gunzip | $pax $paxreadflags"
475 ( { test -d "$dst_dir" || mkdir
-p "$dst_dir" ;} && cd "$dst_dir" && gunzip |
$pax $paxreadflags )