From 57182252e3c08159582318ed07729fc27d7180a9 Mon Sep 17 00:00:00 2001 From: Wei Qiu Date: Thu, 6 Oct 2016 12:08:24 +0200 Subject: [PATCH] Fix bugs in copying files to volumes --- .../eudat-gef/gef-docker/dckr/dckr.go | 44 ++++++++++++++++++- .../eudat-gef/gef-docker/dckr/utils.go | 14 +++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/github.com/eudat-gef/gef-docker/dckr/dckr.go b/src/github.com/eudat-gef/gef-docker/dckr/dckr.go index f04b0fb..e063ac7 100644 --- a/src/github.com/eudat-gef/gef-docker/dckr/dckr.go +++ b/src/github.com/eudat-gef/gef-docker/dckr/dckr.go @@ -10,6 +10,8 @@ import ( "strings" docker "github.com/fsouza/go-dockerclient" + "io/ioutil" + "path/filepath" ) const ( @@ -343,15 +345,55 @@ func (c Client) ListVolumeContent(id VolumeID) []string { // BuildVolume creates a volume, copies data from dirpath func (c Client) BuildVolume(dirpath string) (Volume, error) { volume, err := c.c.CreateVolume(docker.CreateVolumeOptions{}); + if err != nil { + return Volume{}, err + } ret := Volume{ ID: VolumeID(volume.Name), Mountpoint: VolumeMountpoint(volume.Mountpoint), } //copy all content to volume - CopyTree(dirpath, string(ret.Mountpoint), &CopyTreeOptions{Symlinks:true}) + err = copyDataToVolume(dirpath, string(ret.Mountpoint)) return ret, err } +func copyDataToVolume(src string, dst string) error{ + log.Printf("Copying data from %s to volume %s \n", src, dst) + entries, err := ioutil.ReadDir(src) + if err != nil { + return err + } + copyTreeOptions := &CopyTreeOptions{Symlinks:true} + + + for _, entry := range entries { + srcPath := filepath.Join(src, entry.Name()) + dstPath := filepath.Join(dst, entry.Name()) + + srcFileInfo, err := os.Stat(srcPath) + if err != nil { + return err + } + + if !srcFileInfo.IsDir() { + //no dir + err = CopyFile(srcPath, dstPath, false); + if err != nil { + log.Println("Copy files failed", err) + return err + } + } else { + //copy dir + err = CopyTree(srcPath, dstPath, copyTreeOptions) + if err != nil { + log.Println("Copy directories failed", err) + return err + } + } + } + return nil +} + //RemoveVolume removes a volume diff --git a/src/github.com/eudat-gef/gef-docker/dckr/utils.go b/src/github.com/eudat-gef/gef-docker/dckr/utils.go index 31fee1e..6d7adc4 100644 --- a/src/github.com/eudat-gef/gef-docker/dckr/utils.go +++ b/src/github.com/eudat-gef/gef-docker/dckr/utils.go @@ -211,7 +211,7 @@ type CopyTreeOptions struct { // Recursively copy a directory tree. // -// The destination directory must already exist. +// The destination directory must not already exist. // // If the optional Symlinks flag is true, symbolic links in the // source tree result in symbolic links in the destination tree; if @@ -258,16 +258,16 @@ func CopyTree(src, dst string, options *CopyTreeOptions) error { return &NotADirectoryError{src} } - dstFileInfo, err := os.Stat(dst) - if err != nil { - return err + if !os.IsNotExist(err) { + return &AlreadyExistsError{dst} } - if !dstFileInfo.IsDir() { - return &NotADirectoryError{dst} + entries, err := ioutil.ReadDir(src) + if err != nil { + return err } - entries, err := ioutil.ReadDir(src) + err = os.MkdirAll(dst, srcFileInfo.Mode()) if err != nil { return err }