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 ascii 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.pathnames.private io.ports io.streams.c
8 io.streams.null io.timeouts kernel libc literals locals math math.bitwise
9 namespaces 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 ] [ throw-windows-error ] 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
106 handle-overlapped [ 0 io-multiplex ] when ;
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 [ win32-error ] unless f
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 ptr>> ;
130 M: windows seek-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?
139 handle>> handle>file-size >boolean ;
141 M: windows handle-length
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 [ throw-windows-error ]
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 ;
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 ;
201 [ make-FileArgs dup setup-read ReadFile ]
202 [ drop [ wait-for-file ] [ finish-read ] bi ] 2bi f ;
204 M: windows (wait-to-write)
205 [ dup handle>> drain ] with-destructors drop ;
207 M: windows (wait-to-read)
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
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)
259 open-read <input-port> ;
261 M: windows (file-writer)
262 open-write <output-port> ;
264 M: windows (file-appender)
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 }
299 } [ mask? [ drop f ] unless ] with { } assoc>map sift ;
301 : win32-file-type ( n -- symbol )
302 FILE_ATTRIBUTE_DIRECTORY mask? +directory+ +regular-file+ ? ;
304 : (set-file-times) ( handle timestamp/f timestamp/f timestamp/f -- )
305 [ timestamp>FILETIME ] tri@
306 SetFileTime win32-error=0/f ;
309 MAX_UNICODE_PATH dup ushort <c-array>
310 [ GetCurrentDirectory win32-error=0/f ] keep alien>native-string ;
313 SetCurrentDirectory win32-error=0/f ;
315 CONSTANT: unicode-prefix "\\\\?\\"
317 M: windows root-directory?
319 { [ dup empty? ] [ drop f ] }
320 { [ dup [ path-separator? ] all? ] [ drop t ] }
321 { [ dup trim-tail-separators { [ length 2 = ]
322 [ second CHAR: : = ] } 1&& ] [ drop t ] }
323 { [ dup unicode-prefix head? ]
324 [ trim-tail-separators length unicode-prefix length 2 + = ] }
328 : prepend-unicode-prefix ( string -- string' )
329 dup unicode-prefix head? [
330 unicode-prefix prepend
333 : remove-unicode-prefix ( string -- string' )
334 unicode-prefix ?head drop ;
336 TR: normalize-separators "/" "\\" ;
340 : unc-path? ( string -- ? )
341 [ "//" head? ] [ "\\\\" head? ] bi or ;
345 M: windows canonicalize-path
346 remove-unicode-prefix canonicalize-path* ;
348 M: windows canonicalize-drive
349 dup windows-absolute-path? [ ":" split1 [ >upper ] dip ":" glue ] when ;
351 M: windows canonicalize-path-full canonicalize-path canonicalize-drive >windows-path ;
353 M: windows root-path remove-unicode-prefix root-path* ;
355 M: windows relative-path remove-unicode-prefix relative-path* ;
357 M: windows normalize-path
363 prepend-unicode-prefix
368 : windows-file-size ( path -- size )
369 normalize-path 0 WIN32_FILE_ATTRIBUTE_DATA <struct>
370 [ GetFileAttributesEx win32-error=0/f ] keep
371 [ nFileSizeLow>> ] [ nFileSizeHigh>> ] bi >64bit ;
375 M: windows open-append
376 [ dup windows-file-size ] [ drop 0 ] recover
377 [ (open-append) ] dip >>ptr ;
381 [ "HOMEDRIVE" os-env "HOMEPATH" os-env append-path ]
382 [ "USERPROFILE" os-env ]
386 : STREAM_DATA>out ( WIN32_FIND_STREAM_DATA -- pair/f )
387 [ cStreamName>> alien>native-string ]
388 [ StreamSize>> ] bi 2array ;
390 : file-streams-rest ( streams handle -- streams )
391 WIN32_FIND_STREAM_DATA <struct>
392 [ FindNextStream ] 2keep
394 GetLastError ERROR_HANDLE_EOF = [ win32-error ] unless
397 pick push file-streams-rest
400 : file-streams ( path -- streams )
402 FindStreamInfoStandard
403 WIN32_FIND_STREAM_DATA <struct>
405 [ FindFirstStream ] keepd
406 over INVALID_HANDLE_VALUE = [
409 1vector swap file-streams-rest
412 : alternate-file-streams ( path -- streams )
413 file-streams [ cStreamName>> alien>native-string "::$DATA" = ] reject ;
415 : alternate-file-streams? ( path -- ? )
416 alternate-file-streams empty? not ;