12 "github.com/docker/docker/pkg/pools"
13 "github.com/docker/docker/pkg/system"
14 rsystem "github.com/opencontainers/runc/libcontainer/system"
20 copyHardlink copyFlags = 1 << iota
23 func copyRegular(srcPath, dstPath string, mode os.FileMode) error {
24 srcFile, err := os.Open(srcPath)
30 dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE, mode)
36 _, err = pools.Copy(dstFile, srcFile)
41 func copyXattr(srcPath, dstPath, attr string) error {
42 data, err := system.Lgetxattr(srcPath, attr)
47 if err := system.Lsetxattr(dstPath, attr, data, 0); err != nil {
54 func copyDir(srcDir, dstDir string, flags copyFlags) error {
55 err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
61 relPath, err := filepath.Rel(srcDir, srcPath)
66 dstPath := filepath.Join(dstDir, relPath)
71 stat, ok := f.Sys().(*syscall.Stat_t)
73 return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath)
78 switch f.Mode() & os.ModeType {
79 case 0: // Regular file
80 if flags©Hardlink != 0 {
82 if err := os.Link(srcPath, dstPath); err != nil {
86 if err := copyRegular(srcPath, dstPath, f.Mode()); err != nil {
92 if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
97 link, err := os.Readlink(srcPath)
102 if err := os.Symlink(link, dstPath); err != nil {
106 case os.ModeNamedPipe:
109 if rsystem.RunningInUserNS() {
110 // cannot create a device if running in user namespace
113 if err := syscall.Mkfifo(dstPath, stat.Mode); err != nil {
118 if err := syscall.Mknod(dstPath, stat.Mode, int(stat.Rdev)); err != nil {
123 return fmt.Errorf("Unknown file type for %s\n", srcPath)
126 // Everything below is copying metadata from src to dst. All this metadata
127 // already shares an inode for hardlinks.
132 if err := os.Lchown(dstPath, int(stat.Uid), int(stat.Gid)); err != nil {
136 if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil {
140 // We need to copy this attribute if it appears in an overlay upper layer, as
141 // this function is used to copy those. It is set by overlay if a directory
142 // is removed and then re-created and should not inherit anything from the
143 // same dir in the lower dir.
144 if err := copyXattr(srcPath, dstPath, "trusted.overlay.opaque"); err != nil {
148 isSymlink := f.Mode()&os.ModeSymlink != 0
150 // There is no LChmod, so ignore mode for symlink. Also, this
151 // must happen after chown, as that can modify the file mode
153 if err := os.Chmod(dstPath, f.Mode()); err != nil {
158 // system.Chtimes doesn't support a NOFOLLOW flag atm
160 aTime := time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
161 mTime := time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec))
162 if err := system.Chtimes(dstPath, aTime, mTime); err != nil {
166 ts := []syscall.Timespec{stat.Atim, stat.Mtim}
167 if err := system.LUtimesNano(dstPath, ts); err != nil {