-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
296 lines (245 loc) · 6.04 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"context"
"fmt"
"os"
"sort"
"time"
blocks "github.com/ipfs/go-ipfs/blocks/blockstore"
bserv "github.com/ipfs/go-ipfs/blockservice"
"github.com/ipfs/go-ipfs/merkledag"
"github.com/ipfs/go-ipfs/pin"
"github.com/ipfs/go-ipfs/repo/fsrepo"
ft "github.com/ipfs/go-ipfs/unixfs"
proto "gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
)
type objectInfo struct {
Cid *cid.Cid
Type string
TotalSize uint64
Pinned bool
}
type objectInfos []objectInfo
func (ois objectInfos) Len() int {
return len(ois)
}
func (ois objectInfos) Swap(i, j int) {
ois[i], ois[j] = ois[j], ois[i]
}
func (ois objectInfos) Less(i, j int) bool {
if ois[i].Type == "unknown" {
if ois[j].Type != "unknown" {
return false
}
if ois[i].Pinned && !ois[j].Pinned {
return true
}
return ois[i].TotalSize > ois[j].TotalSize
}
if ois[j].Type == "unknown" {
return true
}
if ois[i].Pinned && !ois[j].Pinned {
return true
}
return ois[i].TotalSize > ois[j].TotalSize
}
func fatal(i interface{}) {
fmt.Println(i)
os.Exit(1)
}
func main() {
if len(os.Args) == 1 || (os.Args[1] != "lost-pins" && os.Args[1] != "content-stat") {
fmt.Printf("usage: %s [ lost-pins | content-stat ]\n", os.Args[0])
return
}
p, err := fsrepo.BestKnownPath()
if err != nil {
fatal(err)
}
r, err := fsrepo.Open(p)
if err != nil {
fmt.Println("Have you turned your daemon off?")
fatal(err)
}
bs := blocks.NewBlockstore(r.Datastore())
dag := merkledag.NewDAGService(bserv.New(bs, nil))
pinner, err := pin.LoadPinner(r.Datastore(), dag, dag)
if err != nil {
fatal(err)
}
if os.Args[1] == "lost-pins" {
maybelost, err := findMaybeLostPins(bs, dag, pinner)
if err != nil {
fatal(err)
}
for _, c := range maybelost {
fmt.Println(c)
}
} else {
printObjectInfos(bs, dag, pinner)
}
}
func printObjectInfos(bs blocks.Blockstore, dag merkledag.DAGService, pinner pin.Pinner) {
keys, err := bs.AllKeysChan(context.Background())
if err != nil {
fatal(err)
}
recpins := cid.NewSet()
for _, c := range pinner.RecursiveKeys() {
recpins.Add(c)
}
fmt.Printf("%s: started processing keys...\n", time.Now())
allKeys := cid.NewSet()
for bk := range keys {
allKeys.Add(cid.NewCidV0(bk.ToMultihash()))
}
fmt.Printf("%s: initial key gathering complete, now finding graph roots.\n", time.Now())
for _, c := range allKeys.Keys() {
nd, err := dag.Get(context.Background(), c)
if err != nil {
fmt.Printf("error reading dag node (%s): %s\n", c, err)
continue
}
for _, lnk := range nd.Links {
c := cid.NewCidV0(lnk.Hash)
if !recpins.Has(c) {
allKeys.Remove(cid.NewCidV0(lnk.Hash))
}
}
}
fmt.Printf("%s: root selection complete, classifying resulting objects\n", time.Now())
var output []objectInfo
// just left with roots now
for _, c := range allKeys.Keys() {
nd, err := dag.Get(context.Background(), c)
if err != nil {
fmt.Printf("error reading dag node (%s): %s\n", c, err)
continue
}
size, err := nd.Size()
if err != nil {
fmt.Println("error getting size of object: ", err)
}
oi := objectInfo{
Cid: c,
Pinned: recpins.Has(c),
TotalSize: size,
Type: "unknown",
}
fsn, err := ft.FSNodeFromBytes(nd.Data())
if err == nil {
oi.Type = "unixfs-" + fsn.Type.String()
output = append(output, oi)
continue
}
var pinhdr Set
err = proto.Unmarshal(nd.Data(), &pinhdr)
if err != nil {
output = append(output, oi)
continue
}
if pinhdr.GetVersion() != 1 {
fmt.Println("found an object that looks like a pin header, but wasnt")
output = append(output, oi)
continue
}
// Potentially found lost pins!
}
fmt.Printf("%s: classification complete, sorting output...\n", time.Now())
sort.Sort(objectInfos(output))
outputObjectInfos(dag, output)
}
func outputObjectInfos(dag merkledag.DAGService, ois []objectInfo) {
fmt.Println("Hash Type\tSize\tPinned(recursively)")
for _, oi := range ois {
fmt.Printf("%s %s\t%d\t%t\n", oi.Cid, oi.Type, oi.TotalSize, oi.Pinned)
if oi.Type == "unixfs-Directory" {
nd, err := dag.Get(context.Background(), oi.Cid)
if err != nil {
fmt.Println("Error fetching node: ", err)
continue
}
nshow := 5
if len(nd.Links) < 5 {
nshow = len(nd.Links)
}
fmt.Print("\tDirents: [ ")
for _, lnk := range nd.Links[:nshow] {
fmt.Printf("%q ", lnk.Name)
}
fmt.Print("]")
if len(nd.Links) > 5 {
fmt.Print(" ...")
}
fmt.Println()
}
}
}
func findMaybeLostPins(blks blocks.Blockstore, dag merkledag.DAGService, pinner pin.Pinner) ([]*cid.Cid, error) {
pins := cid.NewSet()
for _, reck := range pinner.RecursiveKeys() {
pins.Add(reck)
}
for _, dirk := range pinner.DirectKeys() {
pins.Add(dirk)
}
seen := cid.NewSet()
kchan, err := blks.AllKeysChan(context.Background())
if err != nil {
return nil, err
}
missing := cid.NewSet()
for c := range kchan {
err := processObject(dag, cid.NewCidV0(c.ToMultihash()), seen, pins, missing)
if err != nil {
return nil, err
}
}
return missing.Keys(), nil
}
func processObject(dag merkledag.DAGService, c *cid.Cid, seen, pinned, missing *cid.Set) error {
if seen.Has(c) {
return nil
}
seen.Add(c)
nd, err := dag.Get(context.Background(), c)
if err != nil {
//fmt.Fprintln(os.Stderr, "dag.Get() error: ", err)
return nil
}
var pset Set
err = proto.Unmarshal(nd.Data(), &pset)
if err != nil {
// not a pinset, move on
return nil
}
// might be a pinset! investigate!
if pset.GetVersion() != 1 {
return nil
}
// Woohoo! this looks like a pinset!
fout := int(pset.GetFanout())
if len(nd.Links) > fout {
//Are these pins???
for _, lnk := range nd.Links[fout:] {
c := cid.NewCidV0(lnk.Hash)
if !pinned.Has(c) {
missing.Add(c)
}
}
}
if len(nd.Links) < fout {
fout = len(nd.Links)
}
for _, lnk := range nd.Links[:fout] {
c := cid.NewCidV0(lnk.Hash)
err := processObject(dag, c, seen, pinned, missing)
if err != nil {
return err
}
seen.Add(c)
}
return nil
}