2015-06-01 23:42:27 +00:00
|
|
|
//+build !windows
|
|
|
|
|
2014-10-29 19:06:51 +00:00
|
|
|
package chrootarchive
|
|
|
|
|
|
|
|
import (
|
2014-12-18 02:26:03 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-10-29 19:06:51 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-11-18 21:33:13 +00:00
|
|
|
"io/ioutil"
|
2014-10-29 19:06:51 +00:00
|
|
|
"os"
|
2014-12-08 21:14:56 +00:00
|
|
|
"path/filepath"
|
2014-10-29 19:06:51 +00:00
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-05-14 22:08:00 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2014-10-29 19:06:51 +00:00
|
|
|
)
|
|
|
|
|
2014-12-18 02:26:03 +00:00
|
|
|
type applyLayerResponse struct {
|
|
|
|
LayerSize int64 `json:"layerSize"`
|
|
|
|
}
|
|
|
|
|
2015-06-01 23:42:27 +00:00
|
|
|
// applyLayer is the entry-point for docker-applylayer on re-exec. This is not
|
|
|
|
// used on Windows as it does not support chroot, hence no point sandboxing
|
|
|
|
// through chroot and rexec.
|
2014-10-29 19:06:51 +00:00
|
|
|
func applyLayer() {
|
2015-05-14 22:08:00 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
tmpDir = ""
|
|
|
|
err error
|
|
|
|
)
|
2014-10-29 19:06:51 +00:00
|
|
|
runtime.LockOSThread()
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-06-01 23:42:27 +00:00
|
|
|
if err := chroot(flag.Arg(0)); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2015-05-14 22:08:00 +00:00
|
|
|
|
2015-06-01 23:42:27 +00:00
|
|
|
// We need to be able to set any perms
|
|
|
|
oldmask, err := system.Umask(0)
|
|
|
|
defer system.Umask(oldmask)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
2014-10-29 19:06:51 +00:00
|
|
|
}
|
2014-12-18 02:26:03 +00:00
|
|
|
|
2015-06-01 23:42:27 +00:00
|
|
|
if tmpDir, err = ioutil.TempDir("/", "temp-docker-extract"); err != nil {
|
2014-11-18 21:33:13 +00:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-18 02:26:03 +00:00
|
|
|
|
2014-11-18 21:33:13 +00:00
|
|
|
os.Setenv("TMPDIR", tmpDir)
|
2015-06-01 23:42:27 +00:00
|
|
|
size, err := archive.UnpackLayer("/", os.Stdin)
|
2014-12-08 21:14:56 +00:00
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
if err != nil {
|
2014-10-29 19:06:51 +00:00
|
|
|
fatal(err)
|
|
|
|
}
|
2014-12-18 02:26:03 +00:00
|
|
|
|
|
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
|
|
if err := encoder.Encode(applyLayerResponse{size}); err != nil {
|
|
|
|
fatal(fmt.Errorf("unable to encode layerSize JSON: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
flush(os.Stdout)
|
2014-12-08 23:04:34 +00:00
|
|
|
flush(os.Stdin)
|
2014-10-29 19:06:51 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2015-07-30 20:04:36 +00:00
|
|
|
// applyLayerHandler parses a diff in the standard layer format from `layer`, and
|
|
|
|
// applies it to the directory `dest`. Returns the size in bytes of the
|
|
|
|
// contents of the layer.
|
2015-08-04 01:52:54 +00:00
|
|
|
func applyLayerHandler(dest string, layer archive.Reader, decompress bool) (size int64, err error) {
|
2014-12-08 21:14:56 +00:00
|
|
|
dest = filepath.Clean(dest)
|
2015-07-27 13:46:20 +00:00
|
|
|
if decompress {
|
|
|
|
decompressed, err := archive.DecompressStream(layer)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer decompressed.Close()
|
|
|
|
|
|
|
|
layer = decompressed
|
2014-12-08 21:14:56 +00:00
|
|
|
}
|
2014-12-18 02:26:03 +00:00
|
|
|
|
2014-10-29 19:06:51 +00:00
|
|
|
cmd := reexec.Command("docker-applyLayer", dest)
|
2015-07-27 13:46:20 +00:00
|
|
|
cmd.Stdin = layer
|
2014-12-18 02:26:03 +00:00
|
|
|
|
|
|
|
outBuf, errBuf := new(bytes.Buffer), new(bytes.Buffer)
|
|
|
|
cmd.Stdout, cmd.Stderr = outBuf, errBuf
|
|
|
|
|
|
|
|
if err = cmd.Run(); err != nil {
|
|
|
|
return 0, fmt.Errorf("ApplyLayer %s stdout: %s stderr: %s", err, outBuf, errBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stdout should be a valid JSON struct representing an applyLayerResponse.
|
|
|
|
response := applyLayerResponse{}
|
|
|
|
decoder := json.NewDecoder(outBuf)
|
|
|
|
if err = decoder.Decode(&response); err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to decode ApplyLayer JSON response: %s", err)
|
2014-10-29 19:06:51 +00:00
|
|
|
}
|
2014-12-18 02:26:03 +00:00
|
|
|
|
|
|
|
return response.LayerSize, nil
|
2014-10-29 19:06:51 +00:00
|
|
|
}
|