Skip to content

Commit

Permalink
blockstore: allow to pass a file to write in (#323)
Browse files Browse the repository at this point in the history
This allow the caller to control the file lifecycle and implement a cleaning strategy.
An example would be:
- open a file in a temporary folder
- if the write suceed, close the file and move it to its final destination
- on error, remove the file

If the underlying filesystem support it, an anonymous file can be used instead: open
then immediately delete, use the file descriptor to write. A crash before the end of the
write process would release the used storage automatically.

Co-authored-by: Rod Vagg <rod@vagg.org>
  • Loading branch information
MichaelMure and rvagg committed Aug 12, 2022
1 parent d321c7d commit c2631db
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions v2/blockstore/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/multiformats/go-varint"

carv2 "github.com/ipld/go-car/v2"
"github.com/ipld/go-car/v2/index"
"github.com/ipld/go-car/v2/internal/carv1"
"github.com/ipld/go-car/v2/internal/carv1/util"
internalio "github.com/ipld/go-car/v2/internal/io"
"github.com/multiformats/go-varint"
)

var _ blockstore.Blockstore = (*ReadWrite)(nil)
Expand Down Expand Up @@ -103,6 +104,18 @@ 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)
}
rwbs, err := OpenReadWriteFile(f, roots, opts...)
if err != nil {
return nil, err
}
// close the file when finalizing
rwbs.ronly.carv2Closer = rwbs.f
return rwbs, nil
}

// OpenReadWriteFile is similar as OpenReadWrite but lets you control the file lifecycle.
// You are responsible for closing the given file.
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
Expand Down Expand Up @@ -145,7 +158,6 @@ func OpenReadWrite(path string, roots []cid.Cid, opts ...carv2.Option) (*ReadWri
}
rwbs.ronly.backing = v1r
rwbs.ronly.idx = rwbs.idx
rwbs.ronly.carv2Closer = rwbs.f

if resume {
if err = rwbs.resumeWithRoots(!rwbs.opts.WriteAsCarV1, roots); err != nil {
Expand Down

0 comments on commit c2631db

Please sign in to comment.