diff.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package archive
  2. import (
  3. "archive/tar"
  4. "github.com/dotcloud/docker/utils"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "syscall"
  10. "time"
  11. )
  12. // Linux device nodes are a bit weird due to backwards compat with 16 bit device nodes.
  13. // They are, from low to high: the lower 8 bits of the minor, then 12 bits of the major,
  14. // then the top 12 bits of the minor
  15. func mkdev(major int64, minor int64) uint32 {
  16. return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
  17. }
  18. func timeToTimespec(time time.Time) (ts syscall.Timespec) {
  19. if time.IsZero() {
  20. // Return UTIME_OMIT special value
  21. ts.Sec = 0
  22. ts.Nsec = ((1 << 30) - 2)
  23. return
  24. }
  25. return syscall.NsecToTimespec(time.UnixNano())
  26. }
  27. // ApplyLayer parses a diff in the standard layer format from `layer`, and
  28. // applies it to the directory `dest`.
  29. func ApplyLayer(dest string, layer Archive) error {
  30. // We need to be able to set any perms
  31. oldmask := syscall.Umask(0)
  32. defer syscall.Umask(oldmask)
  33. layer, err := DecompressStream(layer)
  34. if err != nil {
  35. return err
  36. }
  37. tr := tar.NewReader(layer)
  38. var dirs []*tar.Header
  39. // Iterate through the files in the archive.
  40. for {
  41. hdr, err := tr.Next()
  42. if err == io.EOF {
  43. // end of tar archive
  44. break
  45. }
  46. if err != nil {
  47. return err
  48. }
  49. // Normalize name, for safety and for a simple is-root check
  50. hdr.Name = filepath.Clean(hdr.Name)
  51. if !strings.HasSuffix(hdr.Name, "/") {
  52. // Not the root directory, ensure that the parent directory exists.
  53. // This happened in some tests where an image had a tarfile without any
  54. // parent directories.
  55. parent := filepath.Dir(hdr.Name)
  56. parentPath := filepath.Join(dest, parent)
  57. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  58. err = os.MkdirAll(parentPath, 600)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. }
  64. // Skip AUFS metadata dirs
  65. if strings.HasPrefix(hdr.Name, ".wh..wh.") {
  66. continue
  67. }
  68. path := filepath.Join(dest, hdr.Name)
  69. base := filepath.Base(path)
  70. if strings.HasPrefix(base, ".wh.") {
  71. originalBase := base[len(".wh."):]
  72. originalPath := filepath.Join(filepath.Dir(path), originalBase)
  73. if err := os.RemoveAll(originalPath); err != nil {
  74. return err
  75. }
  76. } else {
  77. // If path exits we almost always just want to remove and replace it.
  78. // The only exception is when it is a directory *and* the file from
  79. // the layer is also a directory. Then we want to merge them (i.e.
  80. // just apply the metadata from the layer).
  81. hasDir := false
  82. if fi, err := os.Lstat(path); err == nil {
  83. if fi.IsDir() && hdr.Typeflag == tar.TypeDir {
  84. hasDir = true
  85. } else {
  86. if err := os.RemoveAll(path); err != nil {
  87. return err
  88. }
  89. }
  90. }
  91. switch hdr.Typeflag {
  92. case tar.TypeDir:
  93. if !hasDir {
  94. err = os.Mkdir(path, os.FileMode(hdr.Mode))
  95. if err != nil {
  96. return err
  97. }
  98. }
  99. dirs = append(dirs, hdr)
  100. case tar.TypeReg, tar.TypeRegA:
  101. // Source is regular file
  102. file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.FileMode(hdr.Mode))
  103. if err != nil {
  104. return err
  105. }
  106. if _, err := io.Copy(file, tr); err != nil {
  107. file.Close()
  108. return err
  109. }
  110. file.Close()
  111. case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
  112. mode := uint32(hdr.Mode & 07777)
  113. switch hdr.Typeflag {
  114. case tar.TypeBlock:
  115. mode |= syscall.S_IFBLK
  116. case tar.TypeChar:
  117. mode |= syscall.S_IFCHR
  118. case tar.TypeFifo:
  119. mode |= syscall.S_IFIFO
  120. }
  121. if err := syscall.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
  122. return err
  123. }
  124. case tar.TypeLink:
  125. if err := os.Link(filepath.Join(dest, hdr.Linkname), path); err != nil {
  126. return err
  127. }
  128. case tar.TypeSymlink:
  129. if err := os.Symlink(hdr.Linkname, path); err != nil {
  130. return err
  131. }
  132. default:
  133. utils.Debugf("unhandled type %d\n", hdr.Typeflag)
  134. }
  135. if err = syscall.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
  136. return err
  137. }
  138. // There is no LChmod, so ignore mode for symlink. Also, this
  139. // must happen after chown, as that can modify the file mode
  140. if hdr.Typeflag != tar.TypeSymlink {
  141. err = syscall.Chmod(path, uint32(hdr.Mode&07777))
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. // Directories must be handled at the end to avoid further
  147. // file creation in them to modify the mtime
  148. if hdr.Typeflag != tar.TypeDir {
  149. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  150. // syscall.UtimesNano doesn't support a NOFOLLOW flag atm, and
  151. if hdr.Typeflag != tar.TypeSymlink {
  152. if err := syscall.UtimesNano(path, ts); err != nil {
  153. return err
  154. }
  155. } else {
  156. if err := LUtimesNano(path, ts); err != nil {
  157. return err
  158. }
  159. }
  160. }
  161. }
  162. }
  163. for _, hdr := range dirs {
  164. path := filepath.Join(dest, hdr.Name)
  165. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  166. if err := syscall.UtimesNano(path, ts); err != nil {
  167. return err
  168. }
  169. }
  170. return nil
  171. }