github.com/amylindburg/docker@v1.7.0/daemon/graphdriver/overlay/copy.go (about)

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