1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
This commit is contained in:
Solomon Hykes 2013-02-26 17:26:46 -08:00
parent 4004e86fa9
commit aa12da6f50
12 changed files with 152 additions and 168 deletions

View file

@ -1,30 +1,32 @@
package image
import (
"errors"
"io"
"io/ioutil"
"os/exec"
"errors"
)
type Compression uint32
const (
Uncompressed Compression = iota
Uncompressed Compression = iota
Bzip2
Gzip
)
func (compression *Compression) Flag() string {
switch *compression {
case Bzip2: return "j"
case Gzip: return "z"
case Bzip2:
return "j"
case Gzip:
return "z"
}
return ""
}
func Tar(path string, compression Compression) (io.Reader, error) {
cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-c" + compression.Flag(), ".")
cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-c"+compression.Flag(), ".")
return CmdStream(cmd)
}

View file

@ -1,10 +1,10 @@
package image
import (
"testing"
"io/ioutil"
"os"
"os/exec"
"io/ioutil"
"testing"
)
func TestCmdStreamBad(t *testing.T) {

View file

@ -1,27 +1,25 @@
package image
import (
"encoding/json"
"errors"
"github.com/dotcloud/docker/future"
"io"
"io/ioutil"
"encoding/json"
"time"
"os"
"path"
"path/filepath"
"errors"
"sort"
"os"
"github.com/dotcloud/docker/future"
"strings"
"time"
)
type Store struct {
*Index
Root string
Layers *LayerStore
Root string
Layers *LayerStore
}
func New(root string) (*Store, error) {
abspath, err := filepath.Abs(root)
if err != nil {
@ -38,8 +36,8 @@ func New(root string) (*Store, error) {
return nil, err
}
return &Store{
Root: abspath,
Index: NewIndex(path.Join(root, "index.json")),
Root: abspath,
Index: NewIndex(path.Join(root, "index.json")),
Layers: layers,
}, nil
}
@ -73,20 +71,19 @@ func (store *Store) Create(name string, source string, layers ...string) (*Image
return image, nil
}
// Index
type Index struct {
Path string
ByName map[string]*History
ById map[string]*Image
Path string
ByName map[string]*History
ById map[string]*Image
}
func NewIndex(path string) *Index {
return &Index{
Path: path,
Path: path,
ByName: make(map[string]*History),
ById: make(map[string]*Image),
ById: make(map[string]*Image),
}
}
@ -216,7 +213,7 @@ func (index *Index) Names() []string {
if err := index.load(); err != nil {
return []string{}
}
var names[]string
var names []string
for name := range index.ByName {
names = append(names, name)
}
@ -279,23 +276,23 @@ func (history *History) Add(image *Image) {
func (history *History) Del(id string) {
for idx, image := range *history {
if image.Id == id {
*history = append((*history)[:idx], (*history)[idx + 1:]...)
*history = append((*history)[:idx], (*history)[idx+1:]...)
}
}
}
type Image struct {
Id string // Globally unique identifier
Layers []string // Absolute paths
Created time.Time
Parent string
Id string // Globally unique identifier
Layers []string // Absolute paths
Created time.Time
Parent string
}
func (image *Image) IdParts() (string, string) {
if len(image.Id) < 8 {
return "", image.Id
}
hash := image.Id[len(image.Id)-8:len(image.Id)]
hash := image.Id[len(image.Id)-8 : len(image.Id)]
name := image.Id[:len(image.Id)-9]
return name, hash
}
@ -316,7 +313,7 @@ func generateImageId(name string, layers []string) (string, error) {
for _, layer := range layers {
ids += path.Base(layer)
}
if h, err := future.ComputeId(strings.NewReader(ids)); err != nil {
if h, err := future.ComputeId(strings.NewReader(ids)); err != nil {
return "", err
} else {
hash = h
@ -331,9 +328,9 @@ func NewImage(name string, layers []string, parent string) (*Image, error) {
return nil, err
}
return &Image{
Id: id,
Layers: layers,
Created: time.Now(),
Parent: parent,
Id: id,
Layers: layers,
Created: time.Now(),
Parent: parent,
}, nil
}

View file

@ -2,16 +2,16 @@ package image
import (
"errors"
"path"
"path/filepath"
"github.com/dotcloud/docker/future"
"io"
"io/ioutil"
"os"
"github.com/dotcloud/docker/future"
"path"
"path/filepath"
)
type LayerStore struct {
Root string
Root string
}
func NewLayerStore(root string) (*LayerStore, error) {
@ -66,10 +66,9 @@ func (store *LayerStore) Init() error {
return os.Mkdir(store.Root, 0700)
}
func (store *LayerStore) Mktemp() (string, error) {
tmpName := future.RandomId()
tmpPath := path.Join(store.Root, "tmp-" + tmpName)
tmpPath := path.Join(store.Root, "tmp-"+tmpName)
if err := os.Mkdir(tmpPath, 0700); err != nil {
return "", err
}
@ -80,7 +79,6 @@ func (store *LayerStore) layerPath(id string) string {
return path.Join(store.Root, id)
}
func (store *LayerStore) AddLayer(archive io.Reader) (string, error) {
errors := make(chan error)
// Untar
@ -109,9 +107,10 @@ func (store *LayerStore) AddLayer(archive io.Reader) (string, error) {
return "", err
}
// Wait for goroutines
for i:=0; i<2; i+=1 {
for i := 0; i < 2; i += 1 {
select {
case err := <-errors: {
case err := <-errors:
{
if err != nil {
return "", err
}

View file

@ -1,12 +1,12 @@
package image
import (
"bytes"
"github.com/dotcloud/docker/fake"
"github.com/dotcloud/docker/future"
"io/ioutil"
"os"
"testing"
"io/ioutil"
"bytes"
"github.com/dotcloud/docker/future"
"github.com/dotcloud/docker/fake"
)
func TestAddLayer(t *testing.T) {
@ -45,4 +45,3 @@ func TestComputeId(t *testing.T) {
t.Fatalf("Identical checksums for difference content (%s == %s)", id1, id2)
}
}