Skip to content

Commit

Permalink
Fix bugs in copying files to volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuwei committed Oct 6, 2016
1 parent d84dbf3 commit 5718225
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
44 changes: 43 additions & 1 deletion src/github.com/eudat-gef/gef-docker/dckr/dckr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"

docker "github.com/fsouza/go-dockerclient"
"io/ioutil"
"path/filepath"
)

const (
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/github.com/eudat-gef/gef-docker/dckr/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5718225

Please sign in to comment.