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,22 @@
1
+ //go:build appengine || (!darwin && !dragonfly && !freebsd && !openbsd && !linux && !netbsd && !solaris && !windows)
2
+
3
+ package fsnotify
4
+
5
+ import "errors"
6
+
7
+ type other struct {
8
+ Events chan Event
9
+ Errors chan error
10
+ }
11
+
12
+ var defaultBufferSize = 0
13
+
14
+ func newBackend(ev chan Event, errs chan error) (backend, error) {
15
+ return nil, errors.New("fsnotify not supported on the current platform")
16
+ }
17
+ func (w *other) Close() error { return nil }
18
+ func (w *other) WatchList() []string { return nil }
19
+ func (w *other) Add(name string) error { return nil }
20
+ func (w *other) AddWith(name string, opts ...addOpt) error { return nil }
21
+ func (w *other) Remove(name string) error { return nil }
22
+ func (w *other) xSupports(op Op) bool { return false }
@@ -0,0 +1,702 @@
1
+ //go:build windows
2
+
3
+ // Windows backend based on ReadDirectoryChangesW()
4
+ //
5
+ // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw
6
+
7
+ package fsnotify
8
+
9
+ import (
10
+ "errors"
11
+ "fmt"
12
+ "os"
13
+ "path/filepath"
14
+ "runtime"
15
+ "strings"
16
+ "sync"
17
+ "time"
18
+ "unsafe"
19
+
20
+ "github.com/fsnotify/fsnotify/internal"
21
+ "golang.org/x/sys/windows"
22
+ )
23
+
24
+ type readDirChangesW struct {
25
+ Events chan Event
26
+ Errors chan error
27
+
28
+ port windows.Handle // Handle to completion port
29
+ input chan *input // Inputs to the reader are sent on this channel
30
+ done chan chan<- error
31
+
32
+ mu sync.Mutex // Protects access to watches, closed
33
+ watches watchMap // Map of watches (key: i-number)
34
+ closed bool // Set to true when Close() is first called
35
+ }
36
+
37
+ var defaultBufferSize = 50
38
+
39
+ func isSameOrDescendantPath(path, root string) bool {
40
+ if path == root {
41
+ return true
42
+ }
43
+ return strings.HasPrefix(path, root+string(os.PathSeparator))
44
+ }
45
+
46
+ func newBackend(ev chan Event, errs chan error) (backend, error) {
47
+ port, err := windows.CreateIoCompletionPort(windows.InvalidHandle, 0, 0, 0)
48
+ if err != nil {
49
+ return nil, os.NewSyscallError("CreateIoCompletionPort", err)
50
+ }
51
+ w := &readDirChangesW{
52
+ Events: ev,
53
+ Errors: errs,
54
+ port: port,
55
+ watches: make(watchMap),
56
+ input: make(chan *input, 1),
57
+ done: make(chan chan<- error, 1),
58
+ }
59
+ go w.readEvents()
60
+ return w, nil
61
+ }
62
+
63
+ func (w *readDirChangesW) isClosed() bool {
64
+ w.mu.Lock()
65
+ defer w.mu.Unlock()
66
+ return w.closed
67
+ }
68
+
69
+ func (w *readDirChangesW) sendEvent(name, renamedFrom string, mask uint64) bool {
70
+ if mask == 0 {
71
+ return false
72
+ }
73
+
74
+ event := w.newEvent(name, uint32(mask))
75
+ event.renamedFrom = renamedFrom
76
+ select {
77
+ case ch := <-w.done:
78
+ w.done <- ch
79
+ case w.Events <- event:
80
+ }
81
+ return true
82
+ }
83
+
84
+ // Returns true if the error was sent, or false if watcher is closed.
85
+ func (w *readDirChangesW) sendError(err error) bool {
86
+ if err == nil {
87
+ return true
88
+ }
89
+ select {
90
+ case <-w.done:
91
+ return false
92
+ case w.Errors <- err:
93
+ return true
94
+ }
95
+ }
96
+
97
+ func (w *readDirChangesW) Close() error {
98
+ if w.isClosed() {
99
+ return nil
100
+ }
101
+
102
+ w.mu.Lock()
103
+ w.closed = true
104
+ w.mu.Unlock()
105
+
106
+ // Send "done" message to the reader goroutine
107
+ ch := make(chan error)
108
+ w.done <- ch
109
+ if err := w.wakeupReader(); err != nil {
110
+ return err
111
+ }
112
+ return <-ch
113
+ }
114
+
115
+ func (w *readDirChangesW) Add(name string) error { return w.AddWith(name) }
116
+
117
+ func (w *readDirChangesW) AddWith(name string, opts ...addOpt) error {
118
+ if w.isClosed() {
119
+ return ErrClosed
120
+ }
121
+ if debug {
122
+ fmt.Fprintf(os.Stderr, "FSNOTIFY_DEBUG: %s AddWith(%q)\n",
123
+ time.Now().Format("15:04:05.000000000"), filepath.ToSlash(name))
124
+ }
125
+
126
+ with := getOptions(opts...)
127
+ if !w.xSupports(with.op) {
128
+ return fmt.Errorf("%w: %s", xErrUnsupported, with.op)
129
+ }
130
+ if with.bufsize < 4096 {
131
+ return fmt.Errorf("fsnotify.WithBufferSize: buffer size cannot be smaller than 4096 bytes")
132
+ }
133
+
134
+ in := &input{
135
+ op: opAddWatch,
136
+ path: filepath.Clean(name),
137
+ flags: sysFSALLEVENTS,
138
+ reply: make(chan error),
139
+ bufsize: with.bufsize,
140
+ }
141
+ w.input <- in
142
+ if err := w.wakeupReader(); err != nil {
143
+ return err
144
+ }
145
+ return <-in.reply
146
+ }
147
+
148
+ func (w *readDirChangesW) Remove(name string) error {
149
+ if w.isClosed() {
150
+ return nil
151
+ }
152
+ if debug {
153
+ fmt.Fprintf(os.Stderr, "FSNOTIFY_DEBUG: %s Remove(%q)\n",
154
+ time.Now().Format("15:04:05.000000000"), filepath.ToSlash(name))
155
+ }
156
+
157
+ in := &input{
158
+ op: opRemoveWatch,
159
+ path: filepath.Clean(name),
160
+ reply: make(chan error),
161
+ }
162
+ w.input <- in
163
+ if err := w.wakeupReader(); err != nil {
164
+ return err
165
+ }
166
+ return <-in.reply
167
+ }
168
+
169
+ func (w *readDirChangesW) WatchList() []string {
170
+ if w.isClosed() {
171
+ return nil
172
+ }
173
+
174
+ w.mu.Lock()
175
+ defer w.mu.Unlock()
176
+
177
+ entries := make([]string, 0, len(w.watches))
178
+ for _, entry := range w.watches {
179
+ for _, watchEntry := range entry {
180
+ for name := range watchEntry.names {
181
+ entries = append(entries, filepath.Join(watchEntry.path, name))
182
+ }
183
+ // the directory itself is being watched
184
+ if watchEntry.mask != 0 {
185
+ entries = append(entries, watchEntry.path)
186
+ }
187
+ }
188
+ }
189
+
190
+ return entries
191
+ }
192
+
193
+ // These options are from the old golang.org/x/exp/winfsnotify, where you could
194
+ // add various options to the watch. This has long since been removed.
195
+ //
196
+ // The "sys" in the name is misleading as they're not part of any "system".
197
+ //
198
+ // This should all be removed at some point, and just use windows.FILE_NOTIFY_*
199
+ const (
200
+ sysFSALLEVENTS = 0xfff
201
+ sysFSCREATE = 0x100
202
+ sysFSDELETE = 0x200
203
+ sysFSDELETESELF = 0x400
204
+ sysFSMODIFY = 0x2
205
+ sysFSMOVE = 0xc0
206
+ sysFSMOVEDFROM = 0x40
207
+ sysFSMOVEDTO = 0x80
208
+ sysFSMOVESELF = 0x800
209
+ sysFSIGNORED = 0x8000
210
+ )
211
+
212
+ func (w *readDirChangesW) newEvent(name string, mask uint32) Event {
213
+ e := Event{Name: name}
214
+ if mask&sysFSCREATE == sysFSCREATE || mask&sysFSMOVEDTO == sysFSMOVEDTO {
215
+ e.Op |= Create
216
+ }
217
+ if mask&sysFSDELETE == sysFSDELETE || mask&sysFSDELETESELF == sysFSDELETESELF {
218
+ e.Op |= Remove
219
+ }
220
+ if mask&sysFSMODIFY == sysFSMODIFY {
221
+ e.Op |= Write
222
+ }
223
+ if mask&sysFSMOVE == sysFSMOVE || mask&sysFSMOVESELF == sysFSMOVESELF || mask&sysFSMOVEDFROM == sysFSMOVEDFROM {
224
+ e.Op |= Rename
225
+ }
226
+ return e
227
+ }
228
+
229
+ const (
230
+ opAddWatch = iota
231
+ opRemoveWatch
232
+ )
233
+
234
+ const (
235
+ provisional uint64 = 1 << (32 + iota)
236
+ )
237
+
238
+ type input struct {
239
+ op int
240
+ path string
241
+ flags uint32
242
+ bufsize int
243
+ reply chan error
244
+ }
245
+
246
+ type inode struct {
247
+ handle windows.Handle
248
+ volume uint32
249
+ index uint64
250
+ }
251
+
252
+ type watch struct {
253
+ ov windows.Overlapped
254
+ ino *inode // i-number
255
+ recurse bool // Recursive watch?
256
+ path string // Directory path
257
+ mask uint64 // Directory itself is being watched with these notify flags
258
+ names map[string]uint64 // Map of names being watched and their notify flags
259
+ rename string // Remembers the old name while renaming a file
260
+ buf []byte // buffer, allocated later
261
+ }
262
+
263
+ type (
264
+ indexMap map[uint64]*watch
265
+ watchMap map[uint32]indexMap
266
+ )
267
+
268
+ func (w *readDirChangesW) wakeupReader() error {
269
+ err := windows.PostQueuedCompletionStatus(w.port, 0, 0, nil)
270
+ if err != nil {
271
+ return os.NewSyscallError("PostQueuedCompletionStatus", err)
272
+ }
273
+ return nil
274
+ }
275
+
276
+ func (w *readDirChangesW) getDir(pathname string) (dir string, err error) {
277
+ attr, err := windows.GetFileAttributes(windows.StringToUTF16Ptr(pathname))
278
+ if err != nil {
279
+ return "", os.NewSyscallError("GetFileAttributes", err)
280
+ }
281
+ if attr&windows.FILE_ATTRIBUTE_DIRECTORY != 0 {
282
+ dir = pathname
283
+ } else {
284
+ dir, _ = filepath.Split(pathname)
285
+ dir = filepath.Clean(dir)
286
+ }
287
+ return
288
+ }
289
+
290
+ func (w *readDirChangesW) getIno(path string) (ino *inode, err error) {
291
+ h, err := windows.CreateFile(windows.StringToUTF16Ptr(path),
292
+ windows.FILE_LIST_DIRECTORY,
293
+ windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE,
294
+ nil, windows.OPEN_EXISTING,
295
+ windows.FILE_FLAG_BACKUP_SEMANTICS|windows.FILE_FLAG_OVERLAPPED, 0)
296
+ if err != nil {
297
+ return nil, os.NewSyscallError("CreateFile", err)
298
+ }
299
+
300
+ var fi windows.ByHandleFileInformation
301
+ err = windows.GetFileInformationByHandle(h, &fi)
302
+ if err != nil {
303
+ windows.CloseHandle(h)
304
+ return nil, os.NewSyscallError("GetFileInformationByHandle", err)
305
+ }
306
+ ino = &inode{
307
+ handle: h,
308
+ volume: fi.VolumeSerialNumber,
309
+ index: uint64(fi.FileIndexHigh)<<32 | uint64(fi.FileIndexLow),
310
+ }
311
+ return ino, nil
312
+ }
313
+
314
+ // Must run within the I/O thread.
315
+ func (m watchMap) get(ino *inode) *watch {
316
+ if i := m[ino.volume]; i != nil {
317
+ return i[ino.index]
318
+ }
319
+ return nil
320
+ }
321
+
322
+ // Must run within the I/O thread.
323
+ func (m watchMap) set(ino *inode, watch *watch) {
324
+ i := m[ino.volume]
325
+ if i == nil {
326
+ i = make(indexMap)
327
+ m[ino.volume] = i
328
+ }
329
+ i[ino.index] = watch
330
+ }
331
+
332
+ // Must run within the I/O thread.
333
+ func (w *readDirChangesW) addWatch(pathname string, flags uint64, bufsize int) error {
334
+ pathname, recurse := recursivePath(pathname)
335
+
336
+ dir, err := w.getDir(pathname)
337
+ if err != nil {
338
+ return err
339
+ }
340
+
341
+ ino, err := w.getIno(dir)
342
+ if err != nil {
343
+ return err
344
+ }
345
+ w.mu.Lock()
346
+ watchEntry := w.watches.get(ino)
347
+ w.mu.Unlock()
348
+ if watchEntry == nil {
349
+ _, err := windows.CreateIoCompletionPort(ino.handle, w.port, 0, 0)
350
+ if err != nil {
351
+ windows.CloseHandle(ino.handle)
352
+ return os.NewSyscallError("CreateIoCompletionPort", err)
353
+ }
354
+ watchEntry = &watch{
355
+ ino: ino,
356
+ path: dir,
357
+ names: make(map[string]uint64),
358
+ recurse: recurse,
359
+ buf: make([]byte, bufsize),
360
+ }
361
+ w.mu.Lock()
362
+ w.watches.set(ino, watchEntry)
363
+ w.mu.Unlock()
364
+ flags |= provisional
365
+ } else {
366
+ windows.CloseHandle(ino.handle)
367
+ }
368
+ w.mu.Lock()
369
+ if pathname == dir {
370
+ watchEntry.mask |= flags
371
+ } else {
372
+ watchEntry.names[filepath.Base(pathname)] |= flags
373
+ }
374
+ w.mu.Unlock()
375
+
376
+ err = w.startRead(watchEntry)
377
+ if err != nil {
378
+ return err
379
+ }
380
+
381
+ w.mu.Lock()
382
+ if pathname == dir {
383
+ watchEntry.mask &= ^provisional
384
+ } else {
385
+ watchEntry.names[filepath.Base(pathname)] &= ^provisional
386
+ }
387
+ w.mu.Unlock()
388
+ return nil
389
+ }
390
+
391
+ // Must run within the I/O thread.
392
+ func (w *readDirChangesW) remWatch(pathname string) error {
393
+ pathname, recurse := recursivePath(pathname)
394
+
395
+ dir, err := w.getDir(pathname)
396
+ if err != nil {
397
+ return err
398
+ }
399
+ ino, err := w.getIno(dir)
400
+ if err != nil {
401
+ return err
402
+ }
403
+
404
+ w.mu.Lock()
405
+ watch := w.watches.get(ino)
406
+ w.mu.Unlock()
407
+ if watch == nil {
408
+ windows.CloseHandle(ino.handle)
409
+ return fmt.Errorf("%w: %s", ErrNonExistentWatch, pathname)
410
+ }
411
+
412
+ if recurse && !watch.recurse {
413
+ windows.CloseHandle(ino.handle)
414
+ return fmt.Errorf("can't use \\... with non-recursive watch %q", pathname)
415
+ }
416
+
417
+ err = windows.CloseHandle(ino.handle)
418
+ if err != nil {
419
+ w.sendError(os.NewSyscallError("CloseHandle", err))
420
+ }
421
+ if pathname == dir {
422
+ w.mu.Lock()
423
+ mask := watch.mask
424
+ watch.mask = 0
425
+ w.mu.Unlock()
426
+ w.sendEvent(watch.path, "", mask&sysFSIGNORED)
427
+ } else {
428
+ name := filepath.Base(pathname)
429
+ w.mu.Lock()
430
+ mask := watch.names[name]
431
+ delete(watch.names, name)
432
+ w.mu.Unlock()
433
+ w.sendEvent(filepath.Join(watch.path, name), "", mask&sysFSIGNORED)
434
+ }
435
+
436
+ return w.startRead(watch)
437
+ }
438
+
439
+ // Must run within the I/O thread.
440
+ func (w *readDirChangesW) deleteWatch(watch *watch) {
441
+ // Snapshot+clear under the lock so concurrent WatchList() readers see a
442
+ // consistent state. sendEvent must run outside the lock since it can
443
+ // block on the user-facing Events channel.
444
+ w.mu.Lock()
445
+ names := watch.names
446
+ watch.names = make(map[string]uint64)
447
+ mask := watch.mask
448
+ watch.mask = 0
449
+ w.mu.Unlock()
450
+
451
+ for name, m := range names {
452
+ if m&provisional == 0 {
453
+ w.sendEvent(filepath.Join(watch.path, name), "", m&sysFSIGNORED)
454
+ }
455
+ }
456
+ if mask != 0 && mask&provisional == 0 {
457
+ w.sendEvent(watch.path, "", mask&sysFSIGNORED)
458
+ }
459
+ }
460
+
461
+ // Must run within the I/O thread.
462
+ func (w *readDirChangesW) startRead(watch *watch) error {
463
+ err := windows.CancelIo(watch.ino.handle)
464
+ if err != nil {
465
+ w.sendError(os.NewSyscallError("CancelIo", err))
466
+ w.deleteWatch(watch)
467
+ }
468
+ mask := w.toWindowsFlags(watch.mask)
469
+ for _, m := range watch.names {
470
+ mask |= w.toWindowsFlags(m)
471
+ }
472
+ if mask == 0 {
473
+ err := windows.CloseHandle(watch.ino.handle)
474
+ if err != nil {
475
+ w.sendError(os.NewSyscallError("CloseHandle", err))
476
+ }
477
+ w.mu.Lock()
478
+ delete(w.watches[watch.ino.volume], watch.ino.index)
479
+ w.mu.Unlock()
480
+ return nil
481
+ }
482
+
483
+ // We need to pass the array, rather than the slice.
484
+ rdErr := windows.ReadDirectoryChanges(watch.ino.handle,
485
+ unsafe.SliceData(watch.buf), uint32(len(watch.buf)),
486
+ watch.recurse, mask, nil, &watch.ov, 0)
487
+ if rdErr != nil {
488
+ err := os.NewSyscallError("ReadDirectoryChanges", rdErr)
489
+ if rdErr == windows.ERROR_ACCESS_DENIED && watch.mask&provisional == 0 {
490
+ // Watched directory was probably removed
491
+ w.sendEvent(watch.path, "", watch.mask&sysFSDELETESELF)
492
+ err = nil
493
+ }
494
+ w.deleteWatch(watch)
495
+ w.startRead(watch)
496
+ return err
497
+ }
498
+ return nil
499
+ }
500
+
501
+ // readEvents reads from the I/O completion port, converts the
502
+ // received events into Event objects and sends them via the Events channel.
503
+ // Entry point to the I/O thread.
504
+ func (w *readDirChangesW) readEvents() {
505
+ var (
506
+ n uint32
507
+ key uintptr
508
+ ov *windows.Overlapped
509
+ )
510
+ runtime.LockOSThread()
511
+
512
+ for {
513
+ // This error is handled after the watch == nil check below.
514
+ qErr := windows.GetQueuedCompletionStatus(w.port, &n, &key, &ov, windows.INFINITE)
515
+
516
+ watch := (*watch)(unsafe.Pointer(ov))
517
+ if watch == nil {
518
+ select {
519
+ case ch := <-w.done:
520
+ w.mu.Lock()
521
+ var indexes []indexMap
522
+ for _, index := range w.watches {
523
+ indexes = append(indexes, index)
524
+ }
525
+ w.mu.Unlock()
526
+ for _, index := range indexes {
527
+ for _, watch := range index {
528
+ w.deleteWatch(watch)
529
+ w.startRead(watch)
530
+ }
531
+ }
532
+
533
+ err := windows.CloseHandle(w.port)
534
+ if err != nil {
535
+ err = os.NewSyscallError("CloseHandle", err)
536
+ }
537
+ close(w.Events)
538
+ close(w.Errors)
539
+ ch <- err
540
+ return
541
+ case in := <-w.input:
542
+ switch in.op {
543
+ case opAddWatch:
544
+ in.reply <- w.addWatch(in.path, uint64(in.flags), in.bufsize)
545
+ case opRemoveWatch:
546
+ in.reply <- w.remWatch(in.path)
547
+ }
548
+ default:
549
+ }
550
+ continue
551
+ }
552
+
553
+ switch qErr {
554
+ case nil:
555
+ // No error
556
+ case windows.ERROR_MORE_DATA:
557
+ if watch == nil {
558
+ w.sendError(errors.New("ERROR_MORE_DATA has unexpectedly null lpOverlapped buffer"))
559
+ } else {
560
+ // The i/o succeeded but the buffer is full.
561
+ // In theory we should be building up a full packet.
562
+ // In practice we can get away with just carrying on.
563
+ n = uint32(unsafe.Sizeof(watch.buf))
564
+ }
565
+ case windows.ERROR_ACCESS_DENIED:
566
+ // Watched directory was probably removed
567
+ w.sendEvent(watch.path, "", watch.mask&sysFSDELETESELF)
568
+ w.deleteWatch(watch)
569
+ w.startRead(watch)
570
+ continue
571
+ case windows.ERROR_OPERATION_ABORTED:
572
+ // CancelIo was called on this handle
573
+ continue
574
+ default:
575
+ w.sendError(os.NewSyscallError("GetQueuedCompletionPort", qErr))
576
+ continue
577
+ }
578
+
579
+ var offset uint32
580
+ for {
581
+ if n == 0 {
582
+ w.sendError(ErrEventOverflow)
583
+ break
584
+ }
585
+
586
+ // Point "raw" to the event in the buffer
587
+ raw := (*windows.FileNotifyInformation)(unsafe.Pointer(&watch.buf[offset]))
588
+
589
+ // Create a buf that is the size of the path name
590
+ size := int(raw.FileNameLength / 2)
591
+ buf := unsafe.Slice(&raw.FileName, size)
592
+ name := windows.UTF16ToString(buf)
593
+ fullname := filepath.Join(watch.path, name)
594
+
595
+ if debug {
596
+ internal.Debug(fullname, raw.Action)
597
+ }
598
+
599
+ var mask uint64
600
+ switch raw.Action {
601
+ case windows.FILE_ACTION_REMOVED:
602
+ mask = sysFSDELETESELF
603
+ case windows.FILE_ACTION_MODIFIED:
604
+ mask = sysFSMODIFY
605
+ case windows.FILE_ACTION_RENAMED_OLD_NAME:
606
+ watch.rename = name
607
+ case windows.FILE_ACTION_RENAMED_NEW_NAME:
608
+ // Update saved path of all sub-watches and rename the
609
+ // names entry under the lock so WatchList() can't observe
610
+ // a torn state.
611
+ old := filepath.Join(watch.path, watch.rename)
612
+ w.mu.Lock()
613
+ for _, watchMap := range w.watches {
614
+ for _, ww := range watchMap {
615
+ if isSameOrDescendantPath(ww.path, old) {
616
+ ww.path = filepath.Join(fullname, strings.TrimPrefix(ww.path, old))
617
+ }
618
+ }
619
+ }
620
+ if watch.names[watch.rename] != 0 {
621
+ watch.names[name] |= watch.names[watch.rename]
622
+ delete(watch.names, watch.rename)
623
+ mask = sysFSMOVESELF
624
+ }
625
+ w.mu.Unlock()
626
+ }
627
+
628
+ if raw.Action != windows.FILE_ACTION_RENAMED_NEW_NAME {
629
+ w.sendEvent(fullname, "", watch.names[name]&mask)
630
+ }
631
+ if raw.Action == windows.FILE_ACTION_REMOVED {
632
+ w.mu.Lock()
633
+ ignored := watch.names[name] & sysFSIGNORED
634
+ delete(watch.names, name)
635
+ w.mu.Unlock()
636
+ w.sendEvent(fullname, "", ignored)
637
+ }
638
+
639
+ if watch.rename != "" && raw.Action == windows.FILE_ACTION_RENAMED_NEW_NAME {
640
+ w.sendEvent(fullname, filepath.Join(watch.path, watch.rename), watch.mask&w.toFSnotifyFlags(raw.Action))
641
+ } else {
642
+ w.sendEvent(fullname, "", watch.mask&w.toFSnotifyFlags(raw.Action))
643
+ }
644
+
645
+ if raw.Action == windows.FILE_ACTION_RENAMED_NEW_NAME {
646
+ w.sendEvent(filepath.Join(watch.path, watch.rename), "", watch.names[name]&mask)
647
+ }
648
+
649
+ // Move to the next event in the buffer
650
+ if raw.NextEntryOffset == 0 {
651
+ break
652
+ }
653
+ offset += raw.NextEntryOffset
654
+
655
+ // Error!
656
+ if offset >= n {
657
+ //lint:ignore ST1005 Windows should be capitalized
658
+ w.sendError(errors.New("Windows system assumed buffer larger than it is, events have likely been missed"))
659
+ break
660
+ }
661
+ }
662
+
663
+ if err := w.startRead(watch); err != nil {
664
+ w.sendError(err)
665
+ }
666
+ }
667
+ }
668
+
669
+ func (w *readDirChangesW) toWindowsFlags(mask uint64) uint32 {
670
+ var m uint32
671
+ if mask&sysFSMODIFY != 0 {
672
+ m |= windows.FILE_NOTIFY_CHANGE_LAST_WRITE
673
+ }
674
+ if mask&(sysFSMOVE|sysFSCREATE|sysFSDELETE) != 0 {
675
+ m |= windows.FILE_NOTIFY_CHANGE_FILE_NAME | windows.FILE_NOTIFY_CHANGE_DIR_NAME
676
+ }
677
+ return m
678
+ }
679
+
680
+ func (w *readDirChangesW) toFSnotifyFlags(action uint32) uint64 {
681
+ switch action {
682
+ case windows.FILE_ACTION_ADDED:
683
+ return sysFSCREATE
684
+ case windows.FILE_ACTION_REMOVED:
685
+ return sysFSDELETE
686
+ case windows.FILE_ACTION_MODIFIED:
687
+ return sysFSMODIFY
688
+ case windows.FILE_ACTION_RENAMED_OLD_NAME:
689
+ return sysFSMOVEDFROM
690
+ case windows.FILE_ACTION_RENAMED_NEW_NAME:
691
+ return sysFSMOVEDTO
692
+ }
693
+ return 0
694
+ }
695
+
696
+ func (w *readDirChangesW) xSupports(op Op) bool {
697
+ if op.Has(xUnportableOpen) || op.Has(xUnportableRead) ||
698
+ op.Has(xUnportableCloseWrite) || op.Has(xUnportableCloseRead) {
699
+ return false
700
+ }
701
+ return true
702
+ }