diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2017-07-12 17:18:52 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2017-07-20 08:09:23 +0200 |
commit | 3d645268b0030fba5cc2c66bdcc715274cd5bb1c (patch) | |
tree | 4b834e8835c07a5bcd69efaa0f565defd9cb086b /storage | |
parent | 78754a7b23e5fc7a40bd3c450d54a193aaf585b1 (diff) | |
download | petrific-3d645268b0030fba5cc2c66bdcc715274cd5bb1c.tar.gz petrific-3d645268b0030fba5cc2c66bdcc715274cd5bb1c.tar.bz2 petrific-3d645268b0030fba5cc2c66bdcc715274cd5bb1c.zip |
Implement restoring
Diffstat (limited to 'storage')
-rw-r--r-- | storage/storage.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/storage/storage.go b/storage/storage.go index 424661c..45b1973 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -4,6 +4,8 @@ import ( "bytes" "code.laria.me/petrific/objects" "errors" + "fmt" + "io" ) var ( @@ -35,3 +37,45 @@ func SetObject(s Storage, o objects.RawObject) (id objects.ObjectId, err error) } return } + +type IdMismatchErr struct { + Want, Have objects.ObjectId +} + +func (iderr IdMismatchErr) Error() string { + return fmt.Sprintf("ID verification failed: want %s, have %s", iderr.Want, iderr.Have) +} + +// GetObjects gets an object from a Storage and parses and verifies it (check it's checksum/id) +func GetObject(s Storage, id objects.ObjectId) (objects.RawObject, error) { + raw, err := s.Get(id) + if err != nil { + return objects.RawObject{}, err + } + + idgen := id.Algo.Generator() + r := io.TeeReader(bytes.NewReader(raw), idgen) + + obj, err := objects.Unserialize(r) + if err != nil { + return objects.RawObject{}, err + } + + if have_id := idgen.GetId(); !have_id.Equals(id) { + return objects.RawObject{}, IdMismatchErr{id, have_id} + } + return obj, nil +} + +func GetObjectOfType(s Storage, id objects.ObjectId, t objects.ObjectType) (objects.Object, error) { + rawobj, err := GetObject(s, id) + if err != nil { + return nil, err + } + + if rawobj.Type != t { + return nil, fmt.Errorf("GetObjectOfType: Wrong object type %s (want %s)", rawobj.Type, t) + } + + return rawobj.Object() +} |