bsdtar:如何避免覆盖现有文件信息?

bsdtar:如何避免覆盖现有文件信息?

bsdtar有一个-k (Do not overwrite existing files)选项,可以避免更改任何现有文件的内容,但它仍然会用存档中的内容覆盖文件信息(例如权限)。有没有办法完全bsdtar跳过覆盖现有文件,使文件信息保持完整,就像--skip-old-filesGNU tar 中的选项一样?

这是一个演示该问题的脚本:

#!/usr/bin/env bash

echo -e "\nCreate an archive with normal files"
rm -rf test-tar
mkdir test-tar
echo "TEST CONTENTS 1" > test-tar/1.txt
echo "TEST CONTENTS 2" > test-tar/2.txt
ls -la test-tar
bsdtar -czf test.tgz test-tar

echo -e "\nChange contents and permissions of one of the files"
echo "MORE CONTENTS" >> test-tar/2.txt
chmod 000 test-tar/2.txt
ls -la test-tar

echo -e "\nUntar the archive with -k (Do not overwrite existing files). The file contents are intact, but the file permissions have changed"
bsdtar -xzkf test.tgz
ls -la test-tar
cat test-tar/2.txt

echo -e "\nUntar the archive without -k"
bsdtar -xzf test.tgz
ls -la test-tar
cat test-tar/2.txt

这是脚本输出:

Create an archive with normal files
total 16
drwxr-xr-x   4 rbrainard  wheel  136 Nov 29 17:53 .
drwxr-xr-x  14 rbrainard  wheel  476 Nov 29 17:53 ..
-rw-r--r--   1 rbrainard  wheel   16 Nov 29 17:53 1.txt
-rw-r--r--   1 rbrainard  wheel   16 Nov 29 17:53 2.txt

Change contents and permissions of one of the files
total 16
drwxr-xr-x   4 rbrainard  wheel  136 Nov 29 17:53 .
drwxr-xr-x  14 rbrainard  wheel  476 Nov 29 17:53 ..
-rw-r--r--   1 rbrainard  wheel   16 Nov 29 17:53 1.txt
----------   1 rbrainard  wheel   30 Nov 29 17:53 2.txt

Untar the archive with -k (Do not overwrite existing files). The file contents are intact, but the file permissions have changed
total 16
drwxr-xr-x   4 rbrainard  wheel  136 Nov 29 17:53 .
drwxr-xr-x  14 rbrainard  wheel  476 Nov 29 17:53 ..
-rw-r--r--   1 rbrainard  wheel   16 Nov 29 17:53 1.txt
-rw-r--r--   1 rbrainard  wheel   30 Nov 29 17:53 2.txt
TEST CONTENTS 2
MORE CONTENTS

Untar the archive without -k
total 16
drwxr-xr-x   4 rbrainard  wheel  136 Nov 29 17:53 .
drwxr-xr-x  14 rbrainard  wheel  476 Nov 29 17:53 ..
-rw-r--r--   1 rbrainard  wheel   16 Nov 29 17:53 1.txt
-rw-r--r--   1 rbrainard  wheel   16 Nov 29 17:53 2.txt
TEST CONTENTS 2

我的bsdtar版本是3.3.2.

答案1

事实证明这是一个bug。我将其交叉发布到 libarchive-讨论一位维护者如此回应。在以下位置提交了问题:https://github.com/libarchive/libarchive/issues/972

相关内容