Skip to content

Commit

Permalink
Move WindowsWriter into imgutil
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Meyer <meyeran@vmware.com>
  • Loading branch information
ameyer-pivotal authored and Javier Romero and Andrew Meyer committed May 5, 2020
1 parent aeb6e51 commit 212fd84
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 294 deletions.
4 changes: 2 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/buildpacks/imgutil"
ilayer "github.com/buildpacks/imgutil/layer"
"github.com/docker/docker/api/types"
"github.com/docker/docker/volume/mounts"
"github.com/google/go-containerregistry/pkg/name"
Expand All @@ -26,7 +27,6 @@ import (
"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/buildpackage"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/layer"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/stack"
"github.com/buildpacks/pack/internal/stringset"
Expand Down Expand Up @@ -454,7 +454,7 @@ func (c *Client) processBuildpacks(ctx context.Context, builderImage imgutil.Ima
return fetchedBPs, order, errors.Wrapf(err, "extracting buildpacks from %s", style.Symbol(bp))
}
} else {
layerWriterFactory, err := layer.NewTarWriterFactory(builderImage)
layerWriterFactory, err := ilayer.NewTarWriterFactory(builderImage)
if err != nil {
return fetchedBPs, order, errors.Wrapf(err, "buildpack layer writer for image %s", style.Symbol(builderImage.Name()))
}
Expand Down
4 changes: 2 additions & 2 deletions create_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Masterminds/semver"
"github.com/buildpacks/imgutil"
ilayer "github.com/buildpacks/imgutil/layer"
"github.com/pkg/errors"

"github.com/buildpacks/pack/internal/buildpack"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/image"
"github.com/buildpacks/pack/internal/layer"
"github.com/buildpacks/pack/internal/style"
)

Expand Down Expand Up @@ -102,7 +102,7 @@ func (c *Client) CreateBuilder(ctx context.Context, opts CreateBuilderOptions) e
return errors.Wrapf(err, "downloading buildpack from %s", style.Symbol(b.URI))
}

layerWriterFactory, err := layer.NewTarWriterFactory(bldr.Image())
layerWriterFactory, err := ilayer.NewTarWriterFactory(bldr.Image())
if err != nil {
return errors.Wrapf(err, "buildpack layer writer for image %s", style.Symbol(bldr.Name()))
}
Expand Down
20 changes: 11 additions & 9 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"path/filepath"
"time"

iarchive "github.com/buildpacks/imgutil/archive"

"github.com/docker/docker/pkg/ioutils"
"github.com/pkg/errors"
)
Expand All @@ -22,25 +24,25 @@ func init() {
}

func ReadDirAsTar(srcDir, basePath string, uid, gid int, mode int64, normalizeModTime bool, fileFilter func(string) bool) io.ReadCloser {
return GenerateTar(func(tw TarWriter) error {
return GenerateTar(func(tw iarchive.TarWriter) error {
return WriteDirToTar(tw, srcDir, basePath, uid, gid, mode, normalizeModTime, fileFilter)
})
}

func ReadZipAsTar(srcPath, basePath string, uid, gid int, mode int64, normalizeModTime bool, fileFilter func(string) bool) io.ReadCloser {
return GenerateTar(func(tw TarWriter) error {
return GenerateTar(func(tw iarchive.TarWriter) error {
return WriteZipToTar(tw, srcPath, basePath, uid, gid, mode, normalizeModTime, fileFilter)
})
}

func GenerateTar(genFn func(TarWriter) error) io.ReadCloser {
return GenerateTarWithWriter(genFn, DefaultTarWriterFactory)
func GenerateTar(genFn func(iarchive.TarWriter) error) io.ReadCloser {
return GenerateTarWithWriter(genFn, iarchive.DefaultTarWriterFactory)
}

// GenerateTarWithTar returns a reader to a tar from a generator function using a writer from the provided factory.
// Note that the generator will not fully execute until the reader is fully read from. Any errors returned by the
// generator will be returned when reading the reader.
func GenerateTarWithWriter(genFn func(TarWriter) error, twf TarWriterFactory) io.ReadCloser {
func GenerateTarWithWriter(genFn func(iarchive.TarWriter) error, twf iarchive.TarWriterFactory) io.ReadCloser {
errChan := make(chan error)
pr, pw := io.Pipe()

Expand Down Expand Up @@ -94,13 +96,13 @@ func aggregateError(base, addition error) error {
func CreateSingleFileTarReader(path, txt string) io.ReadCloser {
tarBuilder := TarBuilder{}
tarBuilder.AddFile(path, 0644, NormalizedDateTime, []byte(txt))
return tarBuilder.Reader(DefaultTarWriterFactory)
return tarBuilder.Reader(iarchive.DefaultTarWriterFactory)
}

func CreateSingleFileTar(tarFile, path, txt string) error {
tarBuilder := TarBuilder{}
tarBuilder.AddFile(path, 0644, NormalizedDateTime, []byte(txt))
return tarBuilder.WriteToPath(tarFile, DefaultTarWriterFactory)
return tarBuilder.WriteToPath(tarFile, iarchive.DefaultTarWriterFactory)
}

// ErrEntryNotExist is an error returned if an entry path doesn't exist
Expand Down Expand Up @@ -138,7 +140,7 @@ func ReadTarEntry(rc io.Reader, entryPath string) (*tar.Header, []byte, error) {

// WriteDirToTar writes the contents of a directory to a tar writer. `basePath` is the "location" in the tar the
// contents will be placed.
func WriteDirToTar(tw TarWriter, srcDir, basePath string, uid, gid int, mode int64, normalizeModTime bool, fileFilter func(string) bool) error {
func WriteDirToTar(tw iarchive.TarWriter, srcDir, basePath string, uid, gid int, mode int64, normalizeModTime bool, fileFilter func(string) bool) error {
return filepath.Walk(srcDir, func(file string, fi os.FileInfo, err error) error {
if fileFilter != nil && !fileFilter(file) {
return nil
Expand Down Expand Up @@ -200,7 +202,7 @@ func WriteDirToTar(tw TarWriter, srcDir, basePath string, uid, gid int, mode int
}

// WriteZipToTar writes the contents of a zip file to a tar writer.
func WriteZipToTar(tw TarWriter, srcZip, basePath string, uid, gid int, mode int64, normalizeModTime bool, fileFilter func(string) bool) error {
func WriteZipToTar(tw iarchive.TarWriter, srcZip, basePath string, uid, gid int, mode int64, normalizeModTime bool, fileFilter func(string) bool) error {
zipReader, err := zip.OpenReader(srcZip)
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions internal/archive/tar_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"time"

iarchive "github.com/buildpacks/imgutil/archive"
"github.com/pkg/errors"

"github.com/buildpacks/pack/internal/style"
Expand Down Expand Up @@ -42,7 +43,7 @@ func (t *TarBuilder) AddDir(path string, mode int64, modTime time.Time) {
})
}

func (t *TarBuilder) Reader(twf TarWriterFactory) io.ReadCloser {
func (t *TarBuilder) Reader(twf iarchive.TarWriterFactory) io.ReadCloser {
pr, pw := io.Pipe()
go func() {
var err error
Expand All @@ -55,7 +56,7 @@ func (t *TarBuilder) Reader(twf TarWriterFactory) io.ReadCloser {
return pr
}

func (t *TarBuilder) WriteToPath(path string, twf TarWriterFactory) error {
func (t *TarBuilder) WriteToPath(path string, twf iarchive.TarWriterFactory) error {
fh, err := os.Create(path)
if err != nil {
return errors.Wrapf(err, "create file for tar: %s", style.Symbol(path))
Expand All @@ -66,7 +67,7 @@ func (t *TarBuilder) WriteToPath(path string, twf TarWriterFactory) error {
return err
}

func (t *TarBuilder) WriteTo(w io.Writer, twf TarWriterFactory) (int64, error) {
func (t *TarBuilder) WriteTo(w io.Writer, twf iarchive.TarWriterFactory) (int64, error) {
var written int64
tw := twf.NewTarWriter(w)
defer tw.Close()
Expand Down
8 changes: 5 additions & 3 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

"github.com/BurntSushi/toml"
"github.com/buildpacks/imgutil"
iarchive "github.com/buildpacks/imgutil/archive"
ilayer "github.com/buildpacks/imgutil/layer"
"github.com/pkg/errors"

"github.com/buildpacks/pack/builder"
Expand Down Expand Up @@ -51,7 +53,7 @@ const (
type Builder struct {
baseImageName string
image imgutil.Image
layerWriterFactory archive.TarWriterFactory
layerWriterFactory iarchive.TarWriterFactory
lifecycle Lifecycle
lifecycleDescriptor LifecycleDescriptor
additionalBuildpacks []dist.Buildpack
Expand Down Expand Up @@ -89,7 +91,7 @@ func New(baseImage imgutil.Image, name string) (*Builder, error) {
}

func constructBuilder(img imgutil.Image, newName string, metadata Metadata) (*Builder, error) {
layerWriterFactory, err := layer.NewTarWriterFactory(img)
layerWriterFactory, err := ilayer.NewTarWriterFactory(img)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -586,7 +588,7 @@ func (b *Builder) stackLayer(dest string) (string, error) {
return layerTar, nil
}

func (b *Builder) embedLifecycleTar(tw archive.TarWriter) error {
func (b *Builder) embedLifecycleTar(tw iarchive.TarWriter) error {
var regex = regexp.MustCompile(`^[^/]+/([^/]+)$`)

lr, err := b.lifecycle.Open()
Expand Down
7 changes: 4 additions & 3 deletions internal/dist/buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path"

"github.com/BurntSushi/toml"
iarchive "github.com/buildpacks/imgutil/archive"
"github.com/pkg/errors"

"github.com/buildpacks/pack/internal/api"
Expand Down Expand Up @@ -77,7 +78,7 @@ func BuildpackFromBlob(bpd BuildpackDescriptor, blob Blob) Buildpack {
// BuildpackFromRootBlob constructs a buildpack from a blob. It is assumed that the buildpack contents reside at the
// root of the blob. The constructed buildpack contents will be structured as per the distribution spec (currently
// a tar with contents under '/cnbs/buildpacks/{ID}/{version}/*').
func BuildpackFromRootBlob(blob Blob, layerWriterFactory archive.TarWriterFactory) (Buildpack, error) {
func BuildpackFromRootBlob(blob Blob, layerWriterFactory iarchive.TarWriterFactory) (Buildpack, error) {
bpd := BuildpackDescriptor{}
rc, err := blob.Open()
if err != nil {
Expand Down Expand Up @@ -106,7 +107,7 @@ func BuildpackFromRootBlob(blob Blob, layerWriterFactory archive.TarWriterFactor
Blob: &distBlob{
openFn: func() io.ReadCloser {
return archive.GenerateTarWithWriter(
func(tw archive.TarWriter) error {
func(tw iarchive.TarWriter) error {
return toDistTar(tw, bpd, blob)
},
layerWriterFactory,
Expand All @@ -124,7 +125,7 @@ func (b *distBlob) Open() (io.ReadCloser, error) {
return b.openFn(), nil
}

func toDistTar(tw archive.TarWriter, bpd BuildpackDescriptor, blob Blob) error {
func toDistTar(tw iarchive.TarWriter, bpd BuildpackDescriptor, blob Blob) error {
ts := archive.NormalizedDateTime

if err := tw.WriteHeader(&tar.Header{
Expand Down
Loading

0 comments on commit 212fd84

Please sign in to comment.