/
fetch.go
196 lines (155 loc) · 3.4 KB
/
fetch.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package core
import (
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"sync"
"github.com/ipfs/go-cid"
ipfs "github.com/ipfs/go-ipfs-api"
"github.com/ipfs/go-ipld-git"
mh "github.com/multiformats/go-multihash"
"github.com/remeh/sizedwaitgroup"
)
var ErrNotProvided = errors.New("block not provided")
type ObjectProvider func(cid string, tracker *Tracker) ([]byte, error)
type Fetch struct {
objectDir string
gitDir string
done uint64
todoc uint64
todo chan string
log *log.Logger
tracker *Tracker
errCh chan error
wg sizedwaitgroup.SizedWaitGroup
doneCh chan []byte
fsLk *sync.Mutex
provider ObjectProvider
api *ipfs.Shell
}
func NewFetch(gitDir string, tracker *Tracker, provider ObjectProvider) *Fetch {
return &Fetch{
objectDir: path.Join(gitDir, "objects"),
gitDir: gitDir,
log: log.New(os.Stderr, "fetch: ", 0),
tracker: tracker,
fsLk: &sync.Mutex{},
wg: sizedwaitgroup.New(512),
//Note: logic below somewhat relies on these channels staying unbuffered
todo: make(chan string),
errCh: make(chan error),
doneCh: make(chan []byte),
provider: provider,
api: ipfs.NewLocalShell(),
}
}
func (f *Fetch) FetchHash(base string) error {
go func() {
f.todo <- base
}()
return f.doWork()
}
func (f *Fetch) doWork() error {
for {
select {
case err := <-f.errCh:
return err
case hash := <-f.todo:
f.todoc++
if err := f.processSingle(hash); err != nil {
return err
}
case <-f.doneCh:
f.done++
}
f.log.Printf("%d/%d\r\x1b[A", f.done, f.todoc)
if f.done == f.todoc {
f.wg.Wait()
f.log.Printf("\n")
return nil
}
}
}
func (f *Fetch) processSingle(hash string) error {
mhash, err := mh.FromHexString("1114" + hash)
if err != nil {
return fmt.Errorf("fetch: %v", err)
}
c := cid.NewCidV1(cid.GitRaw, mhash).String()
sha, err := hex.DecodeString(hash)
if err != nil {
return fmt.Errorf("fetch: %v", err)
}
has, err := f.tracker.HasEntry(sha)
if err != nil {
return err
}
if has {
f.todoc--
return nil
}
// Need to do this early
if err := f.tracker.AddEntry(sha); err != nil {
return fmt.Errorf("fetch: %v", err)
}
go func() {
f.wg.Add()
defer f.wg.Done()
f.fsLk.Lock()
objectPath, err := prepHashPath(f.objectDir, hash)
f.fsLk.Unlock()
if err != nil {
f.errCh <- err
return
}
object, err := f.provider(c, f.tracker)
if err != nil {
if err != ErrNotProvided {
f.errCh <- err
return
}
object, err = f.api.BlockGet(c)
if err != nil {
f.errCh <- fmt.Errorf("fetch: %v", err)
return
}
}
f.processLinks(object)
object = compressObject(object)
/////////////////
err = ioutil.WriteFile(*objectPath, object, 0444)
if err != nil {
f.errCh <- fmt.Errorf("fetch: %v", err)
return
}
//TODO: see if moving this higher would help
f.doneCh <- sha
}()
return nil
}
func (f *Fetch) processLinks(object []byte) error {
nd, err := ipldgit.ParseObjectFromBuffer(object)
if err != nil {
return fmt.Errorf("fetch: %v", err)
}
links := nd.Links()
for _, link := range links {
mhash := link.Cid.Hash()
hash := mhash.HexString()[4:]
f.todo <- hash
}
return nil
}
func prepHashPath(localDir string, hash string) (*string, error) {
base := path.Join(localDir, hash[:2])
err := os.MkdirAll(base, 0777)
if err != nil {
return nil, err
}
objectPath := path.Join(base, hash[2:])
return &objectPath, nil
}