mirror of https://github.com/docker/docs.git
Change misnamed TarFilter to TarWithOptions
Docker-DCO-1.1-Signed-off-by: Travis Cline <travis.cline@gmail.com> (github: tmc)
This commit is contained in:
parent
363b49948a
commit
111ab125b9
|
@ -27,6 +27,7 @@ type (
|
||||||
Compression int
|
Compression int
|
||||||
TarOptions struct {
|
TarOptions struct {
|
||||||
Includes []string
|
Includes []string
|
||||||
|
Excludes []string
|
||||||
Compression Compression
|
Compression Compression
|
||||||
NoLchown bool
|
NoLchown bool
|
||||||
}
|
}
|
||||||
|
@ -286,7 +287,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, L
|
||||||
// Tar creates an archive from the directory at `path`, and returns it as a
|
// Tar creates an archive from the directory at `path`, and returns it as a
|
||||||
// stream of bytes.
|
// stream of bytes.
|
||||||
func Tar(path string, compression Compression) (io.ReadCloser, error) {
|
func Tar(path string, compression Compression) (io.ReadCloser, error) {
|
||||||
return TarFilter(path, &TarOptions{Compression: compression})
|
return TarWithOptions(path, &TarOptions{Compression: compression})
|
||||||
}
|
}
|
||||||
|
|
||||||
func escapeName(name string) string {
|
func escapeName(name string) string {
|
||||||
|
@ -305,12 +306,9 @@ func escapeName(name string) string {
|
||||||
return string(escaped)
|
return string(escaped)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TarFilter creates an archive from the directory at `srcPath` with `options`, and returns it as a
|
// TarWithOptions creates an archive from the directory at `path`, only including files whose relative
|
||||||
// stream of bytes.
|
// paths are included in `options.Includes` (if non-nil) or not in `options.Excludes`.
|
||||||
//
|
func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error) {
|
||||||
// Files are included according to `options.Includes`, default to including all files.
|
|
||||||
// Stream is compressed according to `options.Compression', default to Uncompressed.
|
|
||||||
func TarFilter(srcPath string, options *TarOptions) (io.ReadCloser, error) {
|
|
||||||
pipeReader, pipeWriter := io.Pipe()
|
pipeReader, pipeWriter := io.Pipe()
|
||||||
|
|
||||||
compressWriter, err := CompressStream(pipeWriter, options.Compression)
|
compressWriter, err := CompressStream(pipeWriter, options.Compression)
|
||||||
|
@ -342,6 +340,21 @@ func TarFilter(srcPath string, options *TarOptions) (io.ReadCloser, error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, exclude := range options.Excludes {
|
||||||
|
matched, err := filepath.Match(exclude, relFilePath)
|
||||||
|
if err != nil {
|
||||||
|
utils.Errorf("Error matching: %s (pattern: %s)\n", relFilePath, exclude)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if matched {
|
||||||
|
utils.Debugf("Skipping excluded path: %s\n", relFilePath)
|
||||||
|
if f.IsDir() {
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := addTarFile(filePath, relFilePath, tw); err != nil {
|
if err := addTarFile(filePath, relFilePath, tw); err != nil {
|
||||||
utils.Debugf("Can't add file %s to tar: %s\n", srcPath, err)
|
utils.Debugf("Can't add file %s to tar: %s\n", srcPath, err)
|
||||||
}
|
}
|
||||||
|
@ -482,7 +495,7 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
||||||
// TarUntar aborts and returns the error.
|
// TarUntar aborts and returns the error.
|
||||||
func TarUntar(src string, dst string) error {
|
func TarUntar(src string, dst string) error {
|
||||||
utils.Debugf("TarUntar(%s %s)", src, dst)
|
utils.Debugf("TarUntar(%s %s)", src, dst)
|
||||||
archive, err := TarFilter(src, &TarOptions{Compression: Uncompressed})
|
archive, err := TarWithOptions(src, &TarOptions{Compression: Uncompressed})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,8 +63,8 @@ func TestCmdStreamGood(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tarUntar(t *testing.T, origin string, compression Compression) error {
|
func tarUntar(t *testing.T, origin string, options *TarOptions) ([]Change, error) {
|
||||||
archive, err := Tar(origin, compression)
|
archive, err := TarWithOptions(origin, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -72,37 +72,29 @@ func tarUntar(t *testing.T, origin string, compression Compression) error {
|
||||||
|
|
||||||
buf := make([]byte, 10)
|
buf := make([]byte, 10)
|
||||||
if _, err := archive.Read(buf); err != nil {
|
if _, err := archive.Read(buf); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
wrap := io.MultiReader(bytes.NewReader(buf), archive)
|
wrap := io.MultiReader(bytes.NewReader(buf), archive)
|
||||||
|
|
||||||
detectedCompression := DetectCompression(buf)
|
detectedCompression := DetectCompression(buf)
|
||||||
|
compression := options.Compression
|
||||||
if detectedCompression.Extension() != compression.Extension() {
|
if detectedCompression.Extension() != compression.Extension() {
|
||||||
return fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
|
return nil, fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp, err := ioutil.TempDir("", "docker-test-untar")
|
tmp, err := ioutil.TempDir("", "docker-test-untar")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(tmp)
|
defer os.RemoveAll(tmp)
|
||||||
if err := Untar(wrap, tmp, nil); err != nil {
|
if err := Untar(wrap, tmp, nil); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(tmp); err != nil {
|
if _, err := os.Stat(tmp); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
changes, err := ChangesDirs(origin, tmp)
|
return ChangesDirs(origin, tmp)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(changes) != 0 {
|
|
||||||
t.Fatalf("Unexpected differences after tarUntar: %v", changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTarUntar(t *testing.T) {
|
func TestTarUntar(t *testing.T) {
|
||||||
|
@ -122,9 +114,49 @@ func TestTarUntar(t *testing.T) {
|
||||||
Uncompressed,
|
Uncompressed,
|
||||||
Gzip,
|
Gzip,
|
||||||
} {
|
} {
|
||||||
if err := tarUntar(t, origin, c); err != nil {
|
changes, err := tarUntar(t, origin, &TarOptions{
|
||||||
|
Compression: c,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err)
|
t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(changes) != 0 {
|
||||||
|
t.Fatalf("Unexpected differences after tarUntar: %v", changes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTarWithOptions(t *testing.T) {
|
||||||
|
origin, err := ioutil.TempDir("", "docker-test-untar-origin")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(origin)
|
||||||
|
if err := ioutil.WriteFile(path.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := ioutil.WriteFile(path.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
opts *TarOptions
|
||||||
|
numChanges int
|
||||||
|
}{
|
||||||
|
{&TarOptions{Includes: []string{"1"}}, 1},
|
||||||
|
{&TarOptions{Excludes: []string{"2"}}, 1},
|
||||||
|
}
|
||||||
|
for _, testCase := range cases {
|
||||||
|
changes, err := tarUntar(t, origin, testCase.opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error tar/untar when testing inclusion/exclusion: %s", err)
|
||||||
|
}
|
||||||
|
if len(changes) != testCase.numChanges {
|
||||||
|
t.Errorf("Expected %d changes, got %d for %+v:",
|
||||||
|
testCase.numChanges, len(changes), testCase.opts)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -813,7 +813,7 @@ func (container *Container) Copy(resource string) (io.ReadCloser, error) {
|
||||||
basePath = path.Dir(basePath)
|
basePath = path.Dir(basePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
archive, err := archive.TarFilter(basePath, &archive.TarOptions{
|
archive, err := archive.TarWithOptions(basePath, &archive.TarOptions{
|
||||||
Compression: archive.Uncompressed,
|
Compression: archive.Uncompressed,
|
||||||
Includes: filter,
|
Includes: filter,
|
||||||
})
|
})
|
||||||
|
|
|
@ -295,7 +295,7 @@ func (a *Driver) Put(id string) {
|
||||||
|
|
||||||
// Returns an archive of the contents for the id
|
// Returns an archive of the contents for the id
|
||||||
func (a *Driver) Diff(id string) (archive.Archive, error) {
|
func (a *Driver) Diff(id string) (archive.Archive, error) {
|
||||||
return archive.TarFilter(path.Join(a.rootPath(), "diff", id), &archive.TarOptions{
|
return archive.TarWithOptions(path.Join(a.rootPath(), "diff", id), &archive.TarOptions{
|
||||||
Compression: archive.Uncompressed,
|
Compression: archive.Uncompressed,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue