graph.go
ef711962
 package docker
33f6a0aa
 
 import (
 	"fmt"
 	"io/ioutil"
 	"os"
 	"path"
 	"path/filepath"
 	"time"
 )
 
 type Graph struct {
 	Root string
 }
 
ef711962
 func NewGraph(root string) (*Graph, error) {
33f6a0aa
 	abspath, err := filepath.Abs(root)
 	if err != nil {
 		return nil, err
 	}
 	// Create the root directory if it doesn't exists
 	if err := os.Mkdir(root, 0700); err != nil && !os.IsExist(err) {
 		return nil, err
 	}
 	return &Graph{
 		Root: abspath,
 	}, nil
 }
 
 func (graph *Graph) Exists(id string) bool {
 	if _, err := graph.Get(id); err != nil {
 		return false
 	}
 	return true
 }
 
 func (graph *Graph) Get(id string) (*Image, error) {
379d449c
 	// FIXME: return nil when the image doesn't exist, instead of an error
33f6a0aa
 	img, err := LoadImage(graph.imageRoot(id))
 	if err != nil {
 		return nil, err
 	}
 	if img.Id != id {
 		return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.Id)
 	}
84e8c4aa
 	img.graph = graph
33f6a0aa
 	return img, nil
 }
 
05ae69a6
 func (graph *Graph) Create(layerData Archive, container *Container, comment string) (*Image, error) {
33f6a0aa
 	img := &Image{
 		Id:      GenerateId(),
 		Comment: comment,
 		Created: time.Now(),
 	}
05ae69a6
 	if container != nil {
 		img.Parent = container.Image
0146c80c
 		img.Container = container.Id
 		img.ContainerConfig = *container.Config
05ae69a6
 	}
33f6a0aa
 	if err := graph.Register(layerData, img); err != nil {
 		return nil, err
 	}
 	return img, nil
 }
 
 func (graph *Graph) Register(layerData Archive, img *Image) error {
 	if err := ValidateId(img.Id); err != nil {
 		return err
 	}
 	// (This is a convenience to save time. Race conditions are taken care of by os.Rename)
 	if graph.Exists(img.Id) {
 		return fmt.Errorf("Image %s already exists", img.Id)
 	}
 	tmp, err := graph.Mktemp(img.Id)
 	defer os.RemoveAll(tmp)
 	if err != nil {
 		return fmt.Errorf("Mktemp failed: %s", err)
 	}
 	if err := StoreImage(img, layerData, tmp); err != nil {
 		return err
 	}
 	// Commit
 	if err := os.Rename(tmp, graph.imageRoot(img.Id)); err != nil {
 		return err
 	}
 	img.graph = graph
 	return nil
 }
 
 func (graph *Graph) Mktemp(id string) (string, error) {
ef711962
 	tmp, err := NewGraph(path.Join(graph.Root, ":tmp:"))
33f6a0aa
 	if err != nil {
 		return "", fmt.Errorf("Couldn't create temp: %s", err)
 	}
 	if tmp.Exists(id) {
 		return "", fmt.Errorf("Image %d already exists", id)
 	}
 	return tmp.imageRoot(id), nil
 }
 
 func (graph *Graph) Garbage() (*Graph, error) {
ef711962
 	return NewGraph(path.Join(graph.Root, ":garbage:"))
33f6a0aa
 }
 
 func (graph *Graph) Delete(id string) error {
 	garbage, err := graph.Garbage()
 	if err != nil {
 		return err
 	}
 	return os.Rename(graph.imageRoot(id), garbage.imageRoot(id))
 }
 
 func (graph *Graph) Undelete(id string) error {
 	garbage, err := graph.Garbage()
 	if err != nil {
 		return err
 	}
 	return os.Rename(garbage.imageRoot(id), graph.imageRoot(id))
 }
 
 func (graph *Graph) GarbageCollect() error {
 	garbage, err := graph.Garbage()
 	if err != nil {
 		return err
 	}
 	return os.RemoveAll(garbage.Root)
 }
 
44faa07b
 func (graph *Graph) Map() (map[string]*Image, error) {
 	// FIXME: this should replace All()
 	all, err := graph.All()
 	if err != nil {
 		return nil, err
 	}
 	images := make(map[string]*Image, len(all))
 	for _, image := range all {
 		images[image.Id] = image
 	}
 	return images, nil
 }
 
33f6a0aa
 func (graph *Graph) All() ([]*Image, error) {
d301c7b9
 	var images []*Image
 	err := graph.WalkAll(func(image *Image) {
 		images = append(images, image)
 	})
 	return images, err
 }
 
 func (graph *Graph) WalkAll(handler func(*Image)) error {
33f6a0aa
 	files, err := ioutil.ReadDir(graph.Root)
 	if err != nil {
d301c7b9
 		return err
33f6a0aa
 	}
 	for _, st := range files {
 		if img, err := graph.Get(st.Name()); err != nil {
 			// Skip image
 			continue
d301c7b9
 		} else if handler != nil {
 			handler(img)
 		}
 	}
 	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}
33f6a0aa
 		} else {
d301c7b9
 			byParent[image.Parent] = append(children, image)
33f6a0aa
 		}
d301c7b9
 	})
 	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
33f6a0aa
 	}
d301c7b9
 	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
33f6a0aa
 }
 
 func (graph *Graph) imageRoot(id string) string {
 	return path.Join(graph.Root, id)
 }