]> gitweb.factorcode.org Git - factor.git/blob - basis/io/monitors/windows/nt/nt.factor
Use flags{ instead of flags all over the place
[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.data alien.strings libc destructors
4 locals 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 literals
9 io.encodings.utf16n io windows.errors windows.kernel32 windows.types
10 io.pathnames classes.struct ;
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     flags{ FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_OVERLAPPED }
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     [ [ FileName>> ] [ FileNameLength>> ] bi memory>u16-string ]
59     [ Action>> parse-action ] bi ;
60
61 : (file-notify-records) ( buffer -- buffer )
62     FILE_NOTIFY_INFORMATION memory>struct
63     dup ,
64     dup NextEntryOffset>> zero? [
65         [ NextEntryOffset>> ] [ >c-ptr <displaced-alien> ] bi
66         (file-notify-records)
67     ] unless ;
68
69 : file-notify-records ( buffer -- seq )
70     [ (file-notify-records) drop ] { } make ;
71
72 :: parse-notify-records ( monitor buffer -- )
73     buffer file-notify-records [
74         parse-notify-record
75         [ monitor path>> prepend-path normalize-path ] dip
76         monitor queue-change
77     ] each ;
78
79 : fill-queue ( monitor -- )
80     dup port>> dup check-disposed
81     [ buffer>> ptr>> ] [ read-changes zero? ] bi
82     [ 2dup parse-notify-records ] unless
83     2drop ;
84
85 : (fill-queue-thread) ( monitor -- )
86     dup fill-queue (fill-queue-thread) ;
87
88 : fill-queue-thread ( monitor -- )
89     [ dup fill-queue (fill-queue-thread) ]
90     [ dup already-disposed? [ 2drop ] [ rethrow ] if ] recover ;
91
92 M:: winnt (monitor) ( path recursive? mailbox -- monitor )
93     [
94         path normalize-path mailbox win32-monitor new-monitor
95             path open-directory \ win32-monitor-port <buffered-port>
96                 recursive? >>recursive
97             >>port
98         dup [ fill-queue-thread ] curry
99         "Windows monitor thread" spawn drop
100     ] with-destructors ;
101
102 M: win32-monitor dispose
103     [ port>> dispose ] [ call-next-method ] bi ;