Browse code

Fix bug introduced in c7a944caf28d85bd26f4031465e958006a764ac2 which caused 'docker images' to crash

Solomon Hykes authored on 2013/01/30 05:15:39
Showing 3 changed files
... ...
@@ -7,7 +7,7 @@ import (
7 7
 	"log"
8 8
 	"os"
9 9
 	"path"
10
-	"github.com/dotcloud/docker/image"	// For sorting utility
10
+	"sort"
11 11
 )
12 12
 
13 13
 type Docker struct {
... ...
@@ -17,15 +17,11 @@ type Docker struct {
17 17
 }
18 18
 
19 19
 func (docker *Docker) List() []*Container {
20
-	history := new(image.History)
20
+	containers := new(History)
21 21
 	for e := docker.containers.Front(); e != nil; e = e.Next() {
22
-		history.Add(e.Value.(*Container))
22
+		containers.Add(e.Value.(*Container))
23 23
 	}
24
-	containers := make([]*Container, len(*history))
25
-	for i := range *history {
26
-		containers[i] = (*history)[i].(*Container)
27
-	}
28
-	return containers
24
+	return *containers
29 25
 }
30 26
 
31 27
 func (docker *Docker) getContainerElement(id string) *list.Element {
... ...
@@ -120,3 +116,28 @@ func NewFromDirectory(root string) (*Docker, error) {
120 120
 	}
121 121
 	return docker, nil
122 122
 }
123
+
124
+
125
+type History []*Container
126
+
127
+func (history *History) Len() int {
128
+	return len(*history)
129
+}
130
+
131
+func (history *History) Less(i, j int) bool {
132
+	containers := *history
133
+	return containers[j].When().Before(containers[i].When())
134
+}
135
+
136
+func (history *History) Swap(i, j int) {
137
+	containers := *history
138
+	tmp := containers[i]
139
+	containers[i] = containers[j]
140
+	containers[j] = tmp
141
+}
142
+
143
+func (history *History) Add(container *Container) {
144
+	*history = append(*history, container)
145
+	sort.Sort(history)
146
+}
147
+
... ...
@@ -288,8 +288,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
288 288
 		if nameFilter != "" && nameFilter != name {
289 289
 			continue
290 290
 		}
291
-		for idx, evt := range *srv.images.ByName[name] {
292
-			img := evt.(*image.Image)
291
+		for idx, img := range *srv.images.ByName[name] {
293 292
 			if *limit > 0 && idx >= *limit {
294 293
 				break
295 294
 			}
... ...
@@ -102,7 +102,7 @@ func (index *Index) Find(idOrName string) *Image {
102 102
 	}
103 103
 	// Lookup by name
104 104
 	if history, exists := index.ByName[idOrName]; exists && history.Len() > 0 {
105
-		return (*history)[0].(*Image)
105
+		return (*history)[0]
106 106
 	}
107 107
 	return nil
108 108
 }
... ...
@@ -116,7 +116,7 @@ func (index *Index) Add(name string, image *Image) error {
116 116
 		index.ByName[name] = new(History)
117 117
 	} else {
118 118
 		// If this image is already the latest version, don't add it.
119
-		if (*index.ByName[name])[0].(*Image).Id == image.Id {
119
+		if (*index.ByName[name])[0].Id == image.Id {
120 120
 			return nil
121 121
 		}
122 122
 	}
... ...
@@ -169,8 +169,7 @@ func (index *Index) Rename(oldName, newName string) error {
169 169
 	index.ByName[newName] = index.ByName[oldName]
170 170
 	delete(index.ByName, oldName)
171 171
 	// Change the ID of all images, since they include the name
172
-	for _, event := range *index.ByName[newName] {
173
-		image := event.(*Image)
172
+	for _, image := range *index.ByName[newName] {
174 173
 		if id, err := generateImageId(newName, image.Layers); err != nil {
175 174
 			return err
176 175
 		} else {
... ...
@@ -228,33 +227,37 @@ func (index *Index) save() error {
228 228
 
229 229
 // History wraps an array of images so they can be sorted by date (most recent first)
230 230
 
231
-type Event interface {
232
-	When() time.Time
233
-}
234
-
235
-type History []Event
231
+type History []*Image
236 232
 
237 233
 func (history *History) Len() int {
238 234
 	return len(*history)
239 235
 }
240 236
 
241 237
 func (history *History) Less(i, j int) bool {
242
-	events := *history
243
-	return events[j].When().Before(events[i].When())
238
+	images := *history
239
+	return images[j].Created.Before(images[i].Created)
244 240
 }
245 241
 
246 242
 func (history *History) Swap(i, j int) {
247
-	events := *history
248
-	tmp := events[i]
249
-	events[i] = events[j]
250
-	events[j] = tmp
243
+	images := *history
244
+	tmp := images[i]
245
+	images[i] = images[j]
246
+	images[j] = tmp
251 247
 }
252 248
 
253
-func (history *History) Add(event Event) {
254
-	*history = append(*history, event)
249
+func (history *History) Add(image *Image) {
250
+	*history = append(*history, image)
255 251
 	sort.Sort(history)
256 252
 }
257 253
 
254
+func (history *History) Del(id string) {
255
+	for idx, image := range *history {
256
+		if image.Id == id {
257
+			*history = append((*history)[:idx], (*history)[idx + 1:]...)
258
+		}
259
+	}
260
+}
261
+
258 262
 type Image struct {
259 263
 	Id	string		// Globally unique identifier
260 264
 	Layers	[]string	// Absolute paths
... ...
@@ -262,10 +265,6 @@ type Image struct {
262 262
 	Parent	string
263 263
 }
264 264
 
265
-func (image *Image) When() time.Time {
266
-	return image.Created
267
-}
268
-
269 265
 func (image *Image) IdParts() (string, string) {
270 266
 	if len(image.Id) < 8 {
271 267
 		return "", image.Id