github.com/ahlemtn/fabric@v2.1.1+incompatible/core/container/externalbuilder/copy.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package externalbuilder
     8  
     9  import (
    10  	"io"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  
    15  	"github.com/hyperledger/fabric/common/flogging"
    16  	"github.com/pkg/errors"
    17  )
    18  
    19  // CopyDir creates a copy of a dir
    20  func CopyDir(logger *flogging.FabricLogger, srcroot, destroot string) error {
    21  	err := filepath.Walk(srcroot, func(path string, info os.FileInfo, err error) error {
    22  		if err != nil {
    23  			return err
    24  		}
    25  
    26  		srcsubpath, err := filepath.Rel(srcroot, path)
    27  		if err != nil {
    28  			return err
    29  		}
    30  		destpath := filepath.Join(destroot, srcsubpath)
    31  
    32  		switch {
    33  		case info.IsDir():
    34  			return os.MkdirAll(destpath, info.Mode())
    35  		case info.Mode()&os.ModeSymlink == os.ModeSymlink:
    36  			// filepath.Walk does not follow symbolic links; we need to copy
    37  			// symbolic links as-is because some chaincode types (Node.js) rely
    38  			// on the use of symbolic links.
    39  			return copySymlink(srcroot, path, destpath)
    40  		case info.Mode().IsRegular():
    41  			// Intermediate directories are ensured to exist because parent
    42  			// node is always visited before children in `filepath.Walk`.
    43  			return copyFile(path, destpath)
    44  		default:
    45  			// It's something else that we don't support copying (device, socket, etc)
    46  			return errors.Errorf("refusing to copy unsupported file %s with mode %o", path, info.Mode())
    47  		}
    48  	})
    49  
    50  	// If an error occurred, clean up any created files.
    51  	if err != nil {
    52  		if err := os.RemoveAll(destroot); err != nil {
    53  			logger.Errorf("failed to remove destination directory %s after copy error: %s", destroot, err)
    54  		}
    55  		return errors.WithMessagef(err, "failed to copy %s to %s", srcroot, destroot)
    56  	}
    57  	return nil
    58  }
    59  
    60  func copySymlink(srcroot, srcpath, destpath string) error {
    61  	// If the symlink is absolute, then we do not want to copy it.
    62  	symlinkDest, err := os.Readlink(srcpath)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	if filepath.IsAbs(symlinkDest) {
    67  		return errors.Errorf("refusing to copy absolute symlink %s -> %s", srcpath, symlinkDest)
    68  	}
    69  
    70  	// Determine where the symlink points to. If it points outside
    71  	// of the source root, then we do not want to copy it.
    72  	symlinkDir := filepath.Dir(srcpath)
    73  	symlinkTarget := filepath.Clean(filepath.Join(symlinkDir, symlinkDest))
    74  	relativeTarget, err := filepath.Rel(srcroot, symlinkTarget)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	if relativeTargetElements := strings.Split(relativeTarget, string(os.PathSeparator)); len(relativeTargetElements) >= 1 && relativeTargetElements[0] == ".." {
    79  		return errors.Errorf("refusing to copy symlink %s -> %s pointing outside of source root", srcpath, symlinkDest)
    80  	}
    81  
    82  	return os.Symlink(symlinkDest, destpath)
    83  }
    84  
    85  func copyFile(srcpath, destpath string) error {
    86  	srcFile, err := os.Open(srcpath)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	defer srcFile.Close()
    91  
    92  	info, err := srcFile.Stat()
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	destFile, err := os.Create(destpath)
    98  	if err != nil {
    99  		return err
   100  	}
   101  	defer destFile.Close()
   102  
   103  	if err = os.Chmod(destFile.Name(), info.Mode()); err != nil {
   104  		return err
   105  	}
   106  
   107  	if _, err = io.Copy(destFile, srcFile); err != nil {
   108  		return err
   109  	}
   110  
   111  	return nil
   112  }