1// Copyright 2019 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package main 16 17import ( 18 "bytes" 19 "fmt" 20) 21 22// compareTargetFiles takes two ZipArtifacts and compares the files they contain by examining 23// the path, size, and CRC of each file. 24func compareTargetFiles(priZip, refZip ZipArtifact, artifact string, whitelists []whitelist, filters []string) (zipDiff, error) { 25 priZipFiles, err := priZip.Files() 26 if err != nil { 27 return zipDiff{}, fmt.Errorf("error fetching target file lists from primary zip %v", err) 28 } 29 30 refZipFiles, err := refZip.Files() 31 if err != nil { 32 return zipDiff{}, fmt.Errorf("error fetching target file lists from reference zip %v", err) 33 } 34 35 priZipFiles, err = filterTargetZipFiles(priZipFiles, artifact, filters) 36 if err != nil { 37 return zipDiff{}, err 38 } 39 40 refZipFiles, err = filterTargetZipFiles(refZipFiles, artifact, filters) 41 if err != nil { 42 return zipDiff{}, err 43 } 44 45 // Compare the file lists from both builds 46 diff := diffTargetFilesLists(refZipFiles, priZipFiles) 47 48 return applyWhitelists(diff, whitelists) 49} 50 51// zipDiff contains the list of files that differ between two zip files. 52type zipDiff struct { 53 modified [][2]*ZipArtifactFile 54 onlyInA, onlyInB []*ZipArtifactFile 55} 56 57// String pretty-prints the list of files that differ between two zip files. 58func (d *zipDiff) String() string { 59 buf := &bytes.Buffer{} 60 61 must := func(n int, err error) { 62 if err != nil { 63 panic(err) 64 } 65 } 66 67 var sizeChange int64 68 69 if len(d.modified) > 0 { 70 must(fmt.Fprintln(buf, "files modified:")) 71 for _, f := range d.modified { 72 must(fmt.Fprintf(buf, " %v (%v bytes -> %v bytes)\n", f[0].Name, f[0].UncompressedSize64, f[1].UncompressedSize64)) 73 sizeChange += int64(f[1].UncompressedSize64) - int64(f[0].UncompressedSize64) 74 } 75 } 76 77 if len(d.onlyInA) > 0 { 78 must(fmt.Fprintln(buf, "files removed:")) 79 for _, f := range d.onlyInA { 80 must(fmt.Fprintf(buf, " - %v (%v bytes)\n", f.Name, f.UncompressedSize64)) 81 sizeChange -= int64(f.UncompressedSize64) 82 } 83 } 84 85 if len(d.onlyInB) > 0 { 86 must(fmt.Fprintln(buf, "files added:")) 87 for _, f := range d.onlyInB { 88 must(fmt.Fprintf(buf, " + %v (%v bytes)\n", f.Name, f.UncompressedSize64)) 89 sizeChange += int64(f.UncompressedSize64) 90 } 91 } 92 93 if len(d.modified) > 0 || len(d.onlyInA) > 0 || len(d.onlyInB) > 0 { 94 must(fmt.Fprintf(buf, "total size change: %v bytes\n", sizeChange)) 95 } 96 97 return buf.String() 98} 99 100func diffTargetFilesLists(a, b []*ZipArtifactFile) zipDiff { 101 i := 0 102 j := 0 103 104 diff := zipDiff{} 105 106 for i < len(a) && j < len(b) { 107 if a[i].Name == b[j].Name { 108 if a[i].UncompressedSize64 != b[j].UncompressedSize64 || a[i].CRC32 != b[j].CRC32 { 109 diff.modified = append(diff.modified, [2]*ZipArtifactFile{a[i], b[j]}) 110 } 111 i++ 112 j++ 113 } else if a[i].Name < b[j].Name { 114 // a[i] is not present in b 115 diff.onlyInA = append(diff.onlyInA, a[i]) 116 i++ 117 } else { 118 // b[j] is not present in a 119 diff.onlyInB = append(diff.onlyInB, b[j]) 120 j++ 121 } 122 } 123 for i < len(a) { 124 diff.onlyInA = append(diff.onlyInA, a[i]) 125 i++ 126 } 127 for j < len(b) { 128 diff.onlyInB = append(diff.onlyInB, b[j]) 129 j++ 130 } 131 132 return diff 133} 134