a subtle(?) tar extraction permission problem

cs at zip.com.au cs at zip.com.au
Fri Jul 2 06:34:47 UTC 2004


On 12:43 27 Jun 2004, Robert P. J. Day <rpjday at mindspring.com> wrote:
| >3: Extract only the files - avoid the directories.
| tempting, but i'm wondering what would happen if the tarball has a 
| directory that doesn't exist yet. [...]
| 
| >  A tar file has entries for the dirs, which is of course where the perms 
| >  are. Don't as for them:
| >	tar xvf tarfile a/b/c/f1 a/b/c/f2 ...
| >  You could do this algorithmicly by doing a table of contents,
| >  sucking out the filenames, then doing the extract.
| again, i can't just assume that all of the directories in the tarball 
| already exist on the system.  dang.

Robert, some code.

I've just written a tiny script to list all the antecedant dirs for pathnames
fed in on stdin:

	http://www.cskk.ezoshosting.com/cs/css/bin/pathancestors

So:

    files=`tar tf tarfile | grep -v '/$'`
    dirs=`echo "$files" | pathancestors | sort -u`
    needdirs=`echo "$dirs" | while read dir
			     do [ -d "$dir/." ] || echo "$dir"
			     done
	     `
    tar xf tarfile $needdirs $files

or perhaps cleaner:

    filelist=/tmp/files$$

    # get files
    tar tf tarfile | grep -v '/$' >"$filelist"

    # get needed dirs
    pathancestors < "$filelist" \
    | sort -u \
    | while read dir
      do [ -d "$dir/." ] || echo "$dir"
      done >>"$filelist"

    # extract
    tar -x "--files-from=$filelist" -f tarfile

Untested, may need hacking. I have tested the pathancestors script itself.

And I know it _looks_ like the "pathancestors ... done" stuff might
never see EOF because it's reading from the same file it's appending
to, but the "sort -u" won't produce any output until EOF has been seen,
so no appending will happen until after then.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

If you haven't tried these things you should.
These things are fun, and fun is good.  - Dr. Seuss





More information about the users mailing list