diff --git a/go/resource/resource.go b/go/resource/resource.go index 678880c..e6ebd67 100644 --- a/go/resource/resource.go +++ b/go/resource/resource.go @@ -3,6 +3,7 @@ package possumResource import ( + "fmt" "github.com/anacrolix/missinggo/v2/resource" possum "github.com/anacrolix/possum/go" "io" @@ -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() @@ -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") } diff --git a/go/writer.go b/go/writer.go index 017b4da..c41f23d 100644 --- a/go/writer.go +++ b/go/writer.go @@ -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 { @@ -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