From dfada8514e43498634b5e52fbf740e84b8a6bd49 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 16 May 2015 10:52:35 +1000 Subject: [PATCH] Fix panic on unexpected files in the blob store "completed" directory --- data/blob/store.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/data/blob/store.go b/data/blob/store.go index 727c23f0..662e8cc8 100644 --- a/data/blob/store.go +++ b/data/blob/store.go @@ -51,17 +51,19 @@ func NewStore(baseDir string, opt ...StoreOption) dataPkg.Store { return s } -func hexStringPieceHashArray(s string) (ret [20]byte) { +// Turns 40 byte hex string into its equivalent binary byte array. +func hexStringPieceHashArray(s string) (ret [20]byte, ok bool) { if len(s) != 40 { - panic(s) + return } n, err := hex.Decode(ret[:], []byte(s)) if err != nil { - panic(err) + return } if n != 20 { panic(n) } + ok = true return } @@ -72,10 +74,11 @@ func (me *store) initCompleted() { } me.completed = make(map[[20]byte]struct{}, len(fis)) for _, fi := range fis { - if len(fi.Name()) != 40 { + binHash, ok := hexStringPieceHashArray(fi.Name()) + if !ok { continue } - me.completed[hexStringPieceHashArray(fi.Name())] = struct{}{} + me.completed[binHash] = struct{}{} } } @@ -164,7 +167,10 @@ func (me *store) removeCompleted(name string) (err error) { if err != nil { return err } - delete(me.completed, hexStringPieceHashArray(name)) + binHash, ok := hexStringPieceHashArray(name) + if ok { + delete(me.completed, binHash) + } return } -- 2.48.1