Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blockstore: allow to pass a file to write in #323

Merged
merged 2 commits into from
Aug 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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