changes.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package archive
  2. import (
  3. "code.google.com/p/go/src/pkg/archive/tar"
  4. "fmt"
  5. "github.com/dotcloud/docker/utils"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "syscall"
  11. "time"
  12. )
  13. type ChangeType int
  14. const (
  15. ChangeModify = iota
  16. ChangeAdd
  17. ChangeDelete
  18. )
  19. type Change struct {
  20. Path string
  21. Kind ChangeType
  22. }
  23. func (change *Change) String() string {
  24. var kind string
  25. switch change.Kind {
  26. case ChangeModify:
  27. kind = "C"
  28. case ChangeAdd:
  29. kind = "A"
  30. case ChangeDelete:
  31. kind = "D"
  32. }
  33. return fmt.Sprintf("%s %s", kind, change.Path)
  34. }
  35. // Gnu tar and the go tar writer don't have sub-second mtime
  36. // precision, which is problematic when we apply changes via tar
  37. // files, we handle this by comparing for exact times, *or* same
  38. // second count and either a or b having exactly 0 nanoseconds
  39. func sameFsTime(a, b time.Time) bool {
  40. return a == b ||
  41. (a.Unix() == b.Unix() &&
  42. (a.Nanosecond() == 0 || b.Nanosecond() == 0))
  43. }
  44. func sameFsTimeSpec(a, b syscall.Timespec) bool {
  45. return a.Sec == b.Sec &&
  46. (a.Nsec == b.Nsec || a.Nsec == 0 || b.Nsec == 0)
  47. }
  48. func Changes(layers []string, rw string) ([]Change, error) {
  49. var changes []Change
  50. err := filepath.Walk(rw, func(path string, f os.FileInfo, err error) error {
  51. if err != nil {
  52. return err
  53. }
  54. // Rebase path
  55. path, err = filepath.Rel(rw, path)
  56. if err != nil {
  57. return err
  58. }
  59. path = filepath.Join("/", path)
  60. // Skip root
  61. if path == "/" {
  62. return nil
  63. }
  64. // Skip AUFS metadata
  65. if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
  66. return err
  67. }
  68. change := Change{
  69. Path: path,
  70. }
  71. // Find out what kind of modification happened
  72. file := filepath.Base(path)
  73. // If there is a whiteout, then the file was removed
  74. if strings.HasPrefix(file, ".wh.") {
  75. originalFile := file[len(".wh."):]
  76. change.Path = filepath.Join(filepath.Dir(path), originalFile)
  77. change.Kind = ChangeDelete
  78. } else {
  79. // Otherwise, the file was added
  80. change.Kind = ChangeAdd
  81. // ...Unless it already existed in a top layer, in which case, it's a modification
  82. for _, layer := range layers {
  83. stat, err := os.Stat(filepath.Join(layer, path))
  84. if err != nil && !os.IsNotExist(err) {
  85. return err
  86. }
  87. if err == nil {
  88. // The file existed in the top layer, so that's a modification
  89. // However, if it's a directory, maybe it wasn't actually modified.
  90. // If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
  91. if stat.IsDir() && f.IsDir() {
  92. if f.Size() == stat.Size() && f.Mode() == stat.Mode() && sameFsTime(f.ModTime(), stat.ModTime()) {
  93. // Both directories are the same, don't record the change
  94. return nil
  95. }
  96. }
  97. change.Kind = ChangeModify
  98. break
  99. }
  100. }
  101. }
  102. // Record change
  103. changes = append(changes, change)
  104. return nil
  105. })
  106. if err != nil && !os.IsNotExist(err) {
  107. return nil, err
  108. }
  109. return changes, nil
  110. }
  111. type FileInfo struct {
  112. parent *FileInfo
  113. name string
  114. stat syscall.Stat_t
  115. children map[string]*FileInfo
  116. }
  117. func (root *FileInfo) LookUp(path string) *FileInfo {
  118. parent := root
  119. if path == "/" {
  120. return root
  121. }
  122. pathElements := strings.Split(path, "/")
  123. for _, elem := range pathElements {
  124. if elem != "" {
  125. child := parent.children[elem]
  126. if child == nil {
  127. return nil
  128. }
  129. parent = child
  130. }
  131. }
  132. return parent
  133. }
  134. func (info *FileInfo) path() string {
  135. if info.parent == nil {
  136. return "/"
  137. }
  138. return filepath.Join(info.parent.path(), info.name)
  139. }
  140. func (info *FileInfo) isDir() bool {
  141. return info.parent == nil || info.stat.Mode&syscall.S_IFDIR == syscall.S_IFDIR
  142. }
  143. func (info *FileInfo) addChanges(oldInfo *FileInfo, changes *[]Change) {
  144. if oldInfo == nil {
  145. // add
  146. change := Change{
  147. Path: info.path(),
  148. Kind: ChangeAdd,
  149. }
  150. *changes = append(*changes, change)
  151. }
  152. // We make a copy so we can modify it to detect additions
  153. // also, we only recurse on the old dir if the new info is a directory
  154. // otherwise any previous delete/change is considered recursive
  155. oldChildren := make(map[string]*FileInfo)
  156. if oldInfo != nil && info.isDir() {
  157. for k, v := range oldInfo.children {
  158. oldChildren[k] = v
  159. }
  160. }
  161. for name, newChild := range info.children {
  162. oldChild, _ := oldChildren[name]
  163. if oldChild != nil {
  164. // change?
  165. oldStat := &oldChild.stat
  166. newStat := &newChild.stat
  167. // Note: We can't compare inode or ctime or blocksize here, because these change
  168. // when copying a file into a container. However, that is not generally a problem
  169. // because any content change will change mtime, and any status change should
  170. // be visible when actually comparing the stat fields. The only time this
  171. // breaks down is if some code intentionally hides a change by setting
  172. // back mtime
  173. if oldStat.Mode != newStat.Mode ||
  174. oldStat.Uid != newStat.Uid ||
  175. oldStat.Gid != newStat.Gid ||
  176. oldStat.Rdev != newStat.Rdev ||
  177. // Don't look at size for dirs, its not a good measure of change
  178. (oldStat.Size != newStat.Size && oldStat.Mode&syscall.S_IFDIR != syscall.S_IFDIR) ||
  179. !sameFsTimeSpec(getLastModification(oldStat), getLastModification(newStat)) {
  180. change := Change{
  181. Path: newChild.path(),
  182. Kind: ChangeModify,
  183. }
  184. *changes = append(*changes, change)
  185. }
  186. // Remove from copy so we can detect deletions
  187. delete(oldChildren, name)
  188. }
  189. newChild.addChanges(oldChild, changes)
  190. }
  191. for _, oldChild := range oldChildren {
  192. // delete
  193. change := Change{
  194. Path: oldChild.path(),
  195. Kind: ChangeDelete,
  196. }
  197. *changes = append(*changes, change)
  198. }
  199. }
  200. func (info *FileInfo) Changes(oldInfo *FileInfo) []Change {
  201. var changes []Change
  202. info.addChanges(oldInfo, &changes)
  203. return changes
  204. }
  205. func newRootFileInfo() *FileInfo {
  206. root := &FileInfo{
  207. name: "/",
  208. children: make(map[string]*FileInfo),
  209. }
  210. return root
  211. }
  212. func collectFileInfo(sourceDir string) (*FileInfo, error) {
  213. root := newRootFileInfo()
  214. err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error {
  215. if err != nil {
  216. return err
  217. }
  218. // Rebase path
  219. relPath, err := filepath.Rel(sourceDir, path)
  220. if err != nil {
  221. return err
  222. }
  223. relPath = filepath.Join("/", relPath)
  224. if relPath == "/" {
  225. return nil
  226. }
  227. parent := root.LookUp(filepath.Dir(relPath))
  228. if parent == nil {
  229. return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
  230. }
  231. info := &FileInfo{
  232. name: filepath.Base(relPath),
  233. children: make(map[string]*FileInfo),
  234. parent: parent,
  235. }
  236. if err := syscall.Lstat(path, &info.stat); err != nil {
  237. return err
  238. }
  239. parent.children[info.name] = info
  240. return nil
  241. })
  242. if err != nil {
  243. return nil, err
  244. }
  245. return root, nil
  246. }
  247. // Compare two directories and generate an array of Change objects describing the changes
  248. func ChangesDirs(newDir, oldDir string) ([]Change, error) {
  249. oldRoot, err := collectFileInfo(oldDir)
  250. if err != nil {
  251. return nil, err
  252. }
  253. newRoot, err := collectFileInfo(newDir)
  254. if err != nil {
  255. return nil, err
  256. }
  257. return newRoot.Changes(oldRoot), nil
  258. }
  259. func ChangesSize(newDir string, changes []Change) int64 {
  260. var size int64
  261. for _, change := range changes {
  262. if change.Kind == ChangeModify || change.Kind == ChangeAdd {
  263. file := filepath.Join(newDir, change.Path)
  264. fileInfo, _ := os.Lstat(file)
  265. if fileInfo != nil && !fileInfo.IsDir() {
  266. size += fileInfo.Size()
  267. }
  268. }
  269. }
  270. return size
  271. }
  272. func major(device uint64) uint64 {
  273. return (device >> 8) & 0xfff
  274. }
  275. func minor(device uint64) uint64 {
  276. return (device & 0xff) | ((device >> 12) & 0xfff00)
  277. }
  278. func ExportChanges(dir string, changes []Change) (Archive, error) {
  279. reader, writer := io.Pipe()
  280. tw := tar.NewWriter(writer)
  281. go func() {
  282. // In general we log errors here but ignore them because
  283. // during e.g. a diff operation the container can continue
  284. // mutating the filesystem and we can see transient errors
  285. // from this
  286. for _, change := range changes {
  287. if change.Kind == ChangeDelete {
  288. whiteOutDir := filepath.Dir(change.Path)
  289. whiteOutBase := filepath.Base(change.Path)
  290. whiteOut := filepath.Join(whiteOutDir, ".wh."+whiteOutBase)
  291. hdr := &tar.Header{
  292. Name: whiteOut[1:],
  293. Size: 0,
  294. ModTime: time.Now(),
  295. AccessTime: time.Now(),
  296. ChangeTime: time.Now(),
  297. }
  298. if err := tw.WriteHeader(hdr); err != nil {
  299. utils.Debugf("Can't write whiteout header: %s\n", err)
  300. }
  301. } else {
  302. path := filepath.Join(dir, change.Path)
  303. if err := addTarFile(path, change.Path[1:], tw); err != nil {
  304. utils.Debugf("Can't add file %s to tar: %s\n", path, err)
  305. }
  306. }
  307. }
  308. // Make sure to check the error on Close.
  309. if err := tw.Close(); err != nil {
  310. utils.Debugf("Can't close layer: %s\n", err)
  311. }
  312. writer.Close()
  313. }()
  314. return reader, nil
  315. }