Skip to content

Commit

Permalink
Fix name collision with tar2files command
Browse files Browse the repository at this point in the history
If two files have the same name but are found in different
subdirectories then tar2files will fail.  An example of this is found
in abseil-cpp where there are multiple headers with the same name, but
sometimes in different subdirectories of `/usr/include`.

This change tweaks our tarfile handling so as to key our lookup map
based on the path minus anything before the expected prefix (this will
remove the `bazel-out/...` bits from the paths generated by the
tar2files rule.  This provides enough context to avoid the name
collisions but is still able to deal with the correct path lookup.
  • Loading branch information
kellyma2 committed Dec 3, 2024
1 parent 7fd6f51 commit c02a518
Showing 1 changed file with 14 additions and 8 deletions.
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

0 comments on commit c02a518

Please sign in to comment.