Skip to content

Commit

Permalink
bugfix: populate volume error
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Li <[email protected]>
  • Loading branch information
shaloulcy committed Aug 14, 2018
1 parent e0111ac commit 166350f
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 9 deletions.
12 changes: 3 additions & 9 deletions daemon/mgr/container_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"

"github.com/alibaba/pouch/apis/opts"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/archive"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/randomid"
"github.com/alibaba/pouch/pkg/system"
Expand Down Expand Up @@ -672,15 +672,9 @@ func copyImageContent(source, destination string) error {
return err
}
if len(dstList) == 0 {
// TODO: refactor

// If the source volume is empty, copies files from the root into the volume
commandLine := fmt.Sprintf("cp -r %s/* %s", source, destination)

cmd := exec.Command("sh", "-c", commandLine)
out, err := cmd.CombinedOutput()
err := archive.CopyWithTar(source, destination)
if err != nil {
logrus.Errorf("copyImageContent: %s, %v", string(out), err)
logrus.Errorf("copyImageContent: %v", err)
return err
}
}
Expand Down
120 changes: 120 additions & 0 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package archive

import (
"archive/tar"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)

func tarFromDir(src string, writer io.Writer) error {
// ensure the src actually exists before trying to tar it
if _, err := os.Stat(src); err != nil {
return fmt.Errorf("Unable to tar files - %v", err.Error())
}

tw := tar.NewWriter(writer)
defer tw.Close()

// walk path
return filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
if err != nil {
return err
}

// create a new dir/file header
header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}

// update the name to correctly reflect the desired destination when untaring
header.Name = strings.TrimPrefix(strings.Replace(file, src, "", 1), string(filepath.Separator))
// write the header
if err := tw.WriteHeader(header); err != nil {
return err
}

if !fi.Mode().IsRegular() {
return nil
}

// open files for taring
f, err := os.Open(file)
if err != nil {
return err
}

// copy file data into tar writer
if _, err := io.Copy(tw, f); err != nil {
return err
}

// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
f.Close()

return nil
})
}

func untarToDir(dst string, r io.Reader) error {
tr := tar.NewReader(r)

for {
header, err := tr.Next()

switch {
case err == io.EOF:
return nil
case err != nil:
return err
case header == nil:
continue
}

// the target location where the dir/file should be created
target := filepath.Join(dst, header.Name)

// check the file type
switch header.Typeflag {
case tar.TypeDir:
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
}
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}

if _, err := io.Copy(f, tr); err != nil {
return err
}

f.Close()
}
}
}

// CopyWithTar create a tar from src directory,
// and untar it in dst directory.
func CopyWithTar(src, dst string) error {
buf := new(bytes.Buffer)

if err := tarFromDir(src, buf); err != nil {
return err
}

if err := untarToDir(dst, buf); err != nil {
return err
}

return nil

}

0 comments on commit 166350f

Please sign in to comment.