如何创建一个不包含其内容时间戳的tar文件? [英] How to create a tar file that omits timestamps for its contents?

查看:359
本文介绍了如何创建一个不包含其内容时间戳的tar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以创建一个.tar文件,该文件忽略其文件/目录的atime/ctime/mtime值?

Is there a way to create a .tar file that omits the values of atime/ctime/mtime for its files/directories?

我们在构建过程中迈出了一步,该过程生成了一个工件目录,该目录被打包到一个tarfile中.我们希望构建步骤是幂等的-给定相同的输入,每次生成完全相同的文件/输出.

We have a step in our build process that generates a directory of artifacts that gets packaged into a tarfile. We expect that build step to be idempotent -- given the same inputs, it produces exactly the same files/output each time.

理想情况下,我们还希望跨干净构建对步骤进行逐位幂运算,以便我们可以使用连续构建的散列来检查是否没有任何变化.但是,因为tar文件为每个条目都包含时间戳记(atime/ctime/mtime),所以即使归档中每个文件的内容都是相同的,由该构建步骤创建的tar文件也永远不会与上一次运行完全相同按位相同.

Ideally, we would like also like the step to be bitwise idempotent across clean builds, so that we can use hashes of successive builds to check that nothing has changed. But because tar files include timestamps (atime/ctime/mtime) for each entry, the tar files created by that build step are never bitwise identical to the previous run, even though the contents of every file inside the archive are bitwise identical.

是否有一种生成可忽略其条目时间戳记的tarfile的方法,以便生成归档文件的步骤可以按位幂等? (我们想利用tar保留的其他文件元数据,例如文件模式位和符号链接.)

Is there a way to generate a tarfile that omits the timestamps of its entries, so that the step that generates the archive could be bitwise idempotent? (We want to leverage other file metadata that tar preserves, such as file mode bits and symlinks.)

推荐答案

要拥有真正等幂的tar,mtime是一个很好的步骤,但还不够. 您还需要为mtime设置排序顺序,所有者和组(以及它们的映射)和正确的时区(否则,在Mac和Linux之间也会出现问题).

To have a truly idempotent tar, mtime is a good step but not enough. You also need to set the sort order, the owner and group (together with their mapping) and a proper timezone for mtime (since otherwise you're gonna have issues as well between Mac and Linux).

我最终以

tar --sort=name --owner=root:0 --group=root:0 --mtime='UTC 2019-01-01' ... | gzip -n

这篇关于如何创建一个不包含其内容时间戳的tar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆