1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

'docker images' doesn't show all anonymous images by default - only anonymous heads

This commit is contained in:
Solomon Hykes 2013-03-23 17:03:30 -07:00
parent f43fbda2a4
commit d301c7b98c
2 changed files with 52 additions and 6 deletions

View file

@ -493,6 +493,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
cmd := rcli.Subcmd(stdout, "images", "[OPTIONS] [NAME]", "List images") cmd := rcli.Subcmd(stdout, "images", "[OPTIONS] [NAME]", "List images")
//limit := cmd.Int("l", 0, "Only show the N most recent versions of each image") //limit := cmd.Int("l", 0, "Only show the N most recent versions of each image")
quiet := cmd.Bool("q", false, "only show numeric IDs") quiet := cmd.Bool("q", false, "only show numeric IDs")
fl_a := cmd.Bool("a", false, "show all images")
if err := cmd.Parse(args); err != nil { if err := cmd.Parse(args); err != nil {
return nil return nil
} }
@ -508,7 +509,13 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri
if !*quiet { if !*quiet {
fmt.Fprintf(w, "REPOSITORY\tTAG\tID\tCREATED\tPARENT\n") fmt.Fprintf(w, "REPOSITORY\tTAG\tID\tCREATED\tPARENT\n")
} }
allImages, err := srv.runtime.graph.Map() var allImages map[string]*Image
var err error
if *fl_a {
allImages, err = srv.runtime.graph.Map()
} else {
allImages, err = srv.runtime.graph.Heads()
}
if err != nil { if err != nil {
return err return err
} }

View file

@ -141,20 +141,59 @@ func (graph *Graph) Map() (map[string]*Image, error) {
} }
func (graph *Graph) All() ([]*Image, error) { func (graph *Graph) All() ([]*Image, error) {
var images []*Image
err := graph.WalkAll(func(image *Image) {
images = append(images, image)
})
return images, err
}
func (graph *Graph) WalkAll(handler func(*Image)) error {
files, err := ioutil.ReadDir(graph.Root) files, err := ioutil.ReadDir(graph.Root)
if err != nil { if err != nil {
return nil, err return err
} }
var images []*Image
for _, st := range files { for _, st := range files {
if img, err := graph.Get(st.Name()); err != nil { if img, err := graph.Get(st.Name()); err != nil {
// Skip image // Skip image
continue continue
} else { } else if handler != nil {
images = append(images, img) handler(img)
} }
} }
return images, nil return nil
}
func (graph *Graph) ByParent() (map[string][]*Image, error) {
byParent := make(map[string][]*Image)
err := graph.WalkAll(func(image *Image) {
image, err := graph.Get(image.Parent)
if err != nil {
return
}
if children, exists := byParent[image.Parent]; exists {
byParent[image.Parent] = []*Image{image}
} else {
byParent[image.Parent] = append(children, image)
}
})
return byParent, err
}
func (graph *Graph) Heads() (map[string]*Image, error) {
heads := make(map[string]*Image)
byParent, err := graph.ByParent()
if err != nil {
return nil, err
}
err = graph.WalkAll(func(image *Image) {
// If it's not in the byParent lookup table, then
// it's not a parent -> so it's a head!
if _, exists := byParent[image.Id]; !exists {
heads[image.Id] = image
}
})
return heads, err
} }
func (graph *Graph) imageRoot(id string) string { func (graph *Graph) imageRoot(id string) string {