factor/basis/io/monitors/windows/windows.factor

104 lines
3.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 Doug Coleman, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
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
system accessors threads splitting io.backend
io.files.windows io.monitors io.ports
io.buffers io.files io.timeouts io.encodings.string literals
io.encodings.utf16n io windows.errors windows.kernel32 windows.types
2009-08-29 15:28:00 -04:00
io.pathnames classes.struct ;
IN: io.monitors.windows
: 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
flags{ FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_OVERLAPPED }
2008-01-28 00:59:36 -05:00
f
CreateFile opened-file ;
TUPLE: win32-monitor-port < input-port recursive ;
TUPLE: win32-monitor < monitor port ;
2008-04-11 22:36:37 -04:00
: begin-reading-changes ( port -- overlapped )
{
[ 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 ? ]
} cleave
FILE_NOTIFY_CHANGE_ALL
2010-10-20 18:42:53 -04:00
0 uint <ref>
(make-overlapped)
[ f ReadDirectoryChangesW win32-error=0/f ] keep ;
2008-05-18 00:50:11 -04:00
: read-changes ( port -- bytes-transferred )
[
2008-05-15 01:13:08 -04:00
[ begin-reading-changes ] [ twiddle-thumbs ] bi
] with-destructors ;
2008-01-28 00:59:36 -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-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 ;
:: 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 -- )
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) ]
[ dup already-disposed? [ 2drop ] [ rethrow ] if ] recover ;
2008-04-11 22:36:37 -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
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-04-11 22:36:37 -04:00
M: win32-monitor dispose
[ port>> dispose ] [ call-next-method ] bi ;