| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package notify |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"runtime" |
| 12 |
"sync" |
| 13 |
"sync/atomic" |
| 14 |
"syscall" |
| 15 |
"unsafe" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
const readBufferSize = 4096 |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
const ( |
| 29 |
stateRewatch uint32 = 1 << (28 + iota) |
| 30 |
stateUnwatch |
| 31 |
stateCPClose |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
const ( |
| 41 |
onlyNotifyChanges uint32 = 0x00000FFF |
| 42 |
onlyNGlobalEvents uint32 = 0x0FF00000 |
| 43 |
onlyMachineStates uint32 = 0xF0000000 |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
type grip struct { |
| 52 |
handle syscall.Handle |
| 53 |
filter uint32 |
| 54 |
recursive bool |
| 55 |
pathw []uint16 |
| 56 |
buffer [readBufferSize]byte |
| 57 |
parent *watched |
| 58 |
ovlapped *overlappedEx |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
type overlappedEx struct { |
| 65 |
syscall.Overlapped |
| 66 |
parent *grip |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
func newGrip(cph syscall.Handle, parent *watched, filter uint32) (*grip, error) { |
| 73 |
g := &grip{ |
| 74 |
handle: syscall.InvalidHandle, |
| 75 |
filter: filter, |
| 76 |
recursive: parent.recursive, |
| 77 |
pathw: parent.pathw, |
| 78 |
parent: parent, |
| 79 |
ovlapped: &overlappedEx{}, |
| 80 |
} |
| 81 |
if err := g.register(cph); err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
g.ovlapped.parent = g |
| 85 |
return g, nil |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func (g *grip) register(cph syscall.Handle) (err error) { |
| 90 |
if g.handle, err = syscall.CreateFile( |
| 91 |
&g.pathw[0], |
| 92 |
syscall.FILE_LIST_DIRECTORY, |
| 93 |
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE, |
| 94 |
nil, |
| 95 |
syscall.OPEN_EXISTING, |
| 96 |
syscall.FILE_FLAG_BACKUP_SEMANTICS|syscall.FILE_FLAG_OVERLAPPED, |
| 97 |
0, |
| 98 |
); err != nil { |
| 99 |
return |
| 100 |
} |
| 101 |
if _, err = syscall.CreateIoCompletionPort(g.handle, cph, 0, 0); err != nil { |
| 102 |
syscall.CloseHandle(g.handle) |
| 103 |
return |
| 104 |
} |
| 105 |
return g.readDirChanges() |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
func (g *grip) readDirChanges() error { |
| 112 |
handle := syscall.Handle(atomic.LoadUintptr((*uintptr)(&g.handle))) |
| 113 |
if handle == syscall.InvalidHandle { |
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
return syscall.ReadDirectoryChanges( |
| 118 |
handle, |
| 119 |
&g.buffer[0], |
| 120 |
uint32(unsafe.Sizeof(g.buffer)), |
| 121 |
g.recursive, |
| 122 |
encode(g.filter), |
| 123 |
nil, |
| 124 |
(*syscall.Overlapped)(unsafe.Pointer(g.ovlapped)), |
| 125 |
0, |
| 126 |
) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
func encode(filter uint32) uint32 { |
| 133 |
e := Event(filter & (onlyNGlobalEvents | onlyNotifyChanges)) |
| 134 |
if e&dirmarker != 0 { |
| 135 |
return uint32(FileNotifyChangeDirName) |
| 136 |
} |
| 137 |
if e&Create != 0 { |
| 138 |
e = (e ^ Create) | FileNotifyChangeFileName |
| 139 |
} |
| 140 |
if e&Remove != 0 { |
| 141 |
e = (e ^ Remove) | FileNotifyChangeFileName |
| 142 |
} |
| 143 |
if e&Write != 0 { |
| 144 |
e = (e ^ Write) | FileNotifyChangeAttributes | FileNotifyChangeSize | |
| 145 |
FileNotifyChangeCreation | FileNotifyChangeSecurity |
| 146 |
} |
| 147 |
if e&Rename != 0 { |
| 148 |
e = (e ^ Rename) | FileNotifyChangeFileName |
| 149 |
} |
| 150 |
return uint32(e) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
type watched struct { |
| 158 |
filter uint32 |
| 159 |
recursive bool |
| 160 |
count uint8 |
| 161 |
pathw []uint16 |
| 162 |
digrip [2]*grip |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
func newWatched(cph syscall.Handle, filter uint32, recursive bool, |
| 171 |
path string) (wd *watched, err error) { |
| 172 |
wd = &watched{ |
| 173 |
filter: filter, |
| 174 |
recursive: recursive, |
| 175 |
} |
| 176 |
if wd.pathw, err = syscall.UTF16FromString(path); err != nil { |
| 177 |
return |
| 178 |
} |
| 179 |
if err = wd.recreate(cph); err != nil { |
| 180 |
return |
| 181 |
} |
| 182 |
return wd, nil |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
func (wd *watched) recreate(cph syscall.Handle) (err error) { |
| 187 |
filefilter := wd.filter &^ uint32(FileNotifyChangeDirName) |
| 188 |
if err = wd.updateGrip(0, cph, filefilter == 0, filefilter); err != nil { |
| 189 |
return |
| 190 |
} |
| 191 |
dirfilter := wd.filter & uint32(FileNotifyChangeDirName|Create|Remove) |
| 192 |
if err = wd.updateGrip(1, cph, dirfilter == 0, wd.filter|uint32(dirmarker)); err != nil { |
| 193 |
return |
| 194 |
} |
| 195 |
wd.filter &^= onlyMachineStates |
| 196 |
return |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
func (wd *watched) updateGrip(idx int, cph syscall.Handle, reset bool, |
| 201 |
newflag uint32) (err error) { |
| 202 |
if reset { |
| 203 |
wd.digrip[idx] = nil |
| 204 |
} else { |
| 205 |
if wd.digrip[idx] == nil { |
| 206 |
if wd.digrip[idx], err = newGrip(cph, wd, newflag); err != nil { |
| 207 |
wd.closeHandle() |
| 208 |
return |
| 209 |
} |
| 210 |
} else { |
| 211 |
wd.digrip[idx].filter = newflag |
| 212 |
wd.digrip[idx].recursive = wd.recursive |
| 213 |
if err = wd.digrip[idx].register(cph); err != nil { |
| 214 |
wd.closeHandle() |
| 215 |
return |
| 216 |
} |
| 217 |
} |
| 218 |
wd.count++ |
| 219 |
} |
| 220 |
return |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
func (wd *watched) closeHandle() (err error) { |
| 227 |
for _, g := range wd.digrip { |
| 228 |
if g == nil { |
| 229 |
continue |
| 230 |
} |
| 231 |
|
| 232 |
for { |
| 233 |
handle := syscall.Handle(atomic.LoadUintptr((*uintptr)(&g.handle))) |
| 234 |
if handle == syscall.InvalidHandle { |
| 235 |
break |
| 236 |
} |
| 237 |
|
| 238 |
e := syscall.CloseHandle(handle) |
| 239 |
if e != nil && err == nil { |
| 240 |
err = e |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
if atomic.CompareAndSwapUintptr((*uintptr)(&g.handle), |
| 247 |
(uintptr)(handle), (uintptr)(syscall.InvalidHandle)) { |
| 248 |
break |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
return |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
type readdcw struct { |
| 259 |
sync.Mutex |
| 260 |
m map[string]*watched |
| 261 |
cph syscall.Handle |
| 262 |
start bool |
| 263 |
wg sync.WaitGroup |
| 264 |
c chan<- EventInfo |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
func newWatcher(c chan<- EventInfo) watcher { |
| 269 |
r := &readdcw{ |
| 270 |
m: make(map[string]*watched), |
| 271 |
cph: syscall.InvalidHandle, |
| 272 |
c: c, |
| 273 |
} |
| 274 |
runtime.SetFinalizer(r, func(r *readdcw) { |
| 275 |
if r.cph != syscall.InvalidHandle { |
| 276 |
syscall.CloseHandle(r.cph) |
| 277 |
} |
| 278 |
}) |
| 279 |
return r |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
func (r *readdcw) Watch(path string, event Event) error { |
| 284 |
return r.watch(path, event, false) |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
func (r *readdcw) RecursiveWatch(path string, event Event) error { |
| 289 |
return r.watch(path, event, true) |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
func (r *readdcw) watch(path string, event Event, recursive bool) error { |
| 296 |
if event&^(All|fileNotifyChangeAll) != 0 { |
| 297 |
return errors.New("notify: unknown event") |
| 298 |
} |
| 299 |
|
| 300 |
r.Lock() |
| 301 |
defer r.Unlock() |
| 302 |
|
| 303 |
if wd, ok := r.m[path]; ok { |
| 304 |
dbgprint("watch: already exists") |
| 305 |
wd.filter &^= stateUnwatch |
| 306 |
return nil |
| 307 |
} |
| 308 |
|
| 309 |
if err := r.lazyinit(); err != nil { |
| 310 |
return err |
| 311 |
} |
| 312 |
|
| 313 |
wd, err := newWatched(r.cph, uint32(event), recursive, path) |
| 314 |
if err != nil { |
| 315 |
return err |
| 316 |
} |
| 317 |
|
| 318 |
r.m[path] = wd |
| 319 |
dbgprint("watch: new watch added") |
| 320 |
|
| 321 |
return nil |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
func (r *readdcw) lazyinit() (err error) { |
| 326 |
invalid := uintptr(syscall.InvalidHandle) |
| 327 |
|
| 328 |
if atomic.LoadUintptr((*uintptr)(&r.cph)) == invalid { |
| 329 |
cph := syscall.InvalidHandle |
| 330 |
if cph, err = syscall.CreateIoCompletionPort(cph, 0, 0, 0); err != nil { |
| 331 |
return |
| 332 |
} |
| 333 |
|
| 334 |
r.cph, r.start = cph, true |
| 335 |
go r.loop() |
| 336 |
} |
| 337 |
|
| 338 |
return |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
func (r *readdcw) loop() { |
| 343 |
var n, key uint32 |
| 344 |
var overlapped *syscall.Overlapped |
| 345 |
for { |
| 346 |
err := syscall.GetQueuedCompletionStatus(r.cph, &n, &key, &overlapped, syscall.INFINITE) |
| 347 |
if key == stateCPClose { |
| 348 |
r.Lock() |
| 349 |
handle := r.cph |
| 350 |
r.cph = syscall.InvalidHandle |
| 351 |
r.Unlock() |
| 352 |
syscall.CloseHandle(handle) |
| 353 |
r.wg.Done() |
| 354 |
return |
| 355 |
} |
| 356 |
if overlapped == nil { |
| 357 |
|
| 358 |
continue |
| 359 |
} |
| 360 |
overEx := (*overlappedEx)(unsafe.Pointer(overlapped)) |
| 361 |
if overEx == nil || overEx.parent == nil { |
| 362 |
dbgprintf("incomplete completion status transferred=%d, overlapped=%#v, key=%#b", n, overEx, key) |
| 363 |
continue |
| 364 |
} else if n != 0 { |
| 365 |
r.loopevent(n, overEx) |
| 366 |
} |
| 367 |
if err = overEx.parent.readDirChanges(); err != nil { |
| 368 |
|
| 369 |
} |
| 370 |
r.loopstate(overEx) |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
func (r *readdcw) loopstate(overEx *overlappedEx) { |
| 376 |
r.Lock() |
| 377 |
defer r.Unlock() |
| 378 |
filter := overEx.parent.parent.filter |
| 379 |
if filter&onlyMachineStates == 0 { |
| 380 |
return |
| 381 |
} |
| 382 |
if overEx.parent.parent.count--; overEx.parent.parent.count == 0 { |
| 383 |
switch filter & onlyMachineStates { |
| 384 |
case stateRewatch: |
| 385 |
dbgprint("loopstate rewatch") |
| 386 |
overEx.parent.parent.recreate(r.cph) |
| 387 |
case stateUnwatch: |
| 388 |
dbgprint("loopstate unwatch") |
| 389 |
overEx.parent.parent.closeHandle() |
| 390 |
delete(r.m, syscall.UTF16ToString(overEx.parent.pathw)) |
| 391 |
case stateCPClose: |
| 392 |
default: |
| 393 |
panic(`notify: windows loopstate logic error`) |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
func (r *readdcw) loopevent(n uint32, overEx *overlappedEx) { |
| 400 |
events := []*event{} |
| 401 |
var currOffset uint32 |
| 402 |
for { |
| 403 |
raw := (*syscall.FileNotifyInformation)(unsafe.Pointer(&overEx.parent.buffer[currOffset])) |
| 404 |
name := syscall.UTF16ToString((*[syscall.MAX_LONG_PATH]uint16)(unsafe.Pointer(&raw.FileName))[:raw.FileNameLength>>1]) |
| 405 |
events = append(events, &event{ |
| 406 |
pathw: overEx.parent.pathw, |
| 407 |
filter: overEx.parent.filter, |
| 408 |
action: raw.Action, |
| 409 |
name: name, |
| 410 |
}) |
| 411 |
if raw.NextEntryOffset == 0 { |
| 412 |
break |
| 413 |
} |
| 414 |
if currOffset += raw.NextEntryOffset; currOffset >= n { |
| 415 |
break |
| 416 |
} |
| 417 |
} |
| 418 |
r.send(events) |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
func (r *readdcw) send(es []*event) { |
| 423 |
for _, e := range es { |
| 424 |
var syse Event |
| 425 |
if e.e, syse = decode(e.filter, e.action); e.e == 0 && syse == 0 { |
| 426 |
continue |
| 427 |
} |
| 428 |
switch { |
| 429 |
case e.action == syscall.FILE_ACTION_MODIFIED: |
| 430 |
e.ftype = fTypeUnknown |
| 431 |
case e.filter&uint32(dirmarker) != 0: |
| 432 |
e.ftype = fTypeDirectory |
| 433 |
default: |
| 434 |
e.ftype = fTypeFile |
| 435 |
} |
| 436 |
switch { |
| 437 |
case e.e == 0: |
| 438 |
e.e = syse |
| 439 |
case syse != 0: |
| 440 |
r.c <- &event{ |
| 441 |
pathw: e.pathw, |
| 442 |
name: e.name, |
| 443 |
ftype: e.ftype, |
| 444 |
action: e.action, |
| 445 |
filter: e.filter, |
| 446 |
e: syse, |
| 447 |
} |
| 448 |
} |
| 449 |
r.c <- e |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
func (r *readdcw) Rewatch(path string, oldevent, newevent Event) error { |
| 455 |
return r.rewatch(path, uint32(oldevent), uint32(newevent), false) |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
func (r *readdcw) RecursiveRewatch(oldpath, newpath string, oldevent, |
| 460 |
newevent Event) error { |
| 461 |
if oldpath != newpath { |
| 462 |
if err := r.unwatch(oldpath); err != nil { |
| 463 |
return err |
| 464 |
} |
| 465 |
return r.watch(newpath, newevent, true) |
| 466 |
} |
| 467 |
return r.rewatch(newpath, uint32(oldevent), uint32(newevent), true) |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
func (r *readdcw) rewatch(path string, oldevent, newevent uint32, recursive bool) (err error) { |
| 472 |
if Event(newevent)&^(All|fileNotifyChangeAll) != 0 { |
| 473 |
return errors.New("notify: unknown event") |
| 474 |
} |
| 475 |
var wd *watched |
| 476 |
r.Lock() |
| 477 |
defer r.Unlock() |
| 478 |
if wd, err = r.nonStateWatchedLocked(path); err != nil { |
| 479 |
return |
| 480 |
} |
| 481 |
if wd.filter&(onlyNotifyChanges|onlyNGlobalEvents) != oldevent { |
| 482 |
panic(`notify: windows re-watcher logic error`) |
| 483 |
} |
| 484 |
wd.filter = stateRewatch | newevent |
| 485 |
wd.recursive, recursive = recursive, wd.recursive |
| 486 |
if err = wd.closeHandle(); err != nil { |
| 487 |
wd.filter = oldevent |
| 488 |
wd.recursive = recursive |
| 489 |
return |
| 490 |
} |
| 491 |
return |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
func (r *readdcw) nonStateWatchedLocked(path string) (wd *watched, err error) { |
| 496 |
wd, ok := r.m[path] |
| 497 |
if !ok || wd == nil { |
| 498 |
err = errors.New(`notify: ` + path + ` path is unwatched`) |
| 499 |
return |
| 500 |
} |
| 501 |
if wd.filter&onlyMachineStates != 0 { |
| 502 |
err = errors.New(`notify: another re/unwatching operation in progress`) |
| 503 |
return |
| 504 |
} |
| 505 |
return |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
func (r *readdcw) Unwatch(path string) error { |
| 510 |
return r.unwatch(path) |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
func (r *readdcw) RecursiveUnwatch(path string) error { |
| 515 |
return r.unwatch(path) |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
func (r *readdcw) unwatch(path string) (err error) { |
| 520 |
var wd *watched |
| 521 |
|
| 522 |
r.Lock() |
| 523 |
defer r.Unlock() |
| 524 |
if wd, err = r.nonStateWatchedLocked(path); err != nil { |
| 525 |
return |
| 526 |
} |
| 527 |
|
| 528 |
wd.filter |= stateUnwatch |
| 529 |
dbgprint("unwatch: set unwatch state") |
| 530 |
|
| 531 |
if _, attrErr := syscall.GetFileAttributes(&wd.pathw[0]); attrErr != nil { |
| 532 |
for _, g := range wd.digrip { |
| 533 |
if g == nil { |
| 534 |
continue |
| 535 |
} |
| 536 |
|
| 537 |
dbgprint("unwatch: posting") |
| 538 |
if err = syscall.PostQueuedCompletionStatus(r.cph, 0, 0, (*syscall.Overlapped)(unsafe.Pointer(g.ovlapped))); err != nil { |
| 539 |
wd.filter &^= stateUnwatch |
| 540 |
return |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
return |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
func (r *readdcw) Close() (err error) { |
| 551 |
r.Lock() |
| 552 |
if !r.start { |
| 553 |
r.Unlock() |
| 554 |
return nil |
| 555 |
} |
| 556 |
for _, wd := range r.m { |
| 557 |
wd.filter &^= onlyMachineStates |
| 558 |
wd.filter |= stateCPClose |
| 559 |
if e := wd.closeHandle(); e != nil && err == nil { |
| 560 |
err = e |
| 561 |
} |
| 562 |
} |
| 563 |
r.start = false |
| 564 |
r.Unlock() |
| 565 |
r.wg.Add(1) |
| 566 |
if e := syscall.PostQueuedCompletionStatus(r.cph, 0, stateCPClose, nil); e != nil && err == nil { |
| 567 |
return e |
| 568 |
} |
| 569 |
r.wg.Wait() |
| 570 |
return |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
func decode(filter, action uint32) (Event, Event) { |
| 578 |
switch action { |
| 579 |
case syscall.FILE_ACTION_ADDED: |
| 580 |
return gensys(filter, Create, FileActionAdded) |
| 581 |
case syscall.FILE_ACTION_REMOVED: |
| 582 |
return gensys(filter, Remove, FileActionRemoved) |
| 583 |
case syscall.FILE_ACTION_MODIFIED: |
| 584 |
return gensys(filter, Write, FileActionModified) |
| 585 |
case syscall.FILE_ACTION_RENAMED_OLD_NAME: |
| 586 |
return gensys(filter, Rename, FileActionRenamedOldName) |
| 587 |
case syscall.FILE_ACTION_RENAMED_NEW_NAME: |
| 588 |
return gensys(filter, Rename, FileActionRenamedNewName) |
| 589 |
} |
| 590 |
dbgprintf("cannot decode internal mask: %d", action) |
| 591 |
|
| 592 |
return 0, 0 |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
func gensys(filter uint32, ge, se Event) (gene, syse Event) { |
| 600 |
isdir := filter&uint32(dirmarker) != 0 |
| 601 |
if isdir && filter&uint32(FileNotifyChangeDirName) != 0 || |
| 602 |
!isdir && filter&uint32(FileNotifyChangeFileName) != 0 || |
| 603 |
filter&uint32(fileNotifyChangeModified) != 0 { |
| 604 |
syse = se |
| 605 |
} |
| 606 |
if filter&uint32(ge) != 0 { |
| 607 |
gene = ge |
| 608 |
} |
| 609 |
return |
| 610 |
} |