This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathcache.go
195 lines (184 loc) · 4.01 KB
/
cache.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
package net
import (
"bytes"
"github.com/limetext/log4go"
"errors"
"fmt"
"github.com/quarnster/completion/content"
"gopkg.in/fsnotify.v1"
"io/ioutil"
"os"
"path/filepath"
"sync"
)
type (
entry struct {
name string
assembly *Assembly
}
loadreq struct {
name string
reload bool
}
Cache struct {
entries []entry
paths []string
watch *fsnotify.Watcher
mutex sync.Mutex
load chan loadreq
}
)
func (c *Cache) reload(e *entry) error {
data, err := ioutil.ReadFile(e.name)
if err != nil {
return err
}
asm, err := LoadAssembly(bytes.NewReader(data))
if err != nil {
return err
}
e.assembly = asm
ci := ConcreteTableIndex{metadataUtil: &asm.MetadataUtil, table: id_AssemblyRef, index: 0}
for i := uint32(0); i < asm.Tables[id_AssemblyRef].Rows; i++ {
ci.index = i + 1
raw, err := ci.Data()
if err != nil {
return err
}
row := raw.(*AssemblyRefRow)
c.load <- loadreq{name: string(row.Name)}
}
return nil
}
func (c *Cache) loaderthread() {
for req := range c.load {
loaded := false
func() {
c.mutex.Lock()
defer c.mutex.Unlock()
for i := range c.entries {
if c.entries[i].name != req.name {
continue
}
loaded = true
if req.reload {
if err := c.reload(&c.entries[i]); err != nil {
log4go.Warn("Error reloading assembly \"%s\": %s", c.entries[i].name, err)
}
}
break
}
}()
if loaded {
continue
}
exts := []string{".dll", ".exe"}
_, err := c.Load(req.name)
if err == nil {
continue
}
for _, ext := range exts {
if _, err := c.Load(req.name + ext); err == nil {
break
}
}
}
}
func (c *Cache) watchthread() {
for {
select {
case ev := <-c.watch.Events:
if ev.Op == fsnotify.Remove {
// File no longer exists so we wont get events for it anymore
// with the old add. Thus need to re-add it like this so that
// we get events for the new file once it is created.
if err := c.watch.Add(ev.Name); err != nil {
log4go.Warn("Failed to re-watch for removed file %s: %s", ev.Name, err)
}
}
c.load <- loadreq{ev.Name, true}
case err := <-c.watch.Errors:
log4go.Error("error:", err)
}
}
}
func (c *Cache) AddPath(path string) {
c.mutex.Lock()
defer c.mutex.Unlock()
path = filepath.Clean(path)
for _, p := range c.paths {
if p == path {
return
}
}
c.paths = append(c.paths, path)
}
func (c *Cache) Load(name string) (*Assembly, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
for i := range c.entries {
if c.entries[i].assembly.Name() == name {
return c.entries[i].assembly, nil
}
}
return c.cache(name)
}
func (c *Cache) cache(assembly string) (*Assembly, error) {
errs := ""
if c.watch == nil {
c.load = make(chan loadreq, 64)
var err error
c.watch, err = fsnotify.NewWatcher()
if err != nil {
return nil, err
}
go c.watchthread()
go c.loaderthread()
}
for _, p := range c.paths {
path := filepath.Join(p, assembly)
if _, err := os.Stat(path); err != nil {
continue
}
if err := c.watch.Add(path); err != nil {
return nil, err
}
e := entry{name: path}
if err := c.reload(&e); err != nil {
if len(errs) != 0 {
errs += "\n"
}
errs += fmt.Sprintf("\t%s", err.Error())
c.watch.Remove(path)
continue
}
c.entries = append(c.entries, e)
return e.assembly, nil
}
if len(errs) == 0 {
errs = fmt.Sprintf("Assembly %s is not in path.", assembly)
} else {
errs = fmt.Sprintf("Could not load assembly %s:\n%s", assembly, errs)
}
return nil, errors.New(errs)
}
func (c *Cache) FindType(t content.FullyQualifiedName) (*TypeDef, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
for i := range c.entries {
if t2, _ := c.entries[i].assembly.FindType(t); t2 != nil {
return t2, nil
}
}
return nil, nil
}
func (c *Cache) Complete(t *content.Type) (*content.CompletionResult, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
for i := range c.entries {
if ret, err := c.entries[i].assembly.Complete(t); err == nil {
return ret, nil
}
}
return nil, errors.New(fmt.Sprintf("Couldn't find type %s", t))
}