This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
/
repo.go
247 lines (209 loc) · 5.65 KB
/
repo.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
package main
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"strings"
cli "gx/ipfs/QmVahSzvB3Upf5dAW15dpktF6PXb4z9V5LohmbcUqktyF4/cli"
blockstore "gx/ipfs/QmTVDM4LCSUMFNQzbDLL9zQwp8usE6QHymFdh3h8vL9v6b/go-ipfs-blockstore"
h "gx/ipfs/QmatUACvrFK3xYg1nd2iLAKfz7Yy5YB56tnzBYHpqiUuhn/go-ipfs/importer/helpers"
mfs "gx/ipfs/QmatUACvrFK3xYg1nd2iLAKfz7Yy5YB56tnzBYHpqiUuhn/go-ipfs/mfs"
pin "gx/ipfs/QmatUACvrFK3xYg1nd2iLAKfz7Yy5YB56tnzBYHpqiUuhn/go-ipfs/pin"
gc "gx/ipfs/QmatUACvrFK3xYg1nd2iLAKfz7Yy5YB56tnzBYHpqiUuhn/go-ipfs/pin/gc"
fsrepo "gx/ipfs/QmatUACvrFK3xYg1nd2iLAKfz7Yy5YB56tnzBYHpqiUuhn/go-ipfs/repo/fsrepo"
ft "gx/ipfs/QmatUACvrFK3xYg1nd2iLAKfz7Yy5YB56tnzBYHpqiUuhn/go-ipfs/unixfs"
cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
)
var repoCommand = cli.Command{
Name: "repo",
Usage: "manipulate the ipfs repo associated with this pack",
Subcommands: []cli.Command{
repoRegenCommand,
repoGcCommand,
repoLsCommand,
repoRmCommand,
},
}
var repoRegenCommand = cli.Command{
Name: "regen",
Usage: "regenerate ipfs-pack repo for this pack",
Action: func(c *cli.Context) error {
workdir := cwd
if c.Args().Present() {
argpath, err := filepath.Abs(c.Args().First())
if err != nil {
return err
}
workdir = argpath
}
fi, err := openManifestFile(workdir)
if err != nil {
return err
}
r, err := getRepo(workdir)
if err != nil {
return err
}
_, dserv := buildDagserv(r.Datastore(), r.FileManager())
root, err := mfs.NewRoot(context.Background(), dserv, ft.EmptyDirNode(), nil)
if err != nil {
return err
}
defaultFmts := DefaultImporterSettings.String()
scan := bufio.NewScanner(fi)
for scan.Scan() {
parts := strings.SplitN(scan.Text(), "\t", 3)
hash := parts[0]
fmts := parts[1]
path, err := unescape(parts[2])
if err != nil {
return err
}
if fmts != defaultFmts {
return fmt.Errorf("error: unsupported import settings string: %s != %s", fmts, defaultFmts)
}
params := &h.DagBuilderParams{
Dagserv: dserv,
NoCopy: true,
RawLeaves: true,
Maxlinks: h.DefaultLinksPerBlock,
}
st, err := os.Lstat(path)
switch {
case os.IsNotExist(err):
return fmt.Errorf("error: in manifest, missing from pack: %s\n", path)
default:
return fmt.Errorf("error: reading file %s: %s\n", path, err)
case err == nil:
// continue
}
if st.IsDir() {
// TODO: maybe check that the mfs root records this as being correct?
continue
}
nd, err := addItem(filepath.Join(workdir, path), st, params)
if err != nil {
return err
}
if hash != nd.Cid().String() {
return fmt.Errorf("error: checksum fail on %s (exp %s, got %s)", path, hash, nd.Cid())
}
err = mfs.Mkdir(root, filepath.Dir(path), mfs.MkdirOpts{Mkparents: true})
if err != nil {
return fmt.Errorf("error reconstructing tree: %s", err)
}
err = mfs.PutNode(root, filepath.Clean(path), nd)
if err != nil {
return fmt.Errorf("error adding tree node: %s", err)
}
}
nd, err := root.GetValue().GetNode()
if err != nil {
return err
}
_ = nd
fmt.Println("ipfs pack repo successfully regenerated.")
return nil
},
}
var repoRmCommand = cli.Command{
Name: "rm",
Usage: "remove this pack's ipfs repo",
Action: func(c *cli.Context) error {
packpath := filepath.Join(cwd, PackRepo)
if !fsrepo.IsInitialized(packpath) {
return fmt.Errorf("no repo found at ./.ipfs-pack")
}
return os.RemoveAll(packpath)
},
}
var repoGcCommand = cli.Command{
Name: "gc",
Usage: "garbage collect the pack's ipfs repo",
Action: func(c *cli.Context) error {
packpath := filepath.Join(cwd, PackRepo)
if !fsrepo.IsInitialized(packpath) {
return fmt.Errorf("no repo found at ./.ipfs-pack")
}
fsr, err := fsrepo.Open(packpath)
if err != nil {
return err
}
bstore, ds := buildDagserv(fsr.Datastore(), fsr.FileManager())
gcbs := blockstore.NewGCBlockstore(bstore, blockstore.NewGCLocker())
pinner := pin.NewPinner(fsr.Datastore(), ds, ds)
root, err := getManifestRoot(cwd)
if err != nil {
return err
}
pinner.PinWithMode(root, pin.Recursive)
if err := pinner.Flush(); err != nil {
return err
}
out := gc.GC(context.Background(), gcbs, fsr.Datastore(), pinner, nil)
if err != nil {
return err
}
for k := range out {
if k.Error != nil {
fmt.Printf("GC Error: %s\n", k.Error)
} else {
fmt.Printf("removed %s\n", k.KeyRemoved)
}
}
return nil
},
}
var repoLsCommand = cli.Command{
Name: "ls",
Usage: "list all cids in the pack's ipfs repo",
Action: func(c *cli.Context) error {
packpath := filepath.Join(cwd, PackRepo)
if !fsrepo.IsInitialized(packpath) {
return fmt.Errorf("no repo found at ./.ipfs-pack")
}
fsr, err := fsrepo.Open(packpath)
if err != nil {
return err
}
bstore, _ := buildDagserv(fsr.Datastore(), fsr.FileManager())
keys, err := bstore.AllKeysChan(context.Background())
if err != nil {
return err
}
for k := range keys {
fmt.Println(k)
}
return nil
},
}
func getManifestRoot(workdir string) (*cid.Cid, error) {
fi, err := os.Open(filepath.Join(workdir, ManifestFilename))
if err != nil {
switch {
case os.IsNotExist(err):
return nil, fmt.Errorf("error: no %s found in %s", ManifestFilename, workdir)
default:
return nil, fmt.Errorf("error opening %s: %s", ManifestFilename, err)
}
}
st, err := fi.Stat()
if err != nil {
return nil, err
}
if st.Size() > 1024 {
_, err = fi.Seek(-512, os.SEEK_END)
if err != nil {
return nil, err
}
}
scan := bufio.NewScanner(fi)
var lastline string
for scan.Scan() {
lastline = scan.Text()
}
hash := strings.SplitN(lastline, "\t", 2)[0]
return cid.Decode(hash)
}