github.com/kunnos/engine@v1.13.1/layer/mounted_layer.go (about) 1 package layer 2 3 import ( 4 "io" 5 6 "github.com/docker/docker/pkg/archive" 7 ) 8 9 type mountedLayer struct { 10 name string 11 mountID string 12 initID string 13 parent *roLayer 14 path string 15 layerStore *layerStore 16 17 references map[RWLayer]*referencedRWLayer 18 } 19 20 func (ml *mountedLayer) cacheParent() string { 21 if ml.initID != "" { 22 return ml.initID 23 } 24 if ml.parent != nil { 25 return ml.parent.cacheID 26 } 27 return "" 28 } 29 30 func (ml *mountedLayer) TarStream() (io.ReadCloser, error) { 31 return ml.layerStore.driver.Diff(ml.mountID, ml.cacheParent()) 32 } 33 34 func (ml *mountedLayer) Name() string { 35 return ml.name 36 } 37 38 func (ml *mountedLayer) Parent() Layer { 39 if ml.parent != nil { 40 return ml.parent 41 } 42 43 // Return a nil interface instead of an interface wrapping a nil 44 // pointer. 45 return nil 46 } 47 48 func (ml *mountedLayer) Size() (int64, error) { 49 return ml.layerStore.driver.DiffSize(ml.mountID, ml.cacheParent()) 50 } 51 52 func (ml *mountedLayer) Changes() ([]archive.Change, error) { 53 return ml.layerStore.driver.Changes(ml.mountID, ml.cacheParent()) 54 } 55 56 func (ml *mountedLayer) Metadata() (map[string]string, error) { 57 return ml.layerStore.driver.GetMetadata(ml.mountID) 58 } 59 60 func (ml *mountedLayer) getReference() RWLayer { 61 ref := &referencedRWLayer{ 62 mountedLayer: ml, 63 } 64 ml.references[ref] = ref 65 66 return ref 67 } 68 69 func (ml *mountedLayer) hasReferences() bool { 70 return len(ml.references) > 0 71 } 72 73 func (ml *mountedLayer) deleteReference(ref RWLayer) error { 74 if _, ok := ml.references[ref]; !ok { 75 return ErrLayerNotRetained 76 } 77 delete(ml.references, ref) 78 return nil 79 } 80 81 func (ml *mountedLayer) retakeReference(r RWLayer) { 82 if ref, ok := r.(*referencedRWLayer); ok { 83 ml.references[ref] = ref 84 } 85 } 86 87 type referencedRWLayer struct { 88 *mountedLayer 89 } 90 91 func (rl *referencedRWLayer) Mount(mountLabel string) (string, error) { 92 return rl.layerStore.driver.Get(rl.mountedLayer.mountID, mountLabel) 93 } 94 95 // Unmount decrements the activity count and unmounts the underlying layer 96 // Callers should only call `Unmount` once per call to `Mount`, even on error. 97 func (rl *referencedRWLayer) Unmount() error { 98 return rl.layerStore.driver.Put(rl.mountedLayer.mountID) 99 }