2008-01-26 02:40:09 -05:00
|
|
|
! Copyright (C) 2008 Doug Coleman, Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-09-17 23:07:21 -04:00
|
|
|
USING: alien alien.c-types alien.data alien.strings libc destructors
|
|
|
|
locals kernel math assocs namespaces make continuations sequences
|
2008-12-03 07:52:16 -05:00
|
|
|
hashtables sorting arrays combinators math.bitwise strings
|
2010-09-19 15:02:32 -04:00
|
|
|
system accessors threads splitting io.backend
|
|
|
|
io.files.windows io.monitors io.ports
|
2010-04-01 15:43:27 -04:00
|
|
|
io.buffers io.files io.timeouts io.encodings.string literals
|
2009-04-30 10:36:25 -04:00
|
|
|
io.encodings.utf16n io windows.errors windows.kernel32 windows.types
|
2009-08-29 15:28:00 -04:00
|
|
|
io.pathnames classes.struct ;
|
2010-09-19 15:02:32 -04:00
|
|
|
IN: io.monitors.windows
|
2008-01-26 02:40:09 -05:00
|
|
|
|
|
|
|
: open-directory ( path -- handle )
|
2008-04-13 01:26:44 -04:00
|
|
|
normalize-path
|
2008-01-28 00:59:36 -05:00
|
|
|
FILE_LIST_DIRECTORY
|
|
|
|
share-mode
|
|
|
|
f
|
|
|
|
OPEN_EXISTING
|
2010-04-01 15:43:27 -04:00
|
|
|
flags{ FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_OVERLAPPED }
|
2008-01-28 00:59:36 -05:00
|
|
|
f
|
2008-05-15 06:20:42 -04:00
|
|
|
CreateFile opened-file ;
|
2008-01-26 02:40:09 -05:00
|
|
|
|
2008-04-11 23:36:24 -04:00
|
|
|
TUPLE: win32-monitor-port < input-port recursive ;
|
|
|
|
|
|
|
|
TUPLE: win32-monitor < monitor port ;
|
2008-02-02 17:23:04 -05:00
|
|
|
|
2008-04-11 22:36:37 -04:00
|
|
|
: begin-reading-changes ( port -- overlapped )
|
2008-04-11 15:09:09 -04:00
|
|
|
{
|
|
|
|
[ handle>> handle>> ]
|
2008-04-13 01:26:44 -04:00
|
|
|
[ buffer>> ptr>> ]
|
|
|
|
[ buffer>> size>> ]
|
2008-04-11 22:36:37 -04:00
|
|
|
[ recursive>> 1 0 ? ]
|
2008-04-11 15:09:09 -04:00
|
|
|
} cleave
|
2008-01-28 02:21:44 -05:00
|
|
|
FILE_NOTIFY_CHANGE_ALL
|
2010-10-20 18:42:53 -04:00
|
|
|
0 uint <ref>
|
2008-01-28 02:21:44 -05:00
|
|
|
(make-overlapped)
|
|
|
|
[ f ReadDirectoryChangesW win32-error=0/f ] keep ;
|
2008-01-26 02:40:09 -05:00
|
|
|
|
2008-05-18 00:50:11 -04:00
|
|
|
: read-changes ( port -- bytes-transferred )
|
2008-01-28 02:21:44 -05:00
|
|
|
[
|
2008-05-15 01:13:08 -04:00
|
|
|
[ begin-reading-changes ] [ twiddle-thumbs ] bi
|
2008-01-28 02:21:44 -05:00
|
|
|
] with-destructors ;
|
2008-01-28 00:59:36 -05:00
|
|
|
|
2008-02-01 18:13:57 -05:00
|
|
|
: parse-action ( action -- changed )
|
|
|
|
{
|
2008-04-11 22:36:37 -04:00
|
|
|
{ FILE_ACTION_ADDED [ +add-file+ ] }
|
|
|
|
{ FILE_ACTION_REMOVED [ +remove-file+ ] }
|
|
|
|
{ FILE_ACTION_MODIFIED [ +modify-file+ ] }
|
|
|
|
{ FILE_ACTION_RENAMED_OLD_NAME [ +rename-file+ ] }
|
|
|
|
{ FILE_ACTION_RENAMED_NEW_NAME [ +rename-file+ ] }
|
2008-04-11 13:56:11 -04:00
|
|
|
[ drop +modify-file+ ]
|
2008-04-13 01:26:44 -04:00
|
|
|
} case 1array ;
|
2008-01-26 22:38:30 -05:00
|
|
|
|
2008-03-08 03:51:26 -05:00
|
|
|
: memory>u16-string ( alien len -- string )
|
2008-11-14 21:18:16 -05:00
|
|
|
memory>byte-array utf16n decode ;
|
2008-03-08 03:51:26 -05:00
|
|
|
|
2008-04-13 01:26:44 -04:00
|
|
|
: parse-notify-record ( buffer -- path changed )
|
2009-08-29 14:45:25 -04:00
|
|
|
[ [ FileName>> ] [ FileNameLength>> ] bi memory>u16-string ]
|
|
|
|
[ Action>> parse-action ] bi ;
|
2008-04-13 01:26:44 -04:00
|
|
|
|
|
|
|
: (file-notify-records) ( buffer -- buffer )
|
2009-08-29 15:28:00 -04:00
|
|
|
FILE_NOTIFY_INFORMATION memory>struct
|
2008-04-13 01:26:44 -04:00
|
|
|
dup ,
|
2009-08-29 14:45:25 -04:00
|
|
|
dup NextEntryOffset>> zero? [
|
2009-08-29 15:28:00 -04:00
|
|
|
[ NextEntryOffset>> ] [ >c-ptr <displaced-alien> ] bi
|
2008-04-13 01:26:44 -04:00
|
|
|
(file-notify-records)
|
|
|
|
] unless ;
|
2008-04-11 22:36:37 -04:00
|
|
|
|
|
|
|
: file-notify-records ( buffer -- seq )
|
2008-04-13 01:26:44 -04:00
|
|
|
[ (file-notify-records) drop ] { } make ;
|
2008-01-26 22:38:30 -05:00
|
|
|
|
2008-05-06 03:10:17 -04:00
|
|
|
:: parse-notify-records ( monitor buffer -- )
|
|
|
|
buffer file-notify-records [
|
|
|
|
parse-notify-record
|
|
|
|
[ monitor path>> prepend-path normalize-path ] dip
|
|
|
|
monitor queue-change
|
|
|
|
] each ;
|
2008-04-11 22:36:37 -04:00
|
|
|
|
|
|
|
: fill-queue ( monitor -- )
|
2014-11-21 11:19:05 -05:00
|
|
|
dup port>> check-disposed
|
2008-04-13 01:26:44 -04:00
|
|
|
[ buffer>> ptr>> ] [ read-changes zero? ] bi
|
|
|
|
[ 2dup parse-notify-records ] unless
|
|
|
|
2drop ;
|
|
|
|
|
|
|
|
: (fill-queue-thread) ( monitor -- )
|
|
|
|
dup fill-queue (fill-queue-thread) ;
|
2008-04-11 22:36:37 -04:00
|
|
|
|
|
|
|
: fill-queue-thread ( monitor -- )
|
2008-04-13 01:26:44 -04:00
|
|
|
[ dup fill-queue (fill-queue-thread) ]
|
2008-05-15 06:20:42 -04:00
|
|
|
[ dup already-disposed? [ 2drop ] [ rethrow ] if ] recover ;
|
2008-04-11 22:36:37 -04:00
|
|
|
|
2011-09-18 21:25:06 -04:00
|
|
|
M:: windows (monitor) ( path recursive? mailbox -- monitor )
|
2008-04-11 22:36:37 -04:00
|
|
|
[
|
2008-04-25 01:59:44 -04:00
|
|
|
path normalize-path mailbox win32-monitor new-monitor
|
2008-04-11 23:36:24 -04:00
|
|
|
path open-directory \ win32-monitor-port <buffered-port>
|
|
|
|
recursive? >>recursive
|
|
|
|
>>port
|
|
|
|
dup [ fill-queue-thread ] curry
|
|
|
|
"Windows monitor thread" spawn drop
|
2008-04-11 22:36:37 -04:00
|
|
|
] with-destructors ;
|
2008-01-26 02:40:09 -05:00
|
|
|
|
2008-04-11 22:36:37 -04:00
|
|
|
M: win32-monitor dispose
|
2010-02-19 06:23:24 -05:00
|
|
|
[ port>> dispose ] [ call-next-method ] bi ;
|