Skip to content

Commit 7b01409

Browse files
committed
Add 'filestore dups' command. Enhance tests.
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 7666eea commit 7b01409

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

core/commands/filestore.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"context"
55
"fmt"
6+
"io"
67

78
//ds "github.com/ipfs/go-datastore"
89
//bs "github.com/ipfs/go-ipfs/blocks/blockstore"
@@ -24,6 +25,7 @@ var FileStoreCmd = &cmds.Command{
2425
Subcommands: map[string]*cmds.Command{
2526
"ls": lsFileStore,
2627
"verify": verifyFileStore,
28+
"dups": dupsFileStore,
2729
},
2830
}
2931

@@ -163,6 +165,45 @@ For ERROR entries the error will also be printed to stderr.
163165
Type: filestore.ListRes{},
164166
}
165167

168+
var dupsFileStore = &cmds.Command{
169+
Helptext: cmds.HelpText{
170+
Tagline: "Print block both in filestore and non-filestore.",
171+
},
172+
Run: func(req cmds.Request, res cmds.Response) {
173+
_, fs, err := getFilestore(req)
174+
if err != nil {
175+
res.SetError(err, cmds.ErrNormal)
176+
return
177+
}
178+
ch, err := fs.FileManager().AllKeysChan(req.Context())
179+
if err != nil {
180+
res.SetError(err, cmds.ErrNormal)
181+
return
182+
}
183+
reader, writer := io.Pipe()
184+
go func() {
185+
defer writer.Close()
186+
for cid := range ch {
187+
have, err := fs.MainBlockstore().Has(cid)
188+
if err != nil {
189+
res.SetError(err, cmds.ErrNormal)
190+
return
191+
}
192+
if have {
193+
fmt.Fprintf(writer, "%v\n", cid)
194+
}
195+
}
196+
}()
197+
res.SetOutput(reader)
198+
},
199+
Marshalers: cmds.MarshalerMap{
200+
cmds.Text: func(res cmds.Response) (io.Reader, error) {
201+
return res.(io.Reader), nil
202+
},
203+
},
204+
}
205+
206+
166207
func getFilestore(req cmds.Request) (*core.IpfsNode, *filestore.Filestore, error) {
167208
n, err := req.InvocContext().GetNode()
168209
if err != nil {
@@ -217,3 +258,4 @@ func perKeyActionToChan(args []string, action func(*cid.Cid) *filestore.ListRes,
217258
}()
218259
return out
219260
}
261+

filestore/filestore.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ type Filestore struct {
1919
bs blockstore.Blockstore
2020
}
2121

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+
2230
func NewFilestore(bs blockstore.Blockstore, fm *FileManager) *Filestore {
2331
return &Filestore{fm, bs}
2432
}

test/sharness/t0271-filestore-verify.sh renamed to test/sharness/t0271-filestore-utils.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ test_filestore_verify() {
114114
'
115115
}
116116

117+
cat <<EOF > dups_expect
118+
zb2rhbcZ3aUXYcrbhhDH1JyrpDcpdw1KFJ5Xs5covjnvMpxDR
119+
EOF
120+
121+
test_filestore_dups() {
122+
test_expect_success "'ipfs filestore dups'" '
123+
ipfs add --raw-leaves somedir/file1 &&
124+
ipfs filestore dups > dups_actual &&
125+
test_cmp dups_expect dups_actual
126+
'
127+
128+
}
129+
117130
init_ipfs_filestore() {
118131
test_expect_success "clean up old node" '
119132
rm -rf "$IPFS_PATH" mountdir ipfs ipns
@@ -134,6 +147,8 @@ test_filestore_adds
134147

135148
test_filestore_verify
136149

150+
test_filestore_dups
151+
137152
echo "WORKING DIR"
138153
echo "IPFS PATH = " $IPFS_PATH
139154
pwd
@@ -143,7 +158,9 @@ test_init_dataset
143158

144159
init_ipfs_filestore
145160

146-
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
147164

148165
test_filestore_adds
149166

0 commit comments

Comments
 (0)