1 ! Copyright (C) 2008 Doug Coleman, Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.strings libc destructors locals
4 kernel math assocs namespaces make continuations sequences
5 hashtables sorting arrays combinators math.bitwise strings
6 system accessors threads splitting io.backend io.backend.windows
7 io.backend.windows.nt io.files.windows.nt io.monitors io.ports
8 io.buffers io.files io.timeouts io.encodings.string
9 io.encodings.utf16n io windows.errors windows.kernel32 windows.types
11 IN: io.monitors.windows.nt
13 : open-directory ( path -- handle )
19 { FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_OVERLAPPED } flags
21 CreateFile opened-file ;
23 TUPLE: win32-monitor-port < input-port recursive ;
25 TUPLE: win32-monitor < monitor port ;
27 : begin-reading-changes ( port -- overlapped )
34 FILE_NOTIFY_CHANGE_ALL
37 [ f ReadDirectoryChangesW win32-error=0/f ] keep ;
39 : read-changes ( port -- bytes-transferred )
41 [ begin-reading-changes ] [ twiddle-thumbs ] bi
44 : parse-action ( action -- changed )
46 { FILE_ACTION_ADDED [ +add-file+ ] }
47 { FILE_ACTION_REMOVED [ +remove-file+ ] }
48 { FILE_ACTION_MODIFIED [ +modify-file+ ] }
49 { FILE_ACTION_RENAMED_OLD_NAME [ +rename-file+ ] }
50 { FILE_ACTION_RENAMED_NEW_NAME [ +rename-file+ ] }
51 [ drop +modify-file+ ]
54 : memory>u16-string ( alien len -- string )
55 memory>byte-array utf16n decode ;
57 : parse-notify-record ( buffer -- path changed )
59 [ FILE_NOTIFY_INFORMATION-FileName ]
60 [ FILE_NOTIFY_INFORMATION-FileNameLength ]
63 [ FILE_NOTIFY_INFORMATION-Action parse-action ] bi ;
65 : (file-notify-records) ( buffer -- buffer )
67 dup FILE_NOTIFY_INFORMATION-NextEntryOffset zero? [
68 [ FILE_NOTIFY_INFORMATION-NextEntryOffset ] keep <displaced-alien>
72 : file-notify-records ( buffer -- seq )
73 [ (file-notify-records) drop ] { } make ;
75 :: parse-notify-records ( monitor buffer -- )
76 buffer file-notify-records [
78 [ monitor path>> prepend-path normalize-path ] dip
82 : fill-queue ( monitor -- )
83 dup port>> dup check-disposed
84 [ buffer>> ptr>> ] [ read-changes zero? ] bi
85 [ 2dup parse-notify-records ] unless
88 : (fill-queue-thread) ( monitor -- )
89 dup fill-queue (fill-queue-thread) ;
91 : fill-queue-thread ( monitor -- )
92 [ dup fill-queue (fill-queue-thread) ]
93 [ dup already-disposed? [ 2drop ] [ rethrow ] if ] recover ;
95 M:: winnt (monitor) ( path recursive? mailbox -- monitor )
97 path normalize-path mailbox win32-monitor new-monitor
98 path open-directory \ win32-monitor-port <buffered-port>
99 recursive? >>recursive
101 dup [ fill-queue-thread ] curry
102 "Windows monitor thread" spawn drop
105 M: win32-monitor dispose