Discussion:
tar xf foo -- how to restore symlink?
(too old to reply)
Gary Aitken
2018-04-18 04:20:23 UTC
Permalink
I've got a tarball with an archived symlink and its contents.
The symlink points to another directory in the tarball, and the
tarball contains copies of the files in the symlinked directory.

A normal extract fails with the message:
$ tar -xf nufraw-0.41.tar.gz
nufraw-0.41/doc-pak: Can't replace existing directory with non-directory

What's the right way to deal with this?

How do I get tar to restore the symlink as a symlink?
-k and --exclude allow tar to continue unpacking,
but don't restore the symlink as such.

"man tar" doesn't seem to have what I want but I'm probably blind.

Thanks,

Gary
Polytropon
2018-04-18 04:30:05 UTC
Permalink
Post by Gary Aitken
I've got a tarball with an archived symlink and its contents.
The symlink points to another directory in the tarball, and the
tarball contains copies of the files in the symlinked directory.
$ tar -xf nufraw-0.41.tar.gz
nufraw-0.41/doc-pak: Can't replace existing directory with non-directory
What's the right way to deal with this?
How do I get tar to restore the symlink as a symlink?
-k and --exclude allow tar to continue unpacking,
but don't restore the symlink as such.
"man tar" doesn't seem to have what I want but I'm probably blind.
Have you checked if -P or -U result in the intended behaviour?

-P, --absolute-paths
Preserve pathnames. By default, absolute pathnames (those that
begin with a / character) have the leading slash removed both
when creating archives and extracting from them. Also, tar will
refuse to extract archive entries whose pathnames contain .. or
whose target directory would be altered by a symlink. This
option suppresses these behaviors.

-U, --unlink, --unlink-first
(x mode only) Unlink files before creating them. This can be a
minor performance optimization if most files already exist, but
can make things slower if most files do not already exist. This
flag also causes tar to remove intervening directory symlinks
instead of reporting an error. See the SECURITY section below
for more details.

Check "man tar" for "symlink". :-)
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
Gary Aitken
2018-04-19 02:20:30 UTC
Permalink
Post by Polytropon
I've got a tarball with an archived symlink and its contents. The
symlink points to another directory in the tarball, and the tarball
contains copies of the files in the symlinked directory.
A normal extract fails with the message: $ tar -xf
nufraw-0.41.tar.gz nufraw-0.41/doc-pak: Can't replace existing
directory with non-directory
What's the right way to deal with this?
How do I get tar to restore the symlink as a symlink? -k and
--exclude allow tar to continue unpacking, but don't restore the
symlink as such.
"man tar" doesn't seem to have what I want but I'm probably blind.
Have you checked if -P or -U result in the intended behaviour?
no, they didn't seem appropriate
Post by Polytropon
-P, --absolute-paths Preserve pathnames. By default, absolute
pathnames (those that begin with a / character) have the leading
slash removed both when creating archives and extracting from them.
Also, tar will refuse to extract archive entries whose pathnames
contain .. or whose target directory would be altered by a symlink.
This option suppresses these behaviors.
The archive is relative, not absolute
Post by Polytropon
-U, --unlink, --unlink-first (x mode only) Unlink files before
creating them. This can be a minor performance optimization if most
files already exist, but can make things slower if most files do not
already exist. This flag also causes tar to remove intervening
directory symlinks instead of reporting an error. See the SECURITY
section below for more details.
Creates duplicate directories, rather than a symlink.
Post by Polytropon
Check "man tar" for "symlink". :-)
Did that, didn't find anything useful.
But then I don't always read so well, much less comprehend...
That's why I asked :-)

Gary
Polytropon
2018-04-19 14:05:03 UTC
Permalink
Post by Gary Aitken
Post by Polytropon
I've got a tarball with an archived symlink and its contents. The
symlink points to another directory in the tarball, and the tarball
contains copies of the files in the symlinked directory.
A normal extract fails with the message: $ tar -xf
nufraw-0.41.tar.gz nufraw-0.41/doc-pak: Can't replace existing
directory with non-directory
What's the right way to deal with this?
How do I get tar to restore the symlink as a symlink? -k and
--exclude allow tar to continue unpacking, but don't restore the
symlink as such.
"man tar" doesn't seem to have what I want but I'm probably blind.
Have you checked if -P or -U result in the intended behaviour?
no, they didn't seem appropriate
Post by Polytropon
-P, --absolute-paths Preserve pathnames. By default, absolute
pathnames (those that begin with a / character) have the leading
slash removed both when creating archives and extracting from them.
Also, tar will refuse to extract archive entries whose pathnames
contain .. or whose target directory would be altered by a symlink.
This option suppresses these behaviors.
The archive is relative, not absolute
Post by Polytropon
-U, --unlink, --unlink-first (x mode only) Unlink files before
creating them. This can be a minor performance optimization if most
files already exist, but can make things slower if most files do not
already exist. This flag also causes tar to remove intervening
directory symlinks instead of reporting an error. See the SECURITY
section below for more details.
Creates duplicate directories, rather than a symlink.
Post by Polytropon
Check "man tar" for "symlink". :-)
Did that, didn't find anything useful.
But then I don't always read so well, much less comprehend...
That's why I asked :-)
Sadly, I do not remember anything specific to help you with tar,
but I do remember that cpio has the feature you are searching for.
I've been using it probably for exactly that purpose: because it
keeps "special entries" in file system hierarchies when restoring
from the archive.

From "man cpio":

-L (o and p modes) All symbolic links will be followed. Normally,
symbolic links are archived and copied as symbolic links. With
this option, the target of the link will be archived or copied
instead.

Maybe if you try to transfer the data in question with cpio,
you can achieve the result you are expecting?
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
Gary Aitken
2018-04-20 04:00:13 UTC
Permalink
Post by Polytropon
Post by Gary Aitken
Post by Polytropon
I've got a tarball with an archived symlink and its contents. The
symlink points to another directory in the tarball, and the tarball
contains copies of the files in the symlinked directory.
A normal extract fails with the message: $ tar -xf
nufraw-0.41.tar.gz nufraw-0.41/doc-pak: Can't replace existing
directory with non-directory
What's the right way to deal with this?
How do I get tar to restore the symlink as a symlink? -k and
--exclude allow tar to continue unpacking, but don't restore the
symlink as such.
"man tar" doesn't seem to have what I want but I'm probably blind.
Have you checked if -P or -U result in the intended behaviour?
no, they didn't seem appropriate
Post by Polytropon
-P, --absolute-paths Preserve pathnames. By default, absolute
pathnames (those that begin with a / character) have the leading
slash removed both when creating archives and extracting from them.
Also, tar will refuse to extract archive entries whose pathnames
contain .. or whose target directory would be altered by a symlink.
This option suppresses these behaviors.
The archive is relative, not absolute
Post by Polytropon
-U, --unlink, --unlink-first (x mode only) Unlink files before
creating them. This can be a minor performance optimization if most
files already exist, but can make things slower if most files do not
already exist. This flag also causes tar to remove intervening
directory symlinks instead of reporting an error. See the SECURITY
section below for more details.
Creates duplicate directories, rather than a symlink.
Post by Polytropon
Check "man tar" for "symlink". :-)
Did that, didn't find anything useful.
But then I don't always read so well, much less comprehend...
That's why I asked :-)
Sadly, I do not remember anything specific to help you with tar,
but I do remember that cpio has the feature you are searching for.
I've been using it probably for exactly that purpose: because it
keeps "special entries" in file system hierarchies when restoring
from the archive.
-L (o and p modes) All symbolic links will be followed. Normally,
symbolic links are archived and copied as symbolic links. With
this option, the target of the link will be archived or copied
instead.
Maybe if you try to transfer the data in question with cpio,
you can achieve the result you are expecting?
Unfortunately, I'm not in control of generating the archive or the way
it's fetched. It's fetched via the ports system. I'm going to punt on
the project for now as I've got a better alternative.

Thanks,

Gary
Lowell Gilbert
2018-04-20 13:24:57 UTC
Permalink
Post by Gary Aitken
Unfortunately, I'm not in control of generating the archive or the way
it's fetched. It's fetched via the ports system. I'm going to punt on
the project for now as I've got a better alternative.
Sounds like you aren't aware that our cpio(1) can extract tar files.
Gary Aitken
2018-04-21 02:34:14 UTC
Permalink
Post by Lowell Gilbert
Post by Gary Aitken
Unfortunately, I'm not in control of generating the archive or the
way it's fetched. It's fetched via the ports system. I'm going
to punt on the project for now as I've got a better alternative.
Sounds like you aren't aware that our cpio(1) can extract tar files.
Not anymore :-) Thanks.
It's still not clear to me it would help.
Polytropon's comment indicated using cpio to create the file being
transferred, and I don't have control of that; or at least I don't
think I do.

In terms of extracting from the given tar file, are you suggesting
cpio can do the following in a general manner:

Tarball:
drwxrwxr-x 0 ml ml 0 May 26 2017 nufraw-0.41/doc/
-rw-rw-r-- 0 ml ml 6195 May 26 2017 nufraw-0.41/doc/ChangeLog
lrwxrwxrwx 0 ml ml 0 May 26 2017 nufraw-0.41/doc-pak -> doc/
-rw-rw-r-- 0 ml ml 6195 May 26 2017 nufraw-0.41/doc-pak/ChangeLog

restore doc-pak as a symlink
ignore the entry doc-pak/ChangeLog

I've tried cpio -i
creates top level directory only
cpio -id
doc-pak not restored as a symlink; hierarchies duplicated
cpio -id -f doc-pak/[a-z,A-Z]*
gets the symlink properly
requires detailed knowledge of the file hierarchy
cpio -id --insecure
"Can't replace existing directory with non-directory"
duplicate hierarchies, no symlink
cpio -idu
"Can't replace existing directory with non-directory"
duplicate hierarchies, no symlink

Gary

Loading...