From 767d4bbb4be3d31f3e1fcfaf710ed6fbb6ebce08 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 25 Jul 2016 20:04:55 -0400 Subject: [PATCH] tar: explicitly close files after populateTree Files don't close properly when `defer`ing inside a for loop, since the surrounding function is still iterating in a for loop. To fix this, just close the files explicitly after `populateTree()` in `readHeaders()` Signed-off-by: Stephen Chung --- tar.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tar.go b/tar.go index 9455853..9ea759d 100644 --- a/tar.go +++ b/tar.go @@ -93,9 +93,6 @@ func (ts *tarStream) readHeaders() { ts.pipeReader.CloseWithError(err) return } - defer tmpFile.Close() - defer os.Remove(tmpFile.Name()) - // Alright, it's either file or directory encodedName, err := Vis(filepath.Base(hdr.Name)) if err != nil { @@ -175,6 +172,8 @@ func (ts *tarStream) readHeaders() { } } populateTree(&root, &e, hdr, ts) + tmpFile.Close() + os.Remove(tmpFile.Name()) } }