aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/build_defs
diff options
context:
space:
mode:
authorGravatar Jeff Grafton <jgrafton@google.com>2017-08-28 11:11:10 +0200
committerGravatar Vladimir Moskva <vladmos@google.com>2017-08-28 16:09:40 +0200
commit72ab9322ac4ea6b743bc304b7b5d05f8b5b56239 (patch)
tree9439ea6355c12c7e489320558ffdc5570bbdbed3 /tools/build_defs
parent81ce9e3f2817389532d752cb325e1f0d14b69cfc (diff)
Don't prepend ./ to filenames in pkg_tar rule
Change-Id: Ib90ce99cc2e229bbe749130dbde517a075d6e333 PiperOrigin-RevId: 166677549
Diffstat (limited to 'tools/build_defs')
-rw-r--r--tools/build_defs/pkg/archive.py38
-rw-r--r--tools/build_defs/pkg/archive_test.py211
-rwxr-xr-xtools/build_defs/pkg/build_test.sh82
-rw-r--r--tools/build_defs/pkg/testdata/tar_test.tarbin10240 -> 10240 bytes
-rw-r--r--tools/build_defs/pkg/testdata/tar_test.tar.bz2bin134 -> 165 bytes
-rw-r--r--tools/build_defs/pkg/testdata/tar_test.tar.gzbin158 -> 165 bytes
-rw-r--r--tools/build_defs/pkg/testdata/tar_test.tar.xzbin192 -> 212 bytes
7 files changed, 216 insertions, 115 deletions
diff --git a/tools/build_defs/pkg/archive.py b/tools/build_defs/pkg/archive.py
index cf83aa997f..259461e4f7 100644
--- a/tools/build_defs/pkg/archive.py
+++ b/tools/build_defs/pkg/archive.py
@@ -152,8 +152,6 @@ class TarFileWriter(object):
TarFileWriter.Error: when the recursion depth has exceeded the
`depth` argument.
"""
- if not (name == '.' or name.startswith('/') or name.startswith('./')):
- name = './' + name
if os.path.isdir(path):
# Remove trailing '/' (index -1 => last character)
if name[-1] == '/':
@@ -235,15 +233,15 @@ class TarFileWriter(object):
# Recurse into directory
self.add_dir(name, file_content, uid, gid, uname, gname, mtime, mode)
return
- if not (name == '.' or name.startswith('/') or name.startswith('./')):
- name = './' + name
if kind == tarfile.DIRTYPE:
name = name.rstrip('/')
if name in self.directories:
return
components = name.rsplit('/', 1)
- if len(components) > 1:
+ # Skip absolute files in root, e.g. /foo splits to "" and foo,
+ # but /foo/bar splits to foo and bar
+ if len(components) > 1 and components[0]:
d = components[0]
self.add_file(d,
tarfile.DIRTYPE,
@@ -302,9 +300,6 @@ class TarFileWriter(object):
Raises:
TarFileWriter.Error: if an error happens when uncompressing the tar file.
"""
- if root and root[0] not in ['/', '.']:
- # Root prefix should start with a '/', adds it if missing
- root = '/' + root
compression = os.path.splitext(tar)[-1][1:]
if compression == 'tgz':
compression = 'gz'
@@ -347,25 +342,24 @@ class TarFileWriter(object):
tarinfo.gname = ''
name = tarinfo.name
- if not name.startswith('/') and not name.startswith('.'):
- name = './' + name
if root is not None:
- if name.startswith('.'):
- name = '.' + root + name.lstrip('.')
+ if not name.startswith('/'):
+ name = os.path.join(root, name)
# Add root dir with same permissions if missing. Note that
# add_file deduplicates directories and is safe to call here.
- self.add_file('.' + root,
- tarfile.DIRTYPE,
- uid=tarinfo.uid,
- gid=tarinfo.gid,
- uname=tarinfo.uname,
- gname=tarinfo.gname,
- mtime=tarinfo.mtime,
- mode=0o755)
+ self.add_file(
+ root,
+ tarfile.DIRTYPE,
+ uid=tarinfo.uid,
+ gid=tarinfo.gid,
+ uname=tarinfo.uname,
+ gname=tarinfo.gname,
+ mtime=tarinfo.mtime,
+ mode=0o755)
# Relocate internal hardlinks as well to avoid breaking them.
link = tarinfo.linkname
- if link.startswith('.') and tarinfo.type == tarfile.LNKTYPE:
- tarinfo.linkname = '.' + root + link.lstrip('.')
+ if not name.startswith('/') and tarinfo.type == tarfile.LNKTYPE:
+ tarinfo.linkname = os.path.join(root, link)
tarinfo.name = name
if tarinfo.isfile():
diff --git a/tools/build_defs/pkg/archive_test.py b/tools/build_defs/pkg/archive_test.py
index 729e533a2c..0855047149 100644
--- a/tools/build_defs/pkg/archive_test.py
+++ b/tools/build_defs/pkg/archive_test.py
@@ -153,6 +153,15 @@ class TarFileWriterTest(unittest.TestCase):
self.assertSimpleFileContent(["./a", "./ab"])
self.assertSimpleFileContent(["./a", "./b", "./ab"])
+ def testAddFileNoLeadingDotSlash(self):
+ # If we don't use ./ when adding files, there shouldn't be a directory entry
+ # for .
+ with archive.TarFileWriter(self.tempfile) as f:
+ f.add_file("a", content="a")
+ f.add_file("b", content="b")
+ content = [{"name": "a"}, {"name": "b"}]
+ self.assertTarFileContent(self.tempfile, content)
+
def testDottedFiles(self):
with archive.TarFileWriter(self.tempfile) as f:
f.add_file("a")
@@ -162,11 +171,48 @@ class TarFileWriterTest(unittest.TestCase):
f.add_file("..e")
f.add_file(".f")
content = [
- {"name": "."}, {"name": "./a"}, {"name": "/b"}, {"name": "./c"},
- {"name": "./.d"}, {"name": "./..e"}, {"name": "./.f"}
+ {
+ "name": "a"
+ },
+ {
+ "name": "/b"
+ },
+ {
+ "name": "."
+ },
+ {
+ "name": "./c"
+ },
+ {
+ "name": "./.d"
+ },
+ {
+ "name": "..e"
+ },
+ {
+ "name": ".f"
+ },
]
self.assertTarFileContent(self.tempfile, content)
+ def testSubdirectories(self):
+ with archive.TarFileWriter(self.tempfile) as f:
+ f.add_file("a")
+ f.add_file("b/c")
+ f.add_file("c/d/e")
+ f.add_file("./d")
+ f.add_file("./e/f")
+ f.add_file("./f/g/h")
+ f.add_file("/g")
+ f.add_file("/h/i")
+ f.add_file("/i/j/k")
+ expected_names = [
+ "a", "b", "b/c", "c", "c/d", "c/d/e", ".", "./d", "./e", "./e/f", "./f",
+ "./f/g", "./f/g/h", "/g", "/h", "/h/i", "/i", "/i/j", "/i/j/k"
+ ]
+ expected_content = [{"name": n} for n in expected_names]
+ self.assertTarFileContent(self.tempfile, expected_content)
+
def testAddDir(self):
# For some strange reason, ending slash is stripped by the test
content = [
@@ -191,36 +237,77 @@ class TarFileWriterTest(unittest.TestCase):
def testMergeTar(self):
content = [
- {"name": "./a", "data": "a"},
- {"name": "./ab", "data": "ab"},
- ]
+ {
+ "name": "a",
+ "data": "a"
+ },
+ {
+ "name": "ab",
+ "data": "ab"
+ },
+ ]
for ext in ["", ".gz", ".bz2", ".xz"]:
with archive.TarFileWriter(self.tempfile) as f:
- f.add_tar(os.path.join(testenv.TESTDATA_PATH, "tar_test.tar" + ext),
- name_filter=lambda n: n != "./b")
+ f.add_tar(
+ os.path.join(testenv.TESTDATA_PATH, "tar_test.tar" + ext),
+ name_filter=lambda n: n != "b")
self.assertTarFileContent(self.tempfile, content)
def testMergeTarRelocated(self):
content = [
- {"name": ".", "mode": 0o755},
- {"name": "./foo", "mode": 0o755},
- {"name": "./foo/a", "data": "a"},
- {"name": "./foo/ab", "data": "ab"},
- ]
+ {
+ "name": "foo",
+ "mode": 0o755
+ },
+ {
+ "name": "foo/a",
+ "data": "a"
+ },
+ {
+ "name": "foo/ab",
+ "data": "ab"
+ },
+ ]
+ with archive.TarFileWriter(self.tempfile) as f:
+ f.add_tar(
+ os.path.join(testenv.TESTDATA_PATH, "tar_test.tar"),
+ name_filter=lambda n: n != "b",
+ root="foo")
+ self.assertTarFileContent(self.tempfile, content)
+
+ def testMergeTarRelocatedAbsolute(self):
+ content = [
+ {
+ "name": "/bar",
+ "mode": 0o755
+ },
+ {
+ "name": "/bar/a",
+ "data": "a"
+ },
+ {
+ "name": "/bar/ab",
+ "data": "ab"
+ },
+ ]
with archive.TarFileWriter(self.tempfile) as f:
- f.add_tar(os.path.join(testenv.TESTDATA_PATH, "tar_test.tar"),
- name_filter=lambda n: n != "./b", root="/foo")
+ f.add_tar(
+ os.path.join(testenv.TESTDATA_PATH, "tar_test.tar"),
+ name_filter=lambda n: n != "b",
+ root="/bar")
self.assertTarFileContent(self.tempfile, content)
def testAddingDirectoriesForFile(self):
with archive.TarFileWriter(self.tempfile) as f:
f.add_file("d/f")
content = [
- {"name": ".",
- "mode": 0o755},
- {"name": "./d",
- "mode": 0o755},
- {"name": "./d/f"},
+ {
+ "name": "d",
+ "mode": 0o755
+ },
+ {
+ "name": "d/f"
+ },
]
self.assertTarFileContent(self.tempfile, content)
@@ -241,18 +328,30 @@ class TarFileWriterTest(unittest.TestCase):
f.add_dir("a", a_dir)
f.add_file("a/b/f")
content = [
- {"name": ".",
- "mode": 0o755},
- {"name": "./d",
- "mode": 0o755},
- {"name": "./d/dir_file"},
- {"name": "./d/f"},
- {"name": "./a",
- "mode": 0o755},
- {"name": "./a/dir_file"},
- {"name": "./a/b",
- "mode": 0o755},
- {"name": "./a/b/f"},
+ {
+ "name": "d",
+ "mode": 0o755
+ },
+ {
+ "name": "d/dir_file"
+ },
+ {
+ "name": "d/f"
+ },
+ {
+ "name": "a",
+ "mode": 0o755
+ },
+ {
+ "name": "a/dir_file"
+ },
+ {
+ "name": "a/b",
+ "mode": 0o755
+ },
+ {
+ "name": "a/b/f"
+ },
]
self.assertTarFileContent(self.tempfile, content)
@@ -270,23 +369,39 @@ class TarFileWriterTest(unittest.TestCase):
f.add_file("x/y/f")
f.add_file("x", tarfile.DIRTYPE)
content = [
- {"name": ".",
- "mode": 0o755},
- {"name": "./d",
- "mode": 0o755},
- {"name": "./d/f"},
- {"name": "./a",
- "mode": 0o755},
- {"name": "./a/b",
- "mode": 0o755},
- {"name": "./a/b/c",
- "mode": 0o755},
- {"name": "./a/b/c/f"},
- {"name": "./x",
- "mode": 0o755},
- {"name": "./x/y",
- "mode": 0o755},
- {"name": "./x/y/f"},
+ {
+ "name": "d",
+ "mode": 0o755
+ },
+ {
+ "name": "d/f"
+ },
+ {
+ "name": "a",
+ "mode": 0o755
+ },
+ {
+ "name": "a/b",
+ "mode": 0o755
+ },
+ {
+ "name": "a/b/c",
+ "mode": 0o755
+ },
+ {
+ "name": "a/b/c/f"
+ },
+ {
+ "name": "x",
+ "mode": 0o755
+ },
+ {
+ "name": "x/y",
+ "mode": 0o755
+ },
+ {
+ "name": "x/y/f"
+ },
]
self.assertTarFileContent(self.tempfile, content)
diff --git a/tools/build_defs/pkg/build_test.sh b/tools/build_defs/pkg/build_test.sh
index 1322f5006d..4e80d85263 100755
--- a/tools/build_defs/pkg/build_test.sh
+++ b/tools/build_defs/pkg/build_test.sh
@@ -97,36 +97,34 @@ function get_changes() {
}
function assert_content() {
- local listing="./
-./etc/
-./etc/nsswitch.conf
-./usr/
-./usr/titi
-./usr/bin/
-./usr/bin/java -> /path/to/bin/java"
+ local listing="etc/
+etc/nsswitch.conf
+usr/
+usr/titi
+usr/bin/
+usr/bin/java -> /path/to/bin/java"
check_eq "$listing" "$(get_tar_listing $1)"
- check_eq "-rwxr-xr-x" "$(get_tar_permission $1 ./usr/titi)"
- check_eq "-rw-r--r--" "$(get_tar_permission $1 ./etc/nsswitch.conf)"
- check_eq "24/42" "$(get_numeric_tar_owner $1 ./etc/)"
- check_eq "24/42" "$(get_numeric_tar_owner $1 ./etc/nsswitch.conf)"
- check_eq "42/24" "$(get_numeric_tar_owner $1 ./usr/)"
- check_eq "42/24" "$(get_numeric_tar_owner $1 ./usr/titi)"
+ check_eq "-rwxr-xr-x" "$(get_tar_permission $1 usr/titi)"
+ check_eq "-rw-r--r--" "$(get_tar_permission $1 etc/nsswitch.conf)"
+ check_eq "24/42" "$(get_numeric_tar_owner $1 etc/)"
+ check_eq "24/42" "$(get_numeric_tar_owner $1 etc/nsswitch.conf)"
+ check_eq "42/24" "$(get_numeric_tar_owner $1 usr/)"
+ check_eq "42/24" "$(get_numeric_tar_owner $1 usr/titi)"
if [ -z "${2-}" ]; then
- check_eq "tata/titi" "$(get_tar_owner $1 ./etc/)"
- check_eq "tata/titi" "$(get_tar_owner $1 ./etc/nsswitch.conf)"
- check_eq "titi/tata" "$(get_tar_owner $1 ./usr/)"
- check_eq "titi/tata" "$(get_tar_owner $1 ./usr/titi)"
+ check_eq "tata/titi" "$(get_tar_owner $1 etc/)"
+ check_eq "tata/titi" "$(get_tar_owner $1 etc/nsswitch.conf)"
+ check_eq "titi/tata" "$(get_tar_owner $1 usr/)"
+ check_eq "titi/tata" "$(get_tar_owner $1 usr/titi)"
fi
}
function test_tar() {
- local listing="./
-./etc/
-./etc/nsswitch.conf
-./usr/
-./usr/titi
-./usr/bin/
-./usr/bin/java -> /path/to/bin/java"
+ local listing="etc/
+etc/nsswitch.conf
+usr/
+usr/titi
+usr/bin/
+usr/bin/java -> /path/to/bin/java"
for i in "" ".gz" ".bz2" ".xz"; do
assert_content "test-tar-${i:1}.tar$i"
# Test merging tar files
@@ -135,18 +133,13 @@ function test_tar() {
assert_content "test-tar-inclusion-${i:1}.tar" "true"
done;
- check_eq "./
-./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-empty.tar)"
- check_eq "./
-./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-none.tar)"
- check_eq "./
-./nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-etc.tar)"
- check_eq "./
-./etc/
-./etc/nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-dot.tar)"
- check_eq "./
-./not-etc/
-./not-etc/mapped-filename.conf" "$(get_tar_listing test-tar-files_dict.tar)"
+ check_eq "nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-empty.tar)"
+ check_eq "nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-none.tar)"
+ check_eq "nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-etc.tar)"
+ check_eq "etc/
+etc/nsswitch.conf" "$(get_tar_listing test-tar-strip_prefix-dot.tar)"
+ check_eq "not-etc/
+not-etc/mapped-filename.conf" "$(get_tar_listing test-tar-files_dict.tar)"
}
function test_deb() {
@@ -154,16 +147,15 @@ function test_deb() {
echo "Unable to run test for debian, no dpkg-deb!" >&2
return 0
fi
- local listing="./
-./etc/
-./etc/nsswitch.conf
-./usr/
-./usr/titi
-./usr/bin/
-./usr/bin/java -> /path/to/bin/java"
+ local listing="etc/
+etc/nsswitch.conf
+usr/
+usr/titi
+usr/bin/
+usr/bin/java -> /path/to/bin/java"
check_eq "$listing" "$(get_deb_listing test-deb.deb)"
- check_eq "-rwxr-xr-x" "$(get_deb_permission test-deb.deb ./usr/titi)"
- check_eq "-rw-r--r--" "$(get_deb_permission test-deb.deb ./etc/nsswitch.conf)"
+ check_eq "-rwxr-xr-x" "$(get_deb_permission test-deb.deb usr/titi)"
+ check_eq "-rw-r--r--" "$(get_deb_permission test-deb.deb etc/nsswitch.conf)"
get_deb_description test-deb.deb >$TEST_log
expect_log "Description: toto"
expect_log "Package: titi"
diff --git a/tools/build_defs/pkg/testdata/tar_test.tar b/tools/build_defs/pkg/testdata/tar_test.tar
index 70b043bd9d..4b915d63f6 100644
--- a/tools/build_defs/pkg/testdata/tar_test.tar
+++ b/tools/build_defs/pkg/testdata/tar_test.tar
Binary files differ
diff --git a/tools/build_defs/pkg/testdata/tar_test.tar.bz2 b/tools/build_defs/pkg/testdata/tar_test.tar.bz2
index c6f7edf5a7..ff5097f480 100644
--- a/tools/build_defs/pkg/testdata/tar_test.tar.bz2
+++ b/tools/build_defs/pkg/testdata/tar_test.tar.bz2
Binary files differ
diff --git a/tools/build_defs/pkg/testdata/tar_test.tar.gz b/tools/build_defs/pkg/testdata/tar_test.tar.gz
index 4b851716dd..1b023dfd47 100644
--- a/tools/build_defs/pkg/testdata/tar_test.tar.gz
+++ b/tools/build_defs/pkg/testdata/tar_test.tar.gz
Binary files differ
diff --git a/tools/build_defs/pkg/testdata/tar_test.tar.xz b/tools/build_defs/pkg/testdata/tar_test.tar.xz
index 1ea3c8b836..5f3aac559d 100644
--- a/tools/build_defs/pkg/testdata/tar_test.tar.xz
+++ b/tools/build_defs/pkg/testdata/tar_test.tar.xz
Binary files differ