Ensure files are closed after they are done being used

This commit is contained in:
Matthew Penner 2023-01-17 18:34:08 -07:00
parent 43b7aa2536
commit e9b8b11fec
No known key found for this signature in database
4 changed files with 7 additions and 1 deletions

View File

@ -95,6 +95,7 @@ func getDownloadFile(c *gin.Context) {
middleware.CaptureAndAbort(c, err)
return
}
defer f.Close()
c.Header("Content-Length", strconv.Itoa(int(st.Size())))
c.Header("Content-Disposition", "attachment; filename="+strconv.Quote(st.Name()))

View File

@ -85,6 +85,7 @@ func (b *LocalBackup) Restore(ctx context.Context, _ io.Reader, callback Restore
if err != nil {
return err
}
defer f.Close()
var reader io.Reader = f
// Steal the logic we use for making backups which will be applied when restoring

View File

@ -148,7 +148,7 @@ func (fs *Filesystem) DecompressFileUnsafe(ctx context.Context, dir string, file
if err != nil {
return err
}
// TODO: defer file close?
defer f.Close()
// Identify the type of archive we are dealing with.
format, input, err := archiver.Identify(filepath.Base(file), f)

View File

@ -92,6 +92,9 @@ func (fs *Filesystem) Touch(p string, flag int) (*os.File, error) {
if err == nil {
return f, nil
}
if f != nil {
_ = f.Close()
}
// If the error is not because it doesn't exist then we just need to bail at this point.
if !errors.Is(err, os.ErrNotExist) {
return nil, errors.Wrap(err, "server/filesystem: touch: failed to open file handle")
@ -114,6 +117,7 @@ func (fs *Filesystem) Touch(p string, flag int) (*os.File, error) {
if err != nil {
return nil, errors.Wrap(err, "server/filesystem: touch: failed to open file with wait")
}
_ = f.Close()
_ = fs.Chown(cleaned)
return f, nil
}