-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_file.go
162 lines (131 loc) · 2.53 KB
/
db_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
package eagle
import (
"encoding/binary"
"errors"
"io"
"os"
"github.com/ostafen/eagle/crypto"
)
type dbFile struct {
*os.File
size uint32
iv []byte
}
var ErrInvalidFile = errors.New("invalid file")
func getIV(iv []byte, offset uint32) []byte {
newIV := make([]byte, 16)
copy(newIV, iv)
binary.BigEndian.PutUint32(newIV[12:], offset)
return newIV
}
func getCypher(iv []byte) *crypto.Cipher {
if !dbOptions.UseEncryption() {
return nil
}
return crypto.NewCipher(dbOptions.EncryptKey, iv)
}
func getSize(file *os.File) (uint32, error) {
if stat, err := file.Stat(); err != nil {
return 0, err
} else {
return uint32(stat.Size()), nil
}
}
func readIV(file *dbFile) error {
if file.size < 12 {
return ErrInvalidFile
}
iv := make([]byte, 12)
_, err := io.ReadFull(file, iv)
file.iv = iv
return err
}
func openDBFile(filename string) (*dbFile, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
size, err := getSize(file)
if err != nil {
return nil, err
}
appendFile := &dbFile{
File: file,
size: size,
}
if dbOptions.UseEncryption() {
err := readIV(appendFile)
return appendFile, err
}
return appendFile, nil
}
func writeIV(writer io.Writer) ([]byte, error) {
iv, err := crypto.GenerateIV()
if err != nil {
return nil, err
}
_, err = writer.Write(iv[:12])
if err != nil {
return nil, err
}
return iv[:12], nil
}
func createDBFile(filename string) (*dbFile, error) {
file, err := os.Create(filename)
if err != nil {
return nil, err
}
var size uint32 = 0
var iv []byte = nil
if dbOptions.UseEncryption() {
iv, err = writeIV(file)
if err != nil {
return nil, err
}
size = uint32(len(iv))
}
return &dbFile{
File: file,
size: size,
iv: iv,
}, nil
}
func (bf *dbFile) Write(p []byte) (int, error) {
n, err := bf.File.Write(p)
if n > 0 {
bf.size += uint32(n)
}
return n, err
}
func (f *dbFile) SyncAndClose() error {
if err := f.Sync(); err != nil {
return err
}
return f.Close()
}
func (f *dbFile) Remove() error {
if err := f.Close(); err != nil {
return err
}
return os.Remove(f.Name())
}
func (f *dbFile) Size() uint32 {
return f.size
}
const repairFileExt = ".repair"
func (file *dbFile) createRepairFile() (*dbFile, error) {
repairFile, err := os.Create(file.File.Name() + repairFileExt)
if err != nil {
return nil, err
}
if len(file.iv) > 0 {
if _, err := repairFile.Write(file.iv); err != nil {
return nil, err
}
}
return &dbFile{
File: repairFile,
size: uint32(len(file.iv)),
iv: file.iv,
}, nil
}