/
patcher.go
57 lines (41 loc) · 868 Bytes
/
patcher.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
57
package patcher
import (
"sync"
shell "github.com/ipfs/go-ipfs-api"
)
const cidV1UnixfsDir = "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"
func NewPatcher(shell *shell.Shell, root string) *Patcher {
if root == "" {
root = cidV1UnixfsDir
}
return &Patcher{
shell: shell,
root: root,
}
}
type Patcher struct {
shell *shell.Shell
root string
mutex sync.Mutex
}
func (p *Patcher) Root() string { return p.root }
func (p *Patcher) Cp(dir, cid string) error {
p.mutex.Lock()
defer p.mutex.Unlock()
newRoot, err := p.shell.PatchLink(p.root, dir, cid, true)
if err != nil {
return err
}
p.root = newRoot
return nil
}
func (p *Patcher) Rm(dir string) error {
p.mutex.Lock()
defer p.mutex.Unlock()
newRoot, err := p.shell.Patch(p.root, "rm-link", dir)
if err != nil {
return err
}
p.root = newRoot
return nil
}