]> gitweb.factorcode.org Git - factor.git/blob - basis/io/monitors/windows/nt/nt.factor
d2408a3dd1810c92b9f9ad7319ff0f3cc35c2bc0
[factor.git] / basis / io / monitors / windows / nt / nt.factor
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 windows.kernel32 windows.types
10 io.pathnames ;
11 IN: io.monitors.windows.nt
12
13 : open-directory ( path -- handle )
14     normalize-path
15     FILE_LIST_DIRECTORY
16     share-mode
17     f
18     OPEN_EXISTING
19     { FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_OVERLAPPED } flags
20     f
21     CreateFile opened-file ;
22
23 TUPLE: win32-monitor-port < input-port recursive ;
24
25 TUPLE: win32-monitor < monitor port ;
26
27 : begin-reading-changes ( port -- overlapped )
28     {
29         [ handle>> handle>> ]
30         [ buffer>> ptr>> ]
31         [ buffer>> size>> ]
32         [ recursive>> 1 0 ? ]
33     } cleave
34     FILE_NOTIFY_CHANGE_ALL
35     0 <uint>
36     (make-overlapped)
37     [ f ReadDirectoryChangesW win32-error=0/f ] keep ;
38
39 : read-changes ( port -- bytes-transferred )
40     [
41         [ begin-reading-changes ] [ twiddle-thumbs ] bi
42     ] with-destructors ;
43
44 : parse-action ( action -- changed )
45     {
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+ ]
52     } case 1array ;
53
54 : memory>u16-string ( alien len -- string )
55     memory>byte-array utf16n decode ;
56
57 : parse-notify-record ( buffer -- path changed )
58     [
59         [ FILE_NOTIFY_INFORMATION-FileName ]
60         [ FILE_NOTIFY_INFORMATION-FileNameLength ]
61         bi memory>u16-string
62     ]
63     [ FILE_NOTIFY_INFORMATION-Action parse-action ] bi ;
64
65 : (file-notify-records) ( buffer -- buffer )
66     dup ,
67     dup FILE_NOTIFY_INFORMATION-NextEntryOffset zero? [
68         [ FILE_NOTIFY_INFORMATION-NextEntryOffset ] keep <displaced-alien>
69         (file-notify-records)
70     ] unless ;
71
72 : file-notify-records ( buffer -- seq )
73     [ (file-notify-records) drop ] { } make ;
74
75 :: parse-notify-records ( monitor buffer -- )
76     buffer file-notify-records [
77         parse-notify-record
78         [ monitor path>> prepend-path normalize-path ] dip
79         monitor queue-change
80     ] each ;
81
82 : fill-queue ( monitor -- )
83     dup port>> dup check-disposed
84     [ buffer>> ptr>> ] [ read-changes zero? ] bi
85     [ 2dup parse-notify-records ] unless
86     2drop ;
87
88 : (fill-queue-thread) ( monitor -- )
89     dup fill-queue (fill-queue-thread) ;
90
91 : fill-queue-thread ( monitor -- )
92     [ dup fill-queue (fill-queue-thread) ]
93     [ dup already-disposed? [ 2drop ] [ rethrow ] if ] recover ;
94
95 M:: winnt (monitor) ( path recursive? mailbox -- monitor )
96     [
97         path normalize-path mailbox win32-monitor new-monitor
98             path open-directory \ win32-monitor-port <buffered-port>
99                 recursive? >>recursive
100             >>port
101         dup [ fill-queue-thread ] curry
102         "Windows monitor thread" spawn drop
103     ] with-destructors ;
104
105 M: win32-monitor dispose
106     port>> dispose ;