forked from DHowett/spectre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grant.go
126 lines (102 loc) · 2.41 KB
/
grant.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
package main
import (
"encoding/gob"
"os"
"path/filepath"
"time"
"github.com/DHowett/gotimeout"
"github.com/golang/glog"
)
type GrantStore struct {
Grants map[GrantID]PasteID
ExpiryJunk *gotimeout.HandleMap
filename string
expirator *gotimeout.Expirator
}
type GrantID string
func (r *GrantStore) Save() error {
asideFilename := r.filename + ".atomic"
file, err := os.Create(asideFilename)
if err != nil {
return err
}
defer file.Close()
enc := gob.NewEncoder(file)
err = enc.Encode(r)
if err != nil {
glog.Error("Failed to save grants: ", err)
return err
}
return os.Rename(asideFilename, r.filename)
}
func (r *GrantStore) NewGrant(id PasteID) GrantID {
newKey, _ := generateRandomBase32String(20, 32)
grantKey := GrantID(newKey)
r.Grants[grantKey] = id
r.expirator.ExpireObject(grantKey, 48*time.Hour)
r.Save()
return grantKey
//GrantID returned
}
func (r *GrantStore) Delete(p GrantID) {
r.expirator.CancelObjectExpiration(p)
delete(r.Grants, p)
r.Save()
}
func (r *GrantStore) Get(p GrantID) (PasteID, bool) {
pid, ok := r.Grants[p]
return pid, ok
}
func LoadGrantStore(filename string) *GrantStore {
var gs *GrantStore
grant_file, err := os.Open(filename)
if err == nil {
dec := gob.NewDecoder(grant_file)
err := dec.Decode(&gs)
if err != nil {
glog.Error("Failed to decode grants: ", err)
}
}
if gs == nil {
gs = &GrantStore{}
}
if gs.Grants == nil {
gs.Grants = make(map[GrantID]PasteID)
}
gs.filename = filename
gs.expirator = gotimeout.NewExpiratorWithStorage(gs, gs)
return gs
}
// grantKey passed in and made sure it exists.
func (e *GrantStore) GetExpirable(grantKey gotimeout.ExpirableID) gotimeout.Expirable {
_, ok := e.Grants[GrantID(grantKey)]
if !ok {
return nil
}
return GrantID(grantKey)
}
func (e *GrantStore) DestroyExpirable(ex gotimeout.Expirable) {
if grant, ok := ex.(GrantID); ok {
e.Delete(grant)
}
}
func (p GrantID) ExpirationID() gotimeout.ExpirableID {
return gotimeout.ExpirableID(p)
}
func (e *GrantStore) RequiresFlush() bool {
return true
}
func (e *GrantStore) SaveExpirationHandles(hm *gotimeout.HandleMap) error {
e.ExpiryJunk = hm
e.Save()
return nil
}
func (e *GrantStore) LoadExpirationHandles() (*gotimeout.HandleMap, error) {
return e.ExpiryJunk, nil
}
var grantStore *GrantStore
func init() {
arguments.register()
arguments.parse()
grantStore = LoadGrantStore(filepath.Join(arguments.root, "grants.gob"))
}