"
hongy...@gmail.com" <
hongy...@gmail.com>:
>I've tried to pin out the pertinent hints/clues from the manual
>first, but lost in the manual of massive information.
Depending on the version of 'tar', the shell command line
cd /tmp && tar -cvf - /etc
will create an archive containing all files and directories of the
'/etc' hierarchy, e. g. the names '/etc', '/etc/passwd',
'/etc/services'…
Now, if you feed that archive (by means of the pipe) to the
extracting 'tar' command
tar -xvf -
that extracting tar command will extract its contents to all files
and directories as named in the archive, that is the '/etc'
hierarchy, e. g. the names '/etc', '/etc/passwd', '/etc/services'
and so on.
=> All files will be written over itself.
The problem is, that, with the pipe, the second 'tar' starts running
before the first 'tar' has finished its job writing the archive.
This might cause a file to be opened for writing before it has been
fully read.
As the second 'tar' opens the files for writing, specifying the
'O_TRUNC' 'open(2)' flag, it will truncate the file before it has
been fully read by the first 'tar'.
To get that job right, you could do
( cd / && tar -cvf - ./etc ) |
( cd /tmp && tar -xvf - . )
The first 'tar' command would read the directory './etc' (which
effectively is the same as '/etc' because the working directory is
'/') and the files './etc/passwd', './etc/services' and so on.
Now, the archive member's names are './etc', './etc/passwd',
'./etc/services' and so on rather than '/etc', '/etc/passwd',
'/etc/services' and so on.
=> The second 'tar' would extract the archive to the directory
'./etc' and the files './etc/passwd', './etc/services' and so on.
But, because the working directory of the second command is '/tmp/'
rather than '/', the contents of the archive will be extracted to
'/tmp/./etc', '/tmp/./etc/passwd', '/tmp/./etc/services' and so on,
which are the same as '/tmp/etc', '/tmp/etc/passwd',
'/tmp/etc/services'.