mounted_layer.go 2.1 KB

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