-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fswatch.go
180 lines (147 loc) · 3.87 KB
/
fswatch.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
// Package fswatch provides bindings for libfswatch.
package fswatch
// #cgo pkg-config: libfswatch
// #cgo CFLAGS: -Wall -Werror
// #include <stdint.h>
// #include <stdlib.h>
// #include <libfswatch/c/libfswatch.h>
// #include "fswatch.h"
import "C"
import (
"runtime/cgo"
"time"
"unsafe"
)
var initialized = false
type Callback func([]Event)
type Session struct {
handle C.FSW_HANDLE
paths []*C.char
callback Callback
cgoHandle cgo.Handle
*opt
}
// Start starts the monitor. Start must be called in a dedicated goroutine.
// Depending on the type of monitor this call might return when a monitor is stopped or not.
func (s *Session) Start() error {
return errs[C.fsw_start_monitor(s.handle)]
}
// Stop stops a running monitor.
func (s *Session) Stop() error {
return errs[C.fsw_stop_monitor(s.handle)]
}
// Destroy frees allocated system resources.
// It must be called when the session is not used anymore.
func (s *Session) Destroy() error {
err := errs[C.fsw_destroy_session(s.handle)]
for _, p := range s.paths {
C.free(unsafe.Pointer(p))
}
for n, v := range s.properties {
C.free(unsafe.Pointer(n))
C.free(unsafe.Pointer(v))
}
for _, f := range s.filters {
C.free(unsafe.Pointer(f.text))
}
s.cgoHandle.Delete()
return err
}
// NewSession creates a new monitor session.
//
// At least one path must be passed.
// The callback is invoked when the monitor receives change events satisfying all the session criteria.
func NewSession(paths []string, c Callback, options ...Option) (*Session, error) {
if !initialized {
if err := errs[C.fsw_init_library()]; err != nil {
return nil, err
}
initialized = true
}
opt := &opt{}
for _, o := range options {
if err := o(opt); err != nil {
return nil, err
}
}
s := &Session{handle: C.fsw_init_session(opt.monitorType), opt: opt}
s.paths = make([]*C.char, len(paths))
for i, p := range paths {
s.paths[i] = C.CString(p)
if err := errs[C.fsw_add_path(s.handle, s.paths[i])]; err != nil {
return nil, err
}
}
for n, v := range opt.properties {
if err := errs[C.fsw_add_property(s.handle, n, v)]; err != nil {
return nil, err
}
}
if opt.allowOverflow {
if err := errs[C.fsw_set_allow_overflow(s.handle, true)]; err != nil {
return nil, err
}
}
if opt.latency != 0 {
if err := errs[C.fsw_set_latency(s.handle, opt.latency)]; err != nil {
return nil, err
}
}
if opt.recursive {
if err := errs[C.fsw_set_recursive(s.handle, true)]; err != nil {
return nil, err
}
}
if opt.directoryOnly {
if err := errs[C.fsw_set_directory_only(s.handle, true)]; err != nil {
return nil, err
}
}
if opt.followSymlinks {
if err := errs[C.fsw_set_follow_symlinks(s.handle, true)]; err != nil {
return nil, err
}
}
for _, et := range opt.eventTypes {
if err := errs[C.fsw_add_event_type_filter(s.handle, C.fsw_event_type_filter{flag: C.enum_fsw_event_flag(et)})]; err != nil {
return nil, err
}
}
for _, f := range opt.filters {
if err := errs[C.fsw_add_filter(s.handle, f)]; err != nil {
return nil, err
}
}
h := cgo.NewHandle(s)
s.callback = c
s.cgoHandle = h
C.set_callback(s.handle, C.uintptr_t(h))
return s, nil
}
//export go_callback
func go_callback(events *C.fsw_cevent, eventNum C.uint, handle C.uintptr_t) {
e := make([]Event, eventNum)
var i C.uint
for ; i < eventNum; i++ {
types := make([]EventType, events.flags_num)
var j C.uint
flags := events.flags
for ; j < events.flags_num; j++ {
types[j] = EventType(*flags)
if j < events.flags_num-1 {
flags = (*C.enum_fsw_event_flag)(unsafe.Add(unsafe.Pointer(flags), C.sizeof_enum_fsw_event_flag))
}
}
e[i] = Event{
C.GoString(events.path),
time.Unix(int64(events.evt_time), 0),
types,
}
if i < eventNum-1 {
events = (*C.fsw_cevent)(unsafe.Add(unsafe.Pointer(events), C.sizeof_fsw_cevent))
}
}
h := cgo.Handle(handle)
s := h.Value().(*Session)
s.callback(e)
}