Allow some duplicates in merged jars

Only take the first MANIFEST.MF or module-info.class file.

Test: m -j checkbuild
Change-Id: Ifbf9fe272437ef2c2bd51ab4849ac8d7ef37b6fc
This commit is contained in:
Colin Cross
2017-09-06 12:52:37 -07:00
parent 32f676a7b4
commit 34540315a0
3 changed files with 30 additions and 18 deletions

View File

@@ -28,7 +28,7 @@ import (
var ( var (
sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)") sortEntries = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)")
sortJava = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)") emulateJar = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)")
) )
func main() { func main() {
@@ -76,7 +76,7 @@ func main() {
} }
// do merge // do merge
if err := mergeZips(readers, writer, *sortEntries, *sortJava); err != nil { if err := mergeZips(readers, writer, *sortEntries, *emulateJar); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
@@ -109,7 +109,7 @@ type fileMapping struct {
dest string dest string
} }
func mergeZips(readers []namedZipReader, writer *zip.Writer, sortEntries bool, sortJava bool) error { func mergeZips(readers []namedZipReader, writer *zip.Writer, sortEntries bool, emulateJar bool) error {
mappingsByDest := make(map[string]fileMapping, 0) mappingsByDest := make(map[string]fileMapping, 0)
orderedMappings := []fileMapping{} orderedMappings := []fileMapping{}
@@ -128,24 +128,33 @@ func mergeZips(readers []namedZipReader, writer *zip.Writer, sortEntries bool, s
source := zipEntry{path: zipEntryPath{zipName: namedReader.path, entryName: file.Name}, content: file} source := zipEntry{path: zipEntryPath{zipName: namedReader.path, entryName: file.Name}, content: file}
newMapping := fileMapping{source: source, dest: dest} newMapping := fileMapping{source: source, dest: dest}
// handle duplicates
if exists { if exists {
// handle duplicates
wasDir := existingMapping.source.content.FileHeader.FileInfo().IsDir() wasDir := existingMapping.source.content.FileHeader.FileInfo().IsDir()
isDir := newMapping.source.content.FileHeader.FileInfo().IsDir() isDir := newMapping.source.content.FileHeader.FileInfo().IsDir()
if !wasDir || !isDir { if wasDir != isDir {
return fmt.Errorf("Directory/file mismatch at %v from %v and %v\n",
dest, existingMapping.source.path, newMapping.source.path)
}
if emulateJar &&
file.Name == jar.ManifestFile || file.Name == jar.ModuleInfoClass {
// Skip manifest and module info files that are not from the first input file
continue
}
if !isDir {
return fmt.Errorf("Duplicate path %v found in %v and %v\n", return fmt.Errorf("Duplicate path %v found in %v and %v\n",
dest, existingMapping.source.path, newMapping.source.path) dest, existingMapping.source.path, newMapping.source.path)
} }
} else {
// save entry
mappingsByDest[mapKey] = newMapping
orderedMappings = append(orderedMappings, newMapping)
} }
// save entry
mappingsByDest[mapKey] = newMapping
orderedMappings = append(orderedMappings, newMapping)
} }
} }
if sortJava { if emulateJar {
jarSort(orderedMappings) jarSort(orderedMappings)
} else if sortEntries { } else if sortEntries {
alphanumericSort(orderedMappings) alphanumericSort(orderedMappings)

View File

@@ -62,9 +62,6 @@ type byteReaderCloser struct {
io.Closer io.Closer
} }
// the file path in the zip at which a Java manifest file gets written
const manifestDest = "META-INF/MANIFEST.MF"
type fileArg struct { type fileArg struct {
pathPrefixInZip, sourcePrefixToStrip string pathPrefixInZip, sourcePrefixToStrip string
sourceFiles []string sourceFiles []string
@@ -360,7 +357,7 @@ func (z *zipWriter) write(out string, pathMappings []pathMapping, manifest strin
if !*emulateJar { if !*emulateJar {
return errors.New("must specify --jar when specifying a manifest via -m") return errors.New("must specify --jar when specifying a manifest via -m")
} }
pathMappings = append(pathMappings, pathMapping{manifestDest, manifest, zip.Deflate}) pathMappings = append(pathMappings, pathMapping{jar.ManifestFile, manifest, zip.Deflate})
} }
if *emulateJar { if *emulateJar {
@@ -372,7 +369,7 @@ func (z *zipWriter) write(out string, pathMappings []pathMapping, manifest strin
defer close(z.writeOps) defer close(z.writeOps)
for _, ele := range pathMappings { for _, ele := range pathMappings {
if *emulateJar && ele.dest == manifestDest { if *emulateJar && ele.dest == jar.ManifestFile {
err = z.addManifest(ele.dest, ele.src, ele.zipMethod) err = z.addManifest(ele.dest, ele.src, ele.zipMethod)
} else { } else {
err = z.addFile(ele.dest, ele.src, ele.zipMethod) err = z.addFile(ele.dest, ele.src, ele.zipMethod)

View File

@@ -19,6 +19,12 @@ import (
"strings" "strings"
) )
const (
MetaDir = "META-INF/"
ManifestFile = MetaDir + "MANIFEST.MF"
ModuleInfoClass = "module-info.class"
)
// EntryNamesLess tells whether <filepathA> should precede <filepathB> in // EntryNamesLess tells whether <filepathA> should precede <filepathB> in
// the order of files with a .jar // the order of files with a .jar
func EntryNamesLess(filepathA string, filepathB string) (less bool) { func EntryNamesLess(filepathA string, filepathB string) (less bool) {
@@ -39,9 +45,9 @@ func patternMatch(pattern, name string) bool {
} }
var jarOrder = []string{ var jarOrder = []string{
"META-INF/", MetaDir,
"META-INF/MANIFEST.MF", ManifestFile,
"META-INF/*", MetaDir + "*",
"*", "*",
} }