changes.go 11 KB

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