Skip to content

Commit

Permalink
blockstore: only close the file on error in OpenReadWrite, not OpenR…
Browse files Browse the repository at this point in the history
…eadWriteFile

OpenReadWriteFile gives full control of the file to the caller, so it should not
decide to close the file, even on error.


This commit was moved from ipld/go-car@794f222
  • Loading branch information
MichaelMure authored and rvagg committed Mar 20, 2023
1 parent ca32fef commit 4cb3791
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions ipld/car/v2/blockstore/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func OpenReadWrite(path string, roots []cid.Cid, opts ...carv2.Option) (*ReadWri
if err != nil {
return nil, fmt.Errorf("could not open read/write file: %w", err)
}
// If construction of blockstore fails, make sure to close off the open file.
defer func() {
if err != nil {
f.Close()
}
}()
rwbs, err := OpenReadWriteFile(f, roots, opts...)
if err != nil {
return nil, err
Expand All @@ -100,17 +106,11 @@ func OpenReadWrite(path string, roots []cid.Cid, opts ...carv2.Option) (*ReadWri
func OpenReadWriteFile(f *os.File, roots []cid.Cid, opts ...carv2.Option) (*ReadWrite, error) {
stat, err := f.Stat()
if err != nil {
// Note, we should not get a an os.ErrNotExist here because the flags used to open file includes os.O_CREATE
// Note, we should not get an os.ErrNotExist here because the flags used to open file includes os.O_CREATE
return nil, err
}
// Try and resume by default if the file size is non-zero.
resume := stat.Size() != 0
// If construction of blockstore fails, make sure to close off the open file.
defer func() {
if err != nil {
f.Close()
}
}()

// Instantiate block store.
// Set the header fileld before applying options since padding options may modify header.
Expand Down

0 comments on commit 4cb3791

Please sign in to comment.