-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.go
More file actions
249 lines (195 loc) · 4.77 KB
/
Copy pathdatabase.go
File metadata and controls
249 lines (195 loc) · 4.77 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
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
// Package subscribe provides a subscription management system.
package subscribe
import (
"encoding/json"
"fmt"
"maps"
"os"
"time"
)
/************************
* Database Methods *
************************/
// GetDB returns an interface to manage events.
func GetDB(stateFile string) (*Subscribe, error) {
sub := &Subscribe{
stateFile: stateFile,
EnableAPIs: make([]string, 0),
Events: &Events{Map: make(map[string]*Rules)},
Subscribers: make([]*Subscriber, 0),
}
err := sub.StateFileLoad()
if err != nil {
return nil, err
}
return sub, nil
}
// StateFileLoad data from a json file.
func (s *Subscribe) StateFileLoad() error {
s.mu.RLock()
stateFile := s.stateFile
s.mu.RUnlock()
if stateFile == "" {
return nil
}
// #nosec G304 -- state file path is user-configured on purpose.
buf, err := os.ReadFile(stateFile)
if os.IsNotExist(err) {
return s.StateFileSave()
}
if err != nil {
return fmt.Errorf("failed reading state file: %w", err)
}
loaded := new(Subscribe)
err = json.Unmarshal(buf, loaded)
if err != nil {
return fmt.Errorf("failed decoding state file: %w", err)
}
normalizeLoadedState(loaded)
s.mu.Lock()
s.EnableAPIs = loaded.EnableAPIs
s.Events = loaded.Events
s.Subscribers = loaded.Subscribers
s.mu.Unlock()
return nil
}
// StateGetJSON returns the state data in json format.
func (s *Subscribe) StateGetJSON() (string, error) {
snapshot := s.snapshot()
b, err := json.Marshal(snapshot)
return string(b), err
}
// StateFileSave writes out the state file.
func (s *Subscribe) StateFileSave() error {
const stateFileMode = 0o600
s.mu.RLock()
stateFile := s.stateFile
s.mu.RUnlock()
if stateFile == "" {
return nil
}
snapshot := s.snapshot()
buf, err := json.Marshal(snapshot)
if err != nil {
return fmt.Errorf("marshaling json: %w", err)
}
err = os.WriteFile(stateFile, buf, stateFileMode)
if err != nil {
return fmt.Errorf("writing file: %w", err)
}
return nil
}
// StateFileRelocate writes the state file to a new location.
func (s *Subscribe) StateFileRelocate(newPath string) error {
s.mu.Lock()
oldPath := s.stateFile
s.stateFile = newPath
s.mu.Unlock()
err := s.StateFileLoad()
if err != nil {
s.mu.Lock()
s.stateFile = oldPath
s.mu.Unlock()
}
return err
}
func normalizeLoadedState(loaded *Subscribe) {
if loaded.EnableAPIs == nil {
loaded.EnableAPIs = make([]string, 0)
}
if loaded.Events == nil {
loaded.Events = &Events{Map: make(map[string]*Rules)}
} else {
normalizeEvents(loaded.Events)
}
if loaded.Subscribers == nil {
loaded.Subscribers = make([]*Subscriber, 0)
}
for _, sub := range loaded.Subscribers {
if sub == nil {
continue
}
if sub.Events == nil {
sub.Events = &Events{Map: make(map[string]*Rules)}
continue
}
normalizeEvents(sub.Events)
}
}
func normalizeEvents(events *Events) {
if events.Map == nil {
events.Map = make(map[string]*Rules)
}
for key, rules := range events.Map {
if rules == nil {
events.Map[key] = &Rules{
D: make(map[string]time.Duration),
I: make(map[string]int),
S: make(map[string]string),
T: make(map[string]time.Time),
}
continue
}
if rules.D == nil {
rules.D = make(map[string]time.Duration)
}
if rules.I == nil {
rules.I = make(map[string]int)
}
if rules.S == nil {
rules.S = make(map[string]string)
}
if rules.T == nil {
rules.T = make(map[string]time.Time)
}
}
}
func (s *Subscribe) snapshot() *Subscribe {
s.mu.RLock()
defer s.mu.RUnlock()
out := &Subscribe{
EnableAPIs: append(make([]string, 0, len(s.EnableAPIs)), s.EnableAPIs...),
Events: snapshotEvents(s.Events),
Subscribers: make([]*Subscriber, 0, len(s.Subscribers)),
}
for _, sub := range s.Subscribers {
out.Subscribers = append(out.Subscribers, snapshotSubscriber(sub))
}
return out
}
func snapshotSubscriber(sub *Subscriber) *Subscriber {
if sub == nil {
return nil
}
// Hold the record's lock across the whole copy, events included, so the
// snapshot cannot mix fields from before a change with events from after.
// This nests Events.mu inside Subscriber.mu, the documented order.
sub.mu.RLock()
defer sub.mu.RUnlock()
out := &Subscriber{
ID: sub.ID,
API: sub.API,
Contact: sub.Contact,
Events: snapshotEvents(sub.Events),
Admin: sub.Admin,
Ignored: sub.Ignored,
FirstSeen: sub.FirstSeen,
}
if sub.Meta != nil {
out.Meta = make(map[string]any, len(sub.Meta))
maps.Copy(out.Meta, sub.Meta)
}
return out
}
func snapshotEvents(events *Events) *Events {
if events == nil {
return &Events{Map: make(map[string]*Rules)}
}
events.mu.RLock()
defer events.mu.RUnlock()
out := &Events{Map: make(map[string]*Rules, len(events.Map))}
for event, rules := range events.Map {
out.Map[event] = cloneRules(rules)
}
return out
}