This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_file_manager.go
272 lines (228 loc) · 5.51 KB
/
template_file_manager.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"os"
"path"
"github.com/nightlyone/lockfile"
)
type TemplateManager interface {
RegisterTemplate(filepath, installpath string) error
RemoveTemplate(installpath string) error
WhiteList(installpath string, vers []WebotsVersion) error
BlackList(installpath string, vers []WebotsVersion) error
ApplyTemplates(basepath string, v WebotsVersion) error
}
type Template struct {
Installpath, Datapath string
Whitelist, Blacklist map[string]bool
}
type HashTemplateManager struct {
byPath map[string]Template
basepath string
lock lockfile.Lockfile
}
func NewHasHTemplateManager(basepath string) (*HashTemplateManager, error) {
res := &HashTemplateManager{
byPath: make(map[string]Template),
basepath: basepath,
}
var err error
err = os.MkdirAll(basepath, 0755)
if err != nil {
return nil, err
}
res.lock, err = lockfile.New(path.Join(basepath, "global.lock"))
if err != nil {
return nil, err
}
if err := res.tryLock(); err != nil {
return nil, err
}
defer res.unlock()
err = res.load()
if err != nil && err != io.EOF {
return nil, err
}
return res, nil
}
func (m *HashTemplateManager) tryLock() error {
if err := m.lock.TryLock(); err != nil {
return fmt.Errorf("COuld not lock %s: %s", m.lock, err)
}
return nil
}
func (m *HashTemplateManager) unlock() {
if err := m.lock.Unlock(); err != nil {
panic(err)
}
}
func (m *HashTemplateManager) load() error {
f, err := os.Open(path.Join(m.basepath, "data.json"))
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
defer f.Close()
dec := json.NewDecoder(f)
err = dec.Decode(&m.byPath)
if err != nil {
return err
}
return nil
}
func (m *HashTemplateManager) save() error {
f, err := os.Create(path.Join(m.basepath, "data.json"))
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
err = enc.Encode(&m.byPath)
if err != nil {
return err
}
return nil
}
func (m *HashTemplateManager) RegisterTemplate(filepath, installpath string) error {
if err := m.tryLock(); err != nil {
return err
}
defer m.unlock()
if _, ok := m.byPath[installpath]; ok == true {
return fmt.Errorf("Template to %s is already installed", installpath)
}
f, err := os.Open(filepath)
if err != nil {
return err
}
defer f.Close()
var content bytes.Buffer
hash := sha256.New()
_, err = io.Copy(io.MultiWriter(&content, hash), f)
if err != nil {
return err
}
filename := fmt.Sprintf("%x.data", hash.Sum([]byte(installpath)))
dest, err := os.Create(path.Join(m.basepath, filename))
_, err = io.Copy(dest, &content)
if err != nil {
return err
}
m.byPath[installpath] = Template{
Datapath: filename,
Installpath: installpath,
Blacklist: make(map[string]bool),
Whitelist: make(map[string]bool),
}
return m.save()
}
func (m *HashTemplateManager) WhiteList(installpath string, vers []WebotsVersion) error {
if err := m.tryLock(); err != nil {
return err
}
defer m.unlock()
_, ok := m.byPath[installpath]
if ok == false {
return fmt.Errorf("Unknown template %s", installpath)
}
for _, v := range vers {
m.byPath[installpath].Whitelist[v.String()] = true
}
return m.save()
}
func (m *HashTemplateManager) BlackList(installpath string, vers []WebotsVersion) error {
if err := m.tryLock(); err != nil {
return err
}
defer m.unlock()
_, ok := m.byPath[installpath]
if ok == false {
return fmt.Errorf("Unknown template %s", installpath)
}
for _, v := range vers {
m.byPath[installpath].Blacklist[v.String()] = true
}
return m.save()
}
func (m *HashTemplateManager) uninstallTemplate(basepath string, t Template) error {
absTarget := path.Join(basepath, t.Installpath)
_, err := os.Lstat(absTarget)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
//check that it is a target
dataTarget, err := os.Readlink(absTarget)
if err != nil {
return err
}
if dataTarget != path.Join(m.basepath, t.Datapath) {
return fmt.Errorf("Synlink %s installed is not a template (points to %s )", absTarget, dataTarget)
}
return os.Remove(absTarget)
}
func (m *HashTemplateManager) installTemplate(basepath string, t Template) error {
absTarget := path.Join(basepath, t.Installpath)
_, err := os.Lstat(absTarget)
if err != nil && os.IsNotExist(err) == false {
return err
}
if err == nil {
return fmt.Errorf("File %s already exists", absTarget)
}
return os.Symlink(path.Join(m.basepath, t.Datapath), absTarget)
}
func (m *HashTemplateManager) ApplyTemplates(basepath string, v WebotsVersion) error {
if err := m.tryLock(); err != nil {
return err
}
defer m.unlock()
//check if blacklisted
for _, t := range m.byPath {
//if blacklisted
shouldRemove := false
if _, ok := t.Blacklist[v.String()]; ok == true {
shouldRemove = true
}
if len(t.Whitelist) != 0 {
if _, ok := t.Whitelist[v.String()]; ok == false {
shouldRemove = true
}
}
if shouldRemove {
if err := m.uninstallTemplate(basepath, t); err != nil {
return err
}
} else {
if err := m.installTemplate(basepath, t); err != nil {
return err
}
}
}
return nil
}
func (m *HashTemplateManager) RemoveTemplate(installpath string) error {
if err := m.tryLock(); err != nil {
return err
}
defer m.unlock()
t, ok := m.byPath[installpath]
if ok == false {
return fmt.Errorf("Id %s not found", installpath)
}
err := os.Remove(path.Join(m.basepath, t.Datapath))
if err != nil {
return err
}
//we still conserve that we should remove these links
delete(m.byPath, installpath)
return m.save()
}