fsdiff.go 4.7 KB

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