From 22f04312a74920f63c413b2ae4c875646217dc26 Mon Sep 17 00:00:00 2001 From: Jakob van Santen Date: Fri, 20 Mar 2026 16:25:44 +0100 Subject: [PATCH] fix: cleanup tempfiles when error prevents calling link When a client provides an invalid or incomplete body for a single-part upload, the handler returns before the link stage. Factor tempfile removal into cleanup() to catch all cases. --- backend/posix/with_otmpfile.go | 7 ++++--- backend/posix/without_otmpfile.go | 4 +--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/backend/posix/with_otmpfile.go b/backend/posix/with_otmpfile.go index 6ebf9850..d5532d20 100644 --- a/backend/posix/with_otmpfile.go +++ b/backend/posix/with_otmpfile.go @@ -25,6 +25,7 @@ import ( "os" "path/filepath" "strconv" + "strings" "syscall" "time" @@ -230,9 +231,6 @@ func (tmp *tmpfile) link() error { func (tmp *tmpfile) fallbackLink() error { tempname := tmp.f.Name() - // cleanup in case anything goes wrong, if rename succeeds then - // this will no longer exist - defer os.Remove(tempname) // reset default file mode because CreateTemp uses 0600 tmp.f.Chmod(fs.FileMode(defaultFilePerm)) @@ -265,6 +263,9 @@ func (tmp *tmpfile) Write(b []byte) (int, error) { func (tmp *tmpfile) cleanup() { tmp.f.Close() + if !strings.HasPrefix(tmp.f.Name(), procfddir) { + os.Remove(tmp.f.Name()) + } } func (tmp *tmpfile) File() *os.File { diff --git a/backend/posix/without_otmpfile.go b/backend/posix/without_otmpfile.go index f017e755..3b144539 100644 --- a/backend/posix/without_otmpfile.go +++ b/backend/posix/without_otmpfile.go @@ -75,9 +75,6 @@ var ( func (tmp *tmpfile) link() error { tempname := tmp.f.Name() - // cleanup in case anything goes wrong, if rename succeeds then - // this will no longer exist - defer os.Remove(tempname) objPath := filepath.Join(tmp.bucket, tmp.objname) @@ -104,6 +101,7 @@ func (tmp *tmpfile) Write(b []byte) (int, error) { func (tmp *tmpfile) cleanup() { tmp.f.Close() + os.Remove(tmp.f.Name()) } func (tmp *tmpfile) File() *os.File {