/
simple.go
162 lines (154 loc) · 3.7 KB
/
simple.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
package watcher
import (
"bytes"
"crypto/sha1"
"io"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
"reflect"
"time"
fsnotify "gopkg.in/fsnotify/fsnotify.v1"
"github.com/tillberg/stringset"
)
func WatchPath(path string) (<-chan PathEvent, error) {
l := NewListener()
l.Path = path
err := l.Start()
if err != nil {
return nil, err
}
return l.NotifyChan, nil
}
func WatchPaths(paths ...string) (<-chan PathEvent, error) {
mainChan := make(chan PathEvent)
subChans := make([]reflect.SelectCase, len(paths))
for i, path := range paths {
l := NewListener()
l.Path = path
err := l.Start()
if err != nil {
return nil, err
}
subChans[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(l.NotifyChan)}
}
go func() {
for {
_, value, _ := reflect.Select(subChans)
mainChan <- value.Interface().(PathEvent)
}
}()
return mainChan, nil
}
func getExePath(pathish string) (string, error) {
if filepath.IsAbs(pathish) {
fileInfo, err := os.Stat(pathish)
if err != nil && !fileInfo.IsDir() {
return pathish, nil
}
}
path, err := exec.LookPath(pathish)
if err != nil {
log.Printf("[watcher] Failed to resolve path to %s: %v", pathish, err)
return "", err
} else if !filepath.IsAbs(path) {
absPath, err := filepath.Abs(path)
if err != nil {
log.Printf("[watcher] Failed to resolve absolute path to %s: %s", path, err)
return "", err
}
path = absPath
}
return filepath.Clean(path), nil
}
func WatchExecutable(pathish string, alsoPoll bool) (<-chan string, error) {
exePath, err := getExePath(pathish)
if err != nil {
return nil, err
}
notify, err := WatchPath(exePath)
if err != nil {
return nil, err
}
pollEvents := make(chan struct{})
debounced := make(chan string)
go func() {
pathsChanged := stringset.New()
for {
if pathsChanged.Len() > 0 {
select {
case pe := <-notify:
// ignore attribute-only change events:
if pe.Op&(^fsnotify.Chmod) != 0 {
pathsChanged.Add(pe.Path)
}
case <-pollEvents:
pathsChanged.Add(exePath)
case <-time.After(400 * time.Millisecond):
for _, p := range pathsChanged.All() {
debounced <- p
}
pathsChanged.Clear()
}
} else {
select {
case pe := <-notify:
// ignore attribute-only change events:
if pe.Op&(^fsnotify.Chmod) != 0 {
pathsChanged.Add(pe.Path)
}
case <-pollEvents:
pathsChanged.Add(exePath)
}
}
}
}()
if alsoPoll {
go func() {
info, err := os.Stat(exePath)
for err != nil {
log.Printf("failed to stat %s: %v\n", exePath, err)
time.Sleep(1 * time.Minute)
info, err = os.Stat(exePath)
}
origModTime := info.ModTime().Unix()
origSize := info.Size()
origSHA1 := calcFileSHA1(exePath)
for {
time.Sleep(time.Duration(15+rand.Intn(5)) * time.Second)
info2, err := os.Stat(exePath)
if err != nil {
log.Printf("[watcher] failed to stat %s: %v\n", exePath, err)
} else {
modTime := info2.ModTime().Unix()
size := info2.Size()
if origModTime != modTime {
newSHA1 := calcFileSHA1(exePath)
if !bytes.Equal(origSHA1, newSHA1) {
// log.Printf("[watcher] WatchExecutable %s modtime changed from %d to %d; confirmed by hash change\n", exePath, origModTime, modTime)
pollEvents <- struct{}{}
origModTime = modTime
}
}
if origSize != size {
// log.Printf("[watcher] WatchExecutable %s size changed from %d to %d\n", exePath, origSize, size)
pollEvents <- struct{}{}
origSize = size
}
}
}
}()
}
return debounced, nil
}
func calcFileSHA1(path string) []byte {
h := sha1.New()
f, err := os.Open(path)
defer f.Close()
if err == nil {
io.Copy(h, f)
}
return h.Sum(nil)[:]
}