mirror of https://github.com/docker/docs.git
Revert "improve untar when using files instead of directories. Specifies behavior on non-existant targets."
This reverts commit 1c8d3106df
.
Conflicts:
archive/archive_test.go
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@docker.com> (github: crosbymichael)
This commit is contained in:
parent
10d066c9cb
commit
4b1a464ac1
|
@ -378,12 +378,10 @@ func TarWithOptions(srcPath string, options *TarOptions) (io.ReadCloser, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Untar reads a stream of bytes from `archive`, parses it as a tar archive,
|
// Untar reads a stream of bytes from `archive`, parses it as a tar archive,
|
||||||
// and unpacks it into the directory at `dest`.
|
// and unpacks it into the directory at `path`.
|
||||||
// The archive may be compressed with one of the following algorithms:
|
// The archive may be compressed with one of the following algorithms:
|
||||||
// identity (uncompressed), gzip, bzip2, xz.
|
// identity (uncompressed), gzip, bzip2, xz.
|
||||||
// If `dest` does not exist, it is created unless there are multiple entries in `archive`.
|
// FIXME: specify behavior when target path exists vs. doesn't exist.
|
||||||
// In the latter case, an error is returned.
|
|
||||||
// An other error is returned if `dest` exists but is not a directory, to prevent overwriting.
|
|
||||||
func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
||||||
if archive == nil {
|
if archive == nil {
|
||||||
return fmt.Errorf("Empty archive")
|
return fmt.Errorf("Empty archive")
|
||||||
|
@ -397,21 +395,7 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
||||||
|
|
||||||
tr := tar.NewReader(decompressedArchive)
|
tr := tar.NewReader(decompressedArchive)
|
||||||
|
|
||||||
var (
|
var dirs []*tar.Header
|
||||||
dirs []*tar.Header
|
|
||||||
destNotExist bool
|
|
||||||
multipleEntries bool
|
|
||||||
)
|
|
||||||
|
|
||||||
if fi, err := os.Lstat(dest); err != nil {
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// destination does not exist, so it is assumed it has to be created.
|
|
||||||
destNotExist = true
|
|
||||||
} else if !fi.IsDir() {
|
|
||||||
return fmt.Errorf("Trying to untar to `%s`: exists but not a directory", dest)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate through the files in the archive.
|
// Iterate through the files in the archive.
|
||||||
for {
|
for {
|
||||||
|
@ -424,11 +408,6 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return an error if destination needs to be created and there is more than 1 entry in the tar stream.
|
|
||||||
if destNotExist && multipleEntries {
|
|
||||||
return fmt.Errorf("Trying to untar an archive with multiple entries to an inexistant target `%s`: did you mean `%s` instead?", dest, filepath.Dir(dest))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize name, for safety and for a simple is-root check
|
// Normalize name, for safety and for a simple is-root check
|
||||||
hdr.Name = filepath.Clean(hdr.Name)
|
hdr.Name = filepath.Clean(hdr.Name)
|
||||||
|
|
||||||
|
@ -444,12 +423,7 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var path string
|
path := filepath.Join(dest, hdr.Name)
|
||||||
if destNotExist {
|
|
||||||
path = dest // we are renaming hdr.Name to dest
|
|
||||||
} else {
|
|
||||||
path = filepath.Join(dest, hdr.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If path exits we almost always just want to remove and replace it
|
// If path exits we almost always just want to remove and replace it
|
||||||
// The only exception is when it is a directory *and* the file from
|
// The only exception is when it is a directory *and* the file from
|
||||||
|
@ -469,9 +443,6 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Successfully added an entry. Predicting multiple entries for next iteration (not current one).
|
|
||||||
multipleEntries = true
|
|
||||||
|
|
||||||
// Directory mtimes must be handled at the end to avoid further
|
// Directory mtimes must be handled at the end to avoid further
|
||||||
// file creation in them to modify the directory mtime
|
// file creation in them to modify the directory mtime
|
||||||
if hdr.Typeflag == tar.TypeDir {
|
if hdr.Typeflag == tar.TypeDir {
|
||||||
|
|
|
@ -160,44 +160,6 @@ func TestTarWithOptions(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTarUntarFile(t *testing.T) {
|
|
||||||
origin, err := ioutil.TempDir("", "docker-test-untar-origin-file")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(origin)
|
|
||||||
|
|
||||||
if err := os.MkdirAll(path.Join(origin, "before"), 0700); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := os.MkdirAll(path.Join(origin, "after"), 0700); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := ioutil.WriteFile(path.Join(origin, "before", "file"), []byte("hello world"), 0700); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tar, err := TarWithOptions(path.Join(origin, "before"), &TarOptions{Compression: Uncompressed, Includes: []string{"file"}})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := Untar(tar, path.Join(origin, "after", "file2"), nil); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
catCmd := exec.Command("cat", path.Join(origin, "after", "file2"))
|
|
||||||
out, err := CmdStream(catCmd, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to start command: %s", err)
|
|
||||||
}
|
|
||||||
if output, err := ioutil.ReadAll(out); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else if string(output) != "hello world" {
|
|
||||||
t.Fatalf("Expected 'hello world', got '%s'", output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Some tar archives such as http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev21.tar.gz
|
// Some tar archives such as http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev21.tar.gz
|
||||||
// use PAX Global Extended Headers.
|
// use PAX Global Extended Headers.
|
||||||
// Failing prevents the archives from being uncompressed during ADD
|
// Failing prevents the archives from being uncompressed during ADD
|
||||||
|
|
Loading…
Reference in New Issue