-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
103 lines (93 loc) · 2.5 KB
/
store.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
package main
import (
"encoding/json"
"os"
"path"
"path/filepath"
"strings"
)
func (hf *heyFil) store(t *Target) error {
if hf.storePath == "" || t == nil || t.ID == "" {
return nil
}
dest, err := os.OpenFile(path.Join(hf.storePath, t.ID+".json"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
logger.Errorw("Failed to open store file for target", "id", t.ID, "err", err)
return err
}
defer dest.Close()
return json.NewEncoder(dest).Encode(t)
}
func (hf *heyFil) loadTargets() error {
if hf.storePath == "" {
return nil
}
var totalLoaded int
hf.targetsMutex.Lock()
defer func() {
hf.targetsMutex.Unlock()
logger.Infow("finished loading SP information from store", "total", totalLoaded)
}()
return filepath.Walk(hf.storePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
logger.Errorw("failed to access path", "path", path, "err", err)
return nil
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".json") {
source, err := os.Open(path)
if err != nil {
logger.Errorw("failed to open target file at path", "path", path, "err", err)
return nil
}
var target Target
if err := json.NewDecoder(source).Decode(&target); err != nil {
logger.Errorw("failed to decode target file at path", "path", path, "err", err)
return nil
}
hf.targets[target.ID] = &target
totalLoaded++
}
return nil
})
}
type recentPieces map[string]string
func (hf *heyFil) storeRecentPieces(rp recentPieces) error {
if hf.storePath == "" || rp == nil {
return nil
}
dest, err := os.OpenFile(path.Join(hf.storePath, "recent.pieces"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
logger.Errorw("Failed to open store file for recent pieces", "err", err)
return err
}
defer dest.Close()
if err := json.NewEncoder(dest).Encode(rp); err != nil {
return err
}
hf.recentPiecesMutex.Lock()
hf.recentPieces = rp
hf.recentPiecesMutex.Unlock()
return nil
}
func (hf *heyFil) loadRecentPieces() error {
if hf.storePath == "" {
return nil
}
hf.recentPiecesMutex.Lock()
defer func() {
hf.recentPiecesMutex.Unlock()
logger.Infow("finished loading recent pieces")
}()
source, err := os.Open(path.Join(hf.storePath, "recent.pieces"))
if err != nil {
logger.Errorw("failed to open recent pieces", "err", err)
return nil
}
var target recentPieces
if err := json.NewDecoder(source).Decode(&target); err != nil {
logger.Errorw("failed to decode recent pieces", "err", err)
return nil
}
hf.recentPieces = target
return nil
}