Skip to content

Commit

Permalink
runtime CopyFrom/CopyTo support uid, gid, mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 authored and CMGS committed Sep 23, 2021
1 parent d1f0546 commit 1cc1e89
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
20 changes: 12 additions & 8 deletions engine/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,13 @@ func (e *Engine) VirtualizationResourceRemap(ctx context.Context, opts *enginety
}

// VirtualizationCopyTo copy things to virtualization
func (e *Engine) VirtualizationCopyTo(ctx context.Context, ID, target string, content io.Reader, AllowOverwriteDirWithFile, CopyUIDGID bool) error {
return withTarfileDump(ctx, target, content, func(target, tarfile string) error {
func (e *Engine) VirtualizationCopyTo(ctx context.Context, ID, target string, content []byte, uid, gid int, mode int64) error {
return withTarfileDump(ctx, target, content, uid, gid, mode, func(target, tarfile string) error {
content, err := os.Open(tarfile)
if err != nil {
return err
}
return e.client.CopyToContainer(ctx, ID, filepath.Dir(target), content, dockertypes.CopyToContainerOptions{AllowOverwriteDirWithFile: AllowOverwriteDirWithFile, CopyUIDGID: CopyUIDGID})
return e.client.CopyToContainer(ctx, ID, filepath.Dir(target), content, dockertypes.CopyToContainerOptions{AllowOverwriteDirWithFile: true, CopyUIDGID: false})
})
}

Expand Down Expand Up @@ -487,12 +487,16 @@ func (e *Engine) VirtualizationUpdateResource(ctx context.Context, ID string, op
}

// VirtualizationCopyFrom copy thing from a virtualization
func (e *Engine) VirtualizationCopyFrom(ctx context.Context, ID, path string) (io.ReadCloser, string, error) {
resp, stat, err := e.client.CopyFromContainer(ctx, ID, path)
func (e *Engine) VirtualizationCopyFrom(ctx context.Context, ID, path string) (content []byte, uid, gid int, mode int64, err error) {
resp, _, err := e.client.CopyFromContainer(ctx, ID, path)
if err != nil {
return nil, "", err
return
}
tarReader := tar.NewReader(resp)
_, err = tarReader.Next()
return ioutil.NopCloser(tarReader), stat.Name, errors.Wrapf(err, "read tarball from docker API failed: %s", path)
header, err := tarReader.Next()
if err != nil {
return
}
content, err = ioutil.ReadAll(tarReader)
return content, header.Uid, header.Gid, header.Mode, err
}
15 changes: 6 additions & 9 deletions engine/docker/tarfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ package docker
import (
"archive/tar"
"context"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/projecteru2/core/log"
)

func withTarfileDump(ctx context.Context, target string, content io.Reader, f func(target, tarfile string) error) error {
bytes, err := ioutil.ReadAll(content)
if err != nil {
return err
}
tarfile, err := tempTarFile(target, bytes)
func withTarfileDump(ctx context.Context, target string, content []byte, uid, gid int, mode int64, f func(target, tarfile string) error) error {
tarfile, err := tempTarFile(target, content, uid, gid, mode)

defer func(tarfile string) {
if err := os.RemoveAll(tarfile); err != nil {
Expand All @@ -30,7 +25,7 @@ func withTarfileDump(ctx context.Context, target string, content io.Reader, f fu
return f(target, tarfile)
}

func tempTarFile(path string, data []byte) (string, error) {
func tempTarFile(path string, data []byte, uid, gid int, mode int64) (string, error) {
filename := filepath.Base(path)
f, err := ioutil.TempFile(os.TempDir(), filename)
if err != nil {
Expand All @@ -43,8 +38,10 @@ func tempTarFile(path string, data []byte) (string, error) {
defer tw.Close()
hdr := &tar.Header{
Name: filename,
Mode: 0755,
Size: int64(len(data)),
Mode: int64(mode),
Uid: int(uid),
Gid: int(gid),
}
if err := tw.WriteHeader(hdr); err != nil {
return name, err
Expand Down
4 changes: 2 additions & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type API interface {

VirtualizationCreate(ctx context.Context, opts *enginetypes.VirtualizationCreateOptions) (*enginetypes.VirtualizationCreated, error)
VirtualizationResourceRemap(context.Context, *enginetypes.VirtualizationRemapOptions) (<-chan enginetypes.VirtualizationRemapMessage, error)
VirtualizationCopyTo(ctx context.Context, ID, target string, content io.Reader, AllowOverwriteDirWithFile, CopyUIDGID bool) error
VirtualizationCopyTo(ctx context.Context, ID, target string, content []byte, uid, gid int, mode int64) error
VirtualizationStart(ctx context.Context, ID string) error
VirtualizationStop(ctx context.Context, ID string, gracefulTimeout time.Duration) error
VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) error
Expand All @@ -47,7 +47,7 @@ type API interface {
VirtualizationResize(ctx context.Context, ID string, height, width uint) error
VirtualizationWait(ctx context.Context, ID, state string) (*enginetypes.VirtualizationWaitResult, error)
VirtualizationUpdateResource(ctx context.Context, ID string, opts *enginetypes.VirtualizationResource) error
VirtualizationCopyFrom(ctx context.Context, ID, path string) (io.ReadCloser, string, error)
VirtualizationCopyFrom(ctx context.Context, ID, path string) (content []byte, uid, gid int, mode int64, _ error)

ResourceValidate(ctx context.Context, cpu float64, cpumap map[string]int64, memory, storage int64) error
}
48 changes: 31 additions & 17 deletions engine/mocks/API.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -179,8 +178,8 @@ func (v *Virt) VirtualizationResourceRemap(ctx context.Context, opts *enginetype
}

// VirtualizationCopyTo copies one.
func (v *Virt) VirtualizationCopyTo(ctx context.Context, ID, dest string, content io.Reader, AllowOverwriteDirWithFile, CopyUIDGID bool) error {
return v.client.CopyToGuest(ctx, ID, dest, content, AllowOverwriteDirWithFile, CopyUIDGID)
func (v *Virt) VirtualizationCopyTo(ctx context.Context, ID, dest string, content []byte, uid, gid int, mode int64) error {
return v.client.CopyToGuest(ctx, ID, dest, bytes.NewReader(content), true, true)
}

// VirtualizationStart boots a guest.
Expand Down Expand Up @@ -261,12 +260,14 @@ func (v *Virt) VirtualizationUpdateResource(ctx context.Context, ID string, opts
}

// VirtualizationCopyFrom copies file content from the container.
func (v *Virt) VirtualizationCopyFrom(ctx context.Context, ID, path string) (io.ReadCloser, string, error) {
func (v *Virt) VirtualizationCopyFrom(ctx context.Context, ID, path string) (content []byte, uid, gid int, mode int64, err error) {
// TODO@zc: virt shall return the properties too
rd, err := v.client.Cat(ctx, ID, path)
if err != nil {
return nil, "", err
return
}
return ioutil.NopCloser(rd), filepath.Base(path), nil
content, err = ioutil.ReadAll(rd)
return
}

// VirtualizationExecute executes commands in running virtual unit
Expand Down

0 comments on commit 1cc1e89

Please sign in to comment.