changes.go 9.1 KB

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