typenative 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/.github/workflows/ci.yml +2 -0
  2. package/CHANGELOG.md +258 -0
  3. package/CONTRIBUTION.md +1 -1
  4. package/README.md +17 -1
  5. package/bin/cli.js +26 -11
  6. package/bin/go/bridge.js +74 -14
  7. package/bin/go/convert.js +59 -1
  8. package/bin/go/metadata.js +13 -4
  9. package/bin/go/overrides.js +4 -6
  10. package/bin/go/stdlib.generated.js +2 -0
  11. package/bin/index.js +338 -47
  12. package/bin/main.js +36 -14
  13. package/bin/node/assert.js +2 -2
  14. package/bin/node/buffer.js +9 -0
  15. package/bin/node/child_process.js +51 -3
  16. package/bin/node/cluster.js +41 -3
  17. package/bin/node/core.js +1 -1
  18. package/bin/node/crypto.js +205 -4
  19. package/bin/node/dgram.js +230 -6
  20. package/bin/node/dns.js +471 -8
  21. package/bin/node/errno_tables.js +445 -0
  22. package/bin/node/events.js +1 -1
  23. package/bin/node/fs.js +22 -15
  24. package/bin/node/fs_watch.js +407 -0
  25. package/bin/node/http.js +128 -30
  26. package/bin/node/http2.js +37 -7
  27. package/bin/node/module_v8.js +142 -0
  28. package/bin/node/net.js +9 -6
  29. package/bin/node/process.js +242 -0
  30. package/bin/node/readline.js +272 -1
  31. package/bin/node/registry.js +3 -1
  32. package/bin/node/sqlite.js +60 -12
  33. package/bin/node/stream.js +145 -10
  34. package/bin/node/test.js +311 -19
  35. package/bin/node/timers.js +1 -1
  36. package/bin/node/typed_arrays.js +608 -0
  37. package/bin/node/url.js +23 -5
  38. package/bin/node/util.js +21 -1
  39. package/bin/node/util_extra.js +6 -30
  40. package/bin/node/worker_threads.js +79 -19
  41. package/bin/node/zlib.js +131 -16
  42. package/bin/transpiler.js +4729 -616
  43. package/bin/typecheck-node.js +1 -0
  44. package/bin/version.js +1 -1
  45. package/conformance.md +1030 -877
  46. package/examples/README.md +54 -0
  47. package/examples/fibonacci/README.md +89 -0
  48. package/examples/fibonacci/bench.mjs +87 -0
  49. package/examples/fibonacci/main.ts +21 -0
  50. package/examples/hello-world/README.md +62 -0
  51. package/examples/hello-world/bench.mjs +66 -0
  52. package/examples/hello-world/main.ts +7 -0
  53. package/examples/json-server/README.md +127 -0
  54. package/examples/json-server/bench.mjs +263 -0
  55. package/examples/json-server/main.ts +67 -0
  56. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  57. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  58. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  59. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  60. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  61. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  62. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  63. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  64. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  65. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  66. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  67. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  68. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  69. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  70. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  71. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  72. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  73. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  74. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  75. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  76. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  77. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  78. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  79. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  80. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  81. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  82. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  83. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  84. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  85. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  86. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  87. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  88. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  89. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  90. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  91. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  92. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  93. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  94. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  95. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  96. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  97. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  98. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  99. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  100. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  101. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  102. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  103. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  104. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  105. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  106. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  107. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  108. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  109. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  110. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  111. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  112. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  113. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  114. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  115. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  116. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  117. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  118. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  119. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  120. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  121. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  122. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  123. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  124. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  125. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  126. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  127. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  128. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  129. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  130. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  131. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  132. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  133. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  134. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  135. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  136. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  137. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  138. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  139. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  140. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  141. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  142. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  143. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  144. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  145. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  146. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  147. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  148. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  149. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  150. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  151. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  152. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  153. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  154. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  155. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  156. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  157. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  158. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  159. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  160. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  161. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  162. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  163. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  164. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  165. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  166. package/govendor/manifest.txt +3 -1
  167. package/package.json +1 -1
  168. package/types/typenative-go.d.ts +23 -0
  169. package/types/typenative-npm.d.ts +241 -5
  170. package/types/typenative.d.ts +302 -12
@@ -0,0 +1,469 @@
1
+ //go:build solaris
2
+
3
+ // FEN backend for illumos (supported) and Solaris (untested, but should work).
4
+ //
5
+ // See port_create(3c) etc. for docs. https://www.illumos.org/man/3C/port_create
6
+
7
+ package fsnotify
8
+
9
+ import (
10
+ "errors"
11
+ "fmt"
12
+ "io/fs"
13
+ "os"
14
+ "path/filepath"
15
+ "sync"
16
+ "time"
17
+
18
+ "github.com/fsnotify/fsnotify/internal"
19
+ "golang.org/x/sys/unix"
20
+ )
21
+
22
+ type fen struct {
23
+ *shared
24
+ Events chan Event
25
+ Errors chan error
26
+
27
+ mu sync.Mutex
28
+ port *unix.EventPort
29
+ dirs map[string]Op // Explicitly watched directories
30
+ watches map[string]Op // Explicitly watched non-directories
31
+ }
32
+
33
+ var defaultBufferSize = 0
34
+
35
+ func newBackend(ev chan Event, errs chan error) (backend, error) {
36
+ w := &fen{
37
+ shared: newShared(ev, errs),
38
+ Events: ev,
39
+ Errors: errs,
40
+ dirs: make(map[string]Op),
41
+ watches: make(map[string]Op),
42
+ }
43
+
44
+ var err error
45
+ w.port, err = unix.NewEventPort()
46
+ if err != nil {
47
+ return nil, fmt.Errorf("fsnotify.NewWatcher: %w", err)
48
+ }
49
+
50
+ go w.readEvents()
51
+ return w, nil
52
+ }
53
+
54
+ func (w *fen) Close() error {
55
+ if w.shared.close() {
56
+ return nil
57
+ }
58
+ return w.port.Close()
59
+ }
60
+
61
+ func (w *fen) Add(name string) error { return w.AddWith(name) }
62
+
63
+ func (w *fen) AddWith(name string, opts ...addOpt) error {
64
+ if w.isClosed() {
65
+ return ErrClosed
66
+ }
67
+ if debug {
68
+ fmt.Fprintf(os.Stderr, "FSNOTIFY_DEBUG: %s AddWith(%q)\n",
69
+ time.Now().Format("15:04:05.000000000"), name)
70
+ }
71
+
72
+ with := getOptions(opts...)
73
+ if !w.xSupports(with.op) {
74
+ return fmt.Errorf("%w: %s", xErrUnsupported, with.op)
75
+ }
76
+
77
+ // Currently we resolve symlinks that were explicitly requested to be
78
+ // watched. Otherwise we would use LStat here.
79
+ stat, err := os.Stat(name)
80
+ if err != nil {
81
+ return err
82
+ }
83
+
84
+ // Associate all files in the directory.
85
+ if stat.IsDir() {
86
+ err := w.handleDirectory(name, stat, true, w.associateFile)
87
+ if err != nil {
88
+ return err
89
+ }
90
+
91
+ w.mu.Lock()
92
+ w.dirs[name] = with.op
93
+ w.mu.Unlock()
94
+ return nil
95
+ }
96
+
97
+ err = w.associateFile(name, stat, true)
98
+ if err != nil {
99
+ return err
100
+ }
101
+
102
+ w.mu.Lock()
103
+ w.watches[name] = with.op
104
+ w.mu.Unlock()
105
+ return nil
106
+ }
107
+
108
+ func (w *fen) Remove(name string) error {
109
+ if w.isClosed() {
110
+ return nil
111
+ }
112
+ if !w.port.PathIsWatched(name) {
113
+ return fmt.Errorf("%w: %s", ErrNonExistentWatch, name)
114
+ }
115
+ if debug {
116
+ fmt.Fprintf(os.Stderr, "FSNOTIFY_DEBUG: %s Remove(%q)\n",
117
+ time.Now().Format("15:04:05.000000000"), name)
118
+ }
119
+
120
+ // The user has expressed an intent. Immediately remove this name from
121
+ // whichever watch list it might be in. If it's not in there the delete
122
+ // doesn't cause harm.
123
+ w.mu.Lock()
124
+ delete(w.watches, name)
125
+ delete(w.dirs, name)
126
+ w.mu.Unlock()
127
+
128
+ stat, err := os.Stat(name)
129
+ if err != nil {
130
+ return err
131
+ }
132
+
133
+ // Remove associations for every file in the directory.
134
+ if stat.IsDir() {
135
+ err := w.handleDirectory(name, stat, false, w.dissociateFile)
136
+ if err != nil {
137
+ return err
138
+ }
139
+ return nil
140
+ }
141
+
142
+ err = w.port.DissociatePath(name)
143
+ if err != nil {
144
+ return err
145
+ }
146
+
147
+ return nil
148
+ }
149
+
150
+ // readEvents contains the main loop that runs in a goroutine watching for events.
151
+ func (w *fen) readEvents() {
152
+ // If this function returns, the watcher has been closed and we can close
153
+ // these channels
154
+ defer func() {
155
+ close(w.Errors)
156
+ close(w.Events)
157
+ }()
158
+
159
+ pevents := make([]unix.PortEvent, 8)
160
+ for {
161
+ count, err := internal.IgnoringEINTR(func() (int, error) {
162
+ return w.port.Get(pevents, 1, nil)
163
+ })
164
+ if err != nil && err != unix.ETIME {
165
+ // Interrupted system call (count should be 0) ignore and continue
166
+ if errors.Is(err, unix.EINTR) && count == 0 {
167
+ continue
168
+ }
169
+ // Get failed because we called w.Close()
170
+ if errors.Is(err, unix.EBADF) && w.isClosed() {
171
+ return
172
+ }
173
+ // There was an error not caused by calling w.Close()
174
+ if !w.sendError(fmt.Errorf("port.Get: %w", err)) {
175
+ return
176
+ }
177
+ }
178
+
179
+ p := pevents[:count]
180
+ for _, pevent := range p {
181
+ if pevent.Source != unix.PORT_SOURCE_FILE {
182
+ // Event from unexpected source received; should never happen.
183
+ if !w.sendError(errors.New("Event from unexpected source received")) {
184
+ return
185
+ }
186
+ continue
187
+ }
188
+
189
+ if debug {
190
+ internal.Debug(pevent.Path, pevent.Events)
191
+ }
192
+
193
+ err = w.handleEvent(&pevent)
194
+ if !w.sendError(err) {
195
+ return
196
+ }
197
+ }
198
+ }
199
+ }
200
+
201
+ func (w *fen) handleDirectory(path string, stat os.FileInfo, follow bool, handler func(string, os.FileInfo, bool) error) error {
202
+ files, err := os.ReadDir(path)
203
+ if err != nil {
204
+ return err
205
+ }
206
+
207
+ // Handle all children of the directory.
208
+ for _, entry := range files {
209
+ finfo, err := entry.Info()
210
+ if err != nil {
211
+ return err
212
+ }
213
+ err = handler(filepath.Join(path, finfo.Name()), finfo, false)
214
+ if err != nil {
215
+ return err
216
+ }
217
+ }
218
+
219
+ // And finally handle the directory itself.
220
+ return handler(path, stat, follow)
221
+ }
222
+
223
+ // handleEvent might need to emit more than one fsnotify event if the events
224
+ // bitmap matches more than one event type (e.g. the file was both modified and
225
+ // had the attributes changed between when the association was created and the
226
+ // when event was returned)
227
+ func (w *fen) handleEvent(event *unix.PortEvent) error {
228
+ var (
229
+ events = event.Events
230
+ path = event.Path
231
+ fmode = event.Cookie.(os.FileMode)
232
+ reRegister = true
233
+ )
234
+
235
+ w.mu.Lock()
236
+ _, watchedDir := w.dirs[path]
237
+ _, watchedPath := w.watches[path]
238
+ w.mu.Unlock()
239
+ isWatched := watchedDir || watchedPath
240
+
241
+ if events&unix.FILE_DELETE != 0 {
242
+ if !w.sendEvent(Event{Name: path, Op: Remove}) {
243
+ return nil
244
+ }
245
+ reRegister = false
246
+ }
247
+ if events&unix.FILE_RENAME_FROM != 0 {
248
+ if !w.sendEvent(Event{Name: path, Op: Rename}) {
249
+ return nil
250
+ }
251
+ // Don't keep watching the new file name
252
+ reRegister = false
253
+ }
254
+ if events&unix.FILE_RENAME_TO != 0 {
255
+ // We don't report a Rename event for this case, because Rename events
256
+ // are interpreted as referring to the _old_ name of the file, and in
257
+ // this case the event would refer to the new name of the file. This
258
+ // type of rename event is not supported by fsnotify.
259
+
260
+ // inotify reports a Remove event in this case, so we simulate this
261
+ // here.
262
+ if !w.sendEvent(Event{Name: path, Op: Remove}) {
263
+ return nil
264
+ }
265
+ // Don't keep watching the file that was removed
266
+ reRegister = false
267
+ }
268
+
269
+ // The file is gone, nothing left to do.
270
+ if !reRegister {
271
+ if watchedDir {
272
+ w.mu.Lock()
273
+ delete(w.dirs, path)
274
+ w.mu.Unlock()
275
+ }
276
+ if watchedPath {
277
+ w.mu.Lock()
278
+ delete(w.watches, path)
279
+ w.mu.Unlock()
280
+ }
281
+ return nil
282
+ }
283
+
284
+ // If we didn't get a deletion the file still exists and we're going to have
285
+ // to watch it again. Let's Stat it now so that we can compare permissions
286
+ // and have what we need to continue watching the file
287
+
288
+ stat, err := os.Lstat(path)
289
+ if err != nil {
290
+ // This is unexpected, but we should still emit an event. This happens
291
+ // most often on "rm -r" of a subdirectory inside a watched directory We
292
+ // get a modify event of something happening inside, but by the time we
293
+ // get here, the sudirectory is already gone. Clearly we were watching
294
+ // this path but now it is gone. Let's tell the user that it was
295
+ // removed.
296
+ if !w.sendEvent(Event{Name: path, Op: Remove}) {
297
+ return nil
298
+ }
299
+ // Suppress extra write events on removed directories; they are not
300
+ // informative and can be confusing.
301
+ return nil
302
+ }
303
+
304
+ // resolve symlinks that were explicitly watched as we would have at Add()
305
+ // time. this helps suppress spurious Chmod events on watched symlinks
306
+ if isWatched {
307
+ stat, err = os.Stat(path)
308
+ if err != nil {
309
+ // The symlink still exists, but the target is gone. Report the
310
+ // Remove similar to above.
311
+ if !w.sendEvent(Event{Name: path, Op: Remove}) {
312
+ return nil
313
+ }
314
+ // Don't return the error
315
+ }
316
+ }
317
+
318
+ if events&unix.FILE_MODIFIED != 0 {
319
+ if fmode.IsDir() && watchedDir {
320
+ if err := w.updateDirectory(path); err != nil {
321
+ return err
322
+ }
323
+ } else {
324
+ if !w.sendEvent(Event{Name: path, Op: Write}) {
325
+ return nil
326
+ }
327
+ }
328
+ }
329
+ if events&unix.FILE_ATTRIB != 0 && stat != nil {
330
+ // Only send Chmod if perms changed
331
+ if stat.Mode().Perm() != fmode.Perm() {
332
+ if !w.sendEvent(Event{Name: path, Op: Chmod}) {
333
+ return nil
334
+ }
335
+ }
336
+ }
337
+
338
+ if stat != nil {
339
+ // If we get here, it means we've hit an event above that requires us to
340
+ // continue watching the file or directory
341
+ err := w.associateFile(path, stat, isWatched)
342
+ if errors.Is(err, fs.ErrNotExist) {
343
+ // Path may have been removed since the stat.
344
+ err = nil
345
+ }
346
+ return err
347
+ }
348
+ return nil
349
+ }
350
+
351
+ // The directory was modified, so we must find unwatched entities and watch
352
+ // them. If something was removed from the directory, nothing will happen, as
353
+ // everything else should still be watched.
354
+ func (w *fen) updateDirectory(path string) error {
355
+ files, err := os.ReadDir(path)
356
+ if err != nil {
357
+ // Directory no longer exists: probably just deleted since we got the
358
+ // event.
359
+ if errors.Is(err, fs.ErrNotExist) {
360
+ return nil
361
+ }
362
+ return err
363
+ }
364
+
365
+ for _, entry := range files {
366
+ path := filepath.Join(path, entry.Name())
367
+ if w.port.PathIsWatched(path) {
368
+ continue
369
+ }
370
+
371
+ finfo, err := entry.Info()
372
+ if err != nil {
373
+ return err
374
+ }
375
+ err = w.associateFile(path, finfo, false)
376
+ if errors.Is(err, fs.ErrNotExist) {
377
+ // File may have disappeared between getting the dir listing and
378
+ // adding the port: that's okay to ignore.
379
+ continue
380
+ }
381
+ if !w.sendError(err) {
382
+ return nil
383
+ }
384
+ if !w.sendEvent(Event{Name: path, Op: Create}) {
385
+ return nil
386
+ }
387
+ }
388
+ return nil
389
+ }
390
+
391
+ func (w *fen) associateFile(path string, stat os.FileInfo, follow bool) error {
392
+ if w.isClosed() {
393
+ return ErrClosed
394
+ }
395
+ // This is primarily protecting the call to AssociatePath but it is
396
+ // important and intentional that the call to PathIsWatched is also
397
+ // protected by this mutex. Without this mutex, AssociatePath has been seen
398
+ // to error out that the path is already associated.
399
+ w.mu.Lock()
400
+ defer w.mu.Unlock()
401
+
402
+ if w.port.PathIsWatched(path) {
403
+ // Remove the old association in favor of this one If we get ENOENT,
404
+ // then while the x/sys/unix wrapper still thought that this path was
405
+ // associated, the underlying event port did not. This call will have
406
+ // cleared up that discrepancy. The most likely cause is that the event
407
+ // has fired but we haven't processed it yet.
408
+ err := w.port.DissociatePath(path)
409
+ if err != nil && !errors.Is(err, unix.ENOENT) {
410
+ return fmt.Errorf("port.DissociatePath(%q): %w", path, err)
411
+ }
412
+ }
413
+
414
+ var events int
415
+ if !follow {
416
+ // Watch symlinks themselves rather than their targets unless this entry
417
+ // is explicitly watched.
418
+ events |= unix.FILE_NOFOLLOW
419
+ }
420
+ if true { // TODO: implement withOps()
421
+ events |= unix.FILE_MODIFIED
422
+ }
423
+ if true {
424
+ events |= unix.FILE_ATTRIB
425
+ }
426
+ err := w.port.AssociatePath(path, stat, events, stat.Mode())
427
+ if err != nil {
428
+ return fmt.Errorf("port.AssociatePath(%q): %w", path, err)
429
+ }
430
+ return nil
431
+ }
432
+
433
+ func (w *fen) dissociateFile(path string, stat os.FileInfo, unused bool) error {
434
+ if !w.port.PathIsWatched(path) {
435
+ return nil
436
+ }
437
+ err := w.port.DissociatePath(path)
438
+ if err != nil {
439
+ return fmt.Errorf("port.DissociatePath(%q): %w", path, err)
440
+ }
441
+ return nil
442
+ }
443
+
444
+ func (w *fen) WatchList() []string {
445
+ if w.isClosed() {
446
+ return nil
447
+ }
448
+
449
+ w.mu.Lock()
450
+ defer w.mu.Unlock()
451
+
452
+ entries := make([]string, 0, len(w.watches)+len(w.dirs))
453
+ for pathname := range w.dirs {
454
+ entries = append(entries, pathname)
455
+ }
456
+ for pathname := range w.watches {
457
+ entries = append(entries, pathname)
458
+ }
459
+
460
+ return entries
461
+ }
462
+
463
+ func (w *fen) xSupports(op Op) bool {
464
+ if op.Has(xUnportableOpen) || op.Has(xUnportableRead) ||
465
+ op.Has(xUnportableCloseWrite) || op.Has(xUnportableCloseRead) {
466
+ return false
467
+ }
468
+ return true
469
+ }