Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix name collision with tar2files command #83

Merged
merged 9 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ bazeldnf(
rpmtree(
name = "something",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-libs-11.0.0-1.fc42.x86_64.rpm//rpm",
"@libvirt-devel-11.0.0-1.fc42.x86_64.rpm//rpm",
],
)

Expand All @@ -67,7 +67,7 @@ tar2files(
files = {
"/usr/lib64": [
"libvirt.so.0",
"libvirt.so.0.6001.0",
"libvirt.so.0.11000.0",
],
},
tar = ":something",
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ processed in any way. They can be added to your `WORKSPACE` file like this:
load("@bazeldnf//bazeldnf:deps.bzl", "rpm")

rpm(
name = "libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
sha256 = "2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
name = "libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
sha256 = "aac272a2ace134b5ef60a41e6624deb24331e79c76699ef6cef0dca22d94ac7e",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
],
)
```
Expand All @@ -34,8 +33,8 @@ load("@bazeldnf//bazeldnf:defs.bzl", "rpmtree")
rpmtree(
name = "rpmarchive",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-libs-11.0.0-1.fc42.x86_64.rpm//rpm",
"@libvirt-devel-11.0.0-1.fc42.x86_64.rpm//rpm",
],
)
```
Expand All @@ -61,8 +60,8 @@ privileged ports:
rpmtree(
name = "rpmarchive",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-libs-11.0.0-1.fc42.x86_64.rpm//rpm",
"@libvirt-devel-11.0.0-1.fc42.x86_64.rpm//rpm",
],
symlinks = {
"/var/run": "../run",
Expand Down
6 changes: 3 additions & 3 deletions e2e/bazel-bzlmod-lock-file/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ rpmtree(
name = "something",
rpms = [
"@bazeldnf-rpms//libvirt-libs",
"@bazeldnf-rpms//libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"@rpms-with-no-name-attribute//libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"@bazeldnf-rpms//libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
"@rpms-with-no-name-attribute//libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
],
)

Expand All @@ -19,7 +19,7 @@ tar2files(
files = {
"/usr/lib64": [
"libvirt.so.0",
"libvirt.so.0.6001.0",
"libvirt.so.0.11000.0",
],
},
tar = ":something",
Expand Down
6 changes: 2 additions & 4 deletions e2e/bazel-bzlmod-lock-file/rpms-with-no-name-attribute.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"rpms": [
{
"sha256": "3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
"sha256": "aac272a2ace134b5ef60a41e6624deb24331e79c76699ef6cef0dca22d94ac7e",
"urls": [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845"
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-libs-11.0.0-1.fc42.x86_64.rpm"
]

}
]
}
11 changes: 4 additions & 7 deletions e2e/bazel-bzlmod-lock-file/rpms.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
"rpms": [
{
"name": "libvirt-libs",
"sha256": "3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
"sha256": "aac272a2ace134b5ef60a41e6624deb24331e79c76699ef6cef0dca22d94ac7e",
"urls": [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845"
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-libs-11.0.0-1.fc42.x86_64.rpm"
]

},
{
"sha256": "2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
"sha256": "dba37bbe57903afe49b5666f1781eb50001baa81af4584b355db0b6a2afad9fa",
"urls": [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6"
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-devel-11.0.0-1.fc42.x86_64.rpm"
]
}
]
Expand Down
6 changes: 3 additions & 3 deletions e2e/bazel-bzlmod-non-legacy-mode/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bazeldnf(
rpmtree(
name = "something",
rpms = [
"@bazeldnf_rpms//libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"@bazeldnf_rpms//libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"@bazeldnf_rpms//libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
"@bazeldnf_rpms//libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
],
)

Expand All @@ -18,7 +18,7 @@ tar2files(
files = {
"/usr/lib64": [
"libvirt.so.0",
"libvirt.so.0.6001.0",
"libvirt.so.0.11000.0",
],
},
tar = ":something",
Expand Down
14 changes: 6 additions & 8 deletions e2e/bazel-bzlmod-non-legacy-mode/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ bazel_dep(name = "rules_pkg", version = "1.0.1")
bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
bazeldnf.config(legacy_mode = False)
bazeldnf.rpm(
name = "libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
sha256 = "3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
name = "libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
sha256 = "aac272a2ace134b5ef60a41e6624deb24331e79c76699ef6cef0dca22d94ac7e",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
],
)
bazeldnf.rpm(
name = "libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
sha256 = "2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
name = "libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
sha256 = "dba37bbe57903afe49b5666f1781eb50001baa81af4584b355db0b6a2afad9fa",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
],
)
use_repo(bazeldnf, "bazeldnf_rpms")
6 changes: 3 additions & 3 deletions e2e/bazel-bzlmod-toolchain-from-source/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bazeldnf(
rpmtree(
name = "something",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-libs-11.0.0-1.fc42.x86_64.rpm//rpm",
"@libvirt-devel-11.0.0-1.fc42.x86_64.rpm//rpm",
],
)

Expand All @@ -18,7 +18,7 @@ tar2files(
files = {
"/usr/lib64": [
"libvirt.so.0",
"libvirt.so.0.6001.0",
"libvirt.so.0.11000.0",
],
},
tar = ":something",
Expand Down
18 changes: 8 additions & 10 deletions e2e/bazel-bzlmod-toolchain-from-source/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,21 @@ use_repo(
bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
bazeldnf.toolchain(disable = True)
bazeldnf.rpm(
name = "libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
sha256 = "3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
name = "libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
sha256 = "aac272a2ace134b5ef60a41e6624deb24331e79c76699ef6cef0dca22d94ac7e",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
],
)
bazeldnf.rpm(
name = "libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
sha256 = "2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
name = "libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
sha256 = "dba37bbe57903afe49b5666f1781eb50001baa81af4584b355db0b6a2afad9fa",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
],
)
use_repo(
bazeldnf,
"libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
"libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
)
6 changes: 3 additions & 3 deletions e2e/bazel-bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bazeldnf(
rpmtree(
name = "something",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-libs-11.0.0-1.fc42.x86_64.rpm//rpm",
"@libvirt-devel-11.0.0-1.fc42.x86_64.rpm//rpm",
],
)

Expand All @@ -18,7 +18,7 @@ tar2files(
files = {
"/usr/lib64": [
"libvirt.so.0",
"libvirt.so.0.6001.0",
"libvirt.so.0.11000.0",
],
},
tar = ":something",
Expand Down
18 changes: 8 additions & 10 deletions e2e/bazel-bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ bazel_dep(name = "rules_pkg", version = "1.0.1")

bazeldnf = use_extension("@bazeldnf//bazeldnf:extensions.bzl", "bazeldnf")
bazeldnf.rpm(
name = "libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
sha256 = "3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
name = "libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
sha256 = "aac272a2ace134b5ef60a41e6624deb24331e79c76699ef6cef0dca22d94ac7e",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/3a0a3d88c6cb90008fbe49fe05e7025056fb9fa3a887c4a78f79e63f8745c845",
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
],
)
bazeldnf.rpm(
name = "libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
sha256 = "2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
name = "libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
sha256 = "dba37bbe57903afe49b5666f1781eb50001baa81af4584b355db0b6a2afad9fa",
urls = [
"https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/32/Everything/x86_64/os/Packages/l/libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"https://storage.googleapis.com/builddeps/2ebb715341b57a74759aff415e0ff53df528c49abaa7ba5b794b4047461fa8d6",
"https://kojipkgs.fedoraproject.org//packages/libvirt/11.0.0/1.fc42/x86_64/libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
],
)
use_repo(
bazeldnf,
"libvirt-devel-6.1.0-2.fc32.x86_64.rpm",
"libvirt-libs-6.1.0-2.fc32.x86_64.rpm",
"libvirt-devel-11.0.0-1.fc42.x86_64.rpm",
"libvirt-libs-11.0.0-1.fc42.x86_64.rpm",
)
6 changes: 3 additions & 3 deletions e2e/bazel-workspace/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bazeldnf(
rpmtree(
name = "something",
rpms = [
"@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-devel-6.1.0-2.fc32.x86_64.rpm//rpm",
"@libvirt-libs-11.0.0-1.fc42.x86_64.rpm//rpm",
"@libvirt-devel-11.0.0-1.fc42.x86_64.rpm//rpm",
],
)

Expand All @@ -18,7 +18,7 @@ tar2files(
files = {
"/usr/lib64": [
"libvirt.so.0",
"libvirt.so.0.6001.0",
"libvirt.so.0.11000.0",
],
},
tar = ":something",
Expand Down
5 changes: 4 additions & 1 deletion pkg/rpm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ go_test(
"rpm_test.go",
"tar_test.go",
],
data = ["@libvirt-libs-6.1.0-2.fc32.x86_64.rpm//rpm"],
data = [
"@abseil-cpp-devel-20240722.1-1.fc42.x86_64.rpm//rpm",
"@libvirt-libs-11.0.0-1.fc42.x86_64.rpm//rpm",
],
embed = [":rpm"],
deps = [
"//pkg/api",
Expand Down
22 changes: 14 additions & 8 deletions pkg/rpm/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ func PrefixFilter(prefix string, reader *tar.Reader, files []string) error {

fileMap := map[string]string{}
for _, file := range files {
fileMap[filepath.Base(file)] = file
prefixIdx := strings.Index(file, prefix)
if prefixIdx == -1 {
return fmt.Errorf("prefix %s is not found in %s", prefix, file)
}
fileMap[file[prefixIdx:]] = file
}

for {
entry, err := reader.Next()
if err == io.EOF {
Expand All @@ -82,13 +87,14 @@ func PrefixFilter(prefix string, reader *tar.Reader, files []string) error {
} else {
continue
}
basename := filepath.Base(name)
if _, exists := fileMap[basename]; !exists {

if _, exists := fileMap[name]; !exists {
continue
}

if entry.Typeflag == tar.TypeReg {
err := func() error {
writer, err := os.Create(fileMap[basename])
writer, err := os.Create(fileMap[name])
if err != nil {
return err
}
Expand All @@ -101,16 +107,16 @@ func PrefixFilter(prefix string, reader *tar.Reader, files []string) error {
if err != nil {
return err
}
delete(fileMap, basename)
delete(fileMap, name)
} else if entry.Typeflag == tar.TypeSymlink {
linkname := strings.TrimPrefix(entry.Linkname, ".")
err = os.Symlink(linkname, fileMap[basename])
err = os.Symlink(linkname, fileMap[name])
if err != nil {
return err
}
delete(fileMap, basename)
delete(fileMap, name)
} else {
return fmt.Errorf("can't extract %s, only symlinks and files can be specified", fileMap[basename])
return fmt.Errorf("can't extract %s, only symlinks and files can be specified", fileMap[name])
}
}

Expand Down
Loading