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
4 changed files with 7 additions and 1 deletions

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
}