gopkg.in/dotcloud/docker.v1@v1.13.1/daemon/graphdriver/overlay/copy.go (about)

     1  // +build linux
     2  
     3  package overlay
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  	"syscall"
    10  	"time"
    11  
    12  	"github.com/docker/docker/pkg/pools"
    13  	"github.com/docker/docker/pkg/system"
    14  	rsystem "github.com/opencontainers/runc/libcontainer/system"
    15  )
    16  
    17  type copyFlags int
    18  
    19  const (
    20  	copyHardlink copyFlags = 1 << iota
    21  )
    22  
    23  func copyRegular(srcPath, dstPath string, mode os.FileMode) error {
    24  	srcFile, err := os.Open(srcPath)
    25  	if err != nil {
    26  		return err
    27  	}
    28  	defer srcFile.Close()
    29  
    30  	dstFile, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE, mode)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	defer dstFile.Close()
    35  
    36  	_, err = pools.Copy(dstFile, srcFile)
    37  
    38  	return err
    39  }
    40  
    41  func copyXattr(srcPath, dstPath, attr string) error {
    42  	data, err := system.Lgetxattr(srcPath, attr)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	if data != nil {
    47  		if err := system.Lsetxattr(dstPath, attr, data, 0); err != nil {
    48  			return err
    49  		}
    50  	}
    51  	return nil
    52  }
    53  
    54  func copyDir(srcDir, dstDir string, flags copyFlags) error {
    55  	err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		// Rebase path
    61  		relPath, err := filepath.Rel(srcDir, srcPath)
    62  		if err != nil {
    63  			return err
    64  		}
    65  
    66  		dstPath := filepath.Join(dstDir, relPath)
    67  		if err != nil {
    68  			return err
    69  		}
    70  
    71  		stat, ok := f.Sys().(*syscall.Stat_t)
    72  		if !ok {
    73  			return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath)
    74  		}
    75  
    76  		isHardlink := false
    77  
    78  		switch f.Mode() & os.ModeType {
    79  		case 0: // Regular file
    80  			if flags&copyHardlink != 0 {
    81  				isHardlink = true
    82  				if err := os.Link(srcPath, dstPath); err != nil {
    83  					return err
    84  				}
    85  			} else {
    86  				if err := copyRegular(srcPath, dstPath, f.Mode()); err != nil {
    87  					return err
    88  				}
    89  			}
    90  
    91  		case os.ModeDir:
    92  			if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
    93  				return err
    94  			}
    95  
    96  		case os.ModeSymlink:
    97  			link, err := os.Readlink(srcPath)
    98  			if err != nil {
    99  				return err
   100  			}
   101  
   102  			if err := os.Symlink(link, dstPath); err != nil {
   103  				return err
   104  			}
   105  
   106  		case os.ModeNamedPipe:
   107  			fallthrough
   108  		case os.ModeSocket:
   109  			if rsystem.RunningInUserNS() {
   110  				// cannot create a device if running in user namespace
   111  				return nil
   112  			}
   113  			if err := syscall.Mkfifo(dstPath, stat.Mode); err != nil {
   114  				return err
   115  			}
   116  
   117  		case os.ModeDevice:
   118  			if err := syscall.Mknod(dstPath, stat.Mode, int(stat.Rdev)); err != nil {
   119  				return err
   120  			}
   121  
   122  		default:
   123  			return fmt.Errorf("Unknown file type for %s\n", srcPath)
   124  		}
   125  
   126  		// Everything below is copying metadata from src to dst. All this metadata
   127  		// already shares an inode for hardlinks.
   128  		if isHardlink {
   129  			return nil
   130  		}
   131  
   132  		if err := os.Lchown(dstPath, int(stat.Uid), int(stat.Gid)); err != nil {
   133  			return err
   134  		}
   135  
   136  		if err := copyXattr(srcPath, dstPath, "security.capability"); err != nil {
   137  			return err
   138  		}
   139  
   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 {
   145  			return err
   146  		}
   147  
   148  		isSymlink := f.Mode()&os.ModeSymlink != 0
   149  
   150  		// There is no LChmod, so ignore mode for symlink. Also, this
   151  		// must happen after chown, as that can modify the file mode
   152  		if !isSymlink {
   153  			if err := os.Chmod(dstPath, f.Mode()); err != nil {
   154  				return err
   155  			}
   156  		}
   157  
   158  		// system.Chtimes doesn't support a NOFOLLOW flag atm
   159  		if !isSymlink {
   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 {
   163  				return err
   164  			}
   165  		} else {
   166  			ts := []syscall.Timespec{stat.Atim, stat.Mtim}
   167  			if err := system.LUtimesNano(dstPath, ts); err != nil {
   168  				return err
   169  			}
   170  		}
   171  		return nil
   172  	})
   173  	return err
   174  }