Avoid errors from tar failing to restore directory permissions

13

3

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.

MvG

Posted 2014-01-09T13:36:04.840

Reputation: 1 259

1Wondering if there's a way to do this at tar creation time as opposed to extraction..? – Roy Truelove – 2016-03-22T18:06:41.537

Answers

22

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

terdon

Posted 2014-01-09T13:36:04.840

Reputation: 45 216

1Also worked for my case, in which I extracted a tar archive to a mounted Samba share. Note that unlike the command suggests, it also suppresses the error when the destination is blank (i.e. all directories that would give errors were created by tar itself). Thanks again! – user149408 – 2016-11-26T01:17:01.410