Skip to content

Commit

Permalink
Add 'filestore dups' command. Enhance tests.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Feb 7, 2017
1 parent 7666eea commit 2340263
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
42 changes: 42 additions & 0 deletions core/commands/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"context"
"fmt"
"io"

//ds "github.com/ipfs/go-datastore"
//bs "github.com/ipfs/go-ipfs/blocks/blockstore"
Expand All @@ -24,6 +25,7 @@ var FileStoreCmd = &cmds.Command{
Subcommands: map[string]*cmds.Command{
"ls": lsFileStore,
"verify": verifyFileStore,
"dups": dupsFileStore,
},
}

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

var dupsFileStore = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Print block both in filestore and non-filestore.",
},
Run: func(req cmds.Request, res cmds.Response) {
_, fs, err := getFilestore(req)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
ch, err := fs.FileManager().AllKeysChan(req.Context())
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
reader, writer := io.Pipe()
go func() {
defer writer.Close()
for cid := range ch {
have, err := fs.MainBlockstore().Has(cid)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if have {
fmt.Fprintf(writer, "%v\n", cid)
}
}
}()
res.SetOutput(reader)
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
return res.(io.Reader), nil
},
},
}


func getFilestore(req cmds.Request) (*core.IpfsNode, *filestore.Filestore, error) {
n, err := req.InvocContext().GetNode()
if err != nil {
Expand Down Expand Up @@ -217,3 +258,4 @@ func perKeyActionToChan(args []string, action func(*cid.Cid) *filestore.ListRes,
}()
return out
}

8 changes: 8 additions & 0 deletions filestore/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ type Filestore struct {
bs blockstore.Blockstore
}

func (f *Filestore) FileManager() *FileManager {
return f.fm
}

func (f *Filestore) MainBlockstore() blockstore.Blockstore {
return f.bs
}

func NewFilestore(bs blockstore.Blockstore, fm *FileManager) *Filestore {
return &Filestore{fm, bs}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ test_filestore_verify() {
'
}

cat <<EOF > dups_expect
$FILE1_HASH
EOF

test_filestore_dups() {
test_expect_success "'ipfs filestore dups'" '
ipfs add --raw-leaves somedir/file1 &&
ipfs filestore dups > dups_actual &&
test_cmp dups_expect dups_actual
'
}

init_ipfs_filestore() {
test_expect_success "clean up old node" '
rm -rf "$IPFS_PATH" mountdir ipfs ipns
Expand All @@ -134,6 +146,8 @@ test_filestore_adds

test_filestore_verify

test_filestore_dups

echo "WORKING DIR"
echo "IPFS PATH = " $IPFS_PATH
pwd
Expand All @@ -143,10 +157,16 @@ test_init_dataset

init_ipfs_filestore

test_launch_ipfs_daemon
# must be in offline mode so tests of retrieving non-exist blocks
# don't hang
test_launch_ipfs_daemon --offline

test_filestore_adds

test_filestore_verify

test_filestore_dups

test_kill_ipfs_daemon

test_done

0 comments on commit 2340263

Please sign in to comment.