2018-02-05 16:05:59 -05:00
|
|
|
package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
|
2014-09-10 23:30:52 -04:00
|
|
|
|
|
|
|
import (
|
2016-10-20 19:40:59 -04:00
|
|
|
"io"
|
2014-09-10 23:30:52 -04:00
|
|
|
"time"
|
|
|
|
|
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"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-09-10 23:30:52 -04:00
|
|
|
)
|
|
|
|
|
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
|
2017-08-03 20:22:00 -04:00
|
|
|
// capability of the Diffing methods on the local file system,
|
|
|
|
// which it may or may not support on its own. See the comment
|
|
|
|
// on the exported 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 "".
|
2016-10-20 19:40:59 -04:00
|
|
|
func (gdw *NaiveDiffDriver) Diff(id, parent string) (arch io.ReadCloser, err error) {
|
2016-03-30 19:16:37 -04:00
|
|
|
startTime := time.Now()
|
2014-09-16 15:13:50 -04:00
|
|
|
driver := gdw.ProtoDriver
|
2014-09-10 23:30:52 -04:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
layerRootFs, err := driver.Get(id, "")
|
2014-09-10 23:30:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
layerFs := layerRootFs.Path()
|
2014-09-10 23:30:52 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
parentRootFs, err := driver.Get(parent, "")
|
2014-09-10 23:30:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer driver.Put(parent)
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
parentFs := parentRootFs.Path()
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
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)
|
2016-03-30 19:16:37 -04:00
|
|
|
|
|
|
|
// NaiveDiffDriver compares file metadata with parent layers. Parent layers
|
|
|
|
// are extracted from tar's with full second precision on modified time.
|
|
|
|
// We need this hack here to make sure calls within same second receive
|
|
|
|
// correct result.
|
2017-09-11 14:55:05 -04:00
|
|
|
time.Sleep(time.Until(startTime.Truncate(time.Second).Add(time.Second)))
|
2014-09-10 23:30:52 -04:00
|
|
|
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
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
layerRootFs, err := driver.Get(id, "")
|
2014-09-10 23:30:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer driver.Put(id)
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
layerFs := layerRootFs.Path()
|
2014-09-10 23:30:52 -04:00
|
|
|
parentFs := ""
|
|
|
|
|
|
|
|
if parent != "" {
|
2017-08-03 20:22:00 -04:00
|
|
|
parentRootFs, err := driver.Get(parent, "")
|
2014-09-10 23:30:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer driver.Put(parent)
|
2017-08-03 20:22:00 -04:00
|
|
|
parentFs = parentRootFs.Path()
|
2014-09-10 23:30:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2016-10-20 19:40:59 -04:00
|
|
|
func (gdw *NaiveDiffDriver) ApplyDiff(id, parent string, diff io.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.
|
2017-08-03 20:22:00 -04:00
|
|
|
layerRootFs, err := driver.Get(id, "")
|
2014-09-10 23:30:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer driver.Put(id)
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
layerFs := layerRootFs.Path()
|
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()
|
2018-10-02 18:14:31 -04:00
|
|
|
logrus.WithField("id", id).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
|
|
|
|
}
|
2018-10-02 18:14:31 -04:00
|
|
|
logrus.WithField("id", id).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)
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
return archive.ChangesSize(layerFs.Path(), changes), nil
|
2014-09-10 23:30:52 -04:00
|
|
|
}
|