Skip to content

Commit

Permalink
fixed unclosed tmp file handle in filestore.go
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dorofeev <dima@yasp.com>
  • Loading branch information
deem0n authored and neilalexander committed Jan 21, 2025
1 parent 1c9423b commit 68607a7
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions server/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5469,16 +5469,23 @@ func (mb *msgBlock) recompressOnDiskIfNeeded() error {
<-dios
tmpFD, err := os.OpenFile(tmpFN, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, defaultFilePerms)
dios <- struct{}{}

if err != nil {
return fmt.Errorf("failed to create temporary file: %w", err)
}

errorCleanup := func(err error) error {
tmpFD.Close()
os.Remove(tmpFN)
return err
}

// The original buffer at this point is uncompressed, so we will now compress
// it if needed. Note that if the selected algorithm is NoCompression, the
// Compress function will just return the input buffer unmodified.
cmpBuf, err := alg.Compress(origBuf)
if err != nil {
return fmt.Errorf("failed to compress block: %w", err)
return errorCleanup(fmt.Errorf("failed to compress block: %w", err))
}

// We only need to write out the metadata header if compression is enabled.
Expand All @@ -5496,19 +5503,14 @@ func (mb *msgBlock) recompressOnDiskIfNeeded() error {
if mb.bek != nil && len(cmpBuf) > 0 {
bek, err := genBlockEncryptionKey(mb.fs.fcfg.Cipher, mb.seed, mb.nonce)
if err != nil {
return err
return errorCleanup(err)
}
mb.bek = bek
mb.bek.XORKeyStream(cmpBuf, cmpBuf)
}

// Write the new block data (which might be compressed or encrypted) to the
// temporary file.
errorCleanup := func(err error) error {
tmpFD.Close()
os.Remove(tmpFN)
return err
}
if n, err := tmpFD.Write(cmpBuf); err != nil {
return errorCleanup(fmt.Errorf("failed to write to temporary file: %w", err))
} else if n != len(cmpBuf) {
Expand Down

0 comments on commit 68607a7

Please sign in to comment.