changes.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. // Note: We can't compare inode or ctime or blocksize here, because these change
  164. // when copying a file into a container. However, that is not generally a problem
  165. // because any content change will change mtime, and any status change should
  166. // be visible when actually comparing the stat fields. The only time this
  167. // breaks down is if some code intentionally hides a change by setting
  168. // back mtime
  169. oldMtime := syscall.NsecToTimeval(oldStat.Mtim.Nano())
  170. newMtime := syscall.NsecToTimeval(oldStat.Mtim.Nano())
  171. if oldStat.Mode != newStat.Mode ||
  172. oldStat.Uid != newStat.Uid ||
  173. oldStat.Gid != newStat.Gid ||
  174. oldStat.Rdev != newStat.Rdev ||
  175. // Don't look at size for dirs, its not a good measure of change
  176. (oldStat.Size != newStat.Size && oldStat.Mode &syscall.S_IFDIR != syscall.S_IFDIR) ||
  177. oldMtime.Sec != newMtime.Sec ||
  178. oldMtime.Usec != newMtime.Usec {
  179. change := Change{
  180. Path: newChild.path(),
  181. Kind: ChangeModify,
  182. }
  183. *changes = append(*changes, change)
  184. }
  185. // Remove from copy so we can detect deletions
  186. delete(oldChildren, name)
  187. }
  188. newChild.addChanges(oldChild, changes)
  189. }
  190. for _, oldChild := range oldChildren {
  191. // delete
  192. change := Change{
  193. Path: oldChild.path(),
  194. Kind: ChangeDelete,
  195. }
  196. *changes = append(*changes, change)
  197. }
  198. }
  199. func (info *FileInfo)Changes(oldInfo *FileInfo) []Change {
  200. var changes []Change
  201. info.addChanges(oldInfo, &changes)
  202. return changes
  203. }
  204. func newRootFileInfo() *FileInfo {
  205. root := &FileInfo {
  206. name: "/",
  207. children: make(map[string]*FileInfo),
  208. }
  209. return root
  210. }
  211. func applyLayer(root *FileInfo, layer string) error {
  212. err := filepath.Walk(layer, func(layerPath string, f os.FileInfo, err error) error {
  213. if err != nil {
  214. return err
  215. }
  216. // Skip root
  217. if layerPath == layer {
  218. return nil
  219. }
  220. // rebase path
  221. relPath, err := filepath.Rel(layer, layerPath)
  222. if err != nil {
  223. return err
  224. }
  225. relPath = filepath.Join("/", relPath)
  226. // Skip AUFS metadata
  227. if matched, err := filepath.Match("/.wh..wh.*", relPath); err != nil || matched {
  228. if err != nil || !f.IsDir() {
  229. return err
  230. }
  231. return filepath.SkipDir
  232. }
  233. var layerStat syscall.Stat_t
  234. err = syscall.Lstat(layerPath, &layerStat)
  235. if err != nil {
  236. return err
  237. }
  238. file := filepath.Base(relPath)
  239. // If there is a whiteout, then the file was removed
  240. if strings.HasPrefix(file, ".wh.") {
  241. originalFile := file[len(".wh."):]
  242. deletePath := filepath.Join(filepath.Dir(relPath), originalFile)
  243. root.Remove(deletePath)
  244. } else {
  245. // Added or changed file
  246. existing := root.LookUp(relPath)
  247. if existing != nil {
  248. // Changed file
  249. existing.stat = layerStat
  250. if !existing.isDir() {
  251. // Changed from dir to non-dir, delete all previous files
  252. existing.children = make(map[string]*FileInfo)
  253. }
  254. } else {
  255. // Added file
  256. parent := root.LookUp(filepath.Dir(relPath))
  257. if parent == nil {
  258. return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
  259. }
  260. info := &FileInfo {
  261. name: filepath.Base(relPath),
  262. children: make(map[string]*FileInfo),
  263. parent: parent,
  264. stat: layerStat,
  265. }
  266. parent.children[info.name] = info
  267. }
  268. }
  269. return nil
  270. })
  271. return err
  272. }
  273. func collectFileInfo(sourceDir string) (*FileInfo, error) {
  274. root := newRootFileInfo()
  275. err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error {
  276. if err != nil {
  277. return err
  278. }
  279. // Rebase path
  280. relPath, err := filepath.Rel(sourceDir, path)
  281. if err != nil {
  282. return err
  283. }
  284. relPath = filepath.Join("/", relPath)
  285. if relPath == "/" {
  286. return nil
  287. }
  288. parent := root.LookUp(filepath.Dir(relPath))
  289. if parent == nil {
  290. return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
  291. }
  292. info := &FileInfo {
  293. name: filepath.Base(relPath),
  294. children: make(map[string]*FileInfo),
  295. parent: parent,
  296. }
  297. if err := syscall.Lstat(path, &info.stat); err != nil {
  298. return err
  299. }
  300. parent.children[info.name] = info
  301. return nil
  302. })
  303. if err != nil {
  304. return nil, err
  305. }
  306. return root, nil
  307. }
  308. func ChangesLayers(newDir string, layers []string) ([]Change, error) {
  309. newRoot, err := collectFileInfo(newDir)
  310. if err != nil {
  311. return nil, err
  312. }
  313. oldRoot := newRootFileInfo()
  314. for i := len(layers)-1; i >= 0; i-- {
  315. layer := layers[i]
  316. if err = applyLayer(oldRoot, layer); err != nil {
  317. return nil, err
  318. }
  319. }
  320. return newRoot.Changes(oldRoot), nil
  321. }
  322. func ChangesDirs(newDir, oldDir string) ([]Change, error) {
  323. oldRoot, err := collectFileInfo(oldDir)
  324. if err != nil {
  325. return nil, err
  326. }
  327. newRoot, err := collectFileInfo(newDir)
  328. if err != nil {
  329. return nil, err
  330. }
  331. // Ignore changes in .docker-id
  332. _ = newRoot.Remove("/.docker-id")
  333. _ = oldRoot.Remove("/.docker-id")
  334. return newRoot.Changes(oldRoot), nil
  335. }