pkg_tar: Create tar files with non-zero mtime
pkg_tar created tars whose contents had a fixed mtime of 0 so that the rule's
output was deterministic. This can lead to undefined behavior in downstream
applications which process those files. Work around this by choosing another
fixed but non-zero mtime value.
Resolves https://github.com/bazelbuild/bazel/issues/1299.
Testing Done:
- `bazel test tools/build_defs/pkg:all`
Closes #7276.
PiperOrigin-RevId: 234450493
diff --git a/tools/build_defs/pkg/archive.py b/tools/build_defs/pkg/archive.py
index 1fd1eaa..45299d7 100644
--- a/tools/build_defs/pkg/archive.py
+++ b/tools/build_defs/pkg/archive.py
@@ -20,6 +20,10 @@
import subprocess
import tarfile
+# Use a deterministic mtime that doesn't confuse other programs.
+# See: https://github.com/bazelbuild/bazel/issues/1299
+MTIME = 946684800 # 2000-01-01 00:00:00.000 UTC
+
class SimpleArFile(object):
"""A simple AR file reader.
@@ -118,7 +122,7 @@
# The Tarfile class doesn't allow us to specify gzip's mtime attribute.
# Instead, we manually re-implement gzopen from tarfile.py and set mtime.
self.fileobj = gzip.GzipFile(
- filename=name, mode='w', compresslevel=9, mtime=0)
+ filename=name, mode='w', compresslevel=9, mtime=MTIME)
self.tar = tarfile.open(name=name, mode=mode, fileobj=self.fileobj)
self.members = set([])
self.directories = set([])
@@ -136,7 +140,7 @@
gid=0,
uname='',
gname='',
- mtime=0,
+ mtime=MTIME,
mode=None,
depth=100):
"""Recursively add a directory.
@@ -219,7 +223,7 @@
gid=0,
uname='',
gname='',
- mtime=0,
+ mtime=MTIME,
mode=None):
"""Add a file to the current tar.
@@ -350,7 +354,7 @@
intar = tarfile.open(name=tar, mode=inmode)
for tarinfo in intar:
if name_filter is None or name_filter(tarinfo.name):
- tarinfo.mtime = 0
+ tarinfo.mtime = MTIME
if rootuid is not None and tarinfo.uid == rootuid:
tarinfo.uid = 0
tarinfo.uname = 'root'
diff --git a/tools/build_defs/pkg/build_test.sh b/tools/build_defs/pkg/build_test.sh
index 99bae8e..0d0148c 100755
--- a/tools/build_defs/pkg/build_test.sh
+++ b/tools/build_defs/pkg/build_test.sh
@@ -155,13 +155,13 @@
check_eq "./
./not-etc/
./not-etc/mapped-filename.conf" "$(get_tar_listing test-tar-files_dict.tar)"
- check_eq "drwxr-xr-x 0/0 0 1970-01-01 00:00 ./
--rwxrwxrwx 0/0 0 1970-01-01 00:00 ./a
--rwxrwxrwx 0/0 0 1970-01-01 00:00 ./b" \
+ check_eq "drwxr-xr-x 0/0 0 2000-01-01 00:00 ./
+-rwxrwxrwx 0/0 0 2000-01-01 00:00 ./a
+-rwxrwxrwx 0/0 0 2000-01-01 00:00 ./b" \
"$(get_tar_verbose_listing test-tar-empty_files.tar)"
- check_eq "drwxr-xr-x 0/0 0 1970-01-01 00:00 ./
-drwxrwxrwx 0/0 0 1970-01-01 00:00 ./tmp/
-drwxrwxrwx 0/0 0 1970-01-01 00:00 ./pmt/" \
+ check_eq "drwxr-xr-x 0/0 0 2000-01-01 00:00 ./
+drwxrwxrwx 0/0 0 2000-01-01 00:00 ./tmp/
+drwxrwxrwx 0/0 0 2000-01-01 00:00 ./pmt/" \
"$(get_tar_verbose_listing test-tar-empty_dirs.tar)"
}