pkg/chrootarchive: pass TarOptions via CLI arg
Signed-off-by: Tibor Vass <teabee89@gmail.com> Conflicts: graph/load.go fixed conflict in imports
This commit is contained in:
parent
e19f49915f
commit
466e44195a
3 changed files with 56 additions and 2 deletions
|
@ -1,11 +1,14 @@
|
||||||
package chrootarchive
|
package chrootarchive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/archive"
|
"github.com/docker/docker/pkg/archive"
|
||||||
|
@ -22,7 +25,12 @@ func untar() {
|
||||||
if err := syscall.Chdir("/"); err != nil {
|
if err := syscall.Chdir("/"); err != nil {
|
||||||
fatal(err)
|
fatal(err)
|
||||||
}
|
}
|
||||||
if err := archive.Untar(os.Stdin, "/", nil); err != nil {
|
options := new(archive.TarOptions)
|
||||||
|
dec := json.NewDecoder(strings.NewReader(flag.Arg(1)))
|
||||||
|
if err := dec.Decode(options); err != nil {
|
||||||
|
fatal(err)
|
||||||
|
}
|
||||||
|
if err := archive.Untar(os.Stdin, "/", options); err != nil {
|
||||||
fatal(err)
|
fatal(err)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -33,12 +41,18 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Untar(archive io.Reader, dest string, options *archive.TarOptions) error {
|
func Untar(archive io.Reader, dest string, options *archive.TarOptions) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
enc := json.NewEncoder(&buf)
|
||||||
|
if err := enc.Encode(options); err != nil {
|
||||||
|
return fmt.Errorf("Untar json encode: %v", err)
|
||||||
|
}
|
||||||
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
||||||
if err := os.MkdirAll(dest, 0777); err != nil {
|
if err := os.MkdirAll(dest, 0777); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cmd := reexec.Command("docker-untar", dest)
|
|
||||||
|
cmd := reexec.Command("docker-untar", dest, buf.String())
|
||||||
cmd.Stdin = archive
|
cmd.Stdin = archive
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
39
chrootarchive/archive_test.go
Normal file
39
chrootarchive/archive_test.go
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
package chrootarchive
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/docker/pkg/archive"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChrootTarUntar(t *testing.T) {
|
||||||
|
tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntar")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpdir)
|
||||||
|
src := filepath.Join(tmpdir, "src")
|
||||||
|
if err := os.MkdirAll(src, 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := ioutil.WriteFile(filepath.Join(src, "toto"), []byte("hello toto"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := ioutil.WriteFile(filepath.Join(src, "lolo"), []byte("hello lolo"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
stream, err := archive.Tar(src, archive.Uncompressed)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
dest := filepath.Join(tmpdir, "src")
|
||||||
|
if err := os.MkdirAll(dest, 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := Untar(stream, dest, &archive.TarOptions{Excludes: []string{"lolo"}}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
reexec.Register("docker-untar", untar)
|
reexec.Register("docker-untar", untar)
|
||||||
reexec.Register("docker-applyLayer", applyLayer)
|
reexec.Register("docker-applyLayer", applyLayer)
|
||||||
|
reexec.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
func fatal(err error) {
|
func fatal(err error) {
|
||||||
|
|
Loading…
Reference in a new issue