22
33package watcher
44
5- // #cgo LDFLAGS: -lwatcher-c -lstdc++
6- // #include <stdint.h>
7- // #include <stdlib.h>
8- // #include "watcher.h"
9- import "C"
105import (
116 "log/slog"
127 "path/filepath"
13- "runtime/cgo"
148 "strings"
15- "time"
16- "unsafe"
179
1810 "github.com/dunglas/frankenphp/internal/fastabs"
11+ "github.com/e-dant/watcher/watcher-go"
1912)
2013
2114type pattern struct {
@@ -24,31 +17,16 @@ type pattern struct {
2417 parsedValues []string
2518 events chan eventHolder
2619 failureCount int
27- watcher C.uintptr_t
28- h cgo.Handle
29- }
3020
31- func (p * pattern ) startSession () error {
32- p .h = cgo .NewHandle (p )
33- cDir := C .CString (p .value )
34- defer C .free (unsafe .Pointer (cDir ))
21+ watcher * watcher.Watcher
22+ }
3523
36- p .watcher = C .start_new_watcher (cDir , C .uintptr_t (p .h ))
37- if p .watcher != 0 {
38- if globalLogger .Enabled (globalCtx , slog .LevelDebug ) {
39- globalLogger .LogAttrs (globalCtx , slog .LevelDebug , "watching" , slog .String ("pattern" , p .value ))
40- }
24+ func (p * pattern ) startSession () {
25+ p .watcher = watcher .NewWatcher (p .value , p .handle )
4126
42- return nil
27+ if globalLogger .Enabled (globalCtx , slog .LevelDebug ) {
28+ globalLogger .LogAttrs (globalCtx , slog .LevelDebug , "watching" , slog .String ("pattern" , p .value ))
4329 }
44-
45- if globalLogger .Enabled (globalCtx , slog .LevelError ) {
46- globalLogger .LogAttrs (globalCtx , slog .LevelError , "couldn't start watching" , slog .String ("pattern" , p .value ))
47- }
48-
49- p .h .Delete ()
50-
51- return ErrUnableToStartWatching
5230}
5331
5432// this method prepares the pattern struct (aka /path/*pattern)
@@ -94,7 +72,7 @@ func (p *pattern) parse() (err error) {
9472 return nil
9573}
9674
97- func (p * pattern ) allowReload (event * Event ) bool {
75+ func (p * pattern ) allowReload (event * watcher. Event ) bool {
9876 if ! isValidEventType (event .EffectType ) || ! isValidPathType (event ) {
9977 return false
10078 }
@@ -105,9 +83,9 @@ func (p *pattern) allowReload(event *Event) bool {
10583 return p .isValidPattern (event .PathName ) || p .isValidPattern (event .AssociatedPathName )
10684}
10785
108- func (p * pattern ) handle (event * Event ) {
86+ func (p * pattern ) handle (event * watcher. Event ) {
10987 // If the watcher prematurely sends the die@ event, retry watching
110- if event .PathType == PathTypeWatcher && strings .HasPrefix (event .PathName , "e/self/die@" ) && watcherIsActive .Load () {
88+ if event .PathType == watcher . PathTypeWatcher && strings .HasPrefix (event .PathName , "e/self/die@" ) && watcherIsActive .Load () {
11189 p .retryWatching ()
11290
11391 return
@@ -119,23 +97,19 @@ func (p *pattern) handle(event *Event) {
11997}
12098
12199func (p * pattern ) stop () {
122- if C .stop_watcher (p .watcher ) == 0 && globalLogger .Enabled (globalCtx , slog .LevelWarn ) {
123- globalLogger .LogAttrs (globalCtx , slog .LevelWarn , "couldn't close the watcher" )
124- }
125-
126- p .h .Delete ()
100+ p .watcher .Close ()
127101}
128102
129- func isValidEventType (effectType EffectType ) bool {
130- return effectType <= EffectTypeDestroy
103+ func isValidEventType (effectType watcher. EffectType ) bool {
104+ return effectType <= watcher . EffectTypeDestroy
131105}
132106
133- func isValidPathType (event * Event ) bool {
134- if event .PathType == PathTypeWatcher && globalLogger .Enabled (globalCtx , slog .LevelDebug ) {
107+ func isValidPathType (event * watcher. Event ) bool {
108+ if event .PathType == watcher . PathTypeWatcher && globalLogger .Enabled (globalCtx , slog .LevelDebug ) {
135109 globalLogger .LogAttrs (globalCtx , slog .LevelDebug , "special e-dant/watcher event" , slog .Any ("event" , event ))
136110 }
137111
138- return event .PathType <= PathTypeHardLink
112+ return event .PathType <= watcher . PathTypeHardLink
139113}
140114
141115func (p * pattern ) isValidPattern (fileName string ) bool {
@@ -234,18 +208,3 @@ func matchPattern(pattern string, fileName string) bool {
234208
235209 return patternMatches
236210}
237-
238- //export go_handle_file_watcher_event
239- func go_handle_file_watcher_event (event C.struct_wtr_watcher_event , handle C.uintptr_t ) {
240- p := cgo .Handle (handle ).Value ().(* pattern )
241-
242- e := & Event {
243- EffectTime : time .Unix (int64 (event .effect_time )/ 1000000000 , int64 (event .effect_time )% 1000000000 ),
244- PathName : C .GoString (event .path_name ),
245- AssociatedPathName : C .GoString (event .associated_path_name ),
246- EffectType : EffectType (event .effect_type ),
247- PathType : PathType (event .path_type ),
248- }
249-
250- p .handle (e )
251- }
0 commit comments