-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_metadata.go
183 lines (158 loc) · 3.78 KB
/
file_metadata.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2018 The go-hdf5 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hdf5
import (
"encoding/binary"
"io"
"github.com/pkg/errors"
)
type Superblock interface {
Version() byte
Offsets() byte
Lengths() byte
decode(r io.Reader) error
}
type SuperblockV0 struct {
version struct {
FreeSpace byte
SymTable byte
_ byte
SharedHeader byte
}
offsets byte
lengths byte
_ byte
GroupLeafNode uint16
GroupInternalNode uint16
Flags uint32
addr struct {
base uint64
free uint64
eof uint64
drv uint64
}
SymTable uint32
}
func (*SuperblockV0) Version() byte { return 0 }
func (sblk *SuperblockV0) Offsets() byte { return sblk.offsets }
func (sblk *SuperblockV0) Lengths() byte { return sblk.lengths }
func (s *SuperblockV0) decode(r io.Reader) error {
buf := make([]byte, 15)
_, err := io.ReadFull(r, buf)
if err != nil {
return err
}
s.version.FreeSpace = buf[0]
s.version.SymTable = buf[1]
s.version.SharedHeader = buf[3]
s.offsets = buf[4]
s.lengths = buf[5]
_ = buf[6]
s.GroupLeafNode = binary.LittleEndian.Uint16(buf[7 : 7+2])
s.GroupInternalNode = binary.LittleEndian.Uint16(buf[9 : 9+2])
s.Flags = binary.LittleEndian.Uint32(buf[11:15])
rr := newRBuffer(r, s.offsets, s.lengths)
s.addr.base = rr.readOffset()
s.addr.free = rr.readOffset()
s.addr.eof = rr.readOffset()
s.addr.drv = rr.readOffset()
s.SymTable = rr.readU32()
return rr.err
}
type SuperblockV1 struct {
version struct {
FreeSpace byte
SymTable byte
}
_ byte
SharedHeader byte
offsets byte
lengths byte
_ byte
GroupLeafNode uint16
GroupInternalNode uint16
flags uint32
IndexedStorage uint16
_ uint16
addr struct {
base uint64
free uint64
eof uint64
drv uint64
}
SymTable uint32
}
func (*SuperblockV1) Version() byte { return 1 }
func (sblk *SuperblockV1) Offsets() byte { return sblk.offsets }
func (sblk *SuperblockV1) Lengths() byte { return sblk.lengths }
func (s *SuperblockV1) decode(r io.Reader) error {
panic("not implemented")
}
type SuperblockV2 struct {
offsets byte
lengths byte
flags uint32
addr struct {
base uint64
super uint64
eof uint64
rootGroup uint64
}
chksum uint32
}
func (*SuperblockV2) Version() byte { return 2 }
func (sblk *SuperblockV2) Offsets() byte { return sblk.offsets }
func (sblk *SuperblockV2) Lengths() byte { return sblk.lengths }
func (s *SuperblockV2) decode(r io.Reader) error {
panic("not implemented")
}
type SuperblockV3 struct {
offsets byte
lengths byte
flags uint32
addr struct {
base uint64
super uint64
eof uint64
rootGroup uint64
}
chksum uint32
}
func (*SuperblockV3) Version() byte { return 3 }
func (sblk *SuperblockV3) Offsets() byte { return sblk.offsets }
func (sblk *SuperblockV3) Lengths() byte { return sblk.lengths }
func (s *SuperblockV3) decode(r io.Reader) error {
panic("not implemented")
}
func decodeSuperblock(r io.Reader) (Superblock, error) {
var buf [8]byte
_, err := io.ReadFull(r, buf[:1])
if err != nil {
return nil, errors.Wrapf(err, "could not read superblock version")
}
var super Superblock
switch buf[0] {
case 0:
super = &SuperblockV0{}
case 1:
super = &SuperblockV1{}
case 2:
super = &SuperblockV2{}
case 3:
super = &SuperblockV3{}
default:
return nil, ErrBadSuperblockVersion
}
err = super.decode(r)
if err != nil {
return nil, err
}
return super, nil
}
var (
_ Superblock = (*SuperblockV0)(nil)
_ Superblock = (*SuperblockV1)(nil)
_ Superblock = (*SuperblockV2)(nil)
_ Superblock = (*SuperblockV3)(nil)
)