12 "github.com/fsouza/go-dockerclient/external/github.com/docker/docker/pkg/system"
15 // walker is used to implement collectFileInfoForChanges on linux. Where this
16 // method in general returns the entire contents of two directory trees, we
17 // optimize some FS calls out on linux. In particular, we take advantage of the
18 // fact that getdents(2) returns the inode of each file in the directory being
19 // walked, which, when walking two trees in parallel to generate a list of
20 // changes, can be used to prune subtrees without ever having to lstat(2) them
21 // directly. Eliminating stat calls in this way can save up to seconds on large
30 // collectFileInfoForChanges returns a complete representation of the trees
31 // rooted at dir1 and dir2, with one important exception: any subtree or
32 // leaf where the inode and device numbers are an exact match between dir1
33 // and dir2 will be pruned from the results. This method is *only* to be used
34 // to generating a list of changes between the two directories, as it does not
35 // reflect the full contents.
36 func collectFileInfoForChanges(dir1, dir2 string) (*FileInfo, *FileInfo, error) {
40 root1: newRootFileInfo(),
41 root2: newRootFileInfo(),
44 i1, err := os.Lstat(w.dir1)
48 i2, err := os.Lstat(w.dir2)
53 if err := w.walk("/", i1, i2); err != nil {
57 return w.root1, w.root2, nil
60 // Given a FileInfo, its path info, and a reference to the root of the tree
61 // being constructed, register this file with the tree.
62 func walkchunk(path string, fi os.FileInfo, dir string, root *FileInfo) error {
66 parent := root.LookUp(filepath.Dir(path))
68 return fmt.Errorf("collectFileInfoForChanges: Unexpectedly no parent for %s", path)
71 name: filepath.Base(path),
72 children: make(map[string]*FileInfo),
75 cpath := filepath.Join(dir, path)
76 stat, err := system.FromStatT(fi.Sys().(*syscall.Stat_t))
81 info.capability, _ = system.Lgetxattr(cpath, "security.capability") // lgetxattr(2): fs access
82 parent.children[info.name] = info
86 // Walk a subtree rooted at the same path in both trees being iterated. For
87 // example, /docker/overlay/1234/a/b/c/d and /docker/overlay/8888/a/b/c/d
88 func (w *walker) walk(path string, i1, i2 os.FileInfo) (err error) {
89 // Register these nodes with the return trees, unless we're still at the
90 // (already-created) roots:
92 if err := walkchunk(path, i1, w.dir1, w.root1); err != nil {
95 if err := walkchunk(path, i2, w.dir2, w.root2); err != nil {
100 is1Dir := i1 != nil && i1.IsDir()
101 is2Dir := i2 != nil && i2.IsDir()
104 if i1 != nil && i2 != nil {
105 si1 := i1.Sys().(*syscall.Stat_t)
106 si2 := i2.Sys().(*syscall.Stat_t)
107 if si1.Dev == si2.Dev {
112 // If these files are both non-existent, or leaves (non-dirs), we are done.
113 if !is1Dir && !is2Dir {
117 // Fetch the names of all the files contained in both directories being walked:
118 var names1, names2 []nameIno
120 names1, err = readdirnames(filepath.Join(w.dir1, path)) // getdents(2): fs access
126 names2, err = readdirnames(filepath.Join(w.dir2, path)) // getdents(2): fs access
132 // We have lists of the files contained in both parallel directories, sorted
133 // in the same order. Walk them in parallel, generating a unique merged list
134 // of all items present in either or both directories.
140 if ix1 >= len(names1) {
143 if ix2 >= len(names2) {
150 switch bytes.Compare([]byte(ni1.name), []byte(ni2.name)) {
151 case -1: // ni1 < ni2 -- advance ni1
152 // we will not encounter ni1 in names2
153 names = append(names, ni1.name)
155 case 0: // ni1 == ni2
156 if ni1.ino != ni2.ino || !sameDevice {
157 names = append(names, ni1.name)
161 case 1: // ni1 > ni2 -- advance ni2
162 // we will not encounter ni2 in names1
163 names = append(names, ni2.name)
167 for ix1 < len(names1) {
168 names = append(names, names1[ix1].name)
171 for ix2 < len(names2) {
172 names = append(names, names2[ix2].name)
176 // For each of the names present in either or both of the directories being
177 // iterated, stat the name under each root, and recurse the pair of them:
178 for _, name := range names {
179 fname := filepath.Join(path, name)
180 var cInfo1, cInfo2 os.FileInfo
182 cInfo1, err = os.Lstat(filepath.Join(w.dir1, fname)) // lstat(2): fs access
183 if err != nil && !os.IsNotExist(err) {
188 cInfo2, err = os.Lstat(filepath.Join(w.dir2, fname)) // lstat(2): fs access
189 if err != nil && !os.IsNotExist(err) {
193 if err = w.walk(fname, cInfo1, cInfo2); err != nil {
200 // {name,inode} pairs used to support the early-pruning logic of the walker type
201 type nameIno struct {
206 type nameInoSlice []nameIno
208 func (s nameInoSlice) Len() int { return len(s) }
209 func (s nameInoSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
210 func (s nameInoSlice) Less(i, j int) bool { return s[i].name < s[j].name }
212 // readdirnames is a hacked-apart version of the Go stdlib code, exposing inode
213 // numbers further up the stack when reading directory contents. Unlike
214 // os.Readdirnames, which returns a list of filenames, this function returns a
215 // list of {filename,inode} pairs.
216 func readdirnames(dirname string) (names []nameIno, err error) {
219 buf = make([]byte, 4096)
225 f, err := os.Open(dirname)
231 names = make([]nameIno, 0, size) // Empty with room to grow.
233 // Refill the buffer if necessary
236 nbuf, err = syscall.ReadDirent(int(f.Fd()), buf) // getdents on linux
241 return nil, os.NewSyscallError("readdirent", err)
249 nb, names = parseDirent(buf[bufp:nbuf], names)
253 sl := nameInoSlice(names)
258 // parseDirent is a minor modification of syscall.ParseDirent (linux version)
259 // which returns {name,inode} pairs instead of just names.
260 func parseDirent(buf []byte, names []nameIno) (consumed int, newnames []nameIno) {
263 dirent := (*syscall.Dirent)(unsafe.Pointer(&buf[0]))
264 buf = buf[dirent.Reclen:]
265 if dirent.Ino == 0 { // File absent in directory.
268 bytes := (*[10000]byte)(unsafe.Pointer(&dirent.Name[0]))
269 var name = string(bytes[0:clen(bytes[:])])
270 if name == "." || name == ".." { // Useless names
273 names = append(names, nameIno{name, dirent.Ino})
275 return origlen - len(buf), names
278 func clen(n []byte) int {
279 for i := 0; i < len(n); i++ {