Skip to content

Commit

Permalink
pkg/download: close files after extracting in tar (#310)
Browse files Browse the repository at this point in the history
* pkg/download: close files after extracting in tar

- Close file handle after extracting files on windows in extractTARGZ().
- Also ensure we close when io.Copy fails on extractZIP().
- Log the ignored error from the cleanup of DownloadPath.

Signed-off-by: Ahmet Alp Balkan <[email protected]>

* add src.close()

Signed-off-by: Ahmet Alp Balkan <[email protected]>
  • Loading branch information
ahmetb authored and k8s-ci-robot committed Aug 22, 2019
1 parent ba619f2 commit bc7b73b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 10 additions & 5 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,19 @@ func extractZIP(targetDir string, read io.ReaderAt, size int64) error {

dst, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, f.Mode())
if err != nil {
src.Close()
return errors.Wrap(err, "can't create file in zip destination dir")
}
close := func() {
src.Close()
dst.Close()
}

if _, err := io.Copy(dst, src); err != nil {
close()
return errors.Wrap(err, "can't copy content to zip destination file")
}

// Cleanup the open fd. Don't use defer in case of many files.
// Don't be blocking
src.Close()
dst.Close()
close()
}

return nil
Expand Down Expand Up @@ -131,9 +133,12 @@ func extractTARGZ(targetDir string, at io.ReaderAt, size int64) error {
if err != nil {
return errors.Wrapf(err, "failed to create file %q", path)
}
close := func() { f.Close() }
if _, err := io.Copy(f, tr); err != nil {
close()
return errors.Wrapf(err, "failed to copy %q from tar into file", hdr.Name)
}
close()
default:
return errors.Errorf("unable to handle file type %d for %q in tar", hdr.Typeflag, hdr.Name)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/installation/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ func install(op installOperation, opts InstallOpts) error {
}
defer func() {
glog.V(3).Infof("Deleting the download staging directory %s", op.downloadStagingDir)
_ = os.RemoveAll(op.downloadStagingDir)
if err := os.RemoveAll(op.downloadStagingDir); err != nil {
glog.Warningf("failed to clean up download staging directory: %s", err)
}
}()
if err := downloadAndExtract(op.downloadStagingDir, op.platform.URI, op.platform.Sha256, opts.ArchiveFileOverride); err != nil {
return errors.Wrap(err, "failed to download and extract")
Expand Down

0 comments on commit bc7b73b

Please sign in to comment.