[comp.unix.wizards] relative pathname question!

tedj@hpcilzb.HP.COM (Ted Johnson) (08/04/88)

I read the manual (really!), but I still can't figure out how
to copy everything from a directory onto a tape (with either
tar or cpio) USING RELATIVE PATHNAMES (as contrasted to absolute
pathnames).

For instance, I want to copy everything in the directory:

	/users/tedj/dir1/dir2/dir3/dir4/dir5/dir6

to tape in such a fashion that I can read dir6 off of tape and
put it wherever I want (e.g., /users/mike/dir6).

What I usually do is:

	cd dir6
	find . -print | cpio -oc | tcio -o /dev/rct/backup

Can anyone please tell me what options are missing???
Thanks!

-Ted

P.S. This is on a HP-UX 6.01, on a 9000/350.

spolsky@eniac.seas.upenn.edu (Joel Spolsky) (08/06/88)

In article <1670003@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes:
>
>I read the manual (really!), but I still can't figure out how
>to copy everything from a directory onto a tape (with either
>tar or cpio) USING RELATIVE PATHNAMES (as contrasted to absolute
>pathnames).

Set your default directory using "cd" and just use 

tar cf /dev/whatever *

This will store all the files in the current directory with relative
pathnames and not absolute ones.

As far as I can tell, there is NO way to extract tar files with
absolute pathnames anywhere except their original positions.

Joel Spolsky
was:     U. of Pennsylvania
is:      Bell Communications Research
will be: Yale College

aad@stpstn.UUCP (Anthony A. Datri) (08/06/88)

In article <5762@super.upenn.edu> spolsky@eniac.seas.upenn.edu.UUCP (Joel Spolsky) writes:
>In article <1670003@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes:
>As far as I can tell, there is NO way to extract tar files with
>absolute pathnames anywhere except their original positions.

  I tried for a year or two to do that, then gave up.  When I make a tar tape
I always explicitly give it a ./foo pathname.  A month or so ago one of my
users (a boss-type, actually) came to me with a tape written by a company we
do work with, and asked me to read it.  Lo and behold!  /usr2/sally/...  on
the tape!  I felt like assaulting the representative of that company who was
here with my df32 platter, but I cooled off.  I just made a /usr2 that was
a symbolic link to where I wanted it to go and everything was okay.  Things
could have been worse because there used to be a user "sally".  Reminds me
of my days back at CMU when a user would come up with a tape they wanted to
read.

me:   What density is the tape?
user: Huh?
me:   What format is the tape?
user: Huh?
me:   Allright, what kind of computer is the tape from?
user: Huh?


(but I digress..)



-- 
@disclaimer(Any concepts or opinions above are entirely mine, not those of my
	    employer, my GIGI, or my 11/34)
beak is								  beak is not
Anthony A. Datri,SysAdmin,StepstoneCorporation,stpstn!aad

jfh@rpp386.UUCP (John F. Haugh II) (08/08/88)

In article <5762@super.upenn.edu> spolsky@eniac.seas.upenn.edu.UUCP (Joel Spolsky) writes:
>As far as I can tell, there is NO way to extract tar files with
>absolute pathnames anywhere except their original positions.

sure is, and a damned clever one at that.  its called "sub-system root".
look up chroot(2) in your unix manual.
-- 
John F. Haugh II                 +--------- Cute Chocolate Quote ---------
HASA, "S" Division               | "USENET should not be confused with
UUCP:   killer!rpp386!jfh        |  something that matters, like CHOCOLATE"
DOMAIN: jfh@rpp386.uucp          |         -- apologizes to Dennis O'Connor

deke@socrates.ee.rochester.edu (Deke Kassabian) (08/08/88)

>> [question about backing up files using tar WITHOUT absolute pathnames]
>Set your default directory using "cd" and just use 
>tar cf /dev/whatever *

I would suggest using "." rather than "*" to avoid the expansion of the commmand
line to ridiculously long lengths.  While the meaning is certainly different, I
have yet to think of any problems with this method when the intent is to tar up
the contents of my current directory.

>>As far as I can tell, there is NO way to extract tar files with
>>absolute pathnames anywhere except their original positions.
> sure is, and a damned clever one at that.  its called "sub-system root".
> look up chroot(2) in your unix manual.

Less "clever" and maybe even more sloppy, a few carefully placed symbolic links
often does the trick as well.  Using chroot(2) is probably a bit better.  But
who wants to do either one?

The use of "." helps to avoid this situation, and can be used from anywhere 
(even from '/').


 ------------------------------------------------------------------------
 \\\  Deke Kassabian, URochester Department of Electrical Engineering  \\\
  \\\ deke@ee.rochester.edu                  "I never metacharacter     \\\
   \\\   or ...!rochester!ur-valhalla!deke     I didn't like......"      \\\
    -------------------------------------------------------------------------
          Quick as a wink, the sly cat stole Monroe's cheese sandwich

rlj-nbg@infohh.rmi.de (R.L. Jakschewitz) (08/09/88)

chroot newroot command
is one way to extract tar-chives that have been written with
absolute pathnames relative to 'newroot'.
See chroot(1).
rlj

jc@minya.UUCP (John Chambers) (08/10/88)

In article <5103@rpp386.UUCP>, jfh@rpp386.UUCP (John F. Haugh II) writes:
> In article <5762@super.upenn.edu> spolsky@eniac.seas.upenn.edu.UUCP (Joel Spolsky) writes:
> >As far as I can tell, there is NO way to extract tar files with
> >absolute pathnames anywhere except their original positions.
> 
> sure is, and a damned clever one at that.  its called "sub-system root".
> look up chroot(2) in your unix manual.

Or, if you have the source lying about, there's an even better solution,
which I've snuck into the libraries of a few systems.  It is easy to spot
the point at which the code has a pointer to the name of a file about to
be created.  You just insert a line like:
	while (*filename == '/') ++filename;
and recompile.  [The actual name isn't 'filename'; I've forgotten what it 
is.  But no big deal; it's easy to find.]

It works fine, and nobody on your system is ever again bothered by tar tapes
with absolute pathnames.

You might also hit the tar.1 manual page while you're at it.

Too bad we don't all have source.  Is there perhaps a PD version of tar
lurking about somewhere?

-- 
John Chambers <{adelie,ima,maynard,mit-eddie}!minya!{jc,root}> (617/484-6393)

[Any errors in the above are due to failures in the logic of the keyboard,
not in the fingers that did the typing.]

greim@sbsvax.UUCP (Michael Greim) (08/10/88)

In <5762@super.upenn.edu> Joel Spolsky writes:
>In article <1670003@hpcilzb.HP.COM> tedj@hpcilzb.HP.COM (Ted Johnson) writes:
>>
>>I read the manual (really!), but I still can't figure out how
>>to copy everything from a directory onto a tape (with either
>>tar or cpio) USING RELATIVE PATHNAMES (as contrasted to absolute
>>pathnames).
>
>Set your default directory using "cd" and just use 
>
>tar cf /dev/whatever *
>
>This will store all the files in the current directory with relative
>pathnames and not absolute ones.

>As far as I can tell, there is NO way to extract tar files with
>absolute pathnames anywhere except their original positions.
There is a way. Here it is:

------- cut here ----------------------------------------------------
static char * sccsid = "@(#)  tarfix.c  (v1.1 2/8/88)";
/*
 * this nifty program is by 
 * Phil Hochstetler
 * Sequent Computer Systems
 * Beaverton, Oregon
 *
 * it prevents tar from using absolute pathnames
 */

# include <stdio.h>

main()
{
	char fixbuf[512];

	if (isatty (0)) {
		fprintf (stderr, "tarfix by Phil Hochstetler\n");
		fprintf (stderr, "proposed usage (MX2) :\n");
		fprintf (stderr, "tarfix < /dev/fl2 | tar -XXf -\n");
		fprintf (stderr, "instead of XX use your own options\n");
		exit (0);
	}
	while (read(0, fixbuf, sizeof (fixbuf)) == sizeof (fixbuf)) {
		if (fixbuf[0] == '/' && fixbuf[99] == 0) {
			strcpy(fixbuf, &fixbuf[1]);
			fixbuf[99] = '/';
		}
		write(1, fixbuf, sizeof (fixbuf));
	}
	exit (0);
}
------- cut here -----------------------------------
This program should be used in a pipe. It reads the tar blocks
and deletes a leading "/" in a pathname so if you have
/a/b/c it will be created in ${cwd}/a/b/c. As relativ as you can get.
If you want it somewhere else you could then copy it, or indeed you
could change the above program to change certain pathnames to certain
other pathnames.
On my machine (MX2), tar for diskettes is called "far".

Absorb, apply and enjoy,
	-mg
-- 
+------------------------------------------------------------------------------+
| UUCP:  ...!uunet!unido!sbsvax!greim   | Michael T. Greim                     |
|        or greim@sbsvax.UUCP           | Universitaet des Saarlandes          |
| CSNET: greim%sbsvax.uucp@Germany.CSnet| FB 10 - Informatik (Dept. of CS)     |
| ARPA:  greim%sbsvax.uucp@uunet.UU.NET | Bau 36, Im Stadtwald 15              |
| Phone: +49 681 302 2434               | D-6600 Saarbruecken 11, West Germany |
+------------------------------------------------------------------------------+
| # include <disclaimers/std.h>                                                |
+------------------------------------------------------------------------------+

wolfe@pdnag1.uucp (0000-Mike Wolfe(0000)) (08/10/88)

In article <5103@rpp386.UUCP> jfh@rpp386.UUCP (The Beach Bum) writes:
>In article <5762@super.upenn.edu> spolsky@eniac.seas.upenn.edu.UUCP (Joel Spolsky) writes:
>>As far as I can tell, there is NO way to extract tar files with
>>absolute pathnames anywhere except their original positions.
>
>sure is, and a damned clever one at that.  its called "sub-system root".
>look up chroot(2) in your unix manual.

Be forewarned (or forearmed :-), When using chroot you change the root dir
for ALL path searches. So when tar calls mkdir (on my old 5.0 system) it
assumes that /bin/mkdir is where it's at. This means that you have to create
some kind of link/copy of mkdir and tar in the new "tree". On 5.3 systems
it becomes even more of a bugger, shared lib's can't be accessed. I assume
this can be worked around in a similar fashion but I was too grossed
out to try, YECH!

Chroot is a kludge! But it does have a limited usefulness in allowing you
to run SOME software on floppy without installing it first.

There is a public domain tar that I believe can be used to extract absolute
path names. This is a much better way than the obsolete chroot.

----
Mike Wolfe
Paradyne Corporation,  Mail stop LF-207   DOMAIN   wolfe@pdn.UUCP
8550 Ulmerton Road, PO Box 2826           UUCP     ...!uunet!pdn!wolfe
Largo, FL  34649-2826                     EARNET   (813) 530-8361

Canada is to space flight as
NASA is to Hockey.

henry@utzoo.uucp (Henry Spencer) (08/10/88)

>As far as I can tell, there is NO way to extract tar files with
>absolute pathnames anywhere except their original positions.

It has long seemed to me that tar x very badly needs a renaming facility,
to cope with this and other problems.
-- 
Intel CPUs are not defective,  |     Henry Spencer at U of Toronto Zoology
they just act that way.        | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

gwyn@smoke.ARPA (Doug Gwyn ) (08/11/88)

In article <453@infohh.rmi.de> rlj-nbg@infohh.rmi.de (R.L. Jakschewitz) writes:
-chroot newroot command
-is one way to extract tar-chives that have been written with
-absolute pathnames relative to 'newroot'.

Only if you set up things like mkdir in the new /bin first!

mfp@sobeco.UUCP (Mark F. Proudman) (08/11/88)

In article <1988Aug10.165751.20311@utzoo.uucp>, henry@utzoo.uucp (Henry Spencer) writes:
> >As far as I can tell, there is NO way to extract tar files with
> >absolute pathnames anywhere except their original positions.
> 
> It has long seemed to me that tar x very badly needs a renaming facility,
> to cope with this and other problems.

I'm joining this discussion late, so maybe someone has already come up
with this, but how about a "chroot newdir tar", prior to running tar?
I don't know how this works on other systems, but it worked on a
tower. Of course, I had to copy tar and mkdir to a bin  directory
relative to the new root.  Of course, cpio -c is far superior... 

jfh@rpp386.UUCP (John F. Haugh II) (08/12/88)

In article <61@minya.UUCP> jc@minya.UUCP (John Chambers) writes:
                                          ^^^^
>In article <5103@rpp386.UUCP>, jfh@rpp386.UUCP (John F. Haugh II) writes:
                                                 ^^^^
>> sure is, and a damned clever one at that.  its called "sub-system root".
>> look up chroot(2) in your unix manual.
>
>Too bad we don't all have source.  Is there perhaps a PD version of tar
>lurking about somewhere?

sure is.  and you guessed it, it was written by john gilmore (i think).
                                                ^^^^
it can be found in comp.sources.unix, volume 12, issues 68, 69 and 70.

aren't us `john's such wonderful people 8^)
-- 
John F. Haugh II                 +--------- Cute Chocolate Quote ---------
HASA, "S" Division               | "USENET should not be confused with
UUCP:   killer!rpp386!jfh        |  something that matters, like CHOCOLATE"
DOMAIN: jfh@rpp386.uucp          |         -- apologizes to Dennis O'Connor

bd@hpsemc.HP.COM (bob desinger) (08/17/88)

Doug Gwyn (gwyn@smoke.ARPA) writes:
> In article <453@infohh.rmi.de> rlj-nbg@infohh.rmi.de (R.L. Jakschewitz) writes:
> -chroot newroot command
> -is one way to extract tar-chives that have been written with
> -absolute pathnames relative to 'newroot'.
>
> Only if you set up things like mkdir in the new /bin first!

Not just mkdir (hm-mmm, my SysV tar calls mkdir() but doesn't fork it
---but I digress).  You'll need the tar binary and the /dev/ device
file path to the tape after running chroot.  The following script does
what you want on System V.  (BSD sites will have to change a few paths
in it, like /dev/rmt/0m.  Strict SysV sites lacking `whoami' will need
to change it to `id' piped to sed.  ...Well, okay, the script works
fine on HP-UX and other modern SysV implementations offering BSD extras.)

-- bd

#! /bin/sh
# This is a shell archive.  Remove anything before this line,
# then unwrap it by saving it in a file and typing "sh file".
#
# Wrapped by bd at hpsemc on Tue Aug 16 12:52:08 1988
# Contents:
#	readtape 	

PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:$PATH; export PATH
echo 'At the end, you should see the message "End of shell archive."'

echo Extracting readtape
cat >readtape <<'@//E*O*F readtape//'
#! /bin/sh
: Reads a tar tape written with absolute pathnames into a relative path.
# @(#)readtape	1.2	10/08/86
# Usage:
#	[1] % cd /some/directory/somewhere
#	[2] % cp `which readtape` readtape
#	[3] % su root -c "cd /some/directory/somewhere && ./readtape"

me=`basename $0`
USAGE="Usage (a three-step process):
	% cd /some/directory/somewhere
	% cp /usr/public/bin/readtape readtape
	% su root -c \"cd \`pwd\` && ./readtape\""

# We assume we're in the place where we want the files to land.
# We also assume that readtape is here already and that you're super-user now,
# but make sure of that first.

if [ ! -f ./readtape ]
then	echo >&2 "$me:  Sorry, no readtape in `pwd`"
	echo >&2 "$USAGE"
	exit 1
elif [ ! -x ./readtape ]
then	echo >&2 "$me:  Sorry, ./readtape isn't executable"
	echo >&2 "$USAGE"
	exit 1
elif [ `whoami` != root ]
then	echo >&2 "$me:  Sorry, you must be root to run readtape"
	echo >&2 "$USAGE"
	exit 1
elif [ $# -ne 0 ]
then	echo >&2 "$USAGE"
	exit 1
fi

# Here we know an executable readtape is here and that we're super-user.
# Get the tar program into a known place.

cp /usr/bin/tar tar

# Create the mag tape device file from which tar will read.

set - `ls -l /dev/rmt/0m`
# crw-rw-rw-   1 root     other      5 0x020000 Aug 22 14:30 /dev/rmt/0m
mkdir dev dev/rmt
mknod dev/rmt/0m c $5 $6
chmod 666 dev/rmt/0m

# Now read in the tape.

/etc/chroot `pwd` ./tar xp
tarstatus=$?

# Clean up after yourself.

dir=`pwd`
test $dir != /		&& rm -rf dev
test $dir != /usr/bin	&& rm -f  tar

# Exit, returning the status returned by tar.
exit $tarstatus
@//E*O*F readtape//

set `wc -lwc <readtape`
if test $1 -ne 61 -o $2 -ne 281 -o $3 -ne 1578
then	echo ! readtape should have 61 lines, 281 words, and 1578 characters
	echo ! but has $1 lines, $2 words, and $3 characters
fi
chmod 555 readtape

echo "End of shell archive."
exit 0

sahayman@watmath.waterloo.edu (Steve Hayman) (08/19/88)

Sorry to leap into this discussion so late.  Anyway, in 
article <591@sbsvax.UUCP> greim@sbsvax.UUCP suggests
using a little filter program to remove leading /'s from
tar pathnames: (i.e.  filter <the-tape | tar xf -)

	
	main()
	{
		char fixbuf[512];
	
		while (read(0, fixbuf, sizeof (fixbuf)) == sizeof (fixbuf)) {
			if (fixbuf[0] == '/' && fixbuf[99] == 0) {
				strcpy(fixbuf, &fixbuf[1]);
				fixbuf[99] = '/';
			}
			write(1, fixbuf, sizeof (fixbuf));
		}


Good idea, but isn't that kind of dangerous?  What happens when you have
a data file that happens to have a '/' and a '\0' in those
positions?    You don't want those moved around, you only
want to diddle tar header blocks.

A program to do this safely really needs to know a little more
about tar-format - fiddle the header blocks only, not the
data blocks.   I think I have a version that will do that,
I'll send it off to comp.sources.misc.

..Steve Hayman

greim@sbsvax.UUCP (Michael Greim) (08/22/88)

In article <20437@watmath.waterloo.edu<, sahayman@watmath.waterloo.edu (Steve Hayman) writes:
< Sorry to leap into this discussion so late.  Anyway, in 
< article <591@sbsvax.UUCP< greim@sbsvax.UUCP suggests
< using a little filter program to remove leading /'s from
< tar pathnames: (i.e.  filter <the-tape | tar xf -)
< 
[stuff deleted]
< Good idea, but isn't that kind of dangerous?  What happens when you have
< a data file that happens to have a '/' and a '\0' in those
< positions?    You don't want those moved around, you only
< want to diddle tar header blocks.
Yes, I wondered about that too. But I decided to worry about it only
after something went wrong. Nothing has so far.
I just tested the behaviour. Yes, it is wrong. Maybe I was just lucky
so far :-)
< 
< A program to do this safely really needs to know a little more
< about tar-format - fiddle the header blocks only, not the
< data blocks.   I think I have a version that will do that,
< I'll send it off to comp.sources.misc.
Ok. Am waiting for it.
< 
< ..Steve Hayman
	-mg
-- 
UUCP:  ...!uunet!unido!sbsvax!greim   | Michael T. Greim
       or greim@sbsvax.UUCP           | Universitaet des Saarlandes
CSNET: greim%sbsvax.uucp@Germany.CSnet| FB 10 - Informatik (Dept. of CS)
ARPA:  greim%sbsvax.uucp@uunet.UU.NET | Bau 36, Im Stadtwald 15
voice: +49 681 302 2434               | D-6600 Saarbruecken 11, West Germany

# include <disclaimers/std.h>