filesystem.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package docker
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "syscall"
  9. "io"
  10. "io/ioutil"
  11. )
  12. type Filesystem struct {
  13. RootFS string
  14. RWPath string
  15. Layers []string
  16. }
  17. func (fs *Filesystem) createMountPoints() error {
  18. if err := os.Mkdir(fs.RootFS, 0700); err != nil && !os.IsExist(err) {
  19. return err
  20. }
  21. if err := os.Mkdir(fs.RWPath, 0700); err != nil && !os.IsExist(err) {
  22. return err
  23. }
  24. return nil
  25. }
  26. func (fs *Filesystem) Mount() error {
  27. if fs.IsMounted() {
  28. return errors.New("Mount: Filesystem already mounted")
  29. }
  30. if err := fs.createMountPoints(); err != nil {
  31. return err
  32. }
  33. rwBranch := fmt.Sprintf("%v=rw", fs.RWPath)
  34. roBranches := ""
  35. for _, layer := range fs.Layers {
  36. roBranches += fmt.Sprintf("%v=ro:", layer)
  37. }
  38. branches := fmt.Sprintf("br:%v:%v", rwBranch, roBranches)
  39. return syscall.Mount("none", fs.RootFS, "aufs", 0, branches)
  40. }
  41. func (fs *Filesystem) Umount() error {
  42. if !fs.IsMounted() {
  43. return errors.New("Umount: Filesystem not mounted")
  44. }
  45. return syscall.Unmount(fs.RootFS, 0)
  46. }
  47. func (fs *Filesystem) IsMounted() bool {
  48. f, err := os.Open(fs.RootFS)
  49. if err != nil {
  50. if os.IsNotExist(err) {
  51. return false
  52. }
  53. panic(err)
  54. }
  55. list, err := f.Readdirnames(1)
  56. f.Close()
  57. if err != nil {
  58. return false
  59. }
  60. if len(list) > 0 {
  61. return true
  62. }
  63. return false
  64. }
  65. // Tar returns the contents of the filesystem as an uncompressed tar stream
  66. func (fs *Filesystem) Tar() (io.Reader, error) {
  67. if err := fs.EnsureMounted(); err != nil {
  68. return nil, err
  69. }
  70. return Tar(fs.RootFS)
  71. }
  72. func (fs *Filesystem) EnsureMounted() error {
  73. if !fs.IsMounted() {
  74. if err := fs.Mount(); err != nil {
  75. return err
  76. }
  77. }
  78. return nil
  79. }
  80. type ChangeType int
  81. const (
  82. ChangeModify = iota
  83. ChangeAdd
  84. ChangeDelete
  85. )
  86. type Change struct {
  87. Path string
  88. Kind ChangeType
  89. }
  90. func (change *Change) String() string {
  91. var kind string
  92. switch change.Kind {
  93. case ChangeModify: kind = "C"
  94. case ChangeAdd: kind = "A"
  95. case ChangeDelete: kind = "D"
  96. }
  97. return fmt.Sprintf("%s %s", kind, change.Path)
  98. }
  99. func (fs *Filesystem) Changes() ([]Change, error) {
  100. var changes []Change
  101. err := filepath.Walk(fs.RWPath, func(path string, f os.FileInfo, err error) error {
  102. if err != nil {
  103. return err
  104. }
  105. // Rebase path
  106. path, err = filepath.Rel(fs.RWPath, path)
  107. if err != nil {
  108. return err
  109. }
  110. path = filepath.Join("/", path)
  111. // Skip root
  112. if path == "/" {
  113. return nil
  114. }
  115. // Skip AUFS metadata
  116. if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched {
  117. return err
  118. }
  119. change := Change{
  120. Path: path,
  121. }
  122. // Find out what kind of modification happened
  123. file := filepath.Base(path)
  124. // If there is a whiteout, then the file was removed
  125. if strings.HasPrefix(file, ".wh.") {
  126. originalFile := strings.TrimLeft(file, ".wh.")
  127. change.Path = filepath.Join(filepath.Dir(path), originalFile)
  128. change.Kind = ChangeDelete
  129. } else {
  130. // Otherwise, the file was added
  131. change.Kind = ChangeAdd
  132. // ...Unless it already existed in a top layer, in which case, it's a modification
  133. for _, layer := range fs.Layers {
  134. stat, err := os.Stat(filepath.Join(layer, path))
  135. if err != nil && !os.IsNotExist(err) {
  136. return err
  137. }
  138. if err == nil {
  139. // The file existed in the top layer, so that's a modification
  140. // However, if it's a directory, maybe it wasn't actually modified.
  141. // If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar
  142. if stat.IsDir() && f.IsDir() {
  143. if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() {
  144. // Both directories are the same, don't record the change
  145. return nil
  146. }
  147. }
  148. change.Kind = ChangeModify
  149. break
  150. }
  151. }
  152. }
  153. // Record change
  154. changes = append(changes, change)
  155. return nil
  156. })
  157. if err != nil {
  158. return nil, err
  159. }
  160. return changes, nil
  161. }
  162. // Reset removes all changes to the filesystem, reverting it to its initial state.
  163. func (fs *Filesystem) Reset() error {
  164. if err := os.RemoveAll(fs.RWPath); err != nil {
  165. return err
  166. }
  167. // We removed the RW directory itself along with its content: let's re-create an empty one.
  168. if err := fs.createMountPoints(); err != nil {
  169. return err
  170. }
  171. return nil
  172. }
  173. // Open opens the named file for reading.
  174. func (fs *Filesystem) OpenFile(path string, flag int, perm os.FileMode) (*os.File, error) {
  175. if err := fs.EnsureMounted(); err != nil {
  176. return nil, err
  177. }
  178. return os.OpenFile(filepath.Join(fs.RootFS, path), flag, perm)
  179. }
  180. // ReadDir reads the directory named by dirname, relative to the Filesystem's root,
  181. // and returns a list of sorted directory entries
  182. func (fs *Filesystem) ReadDir(dirname string) ([]os.FileInfo, error) {
  183. if err := fs.EnsureMounted(); err != nil {
  184. return nil, err
  185. }
  186. return ioutil.ReadDir(filepath.Join(fs.RootFS, dirname))
  187. }
  188. func newFilesystem(rootfs string, rwpath string, layers []string) *Filesystem {
  189. return &Filesystem{
  190. RootFS: rootfs,
  191. RWPath: rwpath,
  192. Layers: layers,
  193. }
  194. }