Carl Wilhelm Soderstrom wrote:
> 
> here's a wierd one. 
> discovered the hard way, that specifying '.' as a destination directory for
> cpio, is dangerous.
> 
> anyone know why this happens:
> chrome at steel:/var/tmp$ find usr/ -xdev
> usr/
> usr/share
> usr/share/examples
> usr/share/examples/apm
> usr/share/examples/apm/script
> chrome at steel:/var/tmp$ mkdir test         
> chrome at steel:/var/tmp$ find usr/ -xdev|cpio -padmuv test/
> test//usr/
> test//usr/share
> test//usr/share/examples
> test//usr/share/examples/apm
> test//usr/share/examples/apm/script
> 4 blocks
> chrome at steel:/var/tmp$ find test/
> test/
> test/usr
> test/usr/share
> test/usr/share/examples
> test/usr/share/examples/apm
> test/usr/share/examples/apm/script
> chrome at steel:/var/tmp$ find usr/ -xdev|cpio -padmuv .    
> ./usr/
> ./usr/share
> ./usr/share/examples
> ./usr/share/examples/apm
> cpio: usr/share/examples/apm/script: No such file or directory
> 0 blocks
> chrome at steel:/var/tmp$ find usr/
> usr/
> usr/share
> usr/share/examples
> usr/share/examples/apm
> 
> cpio should *NOT* be deleting files! but it plainly does. :(
> as the example above shows, it copies things fine, if you specify a
> directory name; but if you spceify a '.'; it won't copy anything, and it'll
> delete the files it copies. :(
> 
> is this a kernel bug, cpio bug, or just ignorance on my part?
> 
> Carl Soderstrom


Not actually a bug... it's a "feature".  You need to understand how cpio
copies a file (via either "cpio p..." or "cpio -i").

The basic algorithm for to copy in a _file_ (not a directory, pipe, et al) is:

   read $i     # actually, this is the cpio header data (inode info) for the
               # file, which is followed in a cpio archive by the file itself
   if [ -e $i ]
   then rm -f $i
   fi
   cat input > $i  # the file size is in the cpio header

Note that in your last cpio example (the one where the file disappears),
your sources for the copy are also your target.

Thus, when we apply the above "copyin" algorithm with source==target, we
delete the file we're trying to copy (onto itself) before we actually
try to copy it... Ta Da!  No more file!

Hope this helps'idly,

-S