fsdiff.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
  2. import (
  3. "context"
  4. "io"
  5. "time"
  6. "github.com/containerd/log"
  7. "github.com/docker/docker/pkg/archive"
  8. "github.com/docker/docker/pkg/chrootarchive"
  9. "github.com/docker/docker/pkg/idtools"
  10. "github.com/docker/docker/pkg/ioutils"
  11. )
  12. // ApplyUncompressedLayer defines the unpack method used by the graph
  13. // driver.
  14. var ApplyUncompressedLayer = chrootarchive.ApplyUncompressedLayer
  15. // NaiveDiffDriver takes a ProtoDriver and adds the
  16. // capability of the Diffing methods on the local file system,
  17. // which it may or may not support on its own. See the comment
  18. // on the exported NewNaiveDiffDriver function below.
  19. type NaiveDiffDriver struct {
  20. ProtoDriver
  21. IDMap idtools.IdentityMapping
  22. // If true, allow ApplyDiff to succeed in spite of failures to set
  23. // extended attributes on the unpacked files due to the destination
  24. // filesystem not supporting them or a lack of permissions. The
  25. // resulting unpacked layer may be subtly broken.
  26. BestEffortXattrs bool
  27. }
  28. // NewNaiveDiffDriver returns a fully functional driver that wraps the
  29. // given ProtoDriver and adds the capability of the following methods which
  30. // it may or may not support on its own:
  31. //
  32. // Diff(id, parent string) (archive.Archive, error)
  33. // Changes(id, parent string) ([]archive.Change, error)
  34. // ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error)
  35. // DiffSize(id, parent string) (size int64, err error)
  36. func NewNaiveDiffDriver(driver ProtoDriver, idMap idtools.IdentityMapping) Driver {
  37. return &NaiveDiffDriver{
  38. ProtoDriver: driver,
  39. IDMap: idMap,
  40. }
  41. }
  42. // Diff produces an archive of the changes between the specified
  43. // layer and its parent layer which may be "".
  44. func (gdw *NaiveDiffDriver) Diff(id, parent string) (arch io.ReadCloser, err error) {
  45. startTime := time.Now()
  46. driver := gdw.ProtoDriver
  47. layerRootFs, err := driver.Get(id, "")
  48. if err != nil {
  49. return nil, err
  50. }
  51. layerFs := layerRootFs
  52. defer func() {
  53. if err != nil {
  54. driver.Put(id)
  55. }
  56. }()
  57. if parent == "" {
  58. archive, err := archive.Tar(layerFs, archive.Uncompressed)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return ioutils.NewReadCloserWrapper(archive, func() error {
  63. err := archive.Close()
  64. driver.Put(id)
  65. return err
  66. }), nil
  67. }
  68. parentFs, err := driver.Get(parent, "")
  69. if err != nil {
  70. return nil, err
  71. }
  72. defer driver.Put(parent)
  73. changes, err := archive.ChangesDirs(layerFs, parentFs)
  74. if err != nil {
  75. return nil, err
  76. }
  77. archive, err := archive.ExportChanges(layerFs, changes, gdw.IDMap)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return ioutils.NewReadCloserWrapper(archive, func() error {
  82. err := archive.Close()
  83. driver.Put(id)
  84. // NaiveDiffDriver compares file metadata with parent layers. Parent layers
  85. // are extracted from tar's with full second precision on modified time.
  86. // We need this hack here to make sure calls within same second receive
  87. // correct result.
  88. time.Sleep(time.Until(startTime.Truncate(time.Second).Add(time.Second)))
  89. return err
  90. }), nil
  91. }
  92. // Changes produces a list of changes between the specified layer
  93. // and its parent layer. If parent is "", then all changes will be ADD changes.
  94. func (gdw *NaiveDiffDriver) Changes(id, parent string) ([]archive.Change, error) {
  95. driver := gdw.ProtoDriver
  96. layerFs, err := driver.Get(id, "")
  97. if err != nil {
  98. return nil, err
  99. }
  100. defer driver.Put(id)
  101. parentFs := ""
  102. if parent != "" {
  103. parentFs, err = driver.Get(parent, "")
  104. if err != nil {
  105. return nil, err
  106. }
  107. defer driver.Put(parent)
  108. }
  109. return archive.ChangesDirs(layerFs, parentFs)
  110. }
  111. // ApplyDiff extracts the changeset from the given diff into the
  112. // layer with the specified id and parent, returning the size of the
  113. // new layer in bytes.
  114. func (gdw *NaiveDiffDriver) ApplyDiff(id, parent string, diff io.Reader) (size int64, err error) {
  115. driver := gdw.ProtoDriver
  116. // Mount the root filesystem so we can apply the diff/layer.
  117. layerRootFs, err := driver.Get(id, "")
  118. if err != nil {
  119. return
  120. }
  121. defer driver.Put(id)
  122. layerFs := layerRootFs
  123. options := &archive.TarOptions{IDMap: gdw.IDMap, BestEffortXattrs: gdw.BestEffortXattrs}
  124. start := time.Now().UTC()
  125. log.G(context.TODO()).WithField("id", id).Debug("Start untar layer")
  126. if size, err = ApplyUncompressedLayer(layerFs, diff, options); err != nil {
  127. return
  128. }
  129. log.G(context.TODO()).WithField("id", id).Debugf("Untar time: %vs", time.Now().UTC().Sub(start).Seconds())
  130. return
  131. }
  132. // DiffSize calculates the changes between the specified layer
  133. // and its parent and returns the size in bytes of the changes
  134. // relative to its base filesystem directory.
  135. func (gdw *NaiveDiffDriver) DiffSize(id, parent string) (size int64, err error) {
  136. driver := gdw.ProtoDriver
  137. changes, err := gdw.Changes(id, parent)
  138. if err != nil {
  139. return
  140. }
  141. layerFs, err := driver.Get(id, "")
  142. if err != nil {
  143. return
  144. }
  145. defer driver.Put(id)
  146. return archive.ChangesSize(layerFs, changes), nil
  147. }