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
6 fry io io.backend io.binary io.buffers io.encodings.utf16n io.files
7 io.files.private io.files.types io.pathnames io.ports
8 io.streams.c io.streams.null io.timeouts kernel libc literals
9 locals make math math.bitwise namespaces sequences
10 specialized-arrays system threads tr windows windows.errors
11 windows.handles windows.kernel32 windows.shell32 windows.time
12 windows.types windows.winsock ;
13 SPECIALIZED-ARRAY: ushort
16 HOOK: CreateFile-flags io-backend ( DWORD -- DWORD )
17 HOOK: open-append os ( path -- win32-file )
19 TUPLE: win32-file < win32-handle ptr ;
21 : <win32-file> ( handle -- win32-file )
22 win32-file new-win32-handle ;
25 [ cancel-operation ] [ call-next-method ] bi ;
34 : default-security-attributes ( -- obj )
35 SECURITY_ATTRIBUTES <struct>
36 SECURITY_ATTRIBUTES heap-size >>nLength ;
39 hFile lpBuffer nNumberOfBytesToRead
40 lpNumberOfBytesRet lpOverlapped ;
42 C: <FileArgs> FileArgs
44 ! Global variable with assoc mapping overlapped to threads
45 SYMBOL: pending-overlapped
47 TUPLE: io-callback port thread ;
49 C: <io-callback> io-callback
51 : <completion-port> ( handle existing -- handle )
52 f 1 CreateIoCompletionPort dup win32-error=0/f ;
54 : <master-completion-port> ( -- handle )
55 INVALID_HANDLE_VALUE f <completion-port> ;
57 SYMBOL: master-completion-port
59 : add-completion ( win32-handle -- win32-handle )
60 dup handle>> master-completion-port get-global <completion-port> drop ;
62 : opened-file ( handle -- win32-file )
63 check-invalid-handle <win32-file> |dispose add-completion ;
66 { [ ERROR_HANDLE_EOF = ] [ ERROR_BROKEN_PIPE = ] } 1|| ;
68 : twiddle-thumbs ( overlapped port -- bytes-transferred )
71 [ self ] dip >c-ptr pending-overlapped get-global set-at
73 { [ dup integer? ] [ ] }
76 [ drop 0 ] [ n>win32-error-string throw ] if
81 :: wait-for-overlapped ( nanos -- bytes-transferred overlapped error? )
82 nanos [ 1,000,000 /i ] [ INFINITE ] if* :> timeout
83 master-completion-port get-global
84 { int void* pointer: OVERLAPPED }
85 [ timeout GetQueuedCompletionStatus zero? ] with-out-parameters
86 :> ( error? bytes key overlapped )
87 bytes overlapped error? ;
89 : resume-callback ( result overlapped -- )
90 >c-ptr pending-overlapped get-global delete-at* drop resume-with ;
92 : handle-overlapped ( nanos -- ? )
95 [ drop GetLastError 1array ] dip resume-callback t
97 ] [ resume-callback t ] if ;
99 M: win32-handle cancel-operation
100 [ handle>> CancelIo win32-error=0/f ] unless-disposed ;
102 M: windows io-multiplex ( nanos -- )
103 handle-overlapped [ 0 io-multiplex ] when ;
105 M: windows init-io ( -- )
106 <master-completion-port> master-completion-port set-global
107 H{ } clone pending-overlapped set-global ;
109 ERROR: invalid-file-size n ;
111 : (handle>file-size) ( handle -- n/f )
112 0 ulonglong <ref> [ GetFileSizeEx ] keep swap
113 [ drop f ] [ drop ulonglong deref ] if-zero ;
115 ! GetFileSizeEx errors with ERROR_INVALID_FUNCTION if handle is not seekable
116 : handle>file-size ( handle -- n/f )
118 GetLastError ERROR_INVALID_FUNCTION =
119 [ f ] [ throw-win32-error ] if
122 ERROR: seek-before-start n ;
124 : set-seek-ptr ( n handle -- )
125 [ dup 0 < [ seek-before-start ] when ] dip ptr<< ;
127 M: windows tell-handle ( handle -- n ) ptr>> ;
129 M: windows seek-handle ( n seek-type handle -- )
131 { seek-absolute [ set-seek-ptr ] }
132 { seek-relative [ [ ptr>> + ] keep set-seek-ptr ] }
133 { seek-end [ [ handle>> handle>file-size + ] keep set-seek-ptr ] }
137 M: windows can-seek-handle? ( handle -- ? )
138 handle>> handle>file-size >boolean ;
140 M: windows handle-length ( handle -- n/f )
141 handle>> handle>file-size
142 dup { 0 f } member? [ drop f ] when ;
144 : file-error? ( n -- eof? )
147 { [ dup expected-io-error? ] [ drop f ] }
148 { [ dup eof? ] [ drop t ] }
149 [ n>win32-error-string throw ]
153 : wait-for-file ( FileArgs n port -- n )
155 [ 2drop 0 ] [ [ lpOverlapped>> ] dip twiddle-thumbs ] if ;
157 : update-file-ptr ( n port -- )
158 handle>> dup ptr>> [ rot + >>ptr drop ] [ 2drop ] if* ;
160 : (make-overlapped) ( -- overlapped-ext )
161 OVERLAPPED malloc-struct &free ;
163 : make-overlapped ( handle -- overlapped-ext )
164 (make-overlapped) swap
165 ptr>> [ [ 32 bits >>offset ] [ -32 shift >>offset-high ] bi ] when* ;
167 : make-FileArgs ( port handle -- <FileArgs> )
168 [ nip dup check-disposed handle>> ]
170 [ buffer>> dup buffer-length 0 DWORD <ref> ] dip make-overlapped
173 : setup-write ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToWrite lpNumberOfBytesWritten lpOverlapped )
176 [ lpBuffer>> [ buffer@ ] [ buffer-length ] bi ]
177 [ lpNumberOfBytesRet>> ]
181 : finish-write ( n port -- )
182 [ update-file-ptr ] [ buffer>> buffer-consume ] 2bi ;
184 M: object drain ( port handle -- event/f )
185 [ make-FileArgs dup setup-write WriteFile ]
186 [ drop [ wait-for-file ] [ finish-write ] bi ] 2bi f ;
188 : setup-read ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToRead lpNumberOfBytesRead lpOverlapped )
191 [ lpBuffer>> [ buffer-end ] [ buffer-capacity ] bi ]
192 [ lpNumberOfBytesRet>> ]
196 : finish-read ( n port -- )
197 [ update-file-ptr ] [ buffer>> n>buffer ] 2bi ;
199 M: object refill ( port handle -- event/f )
200 [ make-FileArgs dup setup-read ReadFile ]
201 [ drop [ wait-for-file ] [ finish-read ] bi ] 2bi f ;
203 M: windows (wait-to-write) ( port -- )
204 [ dup handle>> drain ] with-destructors drop ;
206 M: windows (wait-to-read) ( port -- )
207 [ dup handle>> refill ] with-destructors drop ;
209 : make-fd-set ( socket -- fd_set )
210 fd_set <struct> swap 1array void* >c-array >>fd_array 1 >>fd_count ;
212 : select-sets ( socket event -- read-fds write-fds except-fds )
213 [ make-fd-set ] dip +input+ = [ f f ] [ f swap f ] if ;
215 CONSTANT: select-timeval S{ timeval { sec 0 } { usec 1000 } }
217 M: windows wait-for-fd ( handle event -- )
218 [ file>> handle>> 1 swap ] dip select-sets select-timeval
221 : console-app? ( -- ? ) GetConsoleWindow >boolean ;
223 M: windows init-stdio
226 [ null-reader null-writer null-writer set-stdio ] if ;
228 : open-file ( path access-mode create-mode flags -- handle )
230 [ share-mode default-security-attributes ] 2dip
231 CreateFile-flags f CreateFile opened-file
234 : open-r/w ( path -- win32-file )
235 flags{ GENERIC_READ GENERIC_WRITE }
236 OPEN_EXISTING 0 open-file ;
238 : open-read ( path -- win32-file )
239 GENERIC_READ OPEN_EXISTING 0 open-file 0 >>ptr ;
241 : open-write ( path -- win32-file )
242 GENERIC_WRITE CREATE_ALWAYS 0 open-file 0 >>ptr ;
244 : (open-append) ( path -- win32-file )
245 GENERIC_WRITE OPEN_ALWAYS 0 open-file ;
247 : open-existing ( path -- win32-file )
248 flags{ GENERIC_READ GENERIC_WRITE }
252 FILE_FLAG_BACKUP_SEMANTICS
253 f CreateFileW dup win32-error=0/f <win32-file> ;
255 : maybe-create-file ( path -- win32-file ? )
256 #! return true if file was just created
257 flags{ GENERIC_READ GENERIC_WRITE }
262 f CreateFileW dup win32-error=0/f <win32-file>
263 GetLastError ERROR_ALREADY_EXISTS = not ;
265 : set-file-pointer ( handle length method -- )
266 [ [ handle>> ] dip d>w/w uint <ref> ] dip SetFilePointer
267 INVALID_SET_FILE_POINTER = [ "SetFilePointer failed" throw ] when ;
269 M: windows (file-reader) ( path -- stream )
270 open-read <input-port> ;
272 M: windows (file-writer) ( path -- stream )
273 open-write <output-port> ;
275 M: windows (file-appender) ( path -- stream )
276 open-append <output-port> ;
278 SYMBOLS: +read-only+ +hidden+ +system+
279 +archive+ +device+ +normal+ +temporary+
280 +sparse-file+ +reparse-point+ +compressed+ +offline+
281 +not-content-indexed+ +encrypted+ ;
285 : read-only? ( file-info -- ? )
286 attributes>> +read-only+ swap member? ;
288 : set-file-attributes ( path flags -- )
289 SetFileAttributes win32-error=0/f ;
291 : set-file-normal-attribute ( path -- )
292 FILE_ATTRIBUTE_NORMAL set-file-attributes ;
294 : win32-file-attribute ( n symbol attr -- )
295 rot mask? [ , ] [ drop ] if ;
297 : win32-file-attributes ( n -- seq )
300 [ +read-only+ FILE_ATTRIBUTE_READONLY win32-file-attribute ]
301 [ +hidden+ FILE_ATTRIBUTE_HIDDEN win32-file-attribute ]
302 [ +system+ FILE_ATTRIBUTE_SYSTEM win32-file-attribute ]
303 [ +directory+ FILE_ATTRIBUTE_DIRECTORY win32-file-attribute ]
304 [ +archive+ FILE_ATTRIBUTE_ARCHIVE win32-file-attribute ]
305 [ +device+ FILE_ATTRIBUTE_DEVICE win32-file-attribute ]
306 [ +normal+ FILE_ATTRIBUTE_NORMAL win32-file-attribute ]
307 [ +temporary+ FILE_ATTRIBUTE_TEMPORARY win32-file-attribute ]
308 [ +sparse-file+ FILE_ATTRIBUTE_SPARSE_FILE win32-file-attribute ]
309 [ +reparse-point+ FILE_ATTRIBUTE_REPARSE_POINT win32-file-attribute ]
310 [ +compressed+ FILE_ATTRIBUTE_COMPRESSED win32-file-attribute ]
311 [ +offline+ FILE_ATTRIBUTE_OFFLINE win32-file-attribute ]
312 [ +not-content-indexed+ FILE_ATTRIBUTE_NOT_CONTENT_INDEXED win32-file-attribute ]
313 [ +encrypted+ FILE_ATTRIBUTE_ENCRYPTED win32-file-attribute ]
317 : win32-file-type ( n -- symbol )
318 FILE_ATTRIBUTE_DIRECTORY mask? +directory+ +regular-file+ ? ;
320 : (set-file-times) ( handle timestamp/f timestamp/f timestamp/f -- )
321 [ timestamp>FILETIME ] tri@
322 SetFileTime win32-error=0/f ;
325 MAX_UNICODE_PATH dup ushort <c-array>
326 [ GetCurrentDirectory win32-error=0/f ] keep alien>native-string ;
329 SetCurrentDirectory win32-error=0/f ;
331 CONSTANT: unicode-prefix "\\\\?\\"
333 M: windows root-directory? ( path -- ? )
335 { [ dup empty? ] [ drop f ] }
336 { [ dup [ path-separator? ] all? ] [ drop t ] }
337 { [ dup trim-tail-separators { [ length 2 = ]
338 [ second CHAR: : = ] } 1&& ] [ drop t ] }
339 { [ dup unicode-prefix head? ]
340 [ trim-tail-separators length unicode-prefix length 2 + = ] }
344 : prepend-prefix ( string -- string' )
345 dup unicode-prefix head? [
346 unicode-prefix prepend
349 TR: normalize-separators "/" "\\" ;
353 : unc-path? ( string -- ? )
354 [ "//" head? ] [ "\\\\" head? ] bi or ;
358 M: windows normalize-path ( string -- string' )
367 M: windows CreateFile-flags ( DWORD -- DWORD )
368 FILE_FLAG_OVERLAPPED bitor ;
372 : windows-file-size ( path -- size )
373 normalize-path 0 WIN32_FILE_ATTRIBUTE_DATA <struct>
374 [ GetFileAttributesEx win32-error=0/f ] keep
375 [ nFileSizeLow>> ] [ nFileSizeHigh>> ] bi >64bit ;
379 M: windows open-append
380 [ dup windows-file-size ] [ drop 0 ] recover
381 [ (open-append) ] dip >>ptr ;
385 [ "HOMEDRIVE" os-env "HOMEPATH" os-env append-path ]
386 [ "USERPROFILE" os-env ]