compare.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. package main
  15. import (
  16. "bytes"
  17. "fmt"
  18. )
  19. // compareTargetFiles takes two ZipArtifacts and compares the files they contain by examining
  20. // the path, size, and CRC of each file.
  21. func compareTargetFiles(priZip, refZip ZipArtifact, artifact string, allowLists []allowList, filters []string) (zipDiff, error) {
  22. priZipFiles, err := priZip.Files()
  23. if err != nil {
  24. return zipDiff{}, fmt.Errorf("error fetching target file lists from primary zip %v", err)
  25. }
  26. refZipFiles, err := refZip.Files()
  27. if err != nil {
  28. return zipDiff{}, fmt.Errorf("error fetching target file lists from reference zip %v", err)
  29. }
  30. priZipFiles, err = filterTargetZipFiles(priZipFiles, artifact, filters)
  31. if err != nil {
  32. return zipDiff{}, err
  33. }
  34. refZipFiles, err = filterTargetZipFiles(refZipFiles, artifact, filters)
  35. if err != nil {
  36. return zipDiff{}, err
  37. }
  38. // Compare the file lists from both builds
  39. diff := diffTargetFilesLists(refZipFiles, priZipFiles)
  40. return applyAllowLists(diff, allowLists)
  41. }
  42. // zipDiff contains the list of files that differ between two zip files.
  43. type zipDiff struct {
  44. modified [][2]*ZipArtifactFile
  45. onlyInA, onlyInB []*ZipArtifactFile
  46. }
  47. // String pretty-prints the list of files that differ between two zip files.
  48. func (d *zipDiff) String() string {
  49. buf := &bytes.Buffer{}
  50. must := func(n int, err error) {
  51. if err != nil {
  52. panic(err)
  53. }
  54. }
  55. var sizeChange int64
  56. if len(d.modified) > 0 {
  57. must(fmt.Fprintln(buf, "files modified:"))
  58. for _, f := range d.modified {
  59. must(fmt.Fprintf(buf, " %v (%v bytes -> %v bytes)\n", f[0].Name, f[0].UncompressedSize64, f[1].UncompressedSize64))
  60. sizeChange += int64(f[1].UncompressedSize64) - int64(f[0].UncompressedSize64)
  61. }
  62. }
  63. if len(d.onlyInA) > 0 {
  64. must(fmt.Fprintln(buf, "files removed:"))
  65. for _, f := range d.onlyInA {
  66. must(fmt.Fprintf(buf, " - %v (%v bytes)\n", f.Name, f.UncompressedSize64))
  67. sizeChange -= int64(f.UncompressedSize64)
  68. }
  69. }
  70. if len(d.onlyInB) > 0 {
  71. must(fmt.Fprintln(buf, "files added:"))
  72. for _, f := range d.onlyInB {
  73. must(fmt.Fprintf(buf, " + %v (%v bytes)\n", f.Name, f.UncompressedSize64))
  74. sizeChange += int64(f.UncompressedSize64)
  75. }
  76. }
  77. if len(d.modified) > 0 || len(d.onlyInA) > 0 || len(d.onlyInB) > 0 {
  78. must(fmt.Fprintf(buf, "total size change: %v bytes\n", sizeChange))
  79. }
  80. return buf.String()
  81. }
  82. func diffTargetFilesLists(a, b []*ZipArtifactFile) zipDiff {
  83. i := 0
  84. j := 0
  85. diff := zipDiff{}
  86. for i < len(a) && j < len(b) {
  87. if a[i].Name == b[j].Name {
  88. if a[i].UncompressedSize64 != b[j].UncompressedSize64 || a[i].CRC32 != b[j].CRC32 {
  89. diff.modified = append(diff.modified, [2]*ZipArtifactFile{a[i], b[j]})
  90. }
  91. i++
  92. j++
  93. } else if a[i].Name < b[j].Name {
  94. // a[i] is not present in b
  95. diff.onlyInA = append(diff.onlyInA, a[i])
  96. i++
  97. } else {
  98. // b[j] is not present in a
  99. diff.onlyInB = append(diff.onlyInB, b[j])
  100. j++
  101. }
  102. }
  103. for i < len(a) {
  104. diff.onlyInA = append(diff.onlyInA, a[i])
  105. i++
  106. }
  107. for j < len(b) {
  108. diff.onlyInB = append(diff.onlyInB, b[j])
  109. j++
  110. }
  111. return diff
  112. }