From bad252cdab476b3b9c02b72eecd6f48a170b47d2 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 22 Jun 2016 14:28:40 -0700 Subject: [PATCH] encode keys to datastore with base32 standard encoding License: MIT Signed-off-by: Jeromy --- blocks/blockstore/blockstore.go | 17 ++++++++++------ blocks/key/key.go | 36 +++++++-------------------------- repo/fsrepo/fsrepo.go | 2 +- test/sharness/t0080-repo.sh | 12 +++++------ 4 files changed, 25 insertions(+), 42 deletions(-) diff --git a/blocks/blockstore/blockstore.go b/blocks/blockstore/blockstore.go index 4d940fc06ed4..019bf76d77bb 100644 --- a/blocks/blockstore/blockstore.go +++ b/blocks/blockstore/blockstore.go @@ -148,26 +148,31 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan key.Key, error) { } // this function is here to compartmentalize - get := func() (k key.Key, ok bool) { + get := func() (key.Key, bool) { select { case <-ctx.Done(): - return k, false + return "", false case e, more := <-res.Next(): if !more { - return k, false + return "", false } if e.Error != nil { log.Debug("blockstore.AllKeysChan got err:", e.Error) - return k, false + return "", false } // need to convert to key.Key using key.KeyFromDsKey. - k = key.KeyFromDsKey(ds.NewKey(e.Key)) + k, err := key.KeyFromDsKey(ds.NewKey(e.Key)) + if err != nil { + log.Warningf("error parsing key from DsKey: ", err) + return "", true + } log.Debug("blockstore: query got key", k) // key must be a multihash. else ignore it. - _, err := mh.Cast([]byte(k)) + _, err = mh.Cast([]byte(k)) if err != nil { + log.Warningf("key from datastore was not a multihash: ", err) return "", true } diff --git a/blocks/key/key.go b/blocks/key/key.go index 5c89a3c4c0b0..8812ae7a7a53 100644 --- a/blocks/key/key.go +++ b/blocks/key/key.go @@ -1,6 +1,7 @@ package key import ( + "encoding/base32" "encoding/json" "fmt" @@ -38,7 +39,7 @@ func B58KeyEncode(k Key) string { // DsKey returns a Datastore key func (k Key) DsKey() ds.Key { - return ds.NewKey(string(k)) + return ds.NewKey(base32.StdEncoding.EncodeToString([]byte(k))) } // UnmarshalJSON returns a JSON-encoded Key (string) @@ -68,36 +69,13 @@ func (k *Key) Loggable() map[string]interface{} { } // KeyFromDsKey returns a Datastore key -func KeyFromDsKey(dsk ds.Key) Key { - return Key(dsk.String()[1:]) -} - -// B58KeyConverter -- for KeyTransform datastores -// (static as only one obj needed) -var B58KeyConverter = b58KeyConverter{} - -type b58KeyConverter struct{} - -// ConvertKey returns a B58 encoded Datastore key -// TODO: this is hacky because it encodes every path component. some -// path components may be proper strings already... -func (b58KeyConverter) ConvertKey(dsk ds.Key) ds.Key { - k := ds.NewKey("/") - for _, n := range dsk.Namespaces() { - k = k.ChildString(b58.Encode([]byte(n))) +func KeyFromDsKey(dsk ds.Key) (Key, error) { + dec, err := base32.StdEncoding.DecodeString(dsk.String()[1:]) + if err != nil { + return "", err } - return k -} -// InvertKey returns a b58 decoded Datastore key -// TODO: this is hacky because it encodes every path component. some -// path components may be proper strings already... -func (b58KeyConverter) InvertKey(dsk ds.Key) ds.Key { - k := ds.NewKey("/") - for _, n := range dsk.Namespaces() { - k = k.ChildString(string(b58.Decode(n))) - } - return k + return Key(dec), nil } // KeySlice is used for sorting Keys diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index 450b37a2ebaa..64ad600a0a8b 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -26,7 +26,7 @@ import ( var log = logging.Logger("fsrepo") // version number that we are currently expecting to see -var RepoVersion = "3" +var RepoVersion = "4" var migrationInstructions = `See https://github.com/ipfs/fs-repo-migrations/blob/master/run.md Sorry for the inconvenience. In the future, these will run automatically.` diff --git a/test/sharness/t0080-repo.sh b/test/sharness/t0080-repo.sh index 2da6a824fa78..696fd30eff79 100755 --- a/test/sharness/t0080-repo.sh +++ b/test/sharness/t0080-repo.sh @@ -108,14 +108,14 @@ test_expect_success "'ipfs repo gc' removes file" ' grep "removed $PATCH_ROOT" actual7 ' -# TODO: there seems to be a serious bug with leveldb not returning a key. -test_expect_failure "'ipfs refs local' no longer shows file" ' +test_expect_success "'ipfs refs local' no longer shows file" ' EMPTY_DIR=QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn && - echo "$EMPTY_DIR" >expected8 && - echo "$HASH_WELCOME_DOCS" >>expected8 && - ipfs refs -r "$HASH_WELCOME_DOCS" >>expected8 && ipfs refs local >actual8 && - test_sort_cmp expected8 actual8 + grep "QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y" actual8 && + grep "$EMPTY_DIR" actual8 && + grep "$HASH_WELCOME_DOCS" actual8 && + test_must_fail grep "$HASH" actual8 && + test_must_fail grep "$PATCH_ROOT" actual8 ' test_expect_success "adding multiblock random file succeeds" '