22
33package watcher
44
5+ // #cgo LDFLAGS: -lwatcher-c -lstdc++
56// #include <stdint.h>
67// #include <stdlib.h>
78// #include "watcher.h"
@@ -21,47 +22,47 @@ type patternGroup struct {
2122 callback callbackFunc
2223}
2324
24- type watcher struct {
25- patternGroup * patternGroup
26- name string
27- parsedPatterns []string
28- events chan eventHolder
29- failureCount int
30- watcher C.uintptr_t
25+ type pattern struct {
26+ patternGroup * patternGroup
27+ value string
28+ parsedValues []string
29+ events chan eventHolder
30+ failureCount int
31+ watcher C.uintptr_t
3132}
3233
33- func (p * watcher ) startSession () error {
34+ func (p * pattern ) startSession () error {
3435 handle := cgo .NewHandle (p )
35- cDir := C .CString (p .name )
36+ cDir := C .CString (p .value )
3637 defer C .free (unsafe .Pointer (cDir ))
3738
3839 p .watcher = C .start_new_watcher (cDir , C .uintptr_t (handle ))
3940 if p .watcher != 0 {
4041 if globalLogger .Enabled (globalCtx , slog .LevelDebug ) {
41- globalLogger .LogAttrs (globalCtx , slog .LevelDebug , "watching" , slog .String ("watcher " , p .name ), slog . Any ( "groups" , p . parsedPatterns ))
42+ globalLogger .LogAttrs (globalCtx , slog .LevelDebug , "watching" , slog .String ("pattern " , p .value ))
4243 }
4344
4445 return nil
4546 }
4647
4748 if globalLogger .Enabled (globalCtx , slog .LevelError ) {
48- globalLogger .LogAttrs (globalCtx , slog .LevelError , "couldn't start watching" , slog .String ("watcher " , p .name ))
49+ globalLogger .LogAttrs (globalCtx , slog .LevelError , "couldn't start watching" , slog .String ("pattern " , p .value ))
4950 }
5051
5152 return ErrUnableToStartWatching
5253}
5354
54- // this method prepares the watcher struct (aka /name/*watcher )
55- func (p * watcher ) parse () error {
56- // first we clean the name
57- absPattern , err := fastabs .FastAbs (p .name )
55+ // this method prepares the pattern struct (aka /path/*pattern )
56+ func (p * pattern ) parse () error {
57+ // first we clean the value
58+ absPattern , err := fastabs .FastAbs (p .value )
5859 if err != nil {
5960 return err
6061 }
6162
62- p .name = absPattern
63+ p .value = absPattern
6364
64- // then we split the name to determine where the directory ends and the name starts
65+ // then we split the value to determine where the directory ends and the value starts
6566 splitPattern := strings .Split (absPattern , string (filepath .Separator ))
6667 patternWithoutDir := ""
6768 for i , part := range splitPattern {
@@ -70,33 +71,33 @@ func (p *watcher) parse() error {
7071
7172 if isFilename || isGlobCharacter {
7273 patternWithoutDir = filepath .Join (splitPattern [i :]... )
73- p .name = filepath .Join (splitPattern [:i ]... )
74+ p .value = filepath .Join (splitPattern [:i ]... )
7475
7576 break
7677 }
7778 }
7879
79- // now we split the name according to the recursive '**' syntax
80- p .parsedPatterns = strings .Split (patternWithoutDir , "**" )
81- for i , pp := range p .parsedPatterns {
82- p .parsedPatterns [i ] = strings .Trim (pp , string (filepath .Separator ))
80+ // now we split the value according to the recursive '**' syntax
81+ p .parsedValues = strings .Split (patternWithoutDir , "**" )
82+ for i , pp := range p .parsedValues {
83+ p .parsedValues [i ] = strings .Trim (pp , string (filepath .Separator ))
8384 }
8485
8586 // finally, we remove the trailing separator and add leading separator
86- p .name = string (filepath .Separator ) + strings .Trim (p .name , string (filepath .Separator ))
87+ p .value = string (filepath .Separator ) + strings .Trim (p .value , string (filepath .Separator ))
8788
8889 return nil
8990}
9091
91- func (p * watcher ) allowReload (event * Event ) bool {
92+ func (p * pattern ) allowReload (event * Event ) bool {
9293 if ! isValidEventType (event .EffectType ) || ! isValidPathType (event ) {
9394 return false
9495 }
9596
96- return isValidPattern (event , p .name , p .parsedPatterns )
97+ return isValidPattern (event , p .value , p .parsedValues )
9798}
9899
99- func (p * watcher ) handle (event * Event ) {
100+ func (p * pattern ) handle (event * Event ) {
100101 // If the globalWatcher prematurely sends the die@ event, retry watching
101102 if event .PathType == PathTypeWatcher && strings .HasPrefix (event .PathName , "e/self/die@" ) && watcherIsActive .Load () {
102103 p .retryWatching ()
@@ -109,9 +110,9 @@ func (p *watcher) handle(event *Event) {
109110 }
110111}
111112
112- func (p * watcher ) stop () {
113+ func (p * pattern ) stop () {
113114 if C .stop_watcher (p .watcher ) == 0 && globalLogger .Enabled (globalCtx , slog .LevelWarn ) {
114- globalLogger .LogAttrs (globalCtx , slog .LevelWarn , "couldn't close the globalWatcher " )
115+ globalLogger .LogAttrs (globalCtx , slog .LevelWarn , "couldn't close the watcher " )
115116 }
116117}
117118
@@ -121,30 +122,30 @@ func isValidEventType(effectType EffectType) bool {
121122
122123func isValidPathType (event * Event ) bool {
123124 if event .PathType == PathTypeWatcher && globalLogger .Enabled (globalCtx , slog .LevelDebug ) {
124- globalLogger .LogAttrs (globalCtx , slog .LevelDebug , "special edant/globalWatcher event" , slog .Any ("event" , event ))
125+ globalLogger .LogAttrs (globalCtx , slog .LevelDebug , "special e-dant/watcher event" , slog .Any ("event" , event ))
125126 }
126127
127128 return event .PathType <= PathTypeHardLink
128129}
129130
130131// some editors create temporary files and never actually modify the original file
131- // so we need to also check the associated watcher of an event
132+ // so we need to also check Event.AssociatedPathName
132133// see https://github.com/php/frankenphp/issues/1375
133134func isValidPattern (event * Event , dir string , patterns []string ) bool {
134135 fileName := event .AssociatedPathName
135136 if fileName == "" {
136137 fileName = event .PathName
137138 }
138139
139- // first we remove the name from the name
140+ // first we remove the value from the value
140141 if ! strings .HasPrefix (fileName , dir ) {
141142 return false
142143 }
143144
144- // remove the name and separator from the filename
145+ // remove the value and separator from the filename
145146 fileNameWithoutDir := strings .TrimPrefix (strings .TrimPrefix (fileName , dir ), string (filepath .Separator ))
146147
147- // if the name has size 1 we can match it directly against the filename
148+ // if the value has size 1 we can match it directly against the filename
148149 if len (patterns ) == 1 {
149150 return matchBracketPattern (patterns [0 ], fileNameWithoutDir )
150151 }
@@ -156,16 +157,16 @@ func matchPatterns(patterns []string, fileName string) bool {
156157 partsToMatch := strings .Split (fileName , string (filepath .Separator ))
157158 cursor := 0
158159
159- // if there are multiple parsedPatterns due to '**' we need to match them individually
160+ // if there are multiple parsedValues due to '**' we need to match them individually
160161 for i , pattern := range patterns {
161162 patternSize := strings .Count (pattern , string (filepath .Separator )) + 1
162163
163- // if we are at the last name we will start matching from the end of the filename
164+ // if we are at the last value we will start matching from the end of the filename
164165 if i == len (patterns )- 1 {
165166 cursor = len (partsToMatch ) - patternSize
166167 }
167168
168- // the cursor will move through the fileName until the name matches
169+ // the cursor will move through the fileName until the value matches
169170 for j := cursor ; j < len (partsToMatch ); j ++ {
170171 cursor = j
171172 subPattern := strings .Join (partsToMatch [j :j + patternSize ], string (filepath .Separator ))
@@ -185,7 +186,7 @@ func matchPatterns(patterns []string, fileName string) bool {
185186 return true
186187}
187188
188- // we also check for the following bracket syntax: /name /*.{php,twig,yaml}
189+ // we also check for the following bracket syntax: /value /*.{php,twig,yaml}
189190func matchBracketPattern (pattern string , fileName string ) bool {
190191 openingBracket := strings .Index (pattern , "{" )
191192 closingBracket := strings .Index (pattern , "}" )
@@ -230,14 +231,14 @@ func matchPattern(pattern string, fileName string) bool {
230231
231232//export go_handle_file_watcher_event
232233func go_handle_file_watcher_event (event C.struct_wtr_watcher_event , handle C.uintptr_t ) {
233- p := cgo .Handle (handle ).Value ().(* watcher )
234+ p := cgo .Handle (handle ).Value ().(* pattern )
234235
235236 e := & Event {
236237 time .Unix (int64 (event .effect_time )/ 1000000000 , int64 (event .effect_time )% 1000000000 ),
237238 C .GoString (event .path_name ),
238239 C .GoString (event .associated_path_name ),
239- EffectType (event .path_type ),
240- PathType (event .effect_type ),
240+ EffectType (event .effect_type ),
241+ PathType (event .path_type ),
241242 }
242243
243244 p .handle (e )
0 commit comments