-
Notifications
You must be signed in to change notification settings - Fork 790
/
tokens.go
107 lines (88 loc) · 2.3 KB
/
tokens.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
package ring
import (
"encoding/json"
"errors"
"os"
"sort"
)
// Tokens is a simple list of tokens.
type Tokens []uint32
func (t Tokens) Len() int { return len(t) }
func (t Tokens) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t Tokens) Less(i, j int) bool { return t[i] < t[j] }
// Equals returns whether the tokens are equal to the input ones.
func (t Tokens) Equals(other Tokens) bool {
if len(t) != len(other) {
return false
}
mine := t
sort.Sort(mine)
sort.Sort(other)
for i := 0; i < len(mine); i++ {
if mine[i] != other[i] {
return false
}
}
return true
}
// StoreToFile stores the tokens in the given directory.
func (t Tokens) StoreToFile(tokenFilePath string) error {
if tokenFilePath == "" {
return errors.New("path is empty")
}
// If any operations failed further in the function, we keep the temporary
// file hanging around for debugging.
f, err := os.Create(tokenFilePath + ".tmp")
if err != nil {
return err
}
defer func() {
// If the file was not closed, then there must already be an error, hence ignore
// the error (if any) from f.Close(). If the file was already closed, then
// we would ignore the error in that case too.
_ = f.Close()
}()
b, err := t.Marshal()
if err != nil {
return err
}
if _, err = f.Write(b); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
// Tokens successfully written, replace the temporary file with the actual file path.
return os.Rename(f.Name(), tokenFilePath)
}
// LoadTokensFromFile loads tokens from given file path.
func LoadTokensFromFile(tokenFilePath string) (Tokens, error) {
b, err := os.ReadFile(tokenFilePath)
if err != nil {
return nil, err
}
var t Tokens
err = t.Unmarshal(b)
// Tokens may have been written to file by an older version which
// doesn't guarantee sorted tokens, so we enforce sorting here.
if !sort.IsSorted(t) {
sort.Sort(t)
}
return t, err
}
// Marshal encodes the tokens into JSON.
func (t Tokens) Marshal() ([]byte, error) {
return json.Marshal(tokensJSON{Tokens: t})
}
// Unmarshal reads the tokens from JSON byte stream.
func (t *Tokens) Unmarshal(b []byte) error {
tj := tokensJSON{}
if err := json.Unmarshal(b, &tj); err != nil {
return err
}
*t = tj.Tokens
return nil
}
type tokensJSON struct {
Tokens []uint32 `json:"tokens"`
}