mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
archive: Remove unused features
This simplifies that code that calls out to tar by removing support for now unused features. Docker-DCO-1.1-Signed-off-by: Alexander Larsson <alexl@redhat.com> (github: alexlarsson)
This commit is contained in:
parent
69be8adb68
commit
4fb1db7f74
4 changed files with 11 additions and 79 deletions
|
@ -23,10 +23,7 @@ type Compression int
|
||||||
|
|
||||||
type TarOptions struct {
|
type TarOptions struct {
|
||||||
Includes []string
|
Includes []string
|
||||||
Excludes []string
|
|
||||||
Recursive bool
|
|
||||||
Compression Compression
|
Compression Compression
|
||||||
CreateFiles []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -66,7 +63,7 @@ func DetectCompression(source []byte) Compression {
|
||||||
func xzDecompress(archive io.Reader) (io.Reader, error) {
|
func xzDecompress(archive io.Reader) (io.Reader, error) {
|
||||||
args := []string{"xz", "-d", "-c", "-q"}
|
args := []string{"xz", "-d", "-c", "-q"}
|
||||||
|
|
||||||
return CmdStream(exec.Command(args[0], args[1:]...), archive, nil)
|
return CmdStream(exec.Command(args[0], args[1:]...), archive)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecompressStream(archive io.Reader) (io.Reader, error) {
|
func DecompressStream(archive io.Reader) (io.Reader, error) {
|
||||||
|
@ -207,7 +204,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader *tar.Reader)
|
||||||
// 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.Reader, error) {
|
func Tar(path string, compression Compression) (io.Reader, error) {
|
||||||
return TarFilter(path, &TarOptions{Recursive: true, Compression: compression})
|
return TarFilter(path, &TarOptions{Compression: compression})
|
||||||
}
|
}
|
||||||
|
|
||||||
func escapeName(name string) string {
|
func escapeName(name string) string {
|
||||||
|
@ -235,50 +232,12 @@ func TarFilter(path string, options *TarOptions) (io.Reader, error) {
|
||||||
}
|
}
|
||||||
args = append(args, "-c"+options.Compression.Flag())
|
args = append(args, "-c"+options.Compression.Flag())
|
||||||
|
|
||||||
for _, exclude := range options.Excludes {
|
|
||||||
args = append(args, fmt.Sprintf("--exclude=%s", exclude))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !options.Recursive {
|
|
||||||
args = append(args, "--no-recursion")
|
|
||||||
}
|
|
||||||
|
|
||||||
files := ""
|
files := ""
|
||||||
for _, f := range options.Includes {
|
for _, f := range options.Includes {
|
||||||
files = files + escapeName(f) + "\n"
|
files = files + escapeName(f) + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpDir := ""
|
return CmdStream(exec.Command(args[0], args[1:]...), bytes.NewBufferString(files))
|
||||||
|
|
||||||
if options.CreateFiles != nil {
|
|
||||||
var err error // Can't use := here or we override the outer tmpDir
|
|
||||||
tmpDir, err = ioutil.TempDir("", "docker-tar")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
files = files + "-C" + tmpDir + "\n"
|
|
||||||
for _, f := range options.CreateFiles {
|
|
||||||
path := filepath.Join(tmpDir, f)
|
|
||||||
err := os.MkdirAll(filepath.Dir(path), 0600)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if file, err := os.OpenFile(path, os.O_CREATE, 0600); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
file.Close()
|
|
||||||
}
|
|
||||||
files = files + escapeName(f) + "\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return CmdStream(exec.Command(args[0], args[1:]...), bytes.NewBufferString(files), func() {
|
|
||||||
if tmpDir != "" {
|
|
||||||
_ = os.RemoveAll(tmpDir)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,
|
||||||
|
@ -311,19 +270,6 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if options != nil {
|
|
||||||
excludeFile := false
|
|
||||||
for _, exclude := range options.Excludes {
|
|
||||||
if strings.HasPrefix(hdr.Name, exclude) {
|
|
||||||
excludeFile = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if excludeFile {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
||||||
|
|
||||||
|
@ -378,9 +324,9 @@ func Untar(archive io.Reader, dest string, options *TarOptions) error {
|
||||||
// TarUntar is a convenience function which calls Tar and Untar, with
|
// TarUntar is a convenience function which calls Tar and Untar, with
|
||||||
// the output of one piped into the other. If either Tar or Untar fails,
|
// the output of one piped into the other. If either Tar or Untar fails,
|
||||||
// TarUntar aborts and returns the error.
|
// TarUntar aborts and returns the error.
|
||||||
func TarUntar(src string, filter []string, dst string) error {
|
func TarUntar(src string, dst string) error {
|
||||||
utils.Debugf("TarUntar(%s %s %s)", src, filter, dst)
|
utils.Debugf("TarUntar(%s %s)", src, dst)
|
||||||
archive, err := TarFilter(src, &TarOptions{Compression: Uncompressed, Includes: filter, Recursive: true})
|
archive, err := TarFilter(src, &TarOptions{Compression: Uncompressed})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -417,7 +363,7 @@ func CopyWithTar(src, dst string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
utils.Debugf("Calling TarUntar(%s, %s)", src, dst)
|
utils.Debugf("Calling TarUntar(%s, %s)", src, dst)
|
||||||
return TarUntar(src, nil, dst)
|
return TarUntar(src, dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CopyFileWithTar emulates the behavior of the 'cp' command-line
|
// CopyFileWithTar emulates the behavior of the 'cp' command-line
|
||||||
|
@ -480,13 +426,10 @@ func CopyFileWithTar(src, dst string) (err error) {
|
||||||
// CmdStream executes a command, and returns its stdout as a stream.
|
// CmdStream executes a command, and returns its stdout as a stream.
|
||||||
// If the command fails to run or doesn't complete successfully, an error
|
// If the command fails to run or doesn't complete successfully, an error
|
||||||
// will be returned, including anything written on stderr.
|
// will be returned, including anything written on stderr.
|
||||||
func CmdStream(cmd *exec.Cmd, input io.Reader, atEnd func()) (io.Reader, error) {
|
func CmdStream(cmd *exec.Cmd, input io.Reader) (io.Reader, error) {
|
||||||
if input != nil {
|
if input != nil {
|
||||||
stdin, err := cmd.StdinPipe()
|
stdin, err := cmd.StdinPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if atEnd != nil {
|
|
||||||
atEnd()
|
|
||||||
}
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Write stdin if any
|
// Write stdin if any
|
||||||
|
@ -497,16 +440,10 @@ func CmdStream(cmd *exec.Cmd, input io.Reader, atEnd func()) (io.Reader, error)
|
||||||
}
|
}
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if atEnd != nil {
|
|
||||||
atEnd()
|
|
||||||
}
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
stderr, err := cmd.StderrPipe()
|
stderr, err := cmd.StderrPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if atEnd != nil {
|
|
||||||
atEnd()
|
|
||||||
}
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
pipeR, pipeW := io.Pipe()
|
pipeR, pipeW := io.Pipe()
|
||||||
|
@ -531,9 +468,6 @@ func CmdStream(cmd *exec.Cmd, input io.Reader, atEnd func()) (io.Reader, error)
|
||||||
} else {
|
} else {
|
||||||
pipeW.Close()
|
pipeW.Close()
|
||||||
}
|
}
|
||||||
if atEnd != nil {
|
|
||||||
atEnd()
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
// Run the command and return the pipe
|
// Run the command and return the pipe
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
|
|
||||||
func TestCmdStreamLargeStderr(t *testing.T) {
|
func TestCmdStreamLargeStderr(t *testing.T) {
|
||||||
cmd := exec.Command("/bin/sh", "-c", "dd if=/dev/zero bs=1k count=1000 of=/dev/stderr; echo hello")
|
cmd := exec.Command("/bin/sh", "-c", "dd if=/dev/zero bs=1k count=1000 of=/dev/stderr; echo hello")
|
||||||
out, err := CmdStream(cmd, nil, nil)
|
out, err := CmdStream(cmd, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to start command: %s", err)
|
t.Fatalf("Failed to start command: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ func TestCmdStreamLargeStderr(t *testing.T) {
|
||||||
|
|
||||||
func TestCmdStreamBad(t *testing.T) {
|
func TestCmdStreamBad(t *testing.T) {
|
||||||
badCmd := exec.Command("/bin/sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
|
badCmd := exec.Command("/bin/sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
|
||||||
out, err := CmdStream(badCmd, nil, nil)
|
out, err := CmdStream(badCmd, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to start command: %s", err)
|
t.Fatalf("Failed to start command: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ func TestCmdStreamBad(t *testing.T) {
|
||||||
|
|
||||||
func TestCmdStreamGood(t *testing.T) {
|
func TestCmdStreamGood(t *testing.T) {
|
||||||
cmd := exec.Command("/bin/sh", "-c", "echo hello; exit 0")
|
cmd := exec.Command("/bin/sh", "-c", "echo hello; exit 0")
|
||||||
out, err := CmdStream(cmd, nil, nil)
|
out, err := CmdStream(cmd, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1457,7 +1457,6 @@ func (container *Container) Copy(resource string) (archive.Archive, error) {
|
||||||
return archive.TarFilter(basePath, &archive.TarOptions{
|
return archive.TarFilter(basePath, &archive.TarOptions{
|
||||||
Compression: archive.Uncompressed,
|
Compression: archive.Uncompressed,
|
||||||
Includes: filter,
|
Includes: filter,
|
||||||
Recursive: true,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -225,7 +225,6 @@ func (a *Driver) Get(id string) (string, error) {
|
||||||
// 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.TarFilter(path.Join(a.rootPath(), "diff", id), &archive.TarOptions{
|
||||||
Recursive: true,
|
|
||||||
Compression: archive.Uncompressed,
|
Compression: archive.Uncompressed,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue