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