changes.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  12. log "github.com/Sirupsen/logrus"
  13. "github.com/docker/docker/pkg/pools"
  14. "github.com/docker/docker/pkg/system"
  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. // Changes walks the path rw and determines changes for the files in the path,
  52. // with respect to the parent layers
  53. func Changes(layers []string, rw string) ([]Change, error) {
  54. var changes []Change
  55. err := filepath.Walk(rw, func(path string, f os.FileInfo, err error) error {
  56. if err != nil {
  57. return err
  58. }
  59. // Rebase path
  60. path, err = filepath.Rel(rw, path)
  61. if err != nil {
  62. return err
  63. }
  64. path = filepath.Join("/", path)
  65. // Skip root
  66. if path == "/" {
  67. return nil
  68. }
  69. // Skip AUFS metadata
  70. if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
  71. return err
  72. }
  73. change := Change{
  74. Path: path,
  75. }
  76. // Find out what kind of modification happened
  77. file := filepath.Base(path)
  78. // If there is a whiteout, then the file was removed
  79. if strings.HasPrefix(file, ".wh.") {
  80. originalFile := file[len(".wh."):]
  81. change.Path = filepath.Join(filepath.Dir(path), originalFile)
  82. change.Kind = ChangeDelete
  83. } else {
  84. // Otherwise, the file was added
  85. change.Kind = ChangeAdd
  86. // ...Unless it already existed in a top layer, in which case, it's a modification
  87. for _, layer := range layers {
  88. stat, err := os.Stat(filepath.Join(layer, path))
  89. if err != nil && !os.IsNotExist(err) {
  90. return err
  91. }
  92. if err == nil {
  93. // The file existed in the top layer, so that's a modification
  94. // However, if it's a directory, maybe it wasn't actually modified.
  95. // If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
  96. if stat.IsDir() && f.IsDir() {
  97. if f.Size() == stat.Size() && f.Mode() == stat.Mode() && sameFsTime(f.ModTime(), stat.ModTime()) {
  98. // Both directories are the same, don't record the change
  99. return nil
  100. }
  101. }
  102. change.Kind = ChangeModify
  103. break
  104. }
  105. }
  106. }
  107. // Record change
  108. changes = append(changes, change)
  109. return nil
  110. })
  111. if err != nil && !os.IsNotExist(err) {
  112. return nil, err
  113. }
  114. return changes, nil
  115. }
  116. type FileInfo struct {
  117. parent *FileInfo
  118. name string
  119. stat *system.Stat
  120. children map[string]*FileInfo
  121. capability []byte
  122. added bool
  123. }
  124. func (root *FileInfo) LookUp(path string) *FileInfo {
  125. parent := root
  126. if path == "/" {
  127. return root
  128. }
  129. pathElements := strings.Split(path, "/")
  130. for _, elem := range pathElements {
  131. if elem != "" {
  132. child := parent.children[elem]
  133. if child == nil {
  134. return nil
  135. }
  136. parent = child
  137. }
  138. }
  139. return parent
  140. }
  141. func (info *FileInfo) path() string {
  142. if info.parent == nil {
  143. return "/"
  144. }
  145. return filepath.Join(info.parent.path(), info.name)
  146. }
  147. func (info *FileInfo) isDir() bool {
  148. return info.parent == nil || info.stat.Mode()&syscall.S_IFDIR == syscall.S_IFDIR
  149. }
  150. func (info *FileInfo) addChanges(oldInfo *FileInfo, changes *[]Change) {
  151. sizeAtEntry := len(*changes)
  152. if oldInfo == nil {
  153. // add
  154. change := Change{
  155. Path: info.path(),
  156. Kind: ChangeAdd,
  157. }
  158. *changes = append(*changes, change)
  159. info.added = true
  160. }
  161. // We make a copy so we can modify it to detect additions
  162. // also, we only recurse on the old dir if the new info is a directory
  163. // otherwise any previous delete/change is considered recursive
  164. oldChildren := make(map[string]*FileInfo)
  165. if oldInfo != nil && info.isDir() {
  166. for k, v := range oldInfo.children {
  167. oldChildren[k] = v
  168. }
  169. }
  170. for name, newChild := range info.children {
  171. oldChild, _ := oldChildren[name]
  172. if oldChild != nil {
  173. // change?
  174. oldStat := oldChild.stat
  175. newStat := newChild.stat
  176. // Note: We can't compare inode or ctime or blocksize here, because these change
  177. // when copying a file into a container. However, that is not generally a problem
  178. // because any content change will change mtime, and any status change should
  179. // be visible when actually comparing the stat fields. The only time this
  180. // breaks down is if some code intentionally hides a change by setting
  181. // back mtime
  182. if oldStat.Mode() != newStat.Mode() ||
  183. oldStat.Uid() != newStat.Uid() ||
  184. oldStat.Gid() != newStat.Gid() ||
  185. oldStat.Rdev() != newStat.Rdev() ||
  186. // Don't look at size for dirs, its not a good measure of change
  187. (oldStat.Size() != newStat.Size() && oldStat.Mode()&syscall.S_IFDIR != syscall.S_IFDIR) ||
  188. !sameFsTimeSpec(oldStat.Mtim(), newStat.Mtim()) ||
  189. bytes.Compare(oldChild.capability, newChild.capability) != 0 {
  190. change := Change{
  191. Path: newChild.path(),
  192. Kind: ChangeModify,
  193. }
  194. *changes = append(*changes, change)
  195. newChild.added = true
  196. }
  197. // Remove from copy so we can detect deletions
  198. delete(oldChildren, name)
  199. }
  200. newChild.addChanges(oldChild, changes)
  201. }
  202. for _, oldChild := range oldChildren {
  203. // delete
  204. change := Change{
  205. Path: oldChild.path(),
  206. Kind: ChangeDelete,
  207. }
  208. *changes = append(*changes, change)
  209. }
  210. // If there were changes inside this directory, we need to add it, even if the directory
  211. // itself wasn't changed. This is needed to properly save and restore filesystem permissions.
  212. if len(*changes) > sizeAtEntry && info.isDir() && !info.added && info.path() != "/" {
  213. change := Change{
  214. Path: info.path(),
  215. Kind: ChangeModify,
  216. }
  217. // Let's insert the directory entry before the recently added entries located inside this dir
  218. *changes = append(*changes, change) // just to resize the slice, will be overwritten
  219. copy((*changes)[sizeAtEntry+1:], (*changes)[sizeAtEntry:])
  220. (*changes)[sizeAtEntry] = change
  221. }
  222. }
  223. func (info *FileInfo) Changes(oldInfo *FileInfo) []Change {
  224. var changes []Change
  225. info.addChanges(oldInfo, &changes)
  226. return changes
  227. }
  228. func newRootFileInfo() *FileInfo {
  229. root := &FileInfo{
  230. name: "/",
  231. children: make(map[string]*FileInfo),
  232. }
  233. return root
  234. }
  235. func collectFileInfo(sourceDir string) (*FileInfo, error) {
  236. root := newRootFileInfo()
  237. err := filepath.Walk(sourceDir, func(path string, f os.FileInfo, err error) error {
  238. if err != nil {
  239. return err
  240. }
  241. // Rebase path
  242. relPath, err := filepath.Rel(sourceDir, path)
  243. if err != nil {
  244. return err
  245. }
  246. relPath = filepath.Join("/", relPath)
  247. if relPath == "/" {
  248. return nil
  249. }
  250. parent := root.LookUp(filepath.Dir(relPath))
  251. if parent == nil {
  252. return fmt.Errorf("collectFileInfo: Unexpectedly no parent for %s", relPath)
  253. }
  254. info := &FileInfo{
  255. name: filepath.Base(relPath),
  256. children: make(map[string]*FileInfo),
  257. parent: parent,
  258. }
  259. s, err := system.Lstat(path)
  260. if err != nil {
  261. return err
  262. }
  263. info.stat = s
  264. info.capability, _ = system.Lgetxattr(path, "security.capability")
  265. parent.children[info.name] = info
  266. return nil
  267. })
  268. if err != nil {
  269. return nil, err
  270. }
  271. return root, nil
  272. }
  273. // ChangesDirs compares two directories and generates an array of Change objects describing the changes.
  274. // If oldDir is "", then all files in newDir will be Add-Changes.
  275. func ChangesDirs(newDir, oldDir string) ([]Change, error) {
  276. var (
  277. oldRoot, newRoot *FileInfo
  278. err1, err2 error
  279. errs = make(chan error, 2)
  280. )
  281. go func() {
  282. if oldDir != "" {
  283. oldRoot, err1 = collectFileInfo(oldDir)
  284. }
  285. errs <- err1
  286. }()
  287. go func() {
  288. newRoot, err2 = collectFileInfo(newDir)
  289. errs <- err2
  290. }()
  291. // block until both routines have returned
  292. for i := 0; i < 2; i++ {
  293. if err := <-errs; err != nil {
  294. return nil, err
  295. }
  296. }
  297. return newRoot.Changes(oldRoot), nil
  298. }
  299. // ChangesSize calculates the size in bytes of the provided changes, based on newDir.
  300. func ChangesSize(newDir string, changes []Change) int64 {
  301. var size int64
  302. for _, change := range changes {
  303. if change.Kind == ChangeModify || change.Kind == ChangeAdd {
  304. file := filepath.Join(newDir, change.Path)
  305. fileInfo, _ := os.Lstat(file)
  306. if fileInfo != nil && !fileInfo.IsDir() {
  307. size += fileInfo.Size()
  308. }
  309. }
  310. }
  311. return size
  312. }
  313. // ExportChanges produces an Archive from the provided changes, relative to dir.
  314. func ExportChanges(dir string, changes []Change) (Archive, error) {
  315. reader, writer := io.Pipe()
  316. go func() {
  317. ta := &tarAppender{
  318. TarWriter: tar.NewWriter(writer),
  319. Buffer: pools.BufioWriter32KPool.Get(nil),
  320. SeenFiles: make(map[uint64]string),
  321. }
  322. // this buffer is needed for the duration of this piped stream
  323. defer pools.BufioWriter32KPool.Put(ta.Buffer)
  324. // In general we log errors here but ignore them because
  325. // during e.g. a diff operation the container can continue
  326. // mutating the filesystem and we can see transient errors
  327. // from this
  328. for _, change := range changes {
  329. if change.Kind == ChangeDelete {
  330. whiteOutDir := filepath.Dir(change.Path)
  331. whiteOutBase := filepath.Base(change.Path)
  332. whiteOut := filepath.Join(whiteOutDir, ".wh."+whiteOutBase)
  333. timestamp := time.Now()
  334. hdr := &tar.Header{
  335. Name: whiteOut[1:],
  336. Size: 0,
  337. ModTime: timestamp,
  338. AccessTime: timestamp,
  339. ChangeTime: timestamp,
  340. }
  341. if err := ta.TarWriter.WriteHeader(hdr); err != nil {
  342. log.Debugf("Can't write whiteout header: %s", err)
  343. }
  344. } else {
  345. path := filepath.Join(dir, change.Path)
  346. if err := ta.addTarFile(path, change.Path[1:]); err != nil {
  347. log.Debugf("Can't add file %s to tar: %s", path, err)
  348. }
  349. }
  350. }
  351. // Make sure to check the error on Close.
  352. if err := ta.TarWriter.Close(); err != nil {
  353. log.Debugf("Can't close layer: %s", err)
  354. }
  355. if err := writer.Close(); err != nil {
  356. log.Debugf("failed close Changes writer: %s", err)
  357. }
  358. }()
  359. return reader, nil
  360. }