Skip to content

Commit efdfc52

Browse files
authored
This closes qax-os#2119, remove all temporary files on close workbook (qax-os#2120)
1 parent 3ccbdaa commit efdfc52

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

file.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,23 @@ func (f *File) SaveAs(name string, opts ...Options) error {
8787

8888
// Close closes and cleanup the open temporary file for the spreadsheet.
8989
func (f *File) Close() error {
90-
var err error
90+
var firstErr error
9191
if f.sharedStringTemp != nil {
92-
if err := f.sharedStringTemp.Close(); err != nil {
93-
return err
94-
}
92+
firstErr = f.sharedStringTemp.Close()
93+
f.sharedStringTemp = nil
94+
}
95+
for _, stream := range f.streams {
96+
_ = stream.rawData.Close()
9597
}
98+
f.streams = nil
9699
f.tempFiles.Range(func(k, v interface{}) bool {
97-
if err = os.Remove(v.(string)); err != nil {
98-
return false
100+
if err := os.Remove(v.(string)); err != nil && firstErr == nil {
101+
firstErr = err
99102
}
100103
return true
101104
})
102-
for _, stream := range f.streams {
103-
_ = stream.rawData.Close()
104-
}
105-
return err
105+
f.tempFiles.Clear()
106+
return firstErr
106107
}
107108

108109
// Write provides a function to write to an io.Writer.

file_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math"
88
"os"
99
"path/filepath"
10+
"strconv"
1011
"strings"
1112
"sync"
1213
"testing"
@@ -164,3 +165,24 @@ func TestZip64(t *testing.T) {
164165
assert.NoError(t, f.Close())
165166
})
166167
}
168+
169+
func TestRemoveTempFiles(t *testing.T) {
170+
tmp, err := os.CreateTemp("", "excelize-*")
171+
if err != nil {
172+
t.Fatal(err)
173+
}
174+
tmpName := tmp.Name()
175+
tmp.Close()
176+
f := NewFile()
177+
// fill the tempFiles map with non-existing (erroring on Remove) "files"
178+
for i := 0; i < 1000; i++ {
179+
f.tempFiles.Store(strconv.Itoa(i), "/hopefully not existing")
180+
}
181+
f.tempFiles.Store("existing", tmpName)
182+
183+
require.Error(t, f.Close())
184+
if _, err := os.Stat(tmpName); err == nil {
185+
t.Errorf("temp file %q still exist", tmpName)
186+
os.Remove(tmpName)
187+
}
188+
}

lib.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (f *File) ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) {
8080
// unzipToTemp unzip the zip entity to the system temporary directory and
8181
// returned the unzipped file path.
8282
func (f *File) unzipToTemp(zipFile *zip.File) (string, error) {
83-
tmp, err := os.CreateTemp(os.TempDir(), "excelize-")
83+
tmp, err := os.CreateTemp("", "excelize-")
8484
if err != nil {
8585
return "", err
8686
}

rows.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ func (rows *Rows) Error() error {
139139
// Close closes the open worksheet XML file in the system temporary
140140
// directory.
141141
func (rows *Rows) Close() error {
142-
if rows.tempFile != nil {
143-
return rows.tempFile.Close()
142+
tempFile := rows.tempFile
143+
rows.tempFile = nil
144+
if tempFile != nil {
145+
return tempFile.Close()
144146
}
145147
return nil
146148
}
@@ -366,7 +368,7 @@ func (f *File) getFromStringItem(index int) string {
366368
}()
367369
}
368370
f.sharedStringItem = [][]uint{}
369-
f.sharedStringTemp, _ = os.CreateTemp(os.TempDir(), "excelize-")
371+
f.sharedStringTemp, _ = os.CreateTemp("", "excelize-")
370372
f.tempFiles.Store(defaultTempFileSST, f.sharedStringTemp.Name())
371373
var (
372374
inElement string

stream.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ func (bw *bufferedWriter) Sync() (err error) {
775775
return nil
776776
}
777777
if bw.tmp == nil {
778-
bw.tmp, err = os.CreateTemp(os.TempDir(), "excelize-")
778+
bw.tmp, err = os.CreateTemp("", "excelize-")
779779
if err != nil {
780780
// can not use local storage
781781
return nil

0 commit comments

Comments
 (0)