fix adding symlinks to FS which do not exists

This commit is contained in:
Tejal Desai 2020-01-22 15:27:01 -08:00
parent f1f7297478
commit 478205e5ca
3 changed files with 32 additions and 11 deletions

View File

@ -563,7 +563,6 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
} }
} }
//
filesToSave, err := filesToSave(crossStageDependencies[index]) filesToSave, err := filesToSave(crossStageDependencies[index])
if err != nil { if err != nil {
return nil, err return nil, err
@ -599,7 +598,7 @@ func filesToSave(deps []string) ([]string, error) {
return nil, err return nil, err
} }
for _, f := range srcs { for _, f := range srcs {
if link, err := util.CanonicalizeLink(f); err == nil { if link, err := util.EvalSymLink(f); err == nil {
symLinks = append(symLinks, f) symLinks = append(symLinks, f)
srcFiles = append(srcFiles, link) srcFiles = append(srcFiles, link)
} else { } else {

View File

@ -19,6 +19,7 @@ package snapshot
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"sort" "sort"
"syscall" "syscall"
@ -197,7 +198,6 @@ func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {
return nil, nil, fmt.Errorf("unable to add file %s to layered map: %s", file, err) return nil, nil, fmt.Errorf("unable to add file %s to layered map: %s", file, err)
} }
} }
return filesToAdd, filesToWhiteOut, nil return filesToAdd, filesToWhiteOut, nil
} }
@ -240,11 +240,18 @@ func filesWithParentDirs(files []string) []string {
} }
func filesWithLinks(path string) ([]string, error) { func filesWithLinks(path string) ([]string, error) {
link, err := util.CanonicalizeLink(path) link, err := util.GetSymLink(path)
if err == util.ErrNotSymLink { if err == util.ErrNotSymLink {
return []string{path}, nil return []string{path}, nil
} else if err != nil { } else if err != nil {
return nil, err return nil, err
} }
// Add symlink if it exists in the FS
if !filepath.IsAbs(link) {
link = filepath.Join(filepath.Dir(path), link)
}
if _, err := os.Stat(link); err != nil {
return []string{path}, nil
}
return []string{path, link}, nil return []string{path, link}, nil
} }

View File

@ -506,6 +506,7 @@ func CopyDir(src, dest, buildcontext string) ([]string, error) {
fullPath := filepath.Join(src, file) fullPath := filepath.Join(src, file)
fi, err := os.Lstat(fullPath) fi, err := os.Lstat(fullPath)
if err != nil { if err != nil {
fmt.Println(" i am returning from here this", err)
return nil, err return nil, err
} }
if excludeFile(fullPath, buildcontext) { if excludeFile(fullPath, buildcontext) {
@ -545,7 +546,7 @@ func CopySymlink(src, dest, buildcontext string) (bool, error) {
logrus.Debugf("%s found in .dockerignore, ignoring", src) logrus.Debugf("%s found in .dockerignore, ignoring", src)
return true, nil return true, nil
} }
link, err := filepath.EvalSymlinks(src) link, err := os.Readlink(src)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -693,17 +694,31 @@ func IsSymlink(fi os.FileInfo) bool {
var ErrNotSymLink = fmt.Errorf("not a symlink") var ErrNotSymLink = fmt.Errorf("not a symlink")
func CanonicalizeLink(path string) (string, error) { func GetSymLink(path string) (string, error) {
fi, err := os.Lstat(path) if err := getSymlink(path); err != nil {
if err != nil { return "", err
}
return os.Readlink(path)
}
func EvalSymLink(path string) (string, error) {
if err := getSymlink(path); err != nil {
return "", err return "", err
} }
if !IsSymlink(fi) {
return "", ErrNotSymLink
}
return filepath.EvalSymlinks(path) return filepath.EvalSymlinks(path)
} }
func getSymlink(path string) error {
fi, err := os.Lstat(path)
if err != nil {
return err
}
if !IsSymlink(fi) {
return ErrNotSymLink
}
return nil
}
// otiai10Cpy.Copy in case the src file is a symlink, will copy the target // otiai10Cpy.Copy in case the src file is a symlink, will copy the target
// file at destination instead of creating a symlink. See #915 for more details. // file at destination instead of creating a symlink. See #915 for more details.
func CopyFileOrSymlink(src string, destDir string) error { func CopyFileOrSymlink(src string, destDir string) error {