rename discarder to proxyWriter

This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent d61e0a58f1
commit 1e745bdea1

View File

@ -8,6 +8,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"path/filepath" "path/filepath"
"strings" "strings"
@ -234,27 +235,23 @@ func whiteoutDirs(whreaddir map[string]int, nlayers int) []*tree {
// openTargz creates a tar reader from a targzip or tar // openTargz creates a tar reader from a targzip or tar
func openTargz(r io.Reader) (*tar.Reader, func() error) { func openTargz(r io.Reader) (*tar.Reader, func() error) {
var hdrbuf bytes.Buffer hdrbuf := &bytes.Buffer{}
hdrw := &discarder{w: &hdrbuf} hdrw := &proxyWriter{w: hdrbuf}
gz, err := gzip.NewReader(io.TeeReader(r, hdrw)) gz, err := gzip.NewReader(io.TeeReader(r, hdrw))
if err == nil { if err == nil {
hdrw.w = nil hdrw.w = ioutil.Discard
hdrbuf.Reset() hdrbuf = nil
return tar.NewReader(gz), gz.Close return tar.NewReader(gz), gz.Close
} }
return tar.NewReader(io.MultiReader(&hdrbuf, r)), func() error { return nil } return tar.NewReader(io.MultiReader(hdrbuf, r)), func() error { return nil }
} }
// discarder is a pass-through writer until instructed to 'discard' its writes // proxyWriter is a pass-through writer. Its underlying writer can be changed
// by setting its writer to nil. Useful for proxying writes from a TeeReader // on-the-fly.
// until it's known to be unnecessary. type proxyWriter struct {
type discarder struct {
w io.Writer w io.Writer
} }
func (d *discarder) Write(p []byte) (int, error) { func (pw *proxyWriter) Write(p []byte) (int, error) {
if d.w == nil { return pw.w.Write(p)
return len(p), nil
}
return d.w.Write(p)
} }