gopkg.in/openshift/source-to-image.v1@v1.2.0/pkg/scm/downloaders/file/download.go (about)

     1  package file
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/openshift/source-to-image/pkg/api"
     8  	"github.com/openshift/source-to-image/pkg/api/constants"
     9  	"github.com/openshift/source-to-image/pkg/ignore"
    10  	"github.com/openshift/source-to-image/pkg/scm/git"
    11  	"github.com/openshift/source-to-image/pkg/util/fs"
    12  	utillog "github.com/openshift/source-to-image/pkg/util/log"
    13  )
    14  
    15  var log = utillog.StderrLog
    16  
    17  // RecursiveCopyError indicates a copy operation failed because the destination is within the copy's source tree.
    18  type RecursiveCopyError struct {
    19  	error
    20  }
    21  
    22  // File represents a simplest possible Downloader implementation where the
    23  // sources are just copied from local directory.
    24  type File struct {
    25  	fs.FileSystem
    26  }
    27  
    28  // Download copies sources from a local directory into the working directory.
    29  // Caller guarantees that config.Source.IsLocal() is true.
    30  func (f *File) Download(config *api.Config) (*git.SourceInfo, error) {
    31  	config.WorkingSourceDir = filepath.Join(config.WorkingDir, constants.Source)
    32  
    33  	copySrc := config.Source.LocalPath()
    34  	if len(config.ContextDir) > 0 {
    35  		copySrc = filepath.Join(copySrc, config.ContextDir)
    36  	}
    37  
    38  	log.V(1).Infof("Copying sources from %q to %q", copySrc, config.WorkingSourceDir)
    39  	absWorkingSourceDir, err := filepath.Abs(config.WorkingSourceDir)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	absCopySrc, err := filepath.Abs(copySrc)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	if filepath.HasPrefix(absWorkingSourceDir, absCopySrc) {
    48  		return nil, RecursiveCopyError{error: fmt.Errorf("recursive copy requested, source directory %q contains the target directory %q", copySrc, config.WorkingSourceDir)}
    49  	}
    50  
    51  	di := ignore.DockerIgnorer{}
    52  	filesToIgnore, lerr := di.GetListOfFilesToIgnore(copySrc)
    53  	if lerr != nil {
    54  		return nil, lerr
    55  	}
    56  
    57  	if copySrc != config.WorkingSourceDir {
    58  		f.KeepSymlinks(config.KeepSymlinks)
    59  		err := f.CopyContents(copySrc, config.WorkingSourceDir, filesToIgnore)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  	}
    64  
    65  	return &git.SourceInfo{
    66  		Location:   config.Source.LocalPath(),
    67  		ContextDir: config.ContextDir,
    68  	}, nil
    69  }