Skip to content

Commit

Permalink
go: Return error from Handle.NewWriter
Browse files Browse the repository at this point in the history
This occurs when Handle is closed.
  • Loading branch information
anacrolix committed Jul 12, 2024
1 parent da2cb38 commit b3a359b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
9 changes: 7 additions & 2 deletions go/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package possumResource

import (
"fmt"
"github.com/anacrolix/missinggo/v2/resource"
possum "github.com/anacrolix/possum/go"
"io"
Expand Down Expand Up @@ -61,7 +62,11 @@ func (i *instance) Get() (_ io.ReadCloser, err error) {
}

func (i *instance) Put(reader io.Reader) (err error) {
w := i.handle.NewWriter()
w, err := i.handle.NewWriter()
if err != nil {
err = fmt.Errorf("creating new possum writer: %w", err)
return
}
defer func() {
// TODO: There's no Writer.Drop.
commitErr := w.Commit()
Expand Down Expand Up @@ -100,7 +105,7 @@ func (i *instance) ReadAt(p []byte, off int64) (n int, err error) {
return i.handle.SingleReadAt(i.key, off, p)
}

func (i *instance) WriteAt(bytes []byte, i2 int64) (int, error) {
func (i *instance) WriteAt([]byte, int64) (int, error) {
//TODO implement me
panic("implement me")
}
Expand Down
10 changes: 6 additions & 4 deletions go/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ type Writer struct {
handleRef *Rc[*possumC.Handle]
}

func (me *Handle) NewWriter() *Writer {
func (me *Handle) NewWriter() (w *Writer, err error) {
rc, err := me.cloneRc()
if err != nil {
panic(err)
return
}
return &Writer{
w = &Writer{
c: possumC.NewWriter(rc.Deref()),
handleRef: rc,
}
return
}

type ValueWriter struct {
Expand Down Expand Up @@ -59,7 +60,8 @@ func (me *ValueWriter) NewFile(name string) (f *os.File, err error) {
return
}

// This consumes the Writer.
// This consumes the Writer. You must commit a Writer after it's created or it will leak a reference
// to the Handle. There doesn't seem to be a way to abort it in the Go API.
func (me *Writer) Commit() error {
err := possumC.CommitWriter(me.c)
me.c = nil
Expand Down

0 comments on commit b3a359b

Please sign in to comment.