changes.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package docker
  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. type FileInfo struct {
  32. parent *FileInfo
  33. name string
  34. stat syscall.Stat_t
  35. children map[string]*FileInfo
  36. }
  37. func (root *FileInfo) LookUp(path string) *FileInfo {
  38. parent := root
  39. if path == "/" {
  40. return root
  41. }
  42. pathElements := strings.Split(path, "/")
  43. for _, elem := range pathElements {
  44. if elem != "" {
  45. child := parent.children[elem]
  46. if child == nil {
  47. return nil
  48. }
  49. parent = child
  50. }
  51. }
  52. return parent
  53. }
  54. func (info *FileInfo) path() string {
  55. if info.parent == nil {
  56. return "/"
  57. }
  58. return filepath.Join(info.parent.path(), info.name)
  59. }
  60. func (info *FileInfo) unlink() {
  61. if info.parent != nil {
  62. delete(info.parent.children, info.name)
  63. }
  64. }
  65. func (info *FileInfo) Remove(path string) bool {
  66. child := info.LookUp(path)
  67. if child != nil {
  68. child.unlink()
  69. return true
  70. }
  71. return false
  72. }
  73. func (info *FileInfo) isDir() bool {
  74. return info.parent == nil || info.stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR
  75. }
  76. func (info *FileInfo) addChanges(oldInfo *FileInfo, changes *[]Change) {
  77. if oldInfo == nil {
  78. // add
  79. change := Change{
  80. Path: info.path(),
  81. Kind: ChangeAdd,
  82. }
  83. *changes = append(*changes, change)
  84. }
  85. // We make a copy so we can modify it to detect additions
  86. // also, we only recurse on the old dir if the new info is a directory
  87. // otherwise any previous delete/change is considered recursive
  88. oldChildren := make(map[string]*FileInfo)
  89. if oldInfo != nil && info.isDir() {
  90. for k, v := range oldInfo.children {
  91. oldChildren[k] = v
  92. }
  93. }
  94. for name, newChild := range info.children {
  95. oldChild, _ := oldChildren[name]
  96. if oldChild != nil {
  97. // change?
  98. oldStat := &oldChild.stat
  99. newStat := &newChild.stat
  100. // Note: We can't compare inode or ctime or blocksize here, because these change
  101. // when copying a file into a container. However, that is not generally a problem
  102. // because any content change will change mtime, and any status change should
  103. // be visible when actually comparing the stat fields. The only time this
  104. // breaks down is if some code intentionally hides a change by setting
  105. // back mtime
  106. oldMtime := syscall.NsecToTimeval(oldStat.Mtim.Nano())
  107. newMtime := syscall.NsecToTimeval(oldStat.Mtim.Nano())
  108. if oldStat.Mode != newStat.Mode ||
  109. oldStat.Uid != newStat.Uid ||
  110. oldStat.Gid != newStat.Gid ||
  111. oldStat.Rdev != newStat.Rdev ||
  112. // Don't look at size for dirs, its not a good measure of change
  113. (oldStat.Size != newStat.Size && oldStat.Mode&syscall.S_IFDIR != syscall.S_IFDIR) ||
  114. oldMtime.Sec != newMtime.Sec ||
  115. oldMtime.Usec != newMtime.Usec {
  116. change := Change{
  117. Path: newChild.path(),
  118. Kind: ChangeModify,
  119. }
  120. *changes = append(*changes, change)
  121. }
  122. // Remove from copy so we can detect deletions
  123. delete(oldChildren, name)
  124. }
  125. newChild.addChanges(oldChild, changes)
  126. }
  127. for _, oldChild := range oldChildren {
  128. // delete
  129. change := Change{
  130. Path: oldChild.path(),
  131. Kind: ChangeDelete,
  132. }
  133. *changes = append(*changes, change)
  134. }
  135. }
  136. func (info *FileInfo) Changes(oldInfo *FileInfo) []Change {
  137. var changes []Change
  138. info.addChanges(oldInfo, &changes)
  139. return changes
  140. }
  141. func newRootFileInfo() *FileInfo {
  142. root := &FileInfo{
  143. name: "/",
  144. children: make(map[string]*FileInfo),
  145. }
  146. return root
  147. }
  148. func applyLayer(root *FileInfo, layer string) error {
  149. err := filepath.Walk(layer, func(layerPath string, f os.FileInfo, err error) error {
  150. if err != nil {
  151. return err
  152. }
  153. // Skip root
  154. if layerPath == layer {
  155. return nil
  156. }
  157. // rebase path
  158. relPath, err := filepath.Rel(layer, layerPath)
  159. if err != nil {
  160. return err
  161. }
  162. relPath = filepath.Join("/", relPath)
  163. // Skip AUFS metadata
  164. if matched, err := filepath.Match("/.wh..wh.*", relPath); err != nil || matched {
  165. if err != nil || !f.IsDir() {
  166. return err
  167. }
  168. return filepath.SkipDir
  169. }
  170. var layerStat syscall.Stat_t
  171. err = syscall.Lstat(layerPath, &layerStat)
  172. if err != nil {
  173. return err
  174. }
  175. file := filepath.Base(relPath)
  176. // If there is a whiteout, then the file was removed
  177. if strings.HasPrefix(file, ".wh.") {
  178. originalFile := file[len(".wh."):]
  179. deletePath := filepath.Join(filepath.Dir(relPath), originalFile)
  180. root.Remove(deletePath)
  181. } else {
  182. // Added or changed file
  183. existing := root.LookUp(relPath)
  184. if existing != nil {
  185. // Changed file
  186. existing.stat = layerStat
  187. if !existing.isDir() {
  188. // Changed from dir to non-dir, delete all previous files
  189. existing.children = make(map[string]*FileInfo)
  190. }
  191. } else {
  192. // Added file
  193. parent := root.LookUp(filepath.Dir(relPath))
  194. if parent == nil {
  195. return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
  196. }
  197. info := &FileInfo{
  198. name: filepath.Base(relPath),
  199. children: make(map[string]*FileInfo),
  200. parent: parent,
  201. stat: layerStat,
  202. }
  203. parent.children[info.name] = info
  204. }
  205. }
  206. return nil
  207. })
  208. return err
  209. }
  210. func collectFileInfo(sourceDir string) (*FileInfo, error) {
  211. root := newRootFileInfo()
  212. err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error {
  213. if err != nil {
  214. return err
  215. }
  216. // Rebase path
  217. relPath, err := filepath.Rel(sourceDir, path)
  218. if err != nil {
  219. return err
  220. }
  221. relPath = filepath.Join("/", relPath)
  222. if relPath == "/" {
  223. return nil
  224. }
  225. parent := root.LookUp(filepath.Dir(relPath))
  226. if parent == nil {
  227. return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
  228. }
  229. info := &FileInfo{
  230. name: filepath.Base(relPath),
  231. children: make(map[string]*FileInfo),
  232. parent: parent,
  233. }
  234. if err := syscall.Lstat(path, &info.stat); err != nil {
  235. return err
  236. }
  237. parent.children[info.name] = info
  238. return nil
  239. })
  240. if err != nil {
  241. return nil, err
  242. }
  243. return root, nil
  244. }
  245. func ChangesLayers(newDir string, layers []string) ([]Change, error) {
  246. newRoot, err := collectFileInfo(newDir)
  247. if err != nil {
  248. return nil, err
  249. }
  250. oldRoot := newRootFileInfo()
  251. for i := len(layers) - 1; i >= 0; i-- {
  252. layer := layers[i]
  253. if err = applyLayer(oldRoot, layer); err != nil {
  254. return nil, err
  255. }
  256. }
  257. return newRoot.Changes(oldRoot), nil
  258. }
  259. func ChangesDirs(newDir, oldDir string) ([]Change, error) {
  260. oldRoot, err := collectFileInfo(oldDir)
  261. if err != nil {
  262. return nil, err
  263. }
  264. newRoot, err := collectFileInfo(newDir)
  265. if err != nil {
  266. return nil, err
  267. }
  268. // Ignore changes in .docker-id
  269. _ = newRoot.Remove("/.docker-id")
  270. _ = oldRoot.Remove("/.docker-id")
  271. return newRoot.Changes(oldRoot), nil
  272. }