changes.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. 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) isDir() bool {
  124. return info.parent == nil || info.stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR
  125. }
  126. func (info *FileInfo) addChanges(oldInfo *FileInfo, changes *[]Change) {
  127. if oldInfo == nil {
  128. // add
  129. change := Change{
  130. Path: info.path(),
  131. Kind: ChangeAdd,
  132. }
  133. *changes = append(*changes, change)
  134. }
  135. // We make a copy so we can modify it to detect additions
  136. // also, we only recurse on the old dir if the new info is a directory
  137. // otherwise any previous delete/change is considered recursive
  138. oldChildren := make(map[string]*FileInfo)
  139. if oldInfo != nil && info.isDir() {
  140. for k, v := range oldInfo.children {
  141. oldChildren[k] = v
  142. }
  143. }
  144. for name, newChild := range info.children {
  145. oldChild, _ := oldChildren[name]
  146. if oldChild != nil {
  147. // change?
  148. oldStat := &oldChild.stat
  149. newStat := &newChild.stat
  150. // Note: We can't compare inode or ctime or blocksize here, because these change
  151. // when copying a file into a container. However, that is not generally a problem
  152. // because any content change will change mtime, and any status change should
  153. // be visible when actually comparing the stat fields. The only time this
  154. // breaks down is if some code intentionally hides a change by setting
  155. // back mtime
  156. if oldStat.Mode != newStat.Mode ||
  157. oldStat.Uid != newStat.Uid ||
  158. oldStat.Gid != newStat.Gid ||
  159. oldStat.Rdev != newStat.Rdev ||
  160. // Don't look at size for dirs, its not a good measure of change
  161. (oldStat.Size != newStat.Size && oldStat.Mode&syscall.S_IFDIR != syscall.S_IFDIR) ||
  162. oldStat.Mtim != newStat.Mtim {
  163. change := Change{
  164. Path: newChild.path(),
  165. Kind: ChangeModify,
  166. }
  167. *changes = append(*changes, change)
  168. }
  169. // Remove from copy so we can detect deletions
  170. delete(oldChildren, name)
  171. }
  172. newChild.addChanges(oldChild, changes)
  173. }
  174. for _, oldChild := range oldChildren {
  175. // delete
  176. change := Change{
  177. Path: oldChild.path(),
  178. Kind: ChangeDelete,
  179. }
  180. *changes = append(*changes, change)
  181. }
  182. }
  183. func (info *FileInfo) Changes(oldInfo *FileInfo) []Change {
  184. var changes []Change
  185. info.addChanges(oldInfo, &changes)
  186. return changes
  187. }
  188. func newRootFileInfo() *FileInfo {
  189. root := &FileInfo{
  190. name: "/",
  191. children: make(map[string]*FileInfo),
  192. }
  193. return root
  194. }
  195. func collectFileInfo(sourceDir string) (*FileInfo, error) {
  196. root := newRootFileInfo()
  197. err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error {
  198. if err != nil {
  199. return err
  200. }
  201. // Rebase path
  202. relPath, err := filepath.Rel(sourceDir, path)
  203. if err != nil {
  204. return err
  205. }
  206. relPath = filepath.Join("/", relPath)
  207. if relPath == "/" {
  208. return nil
  209. }
  210. parent := root.LookUp(filepath.Dir(relPath))
  211. if parent == nil {
  212. return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
  213. }
  214. info := &FileInfo{
  215. name: filepath.Base(relPath),
  216. children: make(map[string]*FileInfo),
  217. parent: parent,
  218. }
  219. if err := syscall.Lstat(path, &info.stat); err != nil {
  220. return err
  221. }
  222. parent.children[info.name] = info
  223. return nil
  224. })
  225. if err != nil {
  226. return nil, err
  227. }
  228. return root, nil
  229. }
  230. // Compare two directories and generate an array of Change objects describing the changes
  231. func ChangesDirs(newDir, oldDir string) ([]Change, error) {
  232. oldRoot, err := collectFileInfo(oldDir)
  233. if err != nil {
  234. return nil, err
  235. }
  236. newRoot, err := collectFileInfo(newDir)
  237. if err != nil {
  238. return nil, err
  239. }
  240. return newRoot.Changes(oldRoot), nil
  241. }
  242. func ExportChanges(root, rw string) (Archive, error) {
  243. changes, err := ChangesDirs(root, rw)
  244. if err != nil {
  245. return nil, err
  246. }
  247. files := make([]string, 0)
  248. deletions := make([]string, 0)
  249. for _, change := range changes {
  250. if change.Kind == ChangeModify || change.Kind == ChangeAdd {
  251. files = append(files, change.Path)
  252. }
  253. if change.Kind == ChangeDelete {
  254. base := filepath.Base(change.Path)
  255. dir := filepath.Dir(change.Path)
  256. deletions = append(deletions, filepath.Join(dir, ".wh."+base))
  257. }
  258. }
  259. return TarFilter(root, &TarOptions{Compression: Uncompressed, Recursive: false, Includes: files, CreateFiles: deletions})
  260. }