From af035d03a71e55ee95f6a8224872bd821ed19e87 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Tue, 12 Feb 2019 12:42:23 +0100 Subject: [PATCH] Use plain copy for folder sync --- pkg/upload/upload.go | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/pkg/upload/upload.go b/pkg/upload/upload.go index acb81fb..ce8e31e 100644 --- a/pkg/upload/upload.go +++ b/pkg/upload/upload.go @@ -8,6 +8,7 @@ import ( "github.com/imdario/mergo" log "github.com/sirupsen/logrus" "gopkg.in/src-d/go-git.v4/plumbing/object" + "io" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -24,6 +25,7 @@ import ( ) const refAnnotation = "git2kube.github.com/ref" +const bufferSize = 1024 // LoadType upload type type LoadType int @@ -478,11 +480,40 @@ func (u *folderUploader) Upload(commitID string, iter FileIter) error { if _, err := os.Lstat(src); err == nil { src, _ = filepath.Abs(src) } - dest := path.Join(u.name, file.Name) - if _, err := os.Lstat(dest); err == nil { - os.Remove(dest) + dst := path.Join(u.name, file.Name) + + source, err := os.Open(src) + if err != nil { + return err + } + defer source.Close() + + if dir, _ := filepath.Split(dst); dir != "" { + err = os.MkdirAll(dir, 0777) + if err != nil { + return err + } + } + + destination, err := os.Create(dst) + if err != nil { + return err + } + defer destination.Close() + + buf := make([]byte, bufferSize) + for { + n, err := source.Read(buf) + if err != nil && err != io.EOF { + return err + } + if n == 0 { + break + } + if _, err := destination.Write(buf[:n]); err != nil { + return err + } } - return os.Symlink(src, dest) } return nil })