Always allow duplicates with identical CRC32 and size

Don't warn on duplicate files in merge_zips if they have identical
CRC32 and size values.

Test: m checkbuild
Test: merge_zips_test.go
Change-Id: I61336ca4d4d3b7402c24a7abd337bd350fe10930
This commit is contained in:
Colin Cross 2018-10-17 15:05:56 -07:00
parent 153c2f8ba3
commit dc1e829b59
2 changed files with 29 additions and 10 deletions

View File

@ -173,6 +173,10 @@ func (ze zipEntry) CRC32() uint32 {
return ze.content.FileHeader.CRC32 return ze.content.FileHeader.CRC32
} }
func (ze zipEntry) Size() uint64 {
return ze.content.FileHeader.UncompressedSize64
}
func (ze zipEntry) WriteToZip(dest string, zw *zip.Writer) error { func (ze zipEntry) WriteToZip(dest string, zw *zip.Writer) error {
return zw.CopyFrom(ze.content, dest) return zw.CopyFrom(ze.content, dest)
} }
@ -195,6 +199,10 @@ func (be bufferEntry) CRC32() uint32 {
return crc32.ChecksumIEEE(be.content) return crc32.ChecksumIEEE(be.content)
} }
func (be bufferEntry) Size() uint64 {
return uint64(len(be.content))
}
func (be bufferEntry) WriteToZip(dest string, zw *zip.Writer) error { func (be bufferEntry) WriteToZip(dest string, zw *zip.Writer) error {
w, err := zw.CreateHeader(be.fh) w, err := zw.CreateHeader(be.fh)
if err != nil { if err != nil {
@ -215,6 +223,7 @@ type zipSource interface {
String() string String() string
IsDir() bool IsDir() bool
CRC32() uint32 CRC32() uint32
Size() uint64
WriteToZip(dest string, zw *zip.Writer) error WriteToZip(dest string, zw *zip.Writer) error
} }
@ -369,25 +378,27 @@ func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest, entrypoin
return fmt.Errorf("Directory/file mismatch at %v from %v and %v\n", return fmt.Errorf("Directory/file mismatch at %v from %v and %v\n",
dest, existingSource, source) dest, existingSource, source)
} }
if ignoreDuplicates { if ignoreDuplicates {
continue continue
} }
if emulateJar && if emulateJar &&
file.Name == jar.ManifestFile || file.Name == jar.ModuleInfoClass { file.Name == jar.ManifestFile || file.Name == jar.ModuleInfoClass {
// Skip manifest and module info files that are not from the first input file // Skip manifest and module info files that are not from the first input file
continue continue
} }
if !source.IsDir() {
if emulateJar { if source.IsDir() {
if existingSource.CRC32() != source.CRC32() { continue
fmt.Fprintf(os.Stdout, "WARNING: Duplicate path %v found in %v and %v\n",
dest, existingSource, source)
}
} else {
return fmt.Errorf("Duplicate path %v found in %v and %v\n",
dest, existingSource, source)
}
} }
if existingSource.CRC32() == source.CRC32() && existingSource.Size() == source.Size() {
continue
}
return fmt.Errorf("Duplicate path %v found in %v and %v\n",
dest, existingSource, source)
} }
} }
} }

View File

@ -87,6 +87,14 @@ func TestMergeZips(t *testing.T) {
ignoreDuplicates: true, ignoreDuplicates: true,
}, },
{
name: "duplicates identical",
in: [][]testZipEntry{
{a},
{a},
},
out: []testZipEntry{a},
},
{ {
name: "sort", name: "sort",
in: [][]testZipEntry{ in: [][]testZipEntry{