12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- // Package trace provides functionality to trace the origin of an inventory in a container image.
15
+ // Package trace provides functionality to trace the origin of a package in a container image.
16
16
package trace
17
17
18
18
import (
@@ -36,8 +36,8 @@ type locationAndIndex struct {
36
36
index int
37
37
}
38
38
39
- // PopulateLayerDetails populates the LayerDetails field of the inventory with the origin details
40
- // obtained by tracing the inventory in the image.
39
+ // PopulateLayerDetails populates the LayerDetails field of the package with the origin details
40
+ // obtained by tracing the package in the image.
41
41
//
42
42
// It does this by looking at each consecutive pair (n, n+1) of chain layers in reverse order and
43
43
// checking if a package is present in layer n+1, but not layer n. For example, consider the chain
@@ -54,10 +54,10 @@ type locationAndIndex struct {
54
54
//
55
55
// Note that a precondition of this algorithm is that the chain layers are ordered by order of
56
56
// creation.
57
- func PopulateLayerDetails (ctx context.Context , inventory []* extractor.Inventory , chainLayers []scalibrImage.ChainLayer , config * filesystem.Config ) {
57
+ func PopulateLayerDetails (ctx context.Context , pkgs []* extractor.Package , chainLayers []scalibrImage.ChainLayer , config * filesystem.Config ) {
58
58
chainLayerDetailsList := []* extractor.LayerDetails {}
59
59
60
- // Create list of layer details struct to be referenced by inventory .
60
+ // Create list of layer details struct to be referenced by package .
61
61
for i , chainLayer := range chainLayers {
62
62
var diffID string
63
63
if chainLayer .Layer ().IsEmpty () {
@@ -85,112 +85,112 @@ func PopulateLayerDetails(ctx context.Context, inventory []*extractor.Inventory,
85
85
}
86
86
}
87
87
88
- // locationIndexToInventory is used as an inventory cache to avoid re-extracting the same
89
- // inventory from a file multiple times.
90
- locationIndexToInventory := map [locationAndIndex ][]* extractor.Inventory {}
88
+ // locationIndexToPackage is used as a package cache to avoid re-extracting the same
89
+ // package from a file multiple times.
90
+ locationIndexToPackage := map [locationAndIndex ][]* extractor.Package {}
91
91
lastLayerIndex := len (chainLayers ) - 1
92
92
93
- for _ , inv := range inventory {
93
+ for _ , pkg := range pkgs {
94
94
layerDetails := chainLayerDetailsList [lastLayerIndex ]
95
- invExtractor , isFilesystemExtractor := inv .Extractor .(filesystem.Extractor )
95
+ pkgExtractor , isFilesystemExtractor := pkg .Extractor .(filesystem.Extractor )
96
96
97
- // Only filesystem extractors are supported for layer scanning. Also, if the inventory has no
97
+ // Only filesystem extractors are supported for layer scanning. Also, if the package has no
98
98
// locations, it cannot be traced.
99
- isInventoryTraceable := isFilesystemExtractor && len (inv .Locations ) > 0
100
- if ! isInventoryTraceable {
99
+ isPackageTraceable := isFilesystemExtractor && len (pkg .Locations ) > 0
100
+ if ! isPackageTraceable {
101
101
continue
102
102
}
103
103
104
104
var foundOrigin bool
105
- fileLocation := inv .Locations [0 ]
105
+ fileLocation := pkg .Locations [0 ]
106
106
107
- // Go backwards through the chain layers and find the first layer where the inventory is not
108
- // present. Such layer is the layer in which the inventory was introduced. If the inventory is
107
+ // Go backwards through the chain layers and find the first layer where the package is not
108
+ // present. Such layer is the layer in which the package was introduced. If the package is
109
109
// present in all layers, then it means it was introduced in the first layer.
110
110
for i := len (chainLayers ) - 2 ; i >= 0 ; i -- {
111
111
oldChainLayer := chainLayers [i ]
112
112
113
- invLocationAndIndex := locationAndIndex {
113
+ pkgLocationAndIndex := locationAndIndex {
114
114
location : fileLocation ,
115
115
index : i ,
116
116
}
117
117
118
- var oldInventory []* extractor.Inventory
119
- if cachedInventory , ok := locationIndexToInventory [ invLocationAndIndex ]; ok {
120
- oldInventory = cachedInventory
118
+ var oldPackages []* extractor.Package
119
+ if cachedPackage , ok := locationIndexToPackage [ pkgLocationAndIndex ]; ok {
120
+ oldPackages = cachedPackage
121
121
} else if _ , err := oldChainLayer .FS ().Stat (fileLocation ); errors .Is (err , fs .ErrNotExist ) {
122
122
// Check if file still exist in this layer, if not skip extraction.
123
123
// This is both an optimization, and avoids polluting the log output with false file not found errors.
124
- oldInventory = []* extractor.Inventory {}
125
- } else if filesExistInLayer (oldChainLayer , inv .Locations ) {
124
+ oldPackages = []* extractor.Package {}
125
+ } else if filesExistInLayer (oldChainLayer , pkg .Locations ) {
126
126
// Update the extractor config to use the files from the current layer.
127
127
// We only take extract the first location because other locations are derived from the initial
128
128
// extraction location. If other locations can no longer be determined from the first location
129
129
// they should not be included here, and the trace for those packages stops here.
130
- updateExtractorConfig ([]string {fileLocation }, invExtractor , oldChainLayer .FS ())
130
+ updateExtractorConfig ([]string {fileLocation }, pkgExtractor , oldChainLayer .FS ())
131
131
132
132
var err error
133
133
// Runs SCALIBR extraction on the file of interest in oldChainLayer.
134
- oldInventory , _ , err = filesystem .Run (ctx , config )
134
+ oldPackages , _ , err = filesystem .Run (ctx , config )
135
135
if err != nil {
136
136
break
137
137
}
138
138
} else {
139
- // If none of the files from the inventory are present in the underlying layer, then there
140
- // will be no difference in the extracted inventory from oldChainLayer, so extraction can be
141
- // skipped in the chain layer. This is an optimization to avoid extracting the same inventory
139
+ // If none of the files from the packages are present in the underlying layer, then there
140
+ // will be no difference in the extracted packages from oldChainLayer, so extraction can be
141
+ // skipped in the chain layer. This is an optimization to avoid extracting the same package
142
142
// multiple times.
143
143
continue
144
144
}
145
145
146
- // Cache the inventory for future use.
147
- locationIndexToInventory [ invLocationAndIndex ] = oldInventory
146
+ // Cache the packages for future use.
147
+ locationIndexToPackage [ pkgLocationAndIndex ] = oldPackages
148
148
149
149
foundPackage := false
150
- for _ , oldInv := range oldInventory {
151
- if areInventoriesEqual ( inv , oldInv ) {
150
+ for _ , oldPKG := range oldPackages {
151
+ if arePackagesEqual ( pkg , oldPKG ) {
152
152
foundPackage = true
153
153
break
154
154
}
155
155
}
156
156
157
- // If the inventory is not present in the old layer, then it was introduced in layer i+1.
157
+ // If the package is not present in the old layer, then it was introduced in layer i+1.
158
158
if ! foundPackage {
159
159
layerDetails = chainLayerDetailsList [i + 1 ]
160
160
foundOrigin = true
161
161
break
162
162
}
163
163
}
164
164
165
- // If the inventory is present in every layer, then it means it was introduced in the first
165
+ // If the package is present in every layer, then it means it was introduced in the first
166
166
// layer.
167
167
if ! foundOrigin {
168
168
layerDetails = chainLayerDetailsList [0 ]
169
169
}
170
- inv .LayerDetails = layerDetails
170
+ pkg .LayerDetails = layerDetails
171
171
}
172
172
}
173
173
174
- // areInventoriesEqual checks if two inventories are equal. It does this by comparing the PURLs and
175
- // the locations of the inventories .
176
- func areInventoriesEqual ( inv1 * extractor.Inventory , inv2 * extractor.Inventory ) bool {
177
- if inv1 .Extractor == nil || inv2 .Extractor == nil {
174
+ // arePackagesEqual checks if two packages are equal. It does this by comparing the PURLs and
175
+ // the locations of the packages .
176
+ func arePackagesEqual ( pkg1 * extractor.Package , pkg2 * extractor.Package ) bool {
177
+ if pkg1 .Extractor == nil || pkg2 .Extractor == nil {
178
178
return false
179
179
}
180
180
181
181
// Check if the PURLs are equal.
182
- purl1 := inv1 .Extractor .ToPURL (inv1 )
183
- purl2 := inv2 .Extractor .ToPURL (inv2 )
182
+ purl1 := pkg1 .Extractor .ToPURL (pkg1 )
183
+ purl2 := pkg2 .Extractor .ToPURL (pkg2 )
184
184
185
185
if purl1 .String () != purl2 .String () {
186
186
return false
187
187
}
188
188
189
189
// Check if the locations are equal.
190
- locations1 := inv1 .Locations [:]
190
+ locations1 := pkg1 .Locations [:]
191
191
sort .Strings (locations1 )
192
192
193
- locations2 := inv2 .Locations [:]
193
+ locations2 := pkg2 .Locations [:]
194
194
sort .Strings (locations2 )
195
195
196
196
if ! slices .Equal (locations1 , locations2 ) {
0 commit comments