Repository navigation
Expand file tree
/
Copy pathtemporary.go
More file actions
219 lines (176 loc) · 4.32 KB
/
Copy pathtemporary.go
File metadata and controls
219 lines (176 loc) · 4.32 KB
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
package password
import (
"bytes"
"encoding/json"
"errors"
"sort"
"strings"
"sync"
)
var invalidTemporaryStorageIdErr = errors.New("invalid temporary storage id")
// TemporaryStorage is a memory based storage backend.
type TemporaryStorage struct {
registry map[string]string
mutex sync.Mutex
}
// NewTemporaryStorage returns a memory based storage backend.
func NewTemporaryStorage() *TemporaryStorage {
t := new(TemporaryStorage)
t.registry = make(map[string]string)
return t
}
// Store (create/overwrite) the provided data.
func (t *TemporaryStorage) Store(id string, data string) error {
t.mutex.Lock()
defer t.mutex.Unlock()
t.registry[id] = data
return nil
}
// Retrieve data from an existing memory location.
func (t *TemporaryStorage) Retrieve(id string) (string, error) {
t.mutex.Lock()
defer t.mutex.Unlock()
data, ok := t.registry[id]
if !ok {
delete(t.registry, id)
return "", invalidTemporaryStorageIdErr
}
return data, nil
}
// Exists tests if a given id already exists in the storage backend.
func (t *TemporaryStorage) Exists(id string) (bool, error) {
t.mutex.Lock()
defer t.mutex.Unlock()
_, ok := t.registry[id]
if !ok {
delete(t.registry, id)
return false, nil
}
return true, nil
}
// List all stored password-ids.
func (t *TemporaryStorage) List() ([]string, error) {
list := make([]string, 0, 16)
t.mutex.Lock()
defer t.mutex.Unlock()
for id := range t.registry {
list = append(list, id)
}
sort.Strings(list)
return list, nil
}
// Delete an existing password.
func (t *TemporaryStorage) Delete(id string) error {
t.mutex.Lock()
defer t.mutex.Unlock()
_, ok := t.registry[id]
if !ok {
delete(t.registry, id)
return invalidTemporaryStorageIdErr
}
delete(t.registry, id)
return nil
}
// Clean (delete) all stored passwords.
func (t *TemporaryStorage) Clean() error {
t.mutex.Lock()
defer t.mutex.Unlock()
t.registry = make(map[string]string)
return nil
}
// DumpJSON serializes the storage backend to a JSON string.
func (t *TemporaryStorage) DumpJSON() (string, error) {
// prepare encoder
temp := new(bytes.Buffer)
enc := json.NewEncoder(temp)
enc.SetEscapeHTML(false)
enc.SetIndent("", "")
// lock storage
t.mutex.Lock()
defer t.mutex.Unlock()
// serialize
err := enc.Encode(t.registry)
if err != nil {
return "", err
}
return strings.ReplaceAll(temp.String(), "\n", ""), nil
}
// LoadJSON deserializes a JSON string into the storage backend.
func (t *TemporaryStorage) LoadJSON(input string) error {
// prepare decoder
dec := json.NewDecoder(strings.NewReader(input))
dec.DisallowUnknownFields()
// deserialize
temp := make(map[string]interface{})
err := dec.Decode(&temp)
if err != nil {
return err
}
// check value types
for _, v := range temp {
switch v.(type) {
case string:
// pass
default:
return invalidStorageTypeErr
}
}
// lock storage
t.mutex.Lock()
defer t.mutex.Unlock()
// insert data
for k, v := range temp {
t.registry[k] = v.(string)
}
return nil
}
// WriteToDisk saves the temporary storage to files via FileStorage mechanisms.
// Warning: This method does not block operations on the underlying storage backends (read/write/create/delete).
// You should stop operations manually before usage or ignore the reported error.
// Data consistency is guaranteed.
func (t *TemporaryStorage) WriteToDisk(path string) error {
f := NewFileStorage()
f.SetStorePath(path)
list, err := t.List()
if err != nil {
return err
}
var lastErr error = nil
for _, id := range list {
data, err := t.Retrieve(id)
if err != nil {
lastErr = err
continue
}
err = f.Store(id, data)
if err != nil {
lastErr = err
}
}
return lastErr
}
// ReadFromDisk loads a FileStorage backend from disk into a temporary storage.
// Warning: This method does not block operations on the underlying storage backends (read/write/create/delete).
// You should stop operations manually before usage or ignore the reported error.
// Data consistency is guaranteed.
func (t *TemporaryStorage) ReadFromDisk(path string) error {
f := NewFileStorage()
f.SetStorePath(path)
list, err := f.List()
if err != nil {
return err
}
var lastErr error = nil
for _, id := range list {
data, err := f.Retrieve(id)
if err != nil {
lastErr = err
continue
}
err = t.Store(id, data)
if err != nil {
lastErr = err
}
}
return lastErr
}