changes.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package archive
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "syscall"
  8. )
  9. type ChangeType int
  10. const (
  11. ChangeModify = iota
  12. ChangeAdd
  13. ChangeDelete
  14. )
  15. type Change struct {
  16. Path string
  17. Kind ChangeType
  18. }
  19. func (change *Change) String() string {
  20. var kind string
  21. switch change.Kind {
  22. case ChangeModify:
  23. kind = "C"
  24. case ChangeAdd:
  25. kind = "A"
  26. case ChangeDelete:
  27. kind = "D"
  28. }
  29. return fmt.Sprintf("%s %s", kind, change.Path)
  30. }
  31. func Changes(layers []string, rw string) ([]Change, error) {
  32. var changes []Change
  33. err := filepath.Walk(rw, func(path string, f os.FileInfo, err error) error {
  34. if err != nil {
  35. return err
  36. }
  37. // Rebase path
  38. path, err = filepath.Rel(rw, path)
  39. if err != nil {
  40. return err
  41. }
  42. path = filepath.Join("/", path)
  43. // Skip root
  44. if path == "/" {
  45. return nil
  46. }
  47. // Skip AUFS metadata
  48. if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
  49. return err
  50. }
  51. change := Change{
  52. Path: path,
  53. }
  54. // Find out what kind of modification happened
  55. file := filepath.Base(path)
  56. // If there is a whiteout, then the file was removed
  57. if strings.HasPrefix(file, ".wh.") {
  58. originalFile := file[len(".wh."):]
  59. change.Path = filepath.Join(filepath.Dir(path), originalFile)
  60. change.Kind = ChangeDelete
  61. } else {
  62. // Otherwise, the file was added
  63. change.Kind = ChangeAdd
  64. // ...Unless it already existed in a top layer, in which case, it's a modification
  65. for _, layer := range layers {
  66. stat, err := os.Stat(filepath.Join(layer, path))
  67. if err != nil && !os.IsNotExist(err) {
  68. return err
  69. }
  70. if err == nil {
  71. // The file existed in the top layer, so that's a modification
  72. // However, if it's a directory, maybe it wasn't actually modified.
  73. // If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
  74. if stat.IsDir() && f.IsDir() {
  75. if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() {
  76. // Both directories are the same, don't record the change
  77. return nil
  78. }
  79. }
  80. change.Kind = ChangeModify
  81. break
  82. }
  83. }
  84. }
  85. // Record change
  86. changes = append(changes, change)
  87. return nil
  88. })
  89. if err != nil && !os.IsNotExist(err) {
  90. return nil, err
  91. }
  92. return changes, nil
  93. }
  94. func ChangesDirs(newDir, oldDir string) ([]Change, error) {
  95. var changes []Change
  96. err := filepath.Walk(newDir, func(newPath string, f os.FileInfo, err error) error {
  97. if err != nil {
  98. return err
  99. }
  100. var newStat syscall.Stat_t
  101. err = syscall.Lstat(newPath, &newStat)
  102. if err != nil {
  103. return err
  104. }
  105. // Rebase path
  106. relPath, err := filepath.Rel(newDir, newPath)
  107. if err != nil {
  108. return err
  109. }
  110. relPath = filepath.Join("/", relPath)
  111. // Skip root
  112. if relPath == "/" || relPath == "/.docker-id" {
  113. return nil
  114. }
  115. change := Change{
  116. Path: relPath,
  117. }
  118. oldPath := filepath.Join(oldDir, relPath)
  119. var oldStat = &syscall.Stat_t{}
  120. err = syscall.Lstat(oldPath, oldStat)
  121. if err != nil {
  122. if !os.IsNotExist(err) {
  123. return err
  124. }
  125. oldStat = nil
  126. }
  127. if oldStat == nil {
  128. change.Kind = ChangeAdd
  129. changes = append(changes, change)
  130. } else {
  131. if oldStat.Ino != newStat.Ino ||
  132. oldStat.Mode != newStat.Mode ||
  133. oldStat.Uid != newStat.Uid ||
  134. oldStat.Gid != newStat.Gid ||
  135. oldStat.Rdev != newStat.Rdev ||
  136. oldStat.Size != newStat.Size ||
  137. oldStat.Blocks != newStat.Blocks ||
  138. oldStat.Mtim != newStat.Mtim ||
  139. oldStat.Ctim != newStat.Ctim {
  140. change.Kind = ChangeModify
  141. changes = append(changes, change)
  142. }
  143. }
  144. return nil
  145. })
  146. if err != nil {
  147. return nil, err
  148. }
  149. err = filepath.Walk(oldDir, func(oldPath string, f os.FileInfo, err error) error {
  150. if err != nil {
  151. return err
  152. }
  153. // Rebase path
  154. relPath, err := filepath.Rel(oldDir, oldPath)
  155. if err != nil {
  156. return err
  157. }
  158. relPath = filepath.Join("/", relPath)
  159. // Skip root
  160. if relPath == "/" {
  161. return nil
  162. }
  163. change := Change{
  164. Path: relPath,
  165. }
  166. newPath := filepath.Join(newDir, relPath)
  167. var newStat = &syscall.Stat_t{}
  168. err = syscall.Lstat(newPath, newStat)
  169. if err != nil && os.IsNotExist(err) {
  170. change.Kind = ChangeDelete
  171. changes = append(changes, change)
  172. }
  173. return nil
  174. })
  175. if err != nil {
  176. return nil, err
  177. }
  178. return changes, nil
  179. }
  180. func ExportChanges(root, rw string) (Archive, error) {
  181. changes, err := ChangesDirs(root, rw)
  182. if err != nil {
  183. return nil, err
  184. }
  185. files := make([]string, 0)
  186. deletions := make([]string, 0)
  187. for _, change := range changes {
  188. if change.Kind == ChangeModify || change.Kind == ChangeAdd {
  189. files = append(files, change.Path)
  190. }
  191. if change.Kind == ChangeDelete {
  192. base := filepath.Base(change.Path)
  193. dir := filepath.Dir(change.Path)
  194. deletions = append(deletions, filepath.Join(dir, ".wh."+base))
  195. }
  196. }
  197. return TarFilter(root, Uncompressed, files, false, deletions)
  198. }