diff.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // The lower 8 bit is the lower 8 bit in the minor, the following 12 bits are the major,
  14. // and then there is the top 12 bits of then 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. tr := tar.NewReader(layer)
  34. var dirs []*tar.Header
  35. // Iterate through the files in the archive.
  36. for {
  37. hdr, err := tr.Next()
  38. if err == io.EOF {
  39. // end of tar archive
  40. break
  41. }
  42. if err != nil {
  43. return err
  44. }
  45. // Normalize name, for safety and for a simple is-root check
  46. hdr.Name = filepath.Clean(hdr.Name)
  47. if !strings.HasSuffix(hdr.Name, "/") {
  48. // Not the root directory, ensure that the parent directory exists
  49. // This happened in some tests where an image had a tarfile without any
  50. // parent directories
  51. parent := filepath.Dir(hdr.Name)
  52. parentPath := filepath.Join(dest, parent)
  53. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  54. err = os.MkdirAll(parentPath, 600)
  55. if err != nil {
  56. return err
  57. }
  58. }
  59. }
  60. // Skip AUFS metadata dirs
  61. if strings.HasPrefix(hdr.Name, ".wh..wh.") {
  62. continue
  63. }
  64. path := filepath.Join(dest, hdr.Name)
  65. base := filepath.Base(path)
  66. if strings.HasPrefix(base, ".wh.") {
  67. originalBase := base[len(".wh."):]
  68. originalPath := filepath.Join(filepath.Dir(path), originalBase)
  69. if err := os.RemoveAll(originalPath); err != nil {
  70. return err
  71. }
  72. } else {
  73. // If path exits we almost always just want to remove and replace it
  74. // The only exception is when it is a directory *and* the file from
  75. // the layer is also a directory. Then we want to merge them (i.e.
  76. // just apply the metadata from the layer).
  77. hasDir := false
  78. if fi, err := os.Lstat(path); err == nil {
  79. if fi.IsDir() && hdr.Typeflag == tar.TypeDir {
  80. hasDir = true
  81. } else {
  82. if err := os.RemoveAll(path); err != nil {
  83. return err
  84. }
  85. }
  86. }
  87. switch hdr.Typeflag {
  88. case tar.TypeDir:
  89. if !hasDir {
  90. err = os.Mkdir(path, os.FileMode(hdr.Mode))
  91. if err != nil {
  92. return err
  93. }
  94. }
  95. dirs = append(dirs, hdr)
  96. case tar.TypeReg, tar.TypeRegA:
  97. // Source is regular file
  98. file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.FileMode(hdr.Mode))
  99. if err != nil {
  100. return err
  101. }
  102. if _, err := io.Copy(file, tr); err != nil {
  103. file.Close()
  104. return err
  105. }
  106. file.Close()
  107. case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
  108. mode := uint32(hdr.Mode & 07777)
  109. switch hdr.Typeflag {
  110. case tar.TypeBlock:
  111. mode |= syscall.S_IFBLK
  112. case tar.TypeChar:
  113. mode |= syscall.S_IFCHR
  114. case tar.TypeFifo:
  115. mode |= syscall.S_IFIFO
  116. }
  117. if err := syscall.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
  118. return err
  119. }
  120. case tar.TypeLink:
  121. if err := os.Link(filepath.Join(dest, hdr.Linkname), path); err != nil {
  122. return err
  123. }
  124. case tar.TypeSymlink:
  125. if err := os.Symlink(hdr.Linkname, path); err != nil {
  126. return err
  127. }
  128. default:
  129. utils.Debugf("unhandled type %d\n", hdr.Typeflag)
  130. }
  131. if err = syscall.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
  132. return err
  133. }
  134. // There is no LChmod, so ignore mode for symlink. Also, this
  135. // must happen after chown, as that can modify the file mode
  136. if hdr.Typeflag != tar.TypeSymlink {
  137. err = syscall.Chmod(path, uint32(hdr.Mode&07777))
  138. if err != nil {
  139. return err
  140. }
  141. }
  142. // Directories must be handled at the end to avoid further
  143. // file creation in them to modify the mtime
  144. if hdr.Typeflag != tar.TypeDir {
  145. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  146. // syscall.UtimesNano doesn't support a NOFOLLOW flag atm, and
  147. if hdr.Typeflag != tar.TypeSymlink {
  148. if err := syscall.UtimesNano(path, ts); err != nil {
  149. return err
  150. }
  151. } else {
  152. if err := LUtimesNano(path, ts); err != nil {
  153. return err
  154. }
  155. }
  156. }
  157. }
  158. }
  159. for _, hdr := range dirs {
  160. path := filepath.Join(dest, hdr.Name)
  161. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  162. if err := syscall.UtimesNano(path, ts); err != nil {
  163. return err
  164. }
  165. }
  166. return nil
  167. }