-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvod.go
executable file
·267 lines (236 loc) · 5.28 KB
/
kvod.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package kvod
import (
"crypto/sha256"
"encoding/hex"
"io/ioutil"
"log"
"sort"
"sync"
"os"
"path/filepath"
)
const (
dbFilename = "data.db"
)
type dbDetails struct {
Salt []byte
}
// KVod basic struct
type KVod struct {
path string
encrypter crypto
details dbDetails
}
type KVodContainer[T any] struct {
kvod *KVod
path string
pathEncoded string
mu sync.Mutex
}
// Init KVod struct0
func Init(path string, password string) *KVod {
err := os.MkdirAll(path, 0770)
if err != nil {
log.Fatal(err)
}
dbDetailsFile := filepath.Join(path, dbFilename)
salt, err := GenerateRandom(saltSize)
if err != nil {
log.Fatal(err)
}
var details dbDetails
if _, err := os.Stat(dbDetailsFile); os.IsNotExist(err) {
details = dbDetails{
Salt: salt,
}
serializedDetails, err := serialize(details)
if err != nil {
log.Fatal(err)
}
err = write(dbDetailsFile, serializedDetails)
if err != nil {
log.Fatal(err)
}
} else {
serializedDetails, err := read(dbDetailsFile)
if err != nil {
log.Fatal(err)
}
err = deserialize(serializedDetails, &details)
if err != nil {
log.Fatal(err)
}
}
crypto := InitCrypto(password, details.Salt)
return &KVod{path, *crypto, details}
}
func CreateContainer[T any](kvod *KVod, containerPath string) *KVodContainer[T] {
pathEncoded := sha256Hex(containerPath)
err := os.MkdirAll(filepath.Join(kvod.path, pathEncoded), 0770)
if err != nil {
log.Fatal(err)
}
return &KVodContainer[T]{
path: containerPath,
pathEncoded: pathEncoded,
kvod: kvod,
mu: sync.Mutex{},
}
}
// Put a struct using a string as key
func (m *KVodContainer[T]) Put(key string, value T) error {
m.mu.Lock()
defer m.mu.Unlock()
filename := m.getFilename(key)
serializedValue, err := serialize(value)
if err != nil {
return err
}
store := make(map[string][]byte)
store[key] = serializedValue
data, err := serialize(store)
if err != nil {
return err
}
encryptedData, err := m.kvod.encrypter.encrypt(data)
if err != nil {
return err
}
return write(filename, encryptedData)
}
// Get a struct with key
func (m *KVodContainer[T]) Get(key string) (*T, error) {
m.mu.Lock()
defer m.mu.Unlock()
filename := m.getFilename(key)
data, err := read(filename)
if err != nil {
return nil, err
}
s, err := m.kvod.getMapFromData(data)
if err != nil {
return nil, err
}
var ret T
err = deserialize(s[key], &ret)
return &ret, err
}
func (m *KVod) getMapFromData(data []byte) (map[string][]byte, error) {
decrypted, err := m.encrypter.decrypt(data)
if err != nil {
return nil, err
}
s := make(map[string][]byte)
err = deserialize(decrypted, &s)
if err != nil {
return nil, err
}
return s, nil
}
// Delete a value by key
func (m *KVodContainer[T]) Delete(key string) error {
m.mu.Lock()
defer m.mu.Unlock()
return os.Remove(m.getFilename(key))
}
// GetKeys returns a slice of all keys available
func (m *KVodContainer[T]) GetKeys() ([]string, error) {
m.mu.Lock()
defer m.mu.Unlock()
var ret []string
// read all files, decrypt the content and get the key
files, err := ioutil.ReadDir(filepath.Join(m.kvod.path, m.pathEncoded))
if err != nil {
return nil, err
}
for _, f := range files {
if f.Name() != dbFilename {
data, err := read(filepath.Join(m.kvod.path, m.pathEncoded, f.Name()))
if err != nil {
return nil, err
}
kv, err := m.kvod.getMapFromData(data)
if err != nil {
return nil, err
}
for key := range kv {
ret = append(ret, key)
}
}
}
sort.Strings(ret)
return ret, err
}
// GetAll returns a slice of all data available
func (m *KVodContainer[T]) GetData() ([]T, error) {
m.mu.Lock()
defer m.mu.Unlock()
var ret []T
// read all files, decrypt the content and get the data
files, err := ioutil.ReadDir(filepath.Join(m.kvod.path, m.pathEncoded))
if err != nil {
return nil, err
}
for _, f := range files {
if f.Name() != dbFilename {
data, err := read(filepath.Join(m.kvod.path, m.pathEncoded, f.Name()))
if err != nil {
return nil, err
}
kv, err := m.kvod.getMapFromData(data)
if err != nil {
return nil, err
}
for key := range kv {
var val T
err = deserialize(kv[key], &val)
if err != nil {
return nil, err
}
ret = append(ret, val)
}
}
}
return ret, err
}
// GetAll returns a slice of all data available
func (m *KVodContainer[T]) GetAll() (map[string]T, error) {
m.mu.Lock()
defer m.mu.Unlock()
ret := make(map[string]T)
// read all files, decrypt the content and get the data
files, err := ioutil.ReadDir(filepath.Join(m.kvod.path, m.pathEncoded))
if err != nil {
return nil, err
}
for _, f := range files {
if f.Name() != dbFilename {
data, err := read(filepath.Join(m.kvod.path, m.pathEncoded, f.Name()))
if err != nil {
return nil, err
}
kv, err := m.kvod.getMapFromData(data)
if err != nil {
return nil, err
}
for key := range kv {
var val T
err = deserialize(kv[key], &val)
if err != nil {
return nil, err
}
ret[key] = val
}
}
}
return ret, err
}
func (m *KVodContainer[T]) getFilename(key string) string {
filename := sha256Hex(key)
return filepath.Join(m.kvod.path, m.pathEncoded, filename)
}
func sha256Hex(value string) string {
h := sha256.New()
h.Write([]byte(value))
return hex.EncodeToString(h.Sum(nil))
}