@@ -19,6 +19,7 @@ package differs
19
19
import (
20
20
pkgutil "github.com/GoogleContainerTools/container-diff/pkg/util"
21
21
"github.com/GoogleContainerTools/container-diff/util"
22
+ "github.com/sirupsen/logrus"
22
23
)
23
24
24
25
type FileAnalyzer struct {
@@ -30,7 +31,7 @@ func (a FileAnalyzer) Name() string {
30
31
31
32
// FileDiff diffs two packages and compares their contents
32
33
func (a FileAnalyzer ) Diff (image1 , image2 pkgutil.Image ) (util.Result , error ) {
33
- diff , err := diffImageFiles (image1 , image2 )
34
+ diff , err := diffImageFiles (image1 . FSPath , image2 . FSPath )
34
35
return & util.DirDiffResult {
35
36
Image1 : image1 .Source ,
36
37
Image2 : image2 .Source ,
@@ -53,10 +54,7 @@ func (a FileAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
53
54
return & result , err
54
55
}
55
56
56
- func diffImageFiles (image1 , image2 pkgutil.Image ) (util.DirDiff , error ) {
57
- img1 := image1 .FSPath
58
- img2 := image2 .FSPath
59
-
57
+ func diffImageFiles (img1 , img2 string ) (util.DirDiff , error ) {
60
58
var diff util.DirDiff
61
59
62
60
img1Dir , err := pkgutil .GetDirectory (img1 , true )
@@ -71,3 +69,63 @@ func diffImageFiles(image1, image2 pkgutil.Image) (util.DirDiff, error) {
71
69
diff , _ = util .DiffDirectory (img1Dir , img2Dir )
72
70
return diff , nil
73
71
}
72
+
73
+ type FileLayerAnalyzer struct {
74
+ }
75
+
76
+ func (a FileLayerAnalyzer ) Name () string {
77
+ return "FileLayerAnalyzer"
78
+ }
79
+
80
+ // FileDiff diffs two packages and compares their contents
81
+ func (a FileLayerAnalyzer ) Diff (image1 , image2 pkgutil.Image ) (util.Result , error ) {
82
+ var dirDiffs []util.DirDiff
83
+
84
+ // Go through each layer of the first image...
85
+ for index , layer := range image1 .Layers {
86
+ if index >= len (image2 .Layers ) {
87
+ continue
88
+ }
89
+ // ...else, diff as usual
90
+ layer2 := image2 .Layers [index ]
91
+ diff , err := diffImageFiles (layer .FSPath , layer2 .FSPath )
92
+ if err != nil {
93
+ return & util.MultipleDirDiffResult {}, err
94
+ }
95
+ dirDiffs = append (dirDiffs , diff )
96
+ }
97
+
98
+ // check if there are any additional layers in either image
99
+ if len (image1 .Layers ) != len (image2 .Layers ) {
100
+ if len (image1 .Layers ) > len (image2 .Layers ) {
101
+ logrus .Infof ("%s has additional layers, please use container-diff analyze to view the files in these layers" , image1 .Source )
102
+ } else {
103
+ logrus .Infof ("%s has additional layers, please use container-diff analyze to view the files in these layers" , image2 .Source )
104
+ }
105
+ }
106
+ return & util.MultipleDirDiffResult {
107
+ Image1 : image1 .Source ,
108
+ Image2 : image2 .Source ,
109
+ DiffType : "FileLayer" ,
110
+ Diff : util.MultipleDirDiff {
111
+ DirDiffs : dirDiffs ,
112
+ },
113
+ }, nil
114
+ }
115
+
116
+ func (a FileLayerAnalyzer ) Analyze (image pkgutil.Image ) (util.Result , error ) {
117
+ var directoryEntries [][]pkgutil.DirectoryEntry
118
+ for _ , layer := range image .Layers {
119
+ layerDir , err := pkgutil .GetDirectory (layer .FSPath , true )
120
+ if err != nil {
121
+ return util.FileLayerAnalyzeResult {}, err
122
+ }
123
+ directoryEntries = append (directoryEntries , pkgutil .GetDirectoryEntries (layerDir ))
124
+ }
125
+
126
+ return & util.FileLayerAnalyzeResult {
127
+ Image : image .Source ,
128
+ AnalyzeType : "FileLayer" ,
129
+ Analysis : directoryEntries ,
130
+ }, nil
131
+ }
0 commit comments