changes.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. func ChangesAUFS(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. type FileInfo struct {
  95. parent *FileInfo
  96. name string
  97. stat syscall.Stat_t
  98. children map[string]*FileInfo
  99. }
  100. func (root *FileInfo) LookUp(path string) *FileInfo {
  101. parent := root
  102. if path == "/" {
  103. return root
  104. }
  105. pathElements := strings.Split(path, "/")
  106. for _, elem := range pathElements {
  107. if elem != "" {
  108. child := parent.children[elem]
  109. if child == nil {
  110. return nil
  111. }
  112. parent = child
  113. }
  114. }
  115. return parent
  116. }
  117. func (info *FileInfo)path() string {
  118. if info.parent == nil {
  119. return "/"
  120. }
  121. return filepath.Join(info.parent.path(), info.name)
  122. }
  123. func (info *FileInfo)unlink() {
  124. if info.parent != nil {
  125. delete(info.parent.children, info.name)
  126. }
  127. }
  128. func (info *FileInfo)Remove(path string) bool {
  129. child := info.LookUp(path)
  130. if child != nil {
  131. child.unlink()
  132. return true
  133. }
  134. return false
  135. }
  136. func (info *FileInfo)isDir() bool {
  137. return info.parent == nil || info.stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR
  138. }
  139. func (info *FileInfo)addChanges(oldInfo *FileInfo, changes *[]Change) {
  140. if oldInfo == nil {
  141. // add
  142. change := Change{
  143. Path: info.path(),
  144. Kind: ChangeAdd,
  145. }
  146. *changes = append(*changes, change)
  147. }
  148. // We make a copy so we can modify it to detect additions
  149. // also, we only recurse on the old dir if the new info is a directory
  150. // otherwise any previous delete/change is considered recursive
  151. oldChildren := make(map[string]*FileInfo)
  152. if oldInfo != nil && info.isDir() {
  153. for k, v := range oldInfo.children {
  154. oldChildren[k] = v
  155. }
  156. }
  157. for name, newChild := range info.children {
  158. oldChild, _ := oldChildren[name]
  159. if oldChild != nil {
  160. // change?
  161. oldStat := &oldChild.stat
  162. newStat := &newChild.stat
  163. if oldStat.Ino != newStat.Ino ||
  164. oldStat.Mode != newStat.Mode ||
  165. oldStat.Uid != newStat.Uid ||
  166. oldStat.Gid != newStat.Gid ||
  167. oldStat.Rdev != newStat.Rdev ||
  168. oldStat.Size != newStat.Size ||
  169. oldStat.Blocks != newStat.Blocks ||
  170. oldStat.Mtim != newStat.Mtim ||
  171. oldStat.Ctim != newStat.Ctim {
  172. change := Change{
  173. Path: newChild.path(),
  174. Kind: ChangeModify,
  175. }
  176. *changes = append(*changes, change)
  177. }
  178. // Remove from copy so we can detect deletions
  179. delete(oldChildren, name)
  180. }
  181. newChild.addChanges(oldChild, changes)
  182. }
  183. for _, oldChild := range oldChildren {
  184. // delete
  185. change := Change{
  186. Path: oldChild.path(),
  187. Kind: ChangeDelete,
  188. }
  189. *changes = append(*changes, change)
  190. }
  191. }
  192. func (info *FileInfo)Changes(oldInfo *FileInfo) []Change {
  193. var changes []Change
  194. info.addChanges(oldInfo, &changes)
  195. return changes
  196. }
  197. func collectFileInfo(sourceDir string) (*FileInfo, error) {
  198. root := &FileInfo {
  199. name: "/",
  200. children: make(map[string]*FileInfo),
  201. }
  202. err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error {
  203. if err != nil {
  204. return err
  205. }
  206. // Rebase path
  207. relPath, err := filepath.Rel(sourceDir, path)
  208. if err != nil {
  209. return err
  210. }
  211. relPath = filepath.Join("/", relPath)
  212. if relPath == "/" {
  213. return nil
  214. }
  215. parent := root.LookUp(filepath.Dir(relPath))
  216. if parent == nil {
  217. return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
  218. }
  219. info := &FileInfo {
  220. name: filepath.Base(relPath),
  221. children: make(map[string]*FileInfo),
  222. parent: parent,
  223. }
  224. if err := syscall.Lstat(path, &info.stat); err != nil {
  225. return err
  226. }
  227. parent.children[info.name] = info
  228. return nil
  229. })
  230. if err != nil {
  231. return nil, err
  232. }
  233. return root, nil
  234. }
  235. func ChangesDirs(newDir, oldDir string) ([]Change, error) {
  236. oldRoot, err := collectFileInfo(oldDir)
  237. if err != nil {
  238. return nil, err
  239. }
  240. newRoot, err := collectFileInfo(newDir)
  241. if err != nil {
  242. return nil, err
  243. }
  244. // Ignore changes in .docker-id
  245. _ = newRoot.Remove("/.docker-id")
  246. _ = oldRoot.Remove("/.docker-id")
  247. return newRoot.Changes(oldRoot), nil
  248. }