-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
87 lines (78 loc) · 2.58 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package core
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
verify "github.com/axtloss/fsverify/verify/core"
bolt "go.etcd.io/bbolt"
)
// ReadBlock reads the bytes in a specified ranges from a bytes.Reader.
// It additionally verifies that the amount of bytes read match with the size of the area and fails if the they do not match.
func ReadBlock(start int, end int, device *bytes.Reader) ([]byte, error) {
if end-start < 0 {
return []byte{}, fmt.Errorf("tried creating byte slice with negative length. %d to %d total %d\n", start, end, end-start)
}
block := make([]byte, end-start)
_, err := device.Seek(int64(start), 0)
if err != nil {
return []byte{}, err
}
_, err = device.Read(block)
return block, err
}
// CreateNode creates a Node based on given parameters.
// If prevNode is set to nil, meaning this node is the first node in a verification chain, prevNodeHash is set to "EntrypointN" with N being the number of entrypoint.
func CreateNode(blockStart int, blockEnd int, block []byte, prevNode *verify.Node, n string) (verify.Node, error) {
node := verify.Node{}
node.BlockStart = blockStart
node.BlockEnd = blockEnd
blockHash, err := CalculateBlockHash(block)
if err != nil {
return verify.Node{}, err
}
node.BlockSum = blockHash
var prevNodeHash string
if prevNode.PrevNodeSum != "" {
prevNodeHash, err = prevNode.GetHash()
if err != nil {
return verify.Node{}, err
}
} else {
prevNodeHash = "Entrypoint" + n
}
node.PrevNodeSum = prevNodeHash
return node, nil
}
// AddNode adds a node to the bucket "Nodes" in the database.
// It assumes that a database transaction has already been started and takes bolt.Tx as an argument.
func AddNode(node verify.Node, tx *bolt.Tx) error {
if node.BlockStart == node.BlockEnd {
return nil
}
nodes, err := tx.CreateBucketIfNotExists([]byte("Nodes"))
if err != nil {
return err
}
if buf, err := json.Marshal(node); err != nil {
return err
} else if err := nodes.Put([]byte(node.PrevNodeSum), buf); err != nil {
return err
}
return nil
}
// CreateHeader creates a header to be used in an fsverify partition containing all necessary information.
func CreateHeader(unsignedHash string, signedHash string, diskSize int, tableSize int) ([]byte, error) {
header := make([]byte, 200)
header[0] = 0xAC
header[1] = 0xAB
copy(header[2:], []byte(unsignedHash))
copy(header[102:], []byte(signedHash))
disk := make([]byte, 4)
binary.BigEndian.PutUint32(disk, uint32(diskSize))
copy(header[190:], disk)
db := make([]byte, 4)
binary.BigEndian.PutUint32(db, uint32(tableSize))
copy(header[195:], db)
return header, nil
}