RFR: 8173970: jar tool should have a way to extract to a directory

Peter Levart peter.levart at gmail.com
Tue Jun 29 07:05:03 UTC 2021


Hi,

On 29/03/2021 12:46, Jaikiran Pai wrote:
>> Given the support of -C/-dir is new and -P is there for backwards 
>> compatibility then I see the choices are:
>>
>>   * Issue an error when -P and -C/-dir are specified together with -x
>>   * Silently ignore the -P option
>>   * Ignore -C/-dir and the behavior is if -xfP was specified.
>>
>>
>> I am leaning towards an error given this is a new feature when -P and 
>> -C/-dir are specified with -x
>
> That sounds reasonable to me. I'll update the PR accordingly to take 
> this into account.
>
> -Jaikiran
>

If we are trying to follow the semantics of tar -C option (gnutar and 
other Unix tar(s)) then I think -P may be combined with -C when 
extracting. The manpage for gnutar says:

        -C, --directory=DIR
               Change to DIR before performing any operations. This 
option is order-sensitive, i.e. it affects all options that follow.

...the meaning is that relative paths from archive get extracted 
relative to current dir (possibly modified by -C) and that absolute 
paths get extracted to their absolute positions (i.e. not affected by 
current dir). The -C option (as implemented by tar) is also useful when 
gathering files/dirs from different base paths into common archive 
structure. Say you have the following (relative) directory/file structure:

dir1/subdir1/a.txt
dir1/subdir1/b.txt
dir1/file1.txt
dir2/subdir2/a.txt
dir2/subdir2/b.txt
dir2/file2.txt

The command:

tar -cf archive.tar -C dir1 . -C ../dir2 .

...will create archive.tar with the following content (notice the 2nd -C 
option is dependent on the 1st: -C literally changes the current 
directory before executing further options):

./
./subdir1/
./subdir1/a.txt
./subdir1/b.txt
./file1.txt
./
./subdir2/
./subdir2/a.txt
./subdir2/b.txt
./file2.txt


When extracting, -C may be used to scatter different relative archive 
files/dirs into different base paths. For example, the archive.tar from 
above can be extracted into original directory structure with the 
following command (empty dir1 and dir2 have to be already present):

tar -xf archive.tar -C dir1 ./file1.txt ./subdir1 -C ../dir2 ./file2.txt 
./subdir2


So here's a hint about the behavior of multiple -C options: if the 
option is -C, it should follow tar -C behavior.


Regards, Peter




More information about the core-libs-dev mailing list