Linux – Avoid errors from tar failing to restore directory permissions

linuxownershippermissionstartimestamp

I observe the following behavior with both tar 1.26 and 1.27.1:

$ mkdir a b a/diffowner
$ sudo mkdir b/diffowner
$ sudo chmod a+w b/diffowner
$ echo foo > a/diffowner/foo
$ tar -C a -cvf test.tar diffowner
diffowner/
diffowner/foo
$ tar -C b -xvf test.tar diffowner
diffowner/
diffowner/foo
tar: diffowner: Cannot utime: Operation not permitted
tar: diffowner: Cannot change mode to rwxr-xr-x: Operation not permitted
tar: Exiting with failure status due to previous errors

So what I'm trying to do here is extracting a tar file into an existing directory structure, where I'm not the owner of all the directories involved but I do have write permission on all of them. In fact they are shared among a group.

I don't care about timestamps, and I trust that permissions should be correct already. I'm running this as a normal user so it shouldn't be trying to --preserve-permissions unless told so, which I did not. What really does worry me is the exit status: I intend to use this in a script, and want to know whether the actual extraction worked fine.

Is there an option to tar which tells it not to set directory permissions, neither immediately nor delayed? Failing that, what other solutions would you suggest. Right now I'm thinking about extracting to a temporary directory and using rsync to move stuff into the existing tree. But perhaps you know a less hackish approach.

Best Answer

This tar option might be what you're looking for:

 --no-overwrite-dir
       preserve metadata of existing directories

I tested as follows:

$ mkdir a b a/diffowner
$ sudo mkdir b/diffowner
$ sudo chmod a+w b/diffowner
$ echo foo > a/diffowner/foo
$ tar -C a -cvf test.tar diffowner
diffowner/
diffowner/foo
$ tar -C b --no-overwrite-dir -xvf test.tar diffowner
diffowner/
diffowner/foo
$ echo $?
0