1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.strings
4 alien.syntax arrays assocs classes.struct combinators
5 combinators.short-circuit continuations destructors environment io
6 io.backend io.binary io.buffers io.files io.files.private
7 io.files.types io.pathnames io.ports io.streams.c io.streams.null
8 io.timeouts kernel libc literals locals math math.bitwise namespaces
9 sequences specialized-arrays system threads tr vectors windows
10 windows.errors windows.handles windows.kernel32 windows.shell32
11 windows.time windows.types windows.winsock splitting ;
12 SPECIALIZED-ARRAY: ushort
17 : CreateFile-flags ( DWORD -- DWORD )
18 flags{ FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_OVERLAPPED } bitor ;
20 HOOK: open-append os ( path -- win32-file )
22 TUPLE: win32-file < win32-handle ptr ;
24 : <win32-file> ( handle -- win32-file )
25 win32-file new-win32-handle ;
28 [ cancel-operation ] [ call-next-method ] bi ;
37 : default-security-attributes ( -- obj )
38 SECURITY_ATTRIBUTES <struct>
39 SECURITY_ATTRIBUTES heap-size >>nLength ;
42 hFile lpBuffer nNumberOfBytesToRead
43 lpNumberOfBytesRet lpOverlapped ;
45 C: <FileArgs> FileArgs
47 ! Global variable with assoc mapping overlapped to threads
48 SYMBOL: pending-overlapped
50 TUPLE: io-callback port thread ;
52 C: <io-callback> io-callback
54 : <completion-port> ( handle existing -- handle )
55 f 1 CreateIoCompletionPort dup win32-error=0/f ;
57 : <master-completion-port> ( -- handle )
58 INVALID_HANDLE_VALUE f <completion-port> ;
60 SYMBOL: master-completion-port
62 : add-completion ( win32-handle -- win32-handle )
63 dup handle>> master-completion-port get-global <completion-port> drop ;
65 : opened-file ( handle -- win32-file )
66 check-invalid-handle <win32-file> |dispose add-completion ;
69 { [ ERROR_HANDLE_EOF = ] [ ERROR_BROKEN_PIPE = ] } 1|| ;
71 : twiddle-thumbs ( overlapped port -- bytes-transferred )
74 [ self ] dip >c-ptr pending-overlapped get-global set-at
76 { [ dup integer? ] [ ] }
79 [ drop 0 ] [ n>win32-error-string throw ] if
84 :: wait-for-overlapped ( nanos -- bytes-transferred overlapped error? )
85 nanos [ 1,000,000 /i ] [ INFINITE ] if* :> timeout
86 master-completion-port get-global
87 { int void* pointer: OVERLAPPED }
88 [ timeout GetQueuedCompletionStatus zero? ] with-out-parameters
89 :> ( error? bytes key overlapped )
90 bytes overlapped error? ;
92 : resume-callback ( result overlapped -- )
93 >c-ptr pending-overlapped get-global delete-at* drop resume-with ;
95 : handle-overlapped ( nanos -- ? )
98 [ drop GetLastError 1array ] dip resume-callback t
100 ] [ resume-callback t ] if ;
102 M: win32-handle cancel-operation
103 [ handle>> CancelIo win32-error=0/f ] unless-disposed ;
105 M: windows io-multiplex ( nanos -- )
106 handle-overlapped [ 0 io-multiplex ] when ;
108 M: windows init-io ( -- )
109 <master-completion-port> master-completion-port set-global
110 H{ } clone pending-overlapped set-global ;
112 : (handle>file-size) ( handle -- n/f )
113 0 ulonglong <ref> [ GetFileSizeEx ] keep swap
114 [ drop f ] [ drop ulonglong deref ] if-zero ;
116 ! GetFileSizeEx errors with ERROR_INVALID_FUNCTION if handle is not seekable
117 : handle>file-size ( handle -- n/f )
119 GetLastError ERROR_INVALID_FUNCTION =
120 [ f ] [ throw-win32-error ] if
123 ERROR: seek-before-start n ;
125 : set-seek-ptr ( n handle -- )
126 [ dup 0 < [ seek-before-start ] when ] dip ptr<< ;
128 M: windows tell-handle ( handle -- n ) ptr>> ;
130 M: windows seek-handle ( n seek-type handle -- )
132 { seek-absolute [ set-seek-ptr ] }
133 { seek-relative [ [ ptr>> + ] keep set-seek-ptr ] }
134 { seek-end [ [ handle>> handle>file-size + ] keep set-seek-ptr ] }
138 M: windows can-seek-handle? ( handle -- ? )
139 handle>> handle>file-size >boolean ;
141 M: windows handle-length ( handle -- n/f )
142 handle>> handle>file-size
143 dup { 0 f } member? [ drop f ] when ;
145 : file-error? ( n -- eof? )
148 { [ dup expected-io-error? ] [ drop f ] }
149 { [ dup eof? ] [ drop t ] }
150 [ n>win32-error-string throw ]
154 : wait-for-file ( FileArgs n port -- n )
156 [ 2drop 0 ] [ [ lpOverlapped>> ] dip twiddle-thumbs ] if ;
158 : update-file-ptr ( n port -- )
159 handle>> dup ptr>> [ rot + >>ptr drop ] [ 2drop ] if* ;
161 : (make-overlapped) ( -- overlapped-ext )
162 OVERLAPPED malloc-struct &free ;
164 : make-overlapped ( handle -- overlapped-ext )
165 (make-overlapped) swap
166 ptr>> [ [ 32 bits >>offset ] [ -32 shift >>offset-high ] bi ] when* ;
168 : make-FileArgs ( port handle -- <FileArgs> )
169 [ nip check-disposed handle>> ]
171 [ buffer>> dup buffer-length 0 DWORD <ref> ] dip make-overlapped
174 : setup-write ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToWrite lpNumberOfBytesWritten lpOverlapped )
177 [ lpBuffer>> [ buffer@ ] [ buffer-length ] bi ]
178 [ lpNumberOfBytesRet>> ]
182 : finish-write ( n port -- )
183 [ update-file-ptr ] [ buffer>> buffer-consume ] 2bi ;
185 M: object drain ( port handle -- event/f )
186 [ make-FileArgs dup setup-write WriteFile ]
187 [ drop [ wait-for-file ] [ finish-write ] bi ] 2bi f ;
189 : setup-read ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToRead lpNumberOfBytesRead lpOverlapped )
192 [ lpBuffer>> [ buffer-end ] [ buffer-capacity ] bi ]
193 [ lpNumberOfBytesRet>> ]
197 : finish-read ( n port -- )
198 [ update-file-ptr ] [ buffer>> buffer+ ] 2bi ;
200 M: object refill ( port handle -- event/f )
201 [ make-FileArgs dup setup-read ReadFile ]
202 [ drop [ wait-for-file ] [ finish-read ] bi ] 2bi f ;
204 M: windows (wait-to-write) ( port -- )
205 [ dup handle>> drain ] with-destructors drop ;
207 M: windows (wait-to-read) ( port -- )
208 [ dup handle>> refill ] with-destructors drop ;
210 : make-fd-set ( socket -- fd_set )
211 fd_set <struct> swap 1array void* >c-array >>fd_array 1 >>fd_count ;
213 : select-sets ( socket event -- read-fds write-fds except-fds )
214 [ make-fd-set ] dip +input+ = [ f f ] [ f swap f ] if ;
216 CONSTANT: select-timeval S{ timeval { sec 0 } { usec 1000 } }
218 M: windows wait-for-fd ( handle event -- )
219 [ file>> handle>> 1 swap ] dip select-sets select-timeval
222 : console-app? ( -- ? ) GetConsoleWindow >boolean ;
224 M: windows init-stdio
227 [ null-reader null-writer null-writer set-stdio ] if ;
229 : open-file ( path access-mode create-mode flags -- handle )
231 [ share-mode default-security-attributes ] 2dip
232 CreateFile-flags f CreateFileW opened-file
235 : open-r/w ( path -- win32-file )
236 flags{ GENERIC_READ GENERIC_WRITE }
237 OPEN_EXISTING 0 open-file ;
239 : open-read ( path -- win32-file )
240 GENERIC_READ OPEN_EXISTING 0 open-file 0 >>ptr ;
242 : open-write ( path -- win32-file )
243 GENERIC_WRITE CREATE_ALWAYS 0 open-file 0 >>ptr ;
245 : (open-append) ( path -- win32-file )
246 GENERIC_WRITE OPEN_ALWAYS 0 open-file ;
248 : maybe-create-file ( path -- win32-file ? )
249 ! return true if file was just created
250 flags{ GENERIC_READ GENERIC_WRITE }
251 OPEN_ALWAYS 0 open-file
252 GetLastError ERROR_ALREADY_EXISTS = not ;
254 : set-file-pointer ( handle length method -- )
255 [ [ handle>> ] dip d>w/w LONG <ref> ] dip SetFilePointer
256 INVALID_SET_FILE_POINTER = [ "SetFilePointer failed" throw ] when ;
258 M: windows (file-reader) ( path -- stream )
259 open-read <input-port> ;
261 M: windows (file-writer) ( path -- stream )
262 open-write <output-port> ;
264 M: windows (file-appender) ( path -- stream )
265 open-append <output-port> ;
267 SYMBOLS: +read-only+ +hidden+ +system+
268 +archive+ +device+ +normal+ +temporary+
269 +sparse-file+ +reparse-point+ +compressed+ +offline+
270 +not-content-indexed+ +encrypted+ ;
274 : read-only? ( file-info -- ? )
275 attributes>> +read-only+ swap member? ;
277 : set-file-attributes ( path flags -- )
278 SetFileAttributes win32-error=0/f ;
280 : set-file-normal-attribute ( path -- )
281 FILE_ATTRIBUTE_NORMAL set-file-attributes ;
283 : win32-file-attributes ( n -- seq )
285 { +read-only+ FILE_ATTRIBUTE_READONLY }
286 { +hidden+ FILE_ATTRIBUTE_HIDDEN }
287 { +system+ FILE_ATTRIBUTE_SYSTEM }
288 { +directory+ FILE_ATTRIBUTE_DIRECTORY }
289 { +archive+ FILE_ATTRIBUTE_ARCHIVE }
290 { +device+ FILE_ATTRIBUTE_DEVICE }
291 { +normal+ FILE_ATTRIBUTE_NORMAL }
292 { +temporary+ FILE_ATTRIBUTE_TEMPORARY }
293 { +sparse-file+ FILE_ATTRIBUTE_SPARSE_FILE }
294 { +reparse-point+ FILE_ATTRIBUTE_REPARSE_POINT }
295 { +compressed+ FILE_ATTRIBUTE_COMPRESSED }
296 { +offline+ FILE_ATTRIBUTE_OFFLINE }
297 { +not-content-indexed+ FILE_ATTRIBUTE_NOT_CONTENT_INDEXED }
298 { +encrypted+ FILE_ATTRIBUTE_ENCRYPTED }
300 [ execute( -- y ) mask? [ drop f ] unless ] with { } assoc>map sift ;
302 : win32-file-type ( n -- symbol )
303 FILE_ATTRIBUTE_DIRECTORY mask? +directory+ +regular-file+ ? ;
305 : (set-file-times) ( handle timestamp/f timestamp/f timestamp/f -- )
306 [ timestamp>FILETIME ] tri@
307 SetFileTime win32-error=0/f ;
310 MAX_UNICODE_PATH dup ushort <c-array>
311 [ GetCurrentDirectory win32-error=0/f ] keep alien>native-string ;
314 SetCurrentDirectory win32-error=0/f ;
316 CONSTANT: unicode-prefix "\\\\?\\"
318 M: windows root-directory? ( path -- ? )
320 { [ dup empty? ] [ drop f ] }
321 { [ dup [ path-separator? ] all? ] [ drop t ] }
322 { [ dup trim-tail-separators { [ length 2 = ]
323 [ second CHAR: : = ] } 1&& ] [ drop t ] }
324 { [ dup unicode-prefix head? ]
325 [ trim-tail-separators length unicode-prefix length 2 + = ] }
329 : prepend-unicode-prefix ( string -- string' )
330 dup unicode-prefix head? [
331 unicode-prefix prepend
334 : remove-unicode-prefix ( string -- string' )
335 unicode-prefix ?head drop ;
337 TR: normalize-separators "/" "\\" ;
341 : unc-path? ( string -- ? )
342 [ "//" head? ] [ "\\\\" head? ] bi or ;
346 M: windows canonicalize-path
347 remove-unicode-prefix canonicalize-path* ;
349 M: object root-path remove-unicode-prefix root-path* ;
351 M: object relative-path remove-unicode-prefix relative-path* ;
353 M: windows normalize-path ( string -- string' )
359 prepend-unicode-prefix
364 : windows-file-size ( path -- size )
365 normalize-path 0 WIN32_FILE_ATTRIBUTE_DATA <struct>
366 [ GetFileAttributesEx win32-error=0/f ] keep
367 [ nFileSizeLow>> ] [ nFileSizeHigh>> ] bi >64bit ;
371 M: windows open-append
372 [ dup windows-file-size ] [ drop 0 ] recover
373 [ (open-append) ] dip >>ptr ;
377 [ "HOMEDRIVE" os-env "HOMEPATH" os-env append-path ]
378 [ "USERPROFILE" os-env ]
382 : STREAM_DATA>out ( WIN32_FIND_STREAM_DATA -- pair/f )
383 [ cStreamName>> alien>native-string ]
384 [ StreamSize>> ] bi 2array ;
386 : file-streams-rest ( streams handle -- streams )
387 WIN32_FIND_STREAM_DATA <struct>
388 [ FindNextStream ] 2keep
390 GetLastError ERROR_HANDLE_EOF = [ win32-error ] unless
393 pick push file-streams-rest
396 : file-streams ( path -- streams )
398 FindStreamInfoStandard
399 WIN32_FIND_STREAM_DATA <struct>
401 [ FindFirstStream ] keepd
403 2drop throw-win32-error
405 1vector swap file-streams-rest
408 : alternate-file-streams ( path -- streams )
409 file-streams [ cStreamName>> alien>native-string "::$DATA" = ] reject ;
411 : alternate-file-streams? ( path -- streams )
412 alternate-file-streams empty? not ;