@@ -84,11 +84,13 @@ func (i *ImageService) Images(imageFilters filters.Args, all bool, withExtraAttr
8484 return nil , err
8585 }
8686
87- images := []* types.ImageSummary {}
88- var imagesMap map [* image.Image ]* types.ImageSummary
89- var layerRefs map [layer.ChainID ]int
90- var allLayers map [layer.ChainID ]layer.Layer
91- var allContainers []* container.Container
87+ var (
88+ images = []* types.ImageSummary {}
89+ imagesMap = make (map [* image.Image ]* types.ImageSummary )
90+ layerRefs = make (map [layer.ChainID ]int )
91+ allLayers = i .layerStore .Map ()
92+ allContainers []* container.Container
93+ )
9294
9395 for id , img := range allImages {
9496 if beforeFilter != nil {
@@ -188,44 +190,37 @@ func (i *ImageService) Images(imageFilters filters.Args, all bool, withExtraAttr
188190
189191 if withExtraAttrs {
190192 // lazily init variables
191- if imagesMap == nil {
193+ if allContainers == nil {
192194 allContainers = i .containers .List ()
193- allLayers = i .layerStore .Map ()
194- imagesMap = make (map [* image.Image ]* types.ImageSummary )
195- layerRefs = make (map [layer.ChainID ]int )
196195 }
197196
198197 // Get container count
199- newImage .Containers = 0
200198 for _ , c := range allContainers {
201199 if c .ImageID == id {
202200 newImage .Containers ++
203201 }
204202 }
203+ }
205204
206- // count layer references
207- rootFS := * img .RootFS
208- rootFS .DiffIDs = nil
209- for _ , id := range img .RootFS .DiffIDs {
210- rootFS .Append (id )
211- chid := rootFS .ChainID ()
212- layerRefs [chid ]++
213- if _ , ok := allLayers [chid ]; ! ok {
214- return nil , fmt .Errorf ("layer %v was not found (corruption?)" , chid )
215- }
205+ // count layer references
206+ rootFS := image.RootFS {}
207+ for _ , diffID := range img .RootFS .DiffIDs {
208+ rootFS .Append (diffID )
209+ chid := rootFS .ChainID ()
210+ layerRefs [chid ]++
211+ if _ , ok := allLayers [chid ]; ! ok {
212+ return nil , fmt .Errorf ("layer %v was not found (corruption?)" , chid )
216213 }
217- imagesMap [img ] = newImage
218214 }
215+ imagesMap [img ] = newImage
219216
220217 images = append (images , newImage )
221218 }
222219
223220 if withExtraAttrs {
224221 // Get Shared sizes
225222 for img , newImage := range imagesMap {
226- rootFS := * img .RootFS
227- rootFS .DiffIDs = nil
228-
223+ rootFS := image.RootFS {}
229224 newImage .SharedSize = 0
230225 for _ , id := range img .RootFS .DiffIDs {
231226 rootFS .Append (id )
@@ -336,16 +331,21 @@ func (i *ImageService) SquashImage(id, parent string) (string, error) {
336331}
337332
338333func newImage (image * image.Image , size int64 ) * types.ImageSummary {
339- newImage := new (types.ImageSummary )
340- newImage .ParentID = image .Parent .String ()
341- newImage .ID = image .ID ().String ()
342- newImage .Created = image .Created .Unix ()
343- newImage .Size = size
344- newImage .VirtualSize = size
345- newImage .SharedSize = - 1
346- newImage .Containers = - 1
334+ summary := & types.ImageSummary {
335+ ParentID : image .Parent .String (),
336+ ID : image .ID ().String (),
337+ Created : image .Created .Unix (),
338+ Size : size ,
339+ VirtualSize : size ,
340+ // -1 indicates that the value has not been set (avoids ambiguity
341+ // between 0 (default) and "not set". We cannot use a pointer (nil)
342+ // for this, as the JSON representation uses "omitempty", which would
343+ // consider both "0" and "nil" to be "empty".
344+ SharedSize : - 1 ,
345+ Containers : - 1 ,
346+ }
347347 if image .Config != nil {
348- newImage .Labels = image .Config .Labels
348+ summary .Labels = image .Config .Labels
349349 }
350- return newImage
350+ return summary
351351}
0 commit comments