-
Notifications
You must be signed in to change notification settings - Fork 0
/
filewatcher.go
166 lines (158 loc) · 3.81 KB
/
filewatcher.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
package linestogo
import (
"context"
"errors"
"io/ioutil"
"log"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
const (
XochitlCacheDir = `/home/root/.local/share/remarkable/xochitl`
)
// StartPolling triggers two go-routines:
//
// The first goroutine monitors the XochitlCacheDir.
// It feeds a channel with the directory containing the last modified notebook (based on the metadata)
//
// The seecond go-routine is reading the notebook directory from the previous channel and monitors it
// It feeeds the output channel with the name of the current file which is likely the current page.
//
// Caution: the remarkable is writing *a lot*, so a 50ms timer is set to prevent sending too many events on the chan
func StartPolling(ctx context.Context) (<-chan string, context.CancelFunc, error) {
ctx, cancel := context.WithCancel(ctx)
dirC, err := pollXochitlCacheDir(ctx)
if err != nil {
cancel()
return nil, nil, err
}
pageC, err := getCurrentPageName(ctx, dirC)
if err != nil {
cancel()
return nil, nil, err
}
return pageC, cancel, err
}
func pollXochitlCacheDir(ctx context.Context) (<-chan string, error) {
outC := make(chan string)
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// Send initial page
go func() {
dir, err := findMostRecent(XochitlCacheDir)
if err != nil {
log.Println("could not find most recent dir:", err)
}
dir = filepath.Join(XochitlCacheDir, dir)
outC <- dir
defer watcher.Close()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
//log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
if filepath.Ext(event.Name) == ".metadata" {
base := strings.TrimSuffix(event.Name, filepath.Ext(event.Name))
outC <- base
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
case <-ctx.Done():
return
}
}
}()
return outC, watcher.Add(XochitlCacheDir)
}
func getCurrentPageName(ctx context.Context, dirC <-chan string) (<-chan string, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
outC := make(chan string)
go func() {
defer watcher.Close()
currDir := ""
var t time.Duration
var last time.Time
for {
select {
case f := <-dirC:
if f != currDir {
if currDir != "" {
err := watcher.Remove(currDir)
if err != nil {
log.Println(err)
}
}
err = watcher.Add(f)
if err != nil {
log.Println(err)
}
// send initial page
page, err := findMostRecent(f)
if err != nil {
log.Println(err)
}
page = filepath.Join(f, page)
outC <- strings.Trim(strings.Trim(page, filepath.Ext(page)), "-metadata") + ".rm"
currDir = f
}
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
t = time.Since(last)
if strings.Contains(event.Name, "-metadata.json") && t > 50*time.Millisecond {
last = time.Now()
file := strings.Trim(event.Name, "-metadata.json") + ".rm"
outC <- file
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
case <-ctx.Done():
return
}
}
}()
return outC, nil
}
func findMostRecent(dir string) (string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
var modTime time.Time
var names []string
for _, fi := range files {
if fi.Mode().IsRegular() {
if !fi.ModTime().Before(modTime) {
if fi.ModTime().After(modTime) {
modTime = fi.ModTime()
names = names[:0]
}
names = append(names, strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name())))
}
}
}
if len(names) == 1 {
return names[0], nil
}
return "", errors.New("expected only one result")
}