Woes of absolute path names in tar

der Mouse mouse at mcgill-vision.UUCP
Sat Jun 18 03:44:56 AEST 1988


In article <564 at tuck.nott-cs.UUCP>, anw at nott-cs.UUCP writes:
[stuff was archive with]
> 		copy old-dir /nicedisc/anw/archive
> 		: check that all is well, then ...
> 		rm -rf old-dir
[when attempting to restore]
> 		mkdir old-dir
> 		copy /nicedisc/anw/archive old-dir
> 		: Aaarrrggghhh!  Lots of error messages
[This was because "copy" was....]
> 		echo copying from $1 to $2
> 		tar cvf - $1 | (cd $2; tar xfp -)

> [...] this is disastrous.  If "$1" begins with "/", the right-hand
> "tar" overwrites the directory the left-hand "tar" is reading from,

> There are other bugs as well, but to cut a long story short,
> "/usr/bin/copy" *now* looks like this:

[long script.  Many checks, in particular $1 must not begin with a
slash.  Ultimately....]
> 	echo copying from "$1" to "$2"
> 	sleep 10
> 	tar cvf - "$1" | (cd "$2"; tar xfp -)

This still has problems.  If, for example, I want to copy /foo/bar/baz
to /newfoo/bar/gleep, and I do it thus....

	% copy /foo/bar/baz /newfoo/bar/gleep
copy: [error message about from directory must not begin with / here]
me: why in the name of poslfit not?  Oh well....
	% cd /
	% copy foo/bar/baz newfoo/bar/gleep
...pause while it does it
	%

I then find that it has actually copied foo/bar/baz/* to
newfoo/bar/gleep/foo/bar/baz/* instead of newfoo/bar/gleep/* as I
expected.

Now, all this aggravation, including the original one about the leading
slash, could have been avoided if only you'd done....

	( cd "$1" ; tar cf - . ) | ( cd "$2" ; tar xvf - )

The code

FROM=`(cd "$1"; pwd)`
TO=`(cd "$2"; pwd)`

which appears to be intended to guard the subsequent checks against the
target being the same as, or a subdirectory of, the source, has other
problems.  In particular, it assumes that all the directories in the
chain leading to $1 and $2 are readable, which is not necessarily true
(one or more of them may well be execute-only, causing pwd to fail).

As insurance against this sort of fun, *my* tar (plug plug) won't
extract rooted pathnames without a special flag.  (It has other nice
features, like conforming to tar(5).  No, stock tar doesn't, at least
not the ones available to me: 4.3BSD, mtXinu 4.3+NFS, Sun 3.5, and Iris
(version unknown).)

					der Mouse

			uucp: mouse at mcgill-vision.uucp
			arpa: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.unix.wizards mailing list