2018-02-05 16:05:59 -05:00
|
|
|
package layer // import "github.com/docker/docker/layer"
|
2015-11-18 17:15:00 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2017-01-06 20:23:18 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-11-18 17:15:00 -05:00
|
|
|
"github.com/vbatts/tar-split/tar/asm"
|
|
|
|
"github.com/vbatts/tar-split/tar/storage"
|
|
|
|
)
|
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
func (ls *layerStore) ChecksumForGraphID(id, parent, oldTarDataPath, newTarDataPath string) (diffID DiffID, size int64, err error) {
|
|
|
|
defer func() {
|
2015-11-18 17:15:00 -05:00
|
|
|
if err != nil {
|
2015-11-29 22:55:22 -05:00
|
|
|
logrus.Debugf("could not get checksum for %q with tar-split: %q", id, err)
|
|
|
|
diffID, size, err = ls.checksumForGraphIDNoTarsplit(id, parent, newTarDataPath)
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
2015-11-29 22:55:22 -05:00
|
|
|
}()
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
if oldTarDataPath == "" {
|
|
|
|
err = errors.New("no tar-split file")
|
|
|
|
return
|
|
|
|
}
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
tarDataFile, err := os.Open(oldTarDataPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tarDataFile.Close()
|
|
|
|
uncompressed, err := gzip.NewReader(tarDataFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2017-01-06 20:23:18 -05:00
|
|
|
dgst := digest.Canonical.Digester()
|
2017-09-19 15:14:46 -04:00
|
|
|
err = ls.assembleTarTo(id, uncompressed, &size, dgst.Hash())
|
2015-11-29 22:55:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
diffID = DiffID(dgst.Digest())
|
|
|
|
err = os.RemoveAll(newTarDataPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = os.Link(oldTarDataPath, newTarDataPath)
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
return
|
|
|
|
}
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
func (ls *layerStore) checksumForGraphIDNoTarsplit(id, parent, newTarDataPath string) (diffID DiffID, size int64, err error) {
|
2017-09-19 15:14:46 -04:00
|
|
|
rawarchive, err := ls.driver.Diff(id, parent)
|
2015-11-29 22:55:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
2015-11-29 22:55:22 -05:00
|
|
|
defer rawarchive.Close()
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
f, err := os.Create(newTarDataPath)
|
2015-11-18 17:15:00 -05:00
|
|
|
if err != nil {
|
2015-11-29 22:55:22 -05:00
|
|
|
return
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
2015-11-29 22:55:22 -05:00
|
|
|
defer f.Close()
|
|
|
|
mfz := gzip.NewWriter(f)
|
2016-02-18 13:55:38 -05:00
|
|
|
defer mfz.Close()
|
2015-11-29 22:55:22 -05:00
|
|
|
metaPacker := storage.NewJSONPacker(mfz)
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
packerCounter := &packSizeCounter{metaPacker, &size}
|
2015-11-18 17:15:00 -05:00
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
archive, err := asm.NewInputTarStream(rawarchive, packerCounter, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dgst, err := digest.FromReader(archive)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
diffID = DiffID(dgst)
|
|
|
|
return
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
func (ls *layerStore) RegisterByGraphID(graphID string, parent ChainID, diffID DiffID, tarDataFile string, size int64) (Layer, error) {
|
2015-11-18 17:15:00 -05:00
|
|
|
// err is used to hold the error which will always trigger
|
|
|
|
// cleanup of creates sources but may not be an error returned
|
|
|
|
// to the caller (already exists).
|
|
|
|
var err error
|
|
|
|
var p *roLayer
|
|
|
|
if string(parent) != "" {
|
|
|
|
p = ls.get(parent)
|
|
|
|
if p == nil {
|
|
|
|
return nil, ErrLayerDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release parent chain if error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
ls.layerL.Lock()
|
|
|
|
ls.releaseLayer(p)
|
|
|
|
ls.layerL.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new roLayer
|
|
|
|
layer := &roLayer{
|
|
|
|
parent: p,
|
|
|
|
cacheID: graphID,
|
|
|
|
referenceCount: 1,
|
|
|
|
layerStore: ls,
|
|
|
|
references: map[Layer]struct{}{},
|
2015-11-29 22:55:22 -05:00
|
|
|
diffID: diffID,
|
|
|
|
size: size,
|
|
|
|
chainID: createChainIDFromParent(parent, diffID),
|
|
|
|
}
|
|
|
|
|
|
|
|
ls.layerL.Lock()
|
|
|
|
defer ls.layerL.Unlock()
|
|
|
|
|
|
|
|
if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil {
|
|
|
|
// Set error for cleanup, but do not return
|
|
|
|
err = errors.New("layer already exists")
|
|
|
|
return existingLayer.getReference(), nil
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := ls.store.StartTransaction()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Cleaning up transaction after failed migration for %s: %v", graphID, err)
|
|
|
|
if err := tx.Cancel(); err != nil {
|
|
|
|
logrus.Errorf("Error canceling metadata transaction %q: %s", tx.String(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
tsw, err := tx.TarSplitWriter(false)
|
|
|
|
if err != nil {
|
2015-11-18 17:15:00 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-29 22:55:22 -05:00
|
|
|
defer tsw.Close()
|
|
|
|
tdf, err := os.Open(tarDataFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tdf.Close()
|
|
|
|
_, err = io.Copy(tsw, tdf)
|
|
|
|
if err != nil {
|
2015-11-18 17:15:00 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-29 22:55:22 -05:00
|
|
|
if err = storeLayer(tx, layer); err != nil {
|
|
|
|
return nil, err
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.Commit(layer.chainID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ls.layerMap[layer.chainID] = layer
|
|
|
|
|
|
|
|
return layer.getReference(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type unpackSizeCounter struct {
|
|
|
|
unpacker storage.Unpacker
|
|
|
|
size *int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *unpackSizeCounter) Next() (*storage.Entry, error) {
|
|
|
|
e, err := u.unpacker.Next()
|
|
|
|
if err == nil && u.size != nil {
|
|
|
|
*u.size += e.Size
|
|
|
|
}
|
|
|
|
return e, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type packSizeCounter struct {
|
|
|
|
packer storage.Packer
|
|
|
|
size *int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *packSizeCounter) AddEntry(e storage.Entry) (int, error) {
|
|
|
|
n, err := p.packer.AddEntry(e)
|
|
|
|
if err == nil && p.size != nil {
|
|
|
|
*p.size += e.Size
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|