Repository navigation
fix(util): clean up incomplete temporary ZIP archives - #401
anishpentyala wants to merge 5 commits into
Conversation
|
Thanks for this, the bug is real and your tests reproduce it nicely: they fail on 1. What about dropping the create/close/rename/reopen sequence? As far as I can tell, that sequence exists only to give the temp file a // ZipFiles and return the resulting zip's filename
func ZipFiles(files []string) (_ string, err error) {
tmpfile, err := os.CreateTemp("", "qrcp-*.zip")
if err != nil {
return "", err
}
defer func() {
if err != nil {
_ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
}
}()
zip := new(archivex.ZipFile)
if err := zip.CreateWriter(tmpfile.Name(), tmpfile); err != nil {
return "", err
}
for _, filename := range files {
if err := addToZip(zip, filename); err != nil {
return "", err
}
}
if err := zip.Writer.Close(); err != nil {
return "", err
}
if err := tmpfile.Close(); err != nil {
return "", err
}
return tmpfile.Name(), nil
}I don't think the ZIP writer needs closing on the failure path, since the archive is being deleted anyway. Does that match your understanding? 2. What about moving the per-file work into a helper? Right now func addToZip(zip *archivex.ZipFile, filename string) error {
fileinfo, err := os.Stat(filename)
if err != nil {
return err
}
if fileinfo.IsDir() {
return zip.AddAll(filename, true)
}
file, err := os.Open(filename)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
return zip.Add(filename, file, fileinfo)
}3. Could the tests be trimmed a little?
I'll squash on merge, so don't worry about the commit history. Thanks again! EDIT: I can take care of this in case you don't find time C |
|
Thanks, the simpler temp-file path makes sense. Updated to CreateTemp with a .zip suffix and a named-error cleanup defer, moved per-file handling into a helper so each input is closed promptly, and moved the trimmed tests into util_test.go with table-based names. The optional unreadable directory-entry case also passes here. The discarded ZIP writer does not need to be finalised on failure; closing its underlying file is enough before removal. The util race tests, 30 repeated ZIP test runs, and go vet pass. The full race suite passes excluding the previously reported config.TestNew network-interface failure. No Windows run locally; hosted checks are pending. |
ZipFilescreates its temporary archive before checking the input paths. If a later stat, open, or archive write fails, it returns an empty filename but leaves the partial archive and its file handle behind.Add failure cleanup that closes the ZIP writer and file before removing the archive. Track the temporary path across its rename, keep the original error, and leave completed archives available for the caller as before.
Tests cover missing first and later inputs, a failing directory entry, completed ZIP contents, and leaked descriptors on Linux. The symlink test skips when symlink creation is unavailable; descriptor inspection skips without
/proc/self/fd.Validation:
go test -race ./util -count=1go test ./util -run TestZipFiles -count=30go vet ./..../util, no findingsgo test -race ./... -skip '^TestNew$'The unfiltered race suite hits
config.TestNew's "no interfaces found" panic in this environment. The same test fails on the unchanged base. Windows was not tested.