Is there a way to create a .tar
file that omits the values of atime/ctime/mtime for its files/directories?
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.
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.
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.)
GNU tar has a --mtime
argument, which can be used to store a fixed date in the archive rather than a file's actual mtime:
tar --mtime='1970-01-01' input ...
When compressing a tarball with gzip, it's also necessary to specify -n
to prevent name and timestamp of the tar archive from being stored:
tar --mtime='1970-01-01' input ... | gzip -n >input.tar.gz