Skip to content

Commit

Permalink
Move block verification into readDataObj.
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 authored and whyrusleeping committed Mar 2, 2017
1 parent cb711a2 commit 5087db5
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions filestore/fsrefstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,16 @@ func (f *FileManager) Get(c *cid.Cid) (blocks.Block, error) {
return nil, err
}

out, err := f.readDataObj(&dobj)
out, err := f.readDataObj(c, &dobj)
if err != nil {
return nil, err
}

outcid, err := c.Prefix().Sum(out)
if err != nil {
return nil, err
}

if !c.Equals(outcid) {
return nil, &CorruptReferenceError{fmt.Errorf("data in file did not match. %s offset %d", dobj.GetFilePath(), dobj.GetOffset())}
}

return blocks.NewBlockWithCid(out, c)
}

func (f *FileManager) readDataObj(d *pb.DataObj) ([]byte, error) {
// reads and verifies the block
func (f *FileManager) readDataObj(c *cid.Cid, d *pb.DataObj) ([]byte, error) {
p := filepath.FromSlash(d.GetFilePath())
abspath := filepath.Join(f.root, p)

Expand All @@ -142,6 +134,15 @@ func (f *FileManager) readDataObj(d *pb.DataObj) ([]byte, error) {
return nil, &CorruptReferenceError{err}
}

outcid, err := c.Prefix().Sum(outbuf)
if err != nil {
return nil, err
}

if !c.Equals(outcid) {
return nil, &CorruptReferenceError{fmt.Errorf("data in file did not match. %s offset %d", d.GetFilePath(), d.GetOffset())}
}

return outbuf, nil
}

Expand Down

0 comments on commit 5087db5

Please sign in to comment.