From b19ffaac90bfadd1ab85267c094343ec84291f33 Mon Sep 17 00:00:00 2001 From: "Jonas M. Eid" <121502802+j-eid@users.noreply.github.com> Date: Fri, 6 Dec 2024 12:01:46 -0800 Subject: [PATCH 1/2] tar: Add test case for root directory strip_prefix (#978) --- lib/tests/tar/BUILD.bazel | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/tests/tar/BUILD.bazel b/lib/tests/tar/BUILD.bazel index d2b499668..b79ceb1f9 100644 --- a/lib/tests/tar/BUILD.bazel +++ b/lib/tests/tar/BUILD.bazel @@ -465,3 +465,41 @@ assert_unused_listing( "lib/tests/tar/unused/space in name.txt", ], ) + +############# +# Example 16: Ensure that root directories are properly handled with strip_prefix (bug #978) +# Don't assume that files or directories exist after stripping the prefix. See (bug #851) + +_SRCS16 = [ + "src_file", + ":fixture1", +] + +mtree_spec( + name = "mtree16", + srcs = _SRCS16, +) + +mtree_mutate( + name = "strip_prefix16", + mtree = ":mtree16", + strip_prefix = "lib", +) + +tar( + name = "tar_strip_prefix16", + srcs = _SRCS16, + out = "16.tar", + mtree = "strip_prefix16", +) + +assert_tar_listing( + name = "test_strip_prefix16", + actual = "tar_strip_prefix16", + expected = [ + "drwxr-xr-x 0 0 0 0 Jan 1 2023 tests/", + "drwxr-xr-x 0 0 0 0 Jan 1 2023 tests/tar/", + "-rwxr-xr-x 0 0 0 21 Jan 1 2023 tests/tar/src_file", + "-rwxr-xr-x 0 0 0 7 Jan 1 2023 tests/tar/generated.txt", + ], +) From 44dccd0fffa11087bb2b7bbf874971180ab306a1 Mon Sep 17 00:00:00 2001 From: "Jonas M. Eid" <121502802+j-eid@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:35:48 -0800 Subject: [PATCH 2/2] Use space delimiter to skip entry if missing path after strip_prefix --- lib/private/modify_mtree.awk | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/private/modify_mtree.awk b/lib/private/modify_mtree.awk index 346321c0e..a333821f3 100644 --- a/lib/private/modify_mtree.awk +++ b/lib/private/modify_mtree.awk @@ -16,7 +16,13 @@ # To avoid this, we append a slash to the directory path to make it a "full" entry. components = split($1, _, "/"); if ($0 ~ /type=dir/ && components == 1) { - $1 = $1 "/"; + if ($0 !~ /^ /) { + $1 = $1 "/"; + } + else { + # this line is the root directory and only contains orphaned keywords, which will be discarded + next; + } } } else { # this line declares some path under a parent directory, which will be discarded