changes.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package fs
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. )
  8. type ChangeType int
  9. const (
  10. ChangeModify = iota
  11. ChangeAdd
  12. ChangeDelete
  13. )
  14. type Change struct {
  15. Path string
  16. Kind ChangeType
  17. }
  18. func (change *Change) String() string {
  19. var kind string
  20. switch change.Kind {
  21. case ChangeModify:
  22. kind = "C"
  23. case ChangeAdd:
  24. kind = "A"
  25. case ChangeDelete:
  26. kind = "D"
  27. }
  28. return fmt.Sprintf("%s %s", kind, change.Path)
  29. }
  30. func (store *Store) Changes(mp *Mountpoint) ([]Change, error) {
  31. var changes []Change
  32. image, err := store.Get(mp.Image)
  33. if err != nil {
  34. return nil, err
  35. }
  36. layers, err := image.layers()
  37. if err != nil {
  38. return nil, err
  39. }
  40. err = filepath.Walk(mp.Rw, func(path string, f os.FileInfo, err error) error {
  41. if err != nil {
  42. return err
  43. }
  44. // Rebase path
  45. path, err = filepath.Rel(mp.Rw, path)
  46. if err != nil {
  47. return err
  48. }
  49. path = filepath.Join("/", path)
  50. // Skip root
  51. if path == "/" {
  52. return nil
  53. }
  54. // Skip AUFS metadata
  55. if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
  56. return err
  57. }
  58. change := Change{
  59. Path: path,
  60. }
  61. // Find out what kind of modification happened
  62. file := filepath.Base(path)
  63. // If there is a whiteout, then the file was removed
  64. if strings.HasPrefix(file, ".wh.") {
  65. originalFile := strings.TrimLeft(file, ".wh.")
  66. change.Path = filepath.Join(filepath.Dir(path), originalFile)
  67. change.Kind = ChangeDelete
  68. } else {
  69. // Otherwise, the file was added
  70. change.Kind = ChangeAdd
  71. // ...Unless it already existed in a top layer, in which case, it's a modification
  72. for _, layer := range layers {
  73. stat, err := os.Stat(filepath.Join(layer, path))
  74. if err != nil && !os.IsNotExist(err) {
  75. return err
  76. }
  77. if err == nil {
  78. // The file existed in the top layer, so that's a modification
  79. // However, if it's a directory, maybe it wasn't actually modified.
  80. // If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
  81. if stat.IsDir() && f.IsDir() {
  82. if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() {
  83. // Both directories are the same, don't record the change
  84. return nil
  85. }
  86. }
  87. change.Kind = ChangeModify
  88. break
  89. }
  90. }
  91. }
  92. // Record change
  93. changes = append(changes, change)
  94. return nil
  95. })
  96. if err != nil {
  97. return nil, err
  98. }
  99. return changes, nil
  100. }
  101. // Reset removes all changes to the filesystem, reverting it to its initial state.
  102. func (mp *Mountpoint) Reset() error {
  103. if err := os.RemoveAll(mp.Rw); err != nil {
  104. return err
  105. }
  106. // We removed the RW directory itself along with its content: let's re-create an empty one.
  107. if err := mp.createFolders(); err != nil {
  108. return err
  109. }
  110. return nil
  111. }