diff --git a/distroless/private/flatten.sh b/distroless/private/flatten.sh index 25b6762..fd96b99 100755 --- a/distroless/private/flatten.sh +++ b/distroless/private/flatten.sh @@ -35,12 +35,13 @@ if [[ "$deduplicate" == "True" ]]; then # number of occurrences of each path, and the second pass determines whether each entry is the final (or only) # occurrence of that path. - $bsdtar --confirmation "$@" 2< <("${awk}" ' + $bsdtar --confirmation -P -s ',^\([^/.]\),./\1,' "$@" 2< <("${awk}" ' function normalize(p) { - # Strip leading "./" and trailing "/" so that "./etc/" and "etc/" are treated as the same path. - sub(/^\.\//, "", p) - sub(/\/$/, "", p) - return p + if (p == "") return "/" + if (p ~ /^(\/|\.\/)/) { + return p + } + return "./" p } { key = normalize($1) diff --git a/examples/flatten/BUILD.bazel b/examples/flatten/BUILD.bazel index a4dbfca..838e9ba 100644 --- a/examples/flatten/BUILD.bazel +++ b/examples/flatten/BUILD.bazel @@ -112,12 +112,12 @@ assert_tar_listing( name = "test_flatten_dedup_listing", actual = "flatten_dedup", expected = """\ -examples/ -examples/flatten/ -examples/flatten/dir/ -examples/flatten/dir/changelog -examples/flatten/dir/sub/ -examples/flatten/dir/sub/content.txt +./examples/ +./examples/flatten/ +./examples/flatten/dir/ +./examples/flatten/dir/changelog +./examples/flatten/dir/sub/ +./examples/flatten/dir/sub/content.txt """, ) @@ -128,8 +128,8 @@ tar( name = "dotslash_tar", compress = "gzip", mtree = [ - "./etc/ type=dir uid=0 gid=0 mode=0755 time=0.0", - "./etc/sub/ type=dir uid=0 gid=0 mode=0755 time=0.0", + "./top/ type=dir uid=0 gid=0 mode=0755 time=0.0", + "./top/sub/ type=dir uid=0 gid=0 mode=0755 time=0.0", ], ) @@ -137,17 +137,28 @@ tar( name = "nodotslash_tar", compress = "gzip", mtree = [ - "etc/ type=dir uid=0 gid=0 mode=0755 time=0.0", - "etc/sub/ type=dir uid=0 gid=0 mode=0755 time=0.0", + "top/ type=dir uid=0 gid=0 mode=0755 time=0.0", + "top/sub/ type=dir uid=0 gid=0 mode=0755 time=0.0", ], ) +genrule( + name = "absolute_tar_gen", + outs = ["absolute_tar.tar.gz"], + cmd = """ +mkdir -p top +$(BSDTAR_BIN) -czf $@ -P -s ',^top,/top,' top/ +""", + toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"], +) + flatten( name = "flatten_dedup_normalize", deduplicate = True, tars = [ - ":dotslash_tar", ":nodotslash_tar", + ":dotslash_tar", + ":absolute_tar_gen", ], ) @@ -155,7 +166,28 @@ assert_tar_listing( name = "test_flatten_dedup_normalize", actual = "flatten_dedup_normalize", expected = """\ -etc/ -etc/sub/ +./top/ +./top/sub/ +/top/ +""", +) + +flatten( + name = "flatten_dedup_normalize_reversed", + deduplicate = True, + tars = [ + ":absolute_tar_gen", + ":dotslash_tar", + ":nodotslash_tar", + ], +) + +assert_tar_listing( + name = "test_flatten_dedup_normalize_reversed", + actual = "flatten_dedup_normalize_reversed", + expected = """\ +/top/ +./top/ +./top/sub/ """, )