-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
writer.go
56 lines (47 loc) · 1.13 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package possum
import (
possumC "github.com/anacrolix/possum/go/cpossum"
"os"
)
type Writer struct {
c possumC.Writer
}
func (me Handle) NewWriter() Writer {
return Writer{possumC.NewWriter(me.cHandle)}
}
type ValueWriter struct {
c possumC.ValueWriter
files []*os.File
}
func (me Writer) StartNewValue() (vw *ValueWriter, err error) {
c, err := possumC.StartNewValue(me.c)
if err != nil {
return
}
vw = &ValueWriter{c, nil}
return
}
func (me Writer) Stage(key []byte, value *ValueWriter) error {
for _, f := range value.files {
f.Close()
}
return possumC.StageWrite(me.c, key, value.c)
}
// Should this be exposed?
func (me *ValueWriter) Fd() uintptr {
return uintptr(possumC.ValueWriterFd(me.c))
}
func (me *ValueWriter) NewFile(name string) (f *os.File, err error) {
// I wonder if closing this will close the fd belong to possum. If so, we should dup, and then
// kill it remotely if the writer is committed.
fd, err := duplicateFileHandle(me.Fd())
if err != nil {
return
}
f = os.NewFile(fd, name)
me.files = append(me.files, f)
return
}
func (me Writer) Commit() error {
return possumC.CommitWriter(me.c)
}