changes.go 11 KB

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