Skip to content

Commit

Permalink
cache: Delete tempfiles immediately so they can't be left behind
Browse files Browse the repository at this point in the history
closes #63
  • Loading branch information
Jille committed Aug 17, 2024
1 parent 2947962 commit 0bbc671
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion client/transfer/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"io/ioutil"
"os"
"runtime"
)

func New(size int64) (*Cache, error) {
Expand All @@ -11,6 +12,12 @@ func New(size int64) (*Cache, error) {
return nil, err
}
fn := f.Name()
if runtime.GOOS != "windows" {
// Unlink the file so it isn't left behind if we crash
if err := os.Remove(fn); err != nil {
return nil, err
}
}
if err := f.Truncate(size); err != nil {
f.Close()
os.Remove(fn)
Expand All @@ -24,6 +31,8 @@ type Cache struct {
}

func (c *Cache) Close() error {
os.Remove(c.Name())
if runtime.GOOS == "windows" {
os.Remove(c.Name())
}
return c.File.Close()
}

0 comments on commit 0bbc671

Please sign in to comment.