]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix panic on unexpected files in the blob store "completed" directory
authorMatt Joiner <anacrolix@gmail.com>
Sat, 16 May 2015 00:52:35 +0000 (10:52 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 16 May 2015 00:52:35 +0000 (10:52 +1000)
data/blob/store.go

index 727c23f0d6bab988bd1649ada9d9be365513257a..662e8cc8c103932a75f83385669e790e3d0c4761 100644 (file)
@@ -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
 }