/
file.go
executable file
·171 lines (163 loc) · 3.41 KB
/
file.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
package wal
import (
"encoding/binary"
"hash/crc32"
"github.com/infinivision/gaeadb/constant"
"github.com/infinivision/gaeadb/errmsg"
"github.com/infinivision/gaeadb/sum"
"golang.org/x/sys/unix"
)
func (f *file) close() error {
return unix.Munmap(f.buf)
}
func (f *file) flush() error {
return unix.Msync(f.buf, unix.MS_SYNC)
}
func (f *file) alloc(size int32) (int32, error) {
curr := f.size
if curr+size > constant.MaxTransactionSize {
return 0, errmsg.OutOfSpace
}
f.size += size
return curr, nil
}
func (f *file) getSize() {
buf := append([]byte{}, f.buf...)
o := 0
for len(buf) > HeaderSize {
n := int(binary.LittleEndian.Uint32(buf[o+SumSize:]))
if len(buf[o+HeaderSize:]) < n {
f.size = int32(o)
return
}
if sum.Sum(crc32.New(crc32.MakeTable(crc32.Castagnoli)), buf[o+HeaderSize:o+HeaderSize+n]) != binary.LittleEndian.Uint32(buf[o:]) {
f.size = int32(o)
return
}
switch buf[o+HeaderSize] {
case EM:
f.size = int32(o)
return
case EC:
o += 1
case SC:
if len(buf[o+1:]) < 4 {
f.size = int32(o)
return
}
n := int(binary.LittleEndian.Uint32(buf[o+1:]))
if len(buf[o+5:]) < n*8 { // incomplete record
f.size = int32(o)
return
}
o += 5 + n*8
case AT:
if len(buf[o+1:]) < 8 {
f.size = int32(o)
return
}
o += 9
case CT:
if len(buf[o+1:]) < 8 {
f.size = int32(o)
return
}
o += 9
case ST:
if len(buf[o+1:]) < 8 {
f.size = int32(o)
return
}
n := int(binary.LittleEndian.Uint32(buf[o+9:]))
j := 13
for i := 0; i < n; i++ {
if len(buf[o+j:]) < 2 {
f.size = int32(o)
return
}
kn := int(binary.LittleEndian.Uint16(buf[o+j:]))
j += 2
if len(buf[o+j:]) < kn {
f.size = int32(o)
return
}
j += kn
if len(buf[o+j:]) < 2 {
f.size = int32(o)
return
}
vn := int(binary.LittleEndian.Uint16(buf[o:]))
j += 2
if len(buf[o+j:]) < vn {
f.size = int32(o)
return
}
j += vn
}
o += j
case WD:
if len(buf[o+1:]) < 8 {
f.size = int32(o)
return
}
if len(buf[o+9:]) < 8 {
f.size = int32(o)
return
}
n := int(binary.LittleEndian.Uint32(buf[9:]))
if len(buf[o+13:]) < n*8 {
f.size = int32(o)
return
}
o += 13 + n*8
case NP:
if len(buf[o+1:]) < 30 {
f.size = int32(o)
return
}
o += 31
case CP:
if len(buf[o+1:]) < 20 {
f.size = int32(o)
return
}
on := int(binary.LittleEndian.Uint16(buf[o+17:]))
vn := int(binary.LittleEndian.Uint16(buf[o+19:]))
o += 21 + on*2 + vn*8
case NS:
if len(buf[o+1:]) < 26 {
f.size = int32(o)
return
}
o += 27
}
}
f.size = int32(o)
}
func newFile(path string, flag int) (*file, error) {
fd, err := unix.Open(path, unix.O_CREAT|flag, 0664)
if err != nil {
return nil, err
}
defer unix.Close(fd)
if err := unix.Ftruncate(fd, constant.MaxTransactionSize); err != nil {
return nil, err
}
buf, err := unix.Mmap(fd, 0, constant.MaxTransactionSize, unix.PROT_WRITE|unix.PROT_READ, unix.MAP_SHARED)
if err != nil {
return nil, err
}
return &file{size: 0, buf: buf}, nil
}
func openFile(path string, flag int) (*file, error) {
fd, err := unix.Open(path, flag, 0664)
if err != nil {
return nil, err
}
defer unix.Close(fd)
buf, err := unix.Mmap(fd, 0, constant.MaxTransactionSize, unix.PROT_WRITE|unix.PROT_READ, unix.MAP_SHARED)
if err != nil {
return nil, err
}
return &file{size: 0, buf: buf}, nil
}