-
Notifications
You must be signed in to change notification settings - Fork 949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bugfix: populate volume error #2087
Merged
allencloud
merged 1 commit into
AliyunContainerService:master
from
shaloulcy:bugfix_for_populate_volume
Aug 14, 2018
+248
−9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
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("failed to stat source file %s: %v", src, err) | ||
} | ||
|
||
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 | ||
} | ||
defer f.Close() | ||
|
||
// copy file data into tar writer | ||
if _, err := io.Copy(tw, f); err != nil { | ||
return err | ||
} | ||
|
||
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 | ||
} | ||
defer f.Close() | ||
|
||
if _, err := io.Copy(f, tr); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package archive | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alibaba/pouch/pkg/utils" | ||
) | ||
|
||
func TestCopyWithTar(t *testing.T) { | ||
source, err := ioutil.TempDir("", "source") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(source) | ||
|
||
destination, err := ioutil.TempDir("", "destination") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(destination) | ||
|
||
files := []string{"file1", "file2", "dir1/file3", "dir2/file4"} | ||
|
||
err = makeFiles(source, files) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = CopyWithTar(source, destination) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
actualFiles := targetFiles(destination) | ||
|
||
if !utils.StringSliceEqual(files, actualFiles) { | ||
t.Fatalf(" TestCopyWithTar expected get %v, but got %v", files, actualFiles) | ||
} | ||
} | ||
|
||
func makeFiles(baseDir string, files []string) error { | ||
for _, file := range files { | ||
fullPath := path.Join(baseDir, file) | ||
|
||
dir := path.Dir(fullPath) | ||
|
||
// create dir. | ||
err := os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// create file. | ||
fi, err := os.Create(fullPath) | ||
if err != nil { | ||
return err | ||
} | ||
fi.Close() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func targetFiles(baseDir string) []string { | ||
files := []string{} | ||
|
||
filepath.Walk(baseDir, func(name string, fi os.FileInfo, err error) error { | ||
if fi.Mode().IsRegular() { | ||
files = append(files, strings.TrimPrefix(name, baseDir+"/")) | ||
} | ||
return nil | ||
}) | ||
|
||
return files | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add unit-test for this package, thanks a lot!