2014-09-10 23:30:52 -04:00
|
|
|
package graphdriver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-10-08 11:51:41 -04:00
|
|
|
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-10-29 15:06:51 -04:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2014-09-10 23:30:52 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
)
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
var (
|
|
|
|
// ApplyUncompressedLayer defines the unpack method used by the graph
|
|
|
|
// driver.
|
|
|
|
ApplyUncompressedLayer = chrootarchive.ApplyUncompressedLayer
|
|
|
|
)
|
|
|
|
|
2015-09-23 21:21:28 -04:00
|
|
|
// NaiveDiffDriver takes a ProtoDriver and adds the
|
2014-09-16 15:13:50 -04:00
|
|
|
// capability of the Diffing methods which it may or may not
|
|
|
|
// support on its own. See the comment on the exported
|
2015-09-23 21:21:28 -04:00
|
|
|
// NewNaiveDiffDriver function below.
|
2014-09-16 15:13:50 -04:00
|
|
|
// Notably, the AUFS driver doesn't need to be wrapped like this.
|
2015-09-23 21:21:28 -04:00
|
|
|
type NaiveDiffDriver struct {
|
2014-09-16 15:13:50 -04:00
|
|
|
ProtoDriver
|
2015-10-08 11:51:41 -04:00
|
|
|
uidMaps []idtools.IDMap
|
|
|
|
gidMaps []idtools.IDMap
|
2014-09-16 15:13:50 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 21:21:28 -04:00
|
|
|
// NewNaiveDiffDriver returns a fully functional driver that wraps the
|
2014-09-16 15:13:50 -04:00
|
|
|
// given ProtoDriver and adds the capability of the following methods which
|
|
|
|
// it may or may not support on its own:
|
2014-09-10 23:30:52 -04:00
|
|
|
// Diff(id, parent string) (archive.Archive, error)
|
|
|
|
// Changes(id, parent string) ([]archive.Change, error)
|
2015-08-03 21:52:54 -04:00
|
|
|
// ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error)
|
2014-12-17 21:26:03 -05:00
|
|
|
// DiffSize(id, parent string) (size int64, err error)
|
2015-10-08 11:51:41 -04:00
|
|
|
func NewNaiveDiffDriver(driver ProtoDriver, uidMaps, gidMaps []idtools.IDMap) Driver {
|
|
|
|
return &NaiveDiffDriver{ProtoDriver: driver,
|
|
|
|
uidMaps: uidMaps,
|
|
|
|
gidMaps: gidMaps}
|
2014-09-10 23:30:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Diff produces an archive of the changes between the specified
|
|
|
|
// layer and its parent layer which may be "".
|
2015-09-23 21:21:28 -04:00
|
|
|
func (gdw *NaiveDiffDriver) Diff(id, parent string) (arch archive.Archive, err error) {
|
2014-09-16 15:13:50 -04:00
|
|
|
driver := gdw.ProtoDriver
|
2014-09-10 23:30:52 -04:00
|
|
|
|
|
|
|
layerFs, err := driver.Get(id, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
driver.Put(id)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if parent == "" {
|
|
|
|
archive, err := archive.Tar(layerFs, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
|
|
|
driver.Put(id)
|
|
|
|
return err
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parentFs, err := driver.Get(parent, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer driver.Put(parent)
|
|
|
|
|
|
|
|
changes, err := archive.ChangesDirs(layerFs, parentFs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
archive, err := archive.ExportChanges(layerFs, changes, gdw.uidMaps, gdw.gidMaps)
|
2014-09-10 23:30:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
|
|
|
driver.Put(id)
|
|
|
|
return err
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes produces a list of changes between the specified layer
|
|
|
|
// and its parent layer. If parent is "", then all changes will be ADD changes.
|
2015-09-23 21:21:28 -04:00
|
|
|
func (gdw *NaiveDiffDriver) Changes(id, parent string) ([]archive.Change, error) {
|
2014-09-16 15:13:50 -04:00
|
|
|
driver := gdw.ProtoDriver
|
2014-09-10 23:30:52 -04:00
|
|
|
|
|
|
|
layerFs, err := driver.Get(id, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer driver.Put(id)
|
|
|
|
|
|
|
|
parentFs := ""
|
|
|
|
|
|
|
|
if parent != "" {
|
|
|
|
parentFs, err = driver.Get(parent, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer driver.Put(parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return archive.ChangesDirs(layerFs, parentFs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyDiff extracts the changeset from the given diff into the
|
|
|
|
// layer with the specified id and parent, returning the size of the
|
|
|
|
// new layer in bytes.
|
2015-09-23 21:21:28 -04:00
|
|
|
func (gdw *NaiveDiffDriver) ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error) {
|
2014-09-16 15:13:50 -04:00
|
|
|
driver := gdw.ProtoDriver
|
2014-09-10 23:30:52 -04:00
|
|
|
|
|
|
|
// Mount the root filesystem so we can apply the diff/layer.
|
|
|
|
layerFs, err := driver.Get(id, "")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer driver.Put(id)
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
options := &archive.TarOptions{UIDMaps: gdw.uidMaps,
|
|
|
|
GIDMaps: gdw.gidMaps}
|
2014-09-10 23:30:52 -04:00
|
|
|
start := time.Now().UTC()
|
2016-06-11 16:16:55 -04:00
|
|
|
logrus.Debug("Start untar layer")
|
2015-11-18 17:20:54 -05:00
|
|
|
if size, err = ApplyUncompressedLayer(layerFs, diff, options); err != nil {
|
2014-09-10 23:30:52 -04:00
|
|
|
return
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Untar time: %vs", time.Now().UTC().Sub(start).Seconds())
|
2014-09-10 23:30:52 -04:00
|
|
|
|
2014-12-17 21:26:03 -05:00
|
|
|
return
|
2014-09-10 23:30:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiffSize calculates the changes between the specified layer
|
|
|
|
// and its parent and returns the size in bytes of the changes
|
|
|
|
// relative to its base filesystem directory.
|
2015-09-23 21:21:28 -04:00
|
|
|
func (gdw *NaiveDiffDriver) DiffSize(id, parent string) (size int64, err error) {
|
2014-09-16 15:13:50 -04:00
|
|
|
driver := gdw.ProtoDriver
|
2014-09-10 23:30:52 -04:00
|
|
|
|
|
|
|
changes, err := gdw.Changes(id, parent)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
layerFs, err := driver.Get(id, "")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer driver.Put(id)
|
|
|
|
|
|
|
|
return archive.ChangesSize(layerFs, changes), nil
|
|
|
|
}
|