filestore util: Add 'filestore dups' command. Enhance tests.
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Feb 6, 2017 at 21:53 UTC
f7efb34e68c60a844a21221de8b741f83954cd4a
3 files changed
+69
-1
core/commands/filestore.go
+40
@@ -18,6 +18,7 @@ var FileStoreCmd = &cmds.Command{
18
Subcommands: map[string]*cmds.Command{
19
"ls": lsFileStore,
20
"verify": verifyFileStore,
21
+ "dups": dupsFileStore,
22
},
23
}
24
@@ -157,6 +158,44 @@ For ERROR entries the error will also be printed to stderr.
158
Type: filestore.ListRes{},
159
}
160
161
+var dupsFileStore = &cmds.Command{
162
+ Helptext: cmds.HelpText{
163
+ Tagline: "Print block both in filestore and non-filestore.",
164
+ },
165
+ Run: func(req cmds.Request, res cmds.Response) {
166
+ _, fs, err := getFilestore(req)
167
+ if err != nil {
168
+ res.SetError(err, cmds.ErrNormal)
169
+ return
170
+ }
171
+ ch, err := fs.FileManager().AllKeysChan(req.Context())
172
+ if err != nil {
173
+ res.SetError(err, cmds.ErrNormal)
174
+ return
175
+ }
176
+
177
+ out := make(chan interface{}, 128)
178
+ res.SetOutput((<-chan interface{})(out))
179
+
180
+ go func() {
181
+ defer close(out)
182
+ for cid := range ch {
183
+ have, err := fs.MainBlockstore().Has(cid)
184
+ if err != nil {
185
+ out <- &RefWrapper{Err: err.Error()}
186
+ return
187
+ }
188
+ if have {
189
+ out <- &RefWrapper{Ref: cid.String()}
190
+ }
191
+ }
192
+ }()
193
+ },
194
+ Marshalers: refsMarshallerMap,
195
+ Type: RefWrapper{},
196
+}
197
+
198
+
199
func getFilestore(req cmds.Request) (*core.IpfsNode, *filestore.Filestore, error) {
200
n, err := req.InvocContext().GetNode()
201
if err != nil {
@@ -211,3 +250,4 @@ func perKeyActionToChan(args []string, action func(*cid.Cid) *filestore.ListRes,
250
}()
251
return out
252
}
253
+
filestore/filestore.go
+8
@@ -19,6 +19,14 @@ type Filestore struct {
19
bs blockstore.Blockstore
20
}
21
22
+func (f *Filestore) FileManager() *FileManager {
23
+ return f.fm
24
+}
25
+
26
+func (f *Filestore) MainBlockstore() blockstore.Blockstore {
27
+ return f.bs
28
+}
29
+
30
func NewFilestore(bs blockstore.Blockstore, fm *FileManager) *Filestore {
31
return &Filestore{fm, bs}
32
}
test/sharness/t0271-filestore-utils.sh
renamed
+21
-1
@@ -115,6 +115,18 @@ test_filestore_verify() {
115
'
116
}
117
118
+cat <<EOF > dups_expect
119
+$FILE1_HASH
120
+EOF
121
+
122
+test_filestore_dups() {
123
+ test_expect_success "'ipfs filestore dups'" '
124
+ ipfs add --raw-leaves somedir/file1 &&
125
+ ipfs filestore dups > dups_actual &&
126
+ test_cmp dups_expect dups_actual
127
+'
128
+}
129
+
130
init_ipfs_filestore() {
131
test_expect_success "clean up old node" '
132
rm -rf "$IPFS_PATH" mountdir ipfs ipns
@@ -135,6 +147,8 @@ test_filestore_adds
147
148
test_filestore_verify
149
150
+test_filestore_dups
151
+
152
echo "WORKING DIR"
153
echo "IPFS PATH = " $IPFS_PATH
154
pwd
@@ -144,10 +158,16 @@ test_init_dataset
158
159
init_ipfs_filestore
160
147
-test_launch_ipfs_daemon
161
+# must be in offline mode so tests of retrieving non-exist blocks
162
+# don't hang
163
+test_launch_ipfs_daemon --offline
164
165
test_filestore_adds
166
167
+test_filestore_verify
168
+
169
+test_filestore_dups
170
+
171
test_kill_ipfs_daemon
172
173
test_done