changes.go 9.1 KB

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