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 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
13 SPECIALIZED-ARRAY: ushort
16 HOOK: CreateFile-flags io-backend ( DWORD -- DWORD )
17 HOOK: FileArgs-overlapped io-backend ( port -- overlapped/f )
18 HOOK: add-completion io-backend ( port -- port )
19 HOOK: open-append os ( path -- win32-file )
21 TUPLE: win32-file < win32-handle ptr ;
23 : <win32-file> ( handle -- win32-file )
24 win32-file new-win32-handle ;
27 [ cancel-operation ] [ call-next-method ] bi ;
29 : opened-file ( handle -- win32-file )
30 check-invalid-handle <win32-file> |dispose add-completion ;
39 : default-security-attributes ( -- obj )
40 SECURITY_ATTRIBUTES <struct>
41 SECURITY_ATTRIBUTES heap-size >>nLength ;
44 hFile lpBuffer nNumberOfBytesToRead
45 lpNumberOfBytesRet lpOverlapped ;
47 C: <FileArgs> FileArgs
49 : make-FileArgs ( port -- <FileArgs> )
51 [ handle>> check-disposed ]
54 [ buffer>> buffer-length ]
55 [ drop 0 DWORD <ref> ]
56 [ FileArgs-overlapped ]
59 ! Global variable with assoc mapping overlapped to threads
60 SYMBOL: pending-overlapped
62 TUPLE: io-callback port thread ;
64 C: <io-callback> io-callback
66 : (make-overlapped) ( -- overlapped-ext )
67 OVERLAPPED malloc-struct &free ;
69 : make-overlapped ( port -- overlapped-ext )
70 [ (make-overlapped) ] dip
73 [ -32 shift >>offset-high ] bi
76 M: windows FileArgs-overlapped ( port -- overlapped )
79 : <completion-port> ( handle existing -- handle )
80 f 1 CreateIoCompletionPort dup win32-error=0/f ;
82 SYMBOL: master-completion-port
84 : <master-completion-port> ( -- handle )
85 INVALID_HANDLE_VALUE f <completion-port> ;
87 M: windows add-completion ( win32-handle -- win32-handle )
88 dup handle>> master-completion-port get-global <completion-port> drop ;
91 { [ ERROR_HANDLE_EOF = ] [ ERROR_BROKEN_PIPE = ] } 1|| ;
93 : twiddle-thumbs ( overlapped port -- bytes-transferred )
96 [ self ] dip >c-ptr pending-overlapped get-global set-at
98 { [ dup integer? ] [ ] }
101 [ drop 0 ] [ n>win32-error-string throw ] if
106 :: wait-for-overlapped ( nanos -- bytes-transferred overlapped error? )
107 nanos [ 1,000,000 /i ] [ INFINITE ] if* :> timeout
108 master-completion-port get-global
109 { int void* pointer: OVERLAPPED }
110 [ timeout GetQueuedCompletionStatus zero? ] with-out-parameters
111 :> ( error? bytes key overlapped )
112 bytes overlapped error? ;
114 : resume-callback ( result overlapped -- )
115 >c-ptr pending-overlapped get-global delete-at* drop resume-with ;
117 : handle-overlapped ( nanos -- ? )
118 wait-for-overlapped [
120 [ drop GetLastError 1array ] dip resume-callback t
122 ] [ resume-callback t ] if ;
124 M: win32-handle cancel-operation
125 [ handle>> CancelIo win32-error=0/f ] unless-disposed ;
127 M: windows io-multiplex ( nanos -- )
128 handle-overlapped [ 0 io-multiplex ] when ;
130 M: windows init-io ( -- )
131 <master-completion-port> master-completion-port set-global
132 H{ } clone pending-overlapped set-global ;
134 ERROR: invalid-file-size n ;
136 : (handle>file-size) ( handle -- n/f )
137 0 ulonglong <ref> [ GetFileSizeEx ] keep swap
138 [ drop f ] [ drop ulonglong deref ] if-zero ;
140 ! GetFileSizeEx errors with ERROR_INVALID_FUNCTION if handle is not seekable
141 : handle>file-size ( handle -- n/f )
143 GetLastError ERROR_INVALID_FUNCTION =
144 [ f ] [ throw-win32-error ] if
147 ERROR: seek-before-start n ;
149 : set-seek-ptr ( n handle -- )
150 [ dup 0 < [ seek-before-start ] when ] dip ptr<< ;
152 M: windows tell-handle ( handle -- n ) ptr>> ;
154 M: windows seek-handle ( n seek-type handle -- )
156 { seek-absolute [ set-seek-ptr ] }
157 { seek-relative [ [ ptr>> + ] keep set-seek-ptr ] }
158 { seek-end [ [ handle>> handle>file-size + ] keep set-seek-ptr ] }
162 M: windows can-seek-handle? ( handle -- ? )
163 handle>> handle>file-size >boolean ;
165 M: windows handle-length ( handle -- n/f )
166 handle>> handle>file-size
167 dup { 0 f } member? [ drop f ] when ;
169 : file-error? ( n -- eof? )
172 { [ dup expected-io-error? ] [ drop f ] }
173 { [ dup eof? ] [ drop t ] }
174 [ n>win32-error-string throw ]
178 : wait-for-file ( FileArgs n port -- n )
180 [ 2drop 0 ] [ [ lpOverlapped>> ] dip twiddle-thumbs ] if ;
182 : update-file-ptr ( n port -- )
183 handle>> dup ptr>> [ rot + >>ptr drop ] [ 2drop ] if* ;
185 : finish-write ( n port -- )
186 [ update-file-ptr ] [ buffer>> buffer-consume ] 2bi ;
188 : setup-read ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToRead lpNumberOfBytesRead lpOverlapped )
191 [ lpBuffer>> buffer-end ]
192 [ lpBuffer>> buffer-capacity ]
193 [ lpNumberOfBytesRet>> ]
197 : setup-write ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToWrite lpNumberOfBytesWritten lpOverlapped )
200 [ lpBuffer>> buffer@ ]
201 [ lpBuffer>> buffer-length ]
202 [ lpNumberOfBytesRet>> ]
206 M: windows (wait-to-write)
208 [ make-FileArgs dup setup-write WriteFile ]
214 : finish-read ( n port -- )
215 [ update-file-ptr ] [ buffer>> n>buffer ] 2bi ;
217 M: windows (wait-to-read) ( port -- )
219 [ make-FileArgs dup setup-read ReadFile ]
225 : console-app? ( -- ? ) GetConsoleWindow >boolean ;
227 M: windows init-stdio
230 [ null-reader null-writer null-writer set-stdio ] if ;
232 : open-file ( path access-mode create-mode flags -- handle )
234 [ share-mode default-security-attributes ] 2dip
235 CreateFile-flags f CreateFile opened-file
238 : open-r/w ( path -- win32-file )
239 flags{ GENERIC_READ GENERIC_WRITE }
240 OPEN_EXISTING 0 open-file ;
242 : open-read ( path -- win32-file )
243 GENERIC_READ OPEN_EXISTING 0 open-file 0 >>ptr ;
245 : open-write ( path -- win32-file )
246 GENERIC_WRITE CREATE_ALWAYS 0 open-file 0 >>ptr ;
248 : (open-append) ( path -- win32-file )
249 GENERIC_WRITE OPEN_ALWAYS 0 open-file ;
251 : open-existing ( path -- win32-file )
252 flags{ GENERIC_READ GENERIC_WRITE }
256 FILE_FLAG_BACKUP_SEMANTICS
257 f CreateFileW dup win32-error=0/f <win32-file> ;
259 : maybe-create-file ( path -- win32-file ? )
260 #! return true if file was just created
261 flags{ GENERIC_READ GENERIC_WRITE }
266 f CreateFileW dup win32-error=0/f <win32-file>
267 GetLastError ERROR_ALREADY_EXISTS = not ;
269 : set-file-pointer ( handle length method -- )
270 [ [ handle>> ] dip d>w/w uint <ref> ] dip SetFilePointer
271 INVALID_SET_FILE_POINTER = [ "SetFilePointer failed" throw ] when ;
273 M: windows (file-reader) ( path -- stream )
274 open-read <input-port> ;
276 M: windows (file-writer) ( path -- stream )
277 open-write <output-port> ;
279 M: windows (file-appender) ( path -- stream )
280 open-append <output-port> ;
282 SYMBOLS: +read-only+ +hidden+ +system+
283 +archive+ +device+ +normal+ +temporary+
284 +sparse-file+ +reparse-point+ +compressed+ +offline+
285 +not-content-indexed+ +encrypted+ ;
289 : read-only? ( file-info -- ? )
290 attributes>> +read-only+ swap member? ;
292 : set-file-attributes ( path flags -- )
293 SetFileAttributes win32-error=0/f ;
295 : set-file-normal-attribute ( path -- )
296 FILE_ATTRIBUTE_NORMAL set-file-attributes ;
298 : win32-file-attribute ( n symbol attr -- )
299 rot mask? [ , ] [ drop ] if ;
301 : win32-file-attributes ( n -- seq )
304 [ +read-only+ FILE_ATTRIBUTE_READONLY win32-file-attribute ]
305 [ +hidden+ FILE_ATTRIBUTE_HIDDEN win32-file-attribute ]
306 [ +system+ FILE_ATTRIBUTE_SYSTEM win32-file-attribute ]
307 [ +directory+ FILE_ATTRIBUTE_DIRECTORY win32-file-attribute ]
308 [ +archive+ FILE_ATTRIBUTE_ARCHIVE win32-file-attribute ]
309 [ +device+ FILE_ATTRIBUTE_DEVICE win32-file-attribute ]
310 [ +normal+ FILE_ATTRIBUTE_NORMAL win32-file-attribute ]
311 [ +temporary+ FILE_ATTRIBUTE_TEMPORARY win32-file-attribute ]
312 [ +sparse-file+ FILE_ATTRIBUTE_SPARSE_FILE win32-file-attribute ]
313 [ +reparse-point+ FILE_ATTRIBUTE_REPARSE_POINT win32-file-attribute ]
314 [ +compressed+ FILE_ATTRIBUTE_COMPRESSED win32-file-attribute ]
315 [ +offline+ FILE_ATTRIBUTE_OFFLINE win32-file-attribute ]
316 [ +not-content-indexed+ FILE_ATTRIBUTE_NOT_CONTENT_INDEXED win32-file-attribute ]
317 [ +encrypted+ FILE_ATTRIBUTE_ENCRYPTED win32-file-attribute ]
321 : win32-file-type ( n -- symbol )
322 FILE_ATTRIBUTE_DIRECTORY mask? +directory+ +regular-file+ ? ;
324 : (set-file-times) ( handle timestamp/f timestamp/f timestamp/f -- )
325 [ timestamp>FILETIME ] tri@
326 SetFileTime win32-error=0/f ;
329 MAX_UNICODE_PATH dup ushort <c-array>
330 [ GetCurrentDirectory win32-error=0/f ] keep
331 utf16n alien>string ;
334 SetCurrentDirectory win32-error=0/f ;
336 CONSTANT: unicode-prefix "\\\\?\\"
338 M: windows root-directory? ( path -- ? )
340 { [ dup empty? ] [ drop f ] }
341 { [ dup [ path-separator? ] all? ] [ drop t ] }
342 { [ dup trim-tail-separators { [ length 2 = ]
343 [ second CHAR: : = ] } 1&& ] [ drop t ] }
344 { [ dup unicode-prefix head? ]
345 [ trim-tail-separators length unicode-prefix length 2 + = ] }
349 : prepend-prefix ( string -- string' )
350 dup unicode-prefix head? [
351 unicode-prefix prepend
354 TR: normalize-separators "/" "\\" ;
358 : unc-path? ( string -- ? )
359 [ "//" head? ] [ "\\\\" head? ] bi or ;
363 M: windows normalize-path ( string -- string' )
372 M: windows CreateFile-flags ( DWORD -- DWORD )
373 FILE_FLAG_OVERLAPPED bitor ;
377 : windows-file-size ( path -- size )
378 normalize-path 0 WIN32_FILE_ATTRIBUTE_DATA <struct>
379 [ GetFileAttributesEx win32-error=0/f ] keep
380 [ nFileSizeLow>> ] [ nFileSizeHigh>> ] bi >64bit ;
384 M: windows open-append
385 [ dup windows-file-size ] [ drop 0 ] recover
386 [ (open-append) ] dip >>ptr ;
390 [ "HOMEDRIVE" os-env "HOMEPATH" os-env append-path ]
391 [ "USERPROFILE" os-env ]