Skip to content

Commit

Permalink
Merge pull request #54 from openhie/PLAT-676-archive-size-issue-fix
Browse files Browse the repository at this point in the history
PLAT-676 TarSource write too long bug fix
  • Loading branch information
michaelloosen authored Feb 16, 2023
2 parents 2926ae7 + 352becd commit 418b199
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 69 deletions.
2 changes: 1 addition & 1 deletion cli/src/cmd/version/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.2
2.1.3
27 changes: 4 additions & 23 deletions cli/src/core/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/stdcopy"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/log"
Expand Down Expand Up @@ -112,7 +111,7 @@ func mountCustomPackage(ctx context.Context, cli *client.Client, customPackage c
}
}

customPackageReader, err := file.TarSource(CUSTOM_PACKAGE_LOCAL_PATH)
customPackageReader, err := file.TarSource(customPackageTmpLocation)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,30 +152,12 @@ func copyCredsToInstantContainer() (err error) {
return err
}

dstInfo := archive.CopyInfo{
Path: "/root/.docker/",
Exists: true,
IsDir: true,
}

srcInfo, err := archive.CopyInfoSourcePath(dockerCredsPath, false)
if err != nil {
return errors.Wrap(err, "")
}

srcArchive, err := archive.TarResource(srcInfo)
preparedArchive, err := file.TarSource(dockerCredsPath)
if err != nil {
return errors.Wrap(err, "")
}
defer srcArchive.Close()

dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
if err != nil {
return errors.Wrap(err, "")
return err
}
defer preparedArchive.Close()

err = client.CopyToContainer(context.Background(), instantContainer.ID, dstDir, preparedArchive, types.CopyToContainerOptions{
err = client.CopyToContainer(context.Background(), instantContainer.ID, "/root/.docker/", preparedArchive, types.CopyToContainerOptions{
CopyUIDGID: true,
})
if err != nil {
Expand Down
63 changes: 19 additions & 44 deletions cli/src/util/file/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,42 @@ package file

import (
"archive/tar"
"bufio"
"bytes"
"io"
"os"
"path/filepath"
"strings"

"github.com/docker/docker/pkg/archive"
"github.com/luno/jettison/errors"
)

func TarSource(path string) (io.Reader, error) {
var buf bytes.Buffer
tw := tar.NewWriter(&buf)

ok := filepath.Walk(path, func(file string, fi os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err, "")
}

header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return errors.Wrap(err, "")
}
header.Name = strings.TrimPrefix(strings.Replace(file, path, "", -1), string(filepath.Separator))
err = tw.WriteHeader(header)
if err != nil {
return errors.Wrap(err, "")
}

f, err := os.Open(file)
if err != nil {
return errors.Wrap(err, "")
}

if fi.IsDir() {
return nil
}

_, err = io.Copy(tw, f)
if err != nil {
return errors.Wrap(err, "")
}
dstInfo := archive.CopyInfo{
Exists: true,
IsDir: true,
}

err = f.Close()
if err != nil {
return errors.Wrap(err, "")
}
_, err := os.Stat(path)
if err != nil {
return nil, errors.Wrap(err, "")
}

return nil
})
srcInfo, err := archive.CopyInfoSourcePath(path, false)
if err != nil {
return nil, errors.Wrap(err, "")
}

if ok != nil {
return nil, ok
srcArchive, err := archive.TarResource(srcInfo)
if err != nil {
return nil, errors.Wrap(err, "")
}

err := tw.Close()
_, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
if err != nil {
return nil, errors.Wrap(err, "")
}
defer preparedArchive.Close()

return bufio.NewReader(&buf), nil
return preparedArchive, nil
}

func UntarSource(source, destination string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/util/file/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func Test_tarSource(t *testing.T) {
re, err := TarSource(tc.contentFileName)
if err != nil {
expectedErr := fs.PathError{
Op: "lstat",
Op: "stat",
Err: unix.ENOENT,
}

Expand Down

0 comments on commit 418b199

Please sign in to comment.