2018-02-05 21:05:59 +00:00
|
|
|
package layer // import "github.com/docker/docker/layer"
|
2015-11-18 22:15:00 +00:00
|
|
|
|
2015-12-16 22:13:50 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2017-08-04 00:22:00 +00:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2015-12-16 22:13:50 +00:00
|
|
|
)
|
2015-11-18 22:15:00 +00:00
|
|
|
|
|
|
|
type mountedLayer struct {
|
2015-12-16 22:13:50 +00:00
|
|
|
name string
|
|
|
|
mountID string
|
|
|
|
initID string
|
|
|
|
parent *roLayer
|
2016-03-09 21:23:04 +00:00
|
|
|
path string
|
2015-12-16 22:13:50 +00:00
|
|
|
layerStore *layerStore
|
|
|
|
|
|
|
|
references map[RWLayer]*referencedRWLayer
|
2015-11-18 22:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) cacheParent() string {
|
|
|
|
if ml.initID != "" {
|
|
|
|
return ml.initID
|
|
|
|
}
|
|
|
|
if ml.parent != nil {
|
|
|
|
return ml.parent.cacheID
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:39:54 +00:00
|
|
|
func (ml *mountedLayer) TarStream() (io.ReadCloser, error) {
|
2016-04-21 16:08:37 +00:00
|
|
|
return ml.layerStore.driver.Diff(ml.mountID, ml.cacheParent())
|
|
|
|
}
|
|
|
|
|
2015-12-16 22:13:50 +00:00
|
|
|
func (ml *mountedLayer) Name() string {
|
|
|
|
return ml.name
|
2015-11-18 22:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) Parent() Layer {
|
|
|
|
if ml.parent != nil {
|
|
|
|
return ml.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a nil interface instead of an interface wrapping a nil
|
|
|
|
// pointer.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) Size() (int64, error) {
|
|
|
|
return ml.layerStore.driver.DiffSize(ml.mountID, ml.cacheParent())
|
|
|
|
}
|
2015-12-16 22:13:50 +00:00
|
|
|
|
|
|
|
func (ml *mountedLayer) Changes() ([]archive.Change, error) {
|
|
|
|
return ml.layerStore.driver.Changes(ml.mountID, ml.cacheParent())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) Metadata() (map[string]string, error) {
|
|
|
|
return ml.layerStore.driver.GetMetadata(ml.mountID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) getReference() RWLayer {
|
|
|
|
ref := &referencedRWLayer{
|
|
|
|
mountedLayer: ml,
|
|
|
|
}
|
|
|
|
ml.references[ref] = ref
|
|
|
|
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) hasReferences() bool {
|
|
|
|
return len(ml.references) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) deleteReference(ref RWLayer) error {
|
2016-05-06 21:54:28 +00:00
|
|
|
if _, ok := ml.references[ref]; !ok {
|
2015-12-16 22:13:50 +00:00
|
|
|
return ErrLayerNotRetained
|
|
|
|
}
|
|
|
|
delete(ml.references, ref)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 18:42:29 +00:00
|
|
|
func (ml *mountedLayer) retakeReference(r RWLayer) {
|
|
|
|
if ref, ok := r.(*referencedRWLayer); ok {
|
|
|
|
ml.references[ref] = ref
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 22:13:50 +00:00
|
|
|
type referencedRWLayer struct {
|
|
|
|
*mountedLayer
|
|
|
|
}
|
|
|
|
|
2017-08-04 00:22:00 +00:00
|
|
|
func (rl *referencedRWLayer) Mount(mountLabel string) (containerfs.ContainerFS, error) {
|
2016-05-17 20:20:28 +00:00
|
|
|
return rl.layerStore.driver.Get(rl.mountedLayer.mountID, mountLabel)
|
2015-12-16 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 21:23:04 +00:00
|
|
|
// Unmount decrements the activity count and unmounts the underlying layer
|
|
|
|
// Callers should only call `Unmount` once per call to `Mount`, even on error.
|
2015-12-16 22:13:50 +00:00
|
|
|
func (rl *referencedRWLayer) Unmount() error {
|
2016-05-17 20:20:28 +00:00
|
|
|
return rl.layerStore.driver.Put(rl.mountedLayer.mountID)
|
2015-12-16 22:13:50 +00:00
|
|
|
}
|