Skip to content

Commit

Permalink
Merge pull request #65 from buildpacks/close-file-handles-in-fake
Browse files Browse the repository at this point in the history
Closes all opened file handles in fake image
  • Loading branch information
ekcasey authored Oct 8, 2020
2 parents afd98bd + d2ecea5 commit cea9fc5
Showing 1 changed file with 45 additions and 34 deletions.
79 changes: 45 additions & 34 deletions fakes/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,15 @@ func (i *Image) AddLayerWithDiffID(path string, diffID string) error {
}

func shaForFile(path string) (string, error) {
file, err := os.Open(path)
rc, err := os.Open(path)
if err != nil {
return "", errors.Wrapf(err, "failed to open file")
}
defer rc.Close()

hasher := sha256.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", errors.Wrapf(err, "failed to copy file to hasher")
if _, err := io.Copy(hasher, rc); err != nil {
return "", errors.Wrapf(err, "failed to copy rc to hasher")
}

return hex.EncodeToString(hasher.Sum(make([]byte, 0, hasher.Size()))), nil
Expand Down Expand Up @@ -309,10 +310,13 @@ func (i *Image) FindLayerWithPath(path string) (string, error) {
// we iterate backwards over the layer array b/c later layers could replace a file with a given path
for idx := len(i.layers) - 1; idx >= 0; idx-- {
tarPath := i.layers[idx]
r, _ := os.Open(tarPath)
defer r.Close()
rc, err := os.Open(tarPath)
if err != nil {
return "", errors.Wrapf(err, "opening layer file '%s'", tarPath)
}
defer rc.Close()

tr := tar.NewReader(r)
tr := tar.NewReader(rc)
for {
header, err := tr.Next()
if err == io.EOF {
Expand All @@ -330,45 +334,52 @@ func (i *Image) FindLayerWithPath(path string) (string, error) {
}

func (i *Image) tarContents() string {
var strBuilder = strings.Builder{}
var strBuilder = &strings.Builder{}
strBuilder.WriteString("Layers\n-------\n")
for idx, tarPath := range i.layers {
strBuilder.WriteString(fmt.Sprintf("%s\n", filepath.Base(tarPath)))
i.writeLayerContents(strBuilder, tarPath)
if idx < len(i.layers)-1 {
strBuilder.WriteString("\n")
}
}
return strBuilder.String()
}

r, _ := os.Open(tarPath)
defer r.Close()
func (i *Image) writeLayerContents(strBuilder *strings.Builder, tarPath string) {
strBuilder.WriteString(fmt.Sprintf("%s\n", filepath.Base(tarPath)))

tr := tar.NewReader(r)
rc, err := os.Open(tarPath)
if err != nil {
strBuilder.WriteString(fmt.Sprintf("Error reading layer files: %s\n", err))
return
}
defer rc.Close()

hasFiles := false
for {
header, err := tr.Next()
if err == io.EOF {
if !hasFiles {
strBuilder.WriteString(" (empty)\n")
}
break
}
tr := tar.NewReader(rc)

var typ = "F"
var extra = ""
switch header.Typeflag {
case tar.TypeDir:
typ = "D"
case tar.TypeSymlink:
typ = "S"
extra = fmt.Sprintf(" -> %s", header.Linkname)
hasFiles := false
for {
header, err := tr.Next()
if err == io.EOF {
if !hasFiles {
strBuilder.WriteString(" (empty)\n")
}

strBuilder.WriteString(fmt.Sprintf(" - [%s] %s%s\n", typ, header.Name, extra))
hasFiles = true
break
}

if idx < len(i.layers)-1 {
strBuilder.WriteString("\n")
var typ = "F"
var extra = ""
switch header.Typeflag {
case tar.TypeDir:
typ = "D"
case tar.TypeSymlink:
typ = "S"
extra = fmt.Sprintf(" -> %s", header.Linkname)
}

strBuilder.WriteString(fmt.Sprintf(" - [%s] %s%s\n", typ, header.Name, extra))
hasFiles = true
}
return strBuilder.String()
}

func (i *Image) NumberOfAddedLayers() int {
Expand Down

0 comments on commit cea9fc5

Please sign in to comment.