-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "filestore" commands to list contents and verify filestore.
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
- Loading branch information
Showing
5 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
cmds "github.com/ipfs/go-ipfs/commands" | ||
"github.com/ipfs/go-ipfs/filestore" | ||
"github.com/ipfs/go-ipfs/repo/fsrepo" | ||
) | ||
|
||
type chanWriter struct { | ||
ch <-chan *filestore.ListRes | ||
buf string | ||
offset int | ||
} | ||
|
||
func (w *chanWriter) Read(p []byte) (int, error) { | ||
if w.offset >= len(w.buf) { | ||
w.offset = 0 | ||
res, more := <-w.ch | ||
if !more { | ||
return 0, io.EOF | ||
} | ||
w.buf = res.Format() | ||
} | ||
sz := copy(p, w.buf[w.offset:]) | ||
w.offset += sz | ||
return sz, nil | ||
} | ||
|
||
var FileStoreCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Interact with filestore objects", | ||
}, | ||
Subcommands: map[string]*cmds.Command{ | ||
"ls": lsFileStore, | ||
"verify": verifyFileStore, | ||
}, | ||
} | ||
|
||
var lsFileStore = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "List objects on filestore", | ||
}, | ||
|
||
Run: func(req cmds.Request, res cmds.Response) { | ||
node, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
fsrepo, ok := node.Repo.Self().(*fsrepo.FSRepo) | ||
if !ok { | ||
res.SetError(errors.New("Not a FSRepo"), cmds.ErrNormal) | ||
return | ||
} | ||
ch := make(chan *filestore.ListRes) | ||
go func() { | ||
defer close(ch) | ||
filestore.List(fsrepo.Filestore(), ch) | ||
}() | ||
res.SetOutput(&chanWriter{ch, "", 0}) | ||
}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: func(res cmds.Response) (io.Reader, error) { | ||
return res.(io.Reader), nil | ||
}, | ||
}, | ||
} | ||
|
||
var verifyFileStore = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Verify objects in filestore", | ||
}, | ||
|
||
Run: func(req cmds.Request, res cmds.Response) { | ||
node, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
fsrepo, ok := node.Repo.Self().(*fsrepo.FSRepo) | ||
if !ok { | ||
res.SetError(errors.New("Not a FSRepo"), cmds.ErrNormal) | ||
return | ||
} | ||
ch := make(chan *filestore.ListRes) | ||
go func() { | ||
defer close(ch) | ||
filestore.Verify(fsrepo.Filestore(), ch) | ||
}() | ||
res.SetOutput(&chanWriter{ch, "", 0}) | ||
}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: func(res cmds.Response) (io.Reader, error) { | ||
return res.(io.Reader), nil | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package filestore | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore" | ||
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore/query" | ||
b58 "gx/ipfs/QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf/go-base58" | ||
) | ||
|
||
const ( | ||
StatusOk = 1 | ||
StatusMissing = 2 | ||
StatusInvalid = 3 | ||
StatusError = 4 | ||
) | ||
|
||
func statusStr(status int) string { | ||
switch status { | ||
case 0: | ||
return "" | ||
case 1: | ||
return "ok " | ||
case 2: | ||
return "missing " | ||
case 3: | ||
return "invalid " | ||
case 4: | ||
return "error " | ||
default: | ||
return "?? " | ||
} | ||
} | ||
|
||
type ListRes struct { | ||
Key []byte | ||
DataObj | ||
Status int | ||
} | ||
|
||
func (r *ListRes) Format() string { | ||
mhash := b58.Encode(r.Key) | ||
return fmt.Sprintf("%s%s %s\n", statusStr(r.Status), mhash, r.DataObj.Format()) | ||
} | ||
|
||
func list(d *Datastore, out chan<- *ListRes, verify bool) error { | ||
qr, err := d.Query(query.Query{KeysOnly: true}) | ||
if err != nil { | ||
return err | ||
} | ||
for r := range qr.Next() { | ||
if r.Error != nil { | ||
return r.Error | ||
} | ||
key := ds.NewKey(r.Key) | ||
val, _ := d.GetDirect(key) | ||
status := 0 | ||
if verify { | ||
_, err := d.GetData(key, val, true) | ||
if err == nil { | ||
status = StatusOk | ||
} else if os.IsNotExist(err) { | ||
status = StatusMissing | ||
} else if _, ok := err.(InvalidBlock); ok || err == io.EOF || err == io.ErrUnexpectedEOF { | ||
status = StatusInvalid | ||
} else { | ||
status = StatusError | ||
} | ||
} | ||
out <- &ListRes{key.Bytes()[1:], val.StripData(), status} | ||
} | ||
return nil | ||
} | ||
|
||
func List(d *Datastore, out chan<- *ListRes) error { return list(d, out, false) } | ||
|
||
func Verify(d *Datastore, out chan<- *ListRes) error { return list(d, out, true) } |