Skip to content

Commit f067a4f

Browse files
authored
fix(tar): handle strip_prefix of root directory in mtree_mutate (#995)
* tar: Add test case for root directory strip_prefix (#978) * Use space delimiter to skip entry if missing path after strip_prefix
1 parent cb6e9c3 commit f067a4f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/private/modify_mtree.awk

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
# To avoid this, we append a slash to the directory path to make it a "full" entry.
1717
components = split($1, _, "/");
1818
if ($0 ~ /type=dir/ && components == 1) {
19-
$1 = $1 "/";
19+
if ($0 !~ /^ /) {
20+
$1 = $1 "/";
21+
}
22+
else {
23+
# this line is the root directory and only contains orphaned keywords, which will be discarded
24+
next;
25+
}
2026
}
2127
} else {
2228
# this line declares some path under a parent directory, which will be discarded

lib/tests/tar/BUILD.bazel

+38
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,41 @@ assert_unused_listing(
465465
"lib/tests/tar/unused/space in name.txt",
466466
],
467467
)
468+
469+
#############
470+
# Example 16: Ensure that root directories are properly handled with strip_prefix (bug #978)
471+
# Don't assume that files or directories exist after stripping the prefix. See (bug #851)
472+
473+
_SRCS16 = [
474+
"src_file",
475+
":fixture1",
476+
]
477+
478+
mtree_spec(
479+
name = "mtree16",
480+
srcs = _SRCS16,
481+
)
482+
483+
mtree_mutate(
484+
name = "strip_prefix16",
485+
mtree = ":mtree16",
486+
strip_prefix = "lib",
487+
)
488+
489+
tar(
490+
name = "tar_strip_prefix16",
491+
srcs = _SRCS16,
492+
out = "16.tar",
493+
mtree = "strip_prefix16",
494+
)
495+
496+
assert_tar_listing(
497+
name = "test_strip_prefix16",
498+
actual = "tar_strip_prefix16",
499+
expected = [
500+
"drwxr-xr-x 0 0 0 0 Jan 1 2023 tests/",
501+
"drwxr-xr-x 0 0 0 0 Jan 1 2023 tests/tar/",
502+
"-rwxr-xr-x 0 0 0 21 Jan 1 2023 tests/tar/src_file",
503+
"-rwxr-xr-x 0 0 0 7 Jan 1 2023 tests/tar/generated.txt",
504+
],
505+
)

0 commit comments

Comments
 (0)