]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/windows/windows.factor
endian: replaces io.binary and io.binary.fast.
[factor.git] / basis / io / files / windows / windows.factor
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
6 io io.backend io.buffers io.files io.files.private
7 io.files.types io.pathnames io.pathnames.private io.ports
8 io.streams.c io.streams.null io.timeouts kernel libc literals
9 locals math math.bitwise namespaces sequences specialized-arrays
10 system threads tr vectors windows windows.errors windows.handles
11 windows.kernel32 windows.shell32 windows.time windows.types
12 windows.winsock splitting ;
13 SPECIALIZED-ARRAY: ushort
14 IN: io.files.windows
15
16 SLOT: file
17
18 : CreateFile-flags ( DWORD -- DWORD )
19     flags{ FILE_FLAG_BACKUP_SEMANTICS FILE_FLAG_OVERLAPPED } bitor ;
20
21 HOOK: open-append os ( path -- win32-file )
22
23 TUPLE: win32-file < win32-handle ptr ;
24
25 : <win32-file> ( handle -- win32-file )
26     win32-file new-win32-handle ;
27
28 M: win32-file dispose
29     [ cancel-operation ] [ call-next-method ] bi ;
30
31 CONSTANT: share-mode
32     flags{
33         FILE_SHARE_READ
34         FILE_SHARE_WRITE
35         FILE_SHARE_DELETE
36     }
37
38 : default-security-attributes ( -- obj )
39     SECURITY_ATTRIBUTES <struct>
40     SECURITY_ATTRIBUTES heap-size >>nLength ;
41
42 TUPLE: FileArgs
43     hFile lpBuffer nNumberOfBytesToRead
44     lpNumberOfBytesRet lpOverlapped ;
45
46 C: <FileArgs> FileArgs
47
48 ! Global variable with assoc mapping overlapped to threads
49 SYMBOL: pending-overlapped
50
51 TUPLE: io-callback port thread ;
52
53 C: <io-callback> io-callback
54
55 : <completion-port> ( handle existing -- handle )
56      f 1 CreateIoCompletionPort dup win32-error=0/f ;
57
58 : <master-completion-port> ( -- handle )
59     INVALID_HANDLE_VALUE f <completion-port> ;
60
61 SYMBOL: master-completion-port
62
63 : add-completion ( win32-handle -- win32-handle )
64     dup handle>> master-completion-port get-global <completion-port> drop ;
65
66 : opened-file ( handle -- win32-file )
67     check-invalid-handle <win32-file> |dispose add-completion ;
68
69 : eof? ( error -- ? )
70     { [ ERROR_HANDLE_EOF = ] [ ERROR_BROKEN_PIPE = ] } 1|| ;
71
72 : twiddle-thumbs ( overlapped port -- bytes-transferred )
73     [
74         drop
75         [ self ] dip >c-ptr pending-overlapped get-global set-at
76         "I/O" suspend {
77             { [ dup integer? ] [ ] }
78             { [ dup array? ] [
79                 first dup eof?
80                 [ drop 0 ] [ throw-windows-error ] if
81             ] }
82         } cond
83     ] with-timeout ;
84
85 :: wait-for-overlapped ( nanos -- bytes-transferred overlapped error? )
86     nanos [ 1,000,000 /i ] [ INFINITE ] if* :> timeout
87     master-completion-port get-global
88     { int void* pointer: OVERLAPPED }
89     [ timeout GetQueuedCompletionStatus zero? ] with-out-parameters
90     :> ( error? bytes key overlapped )
91     bytes overlapped error? ;
92
93 : resume-callback ( result overlapped -- )
94     >c-ptr pending-overlapped get-global delete-at* drop resume-with ;
95
96 : handle-overlapped ( nanos -- ? )
97     wait-for-overlapped [
98         [
99             [ drop GetLastError 1array ] dip resume-callback t
100         ] [ drop f ] if*
101     ] [ resume-callback t ] if ;
102
103 M: win32-handle cancel-operation
104     [ handle>> CancelIo win32-error=0/f ] unless-disposed ;
105
106 M: windows io-multiplex
107     handle-overlapped [ 0 io-multiplex ] when ;
108
109 M: windows init-io
110     <master-completion-port> master-completion-port set-global
111     H{ } clone pending-overlapped set-global ;
112
113 : (handle>file-size) ( handle -- n/f )
114     0 ulonglong <ref> [ GetFileSizeEx ] keep swap
115     [ drop f ] [ drop ulonglong deref ] if-zero ;
116
117 ! GetFileSizeEx errors with ERROR_INVALID_FUNCTION if handle is not seekable
118 : handle>file-size ( handle -- n/f )
119     (handle>file-size) [
120         GetLastError ERROR_INVALID_FUNCTION =
121         [ win32-error ] unless f
122     ] unless* ;
123
124 ERROR: seek-before-start n ;
125
126 : set-seek-ptr ( n handle -- )
127     [ dup 0 < [ seek-before-start ] when ] dip ptr<< ;
128
129 M: windows tell-handle ptr>> ;
130
131 M: windows seek-handle
132     swap {
133         { seek-absolute [ set-seek-ptr ] }
134         { seek-relative [ [ ptr>> + ] keep set-seek-ptr ] }
135         { seek-end [ [ handle>> handle>file-size + ] keep set-seek-ptr ] }
136         [ bad-seek-type ]
137     } case ;
138
139 M: windows can-seek-handle?
140     handle>> handle>file-size >boolean ;
141
142 M: windows handle-length
143     handle>> handle>file-size
144     dup { 0 f } member? [ drop f ] when ;
145
146 : file-error? ( n -- eof? )
147     zero? [
148         GetLastError {
149             { [ dup expected-io-error? ] [ drop f ] }
150             { [ dup eof? ] [ drop t ] }
151             [ throw-windows-error ]
152         } cond
153     ] [ f ] if ;
154
155 : wait-for-file ( FileArgs n port -- n )
156     swap file-error?
157     [ 2drop 0 ] [ [ lpOverlapped>> ] dip twiddle-thumbs ] if ;
158
159 : update-file-ptr ( n port -- )
160     handle>> dup ptr>> [ rot + >>ptr drop ] [ 2drop ] if* ;
161
162 : (make-overlapped) ( -- overlapped-ext )
163     OVERLAPPED malloc-struct &free ;
164
165 : make-overlapped ( handle -- overlapped-ext )
166     (make-overlapped) swap
167     ptr>> [ [ 32 bits >>offset ] [ -32 shift >>offset-high ] bi ] when* ;
168
169 : make-FileArgs ( port handle -- <FileArgs> )
170     [ nip check-disposed handle>> ]
171     [
172         [ buffer>> dup buffer-length 0 DWORD <ref> ] dip make-overlapped
173     ] 2bi <FileArgs> ;
174
175 : setup-write ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToWrite lpNumberOfBytesWritten lpOverlapped )
176     {
177         [ hFile>> ]
178         [ lpBuffer>> [ buffer@ ] [ buffer-length ] bi ]
179         [ lpNumberOfBytesRet>> ]
180         [ lpOverlapped>> ]
181     } cleave ;
182
183 : finish-write ( n port -- )
184     [ update-file-ptr ] [ buffer>> buffer-consume ] 2bi ;
185
186 M: object drain
187     [ make-FileArgs dup setup-write WriteFile ]
188     [ drop [ wait-for-file ] [ finish-write ] bi ] 2bi f ;
189
190 : setup-read ( <FileArgs> -- hFile lpBuffer nNumberOfBytesToRead lpNumberOfBytesRead lpOverlapped )
191     {
192         [ hFile>> ]
193         [ lpBuffer>> [ buffer-end ] [ buffer-capacity ] bi ]
194         [ lpNumberOfBytesRet>> ]
195         [ lpOverlapped>> ]
196     } cleave ;
197
198 : finish-read ( n port -- )
199     [ update-file-ptr ] [ buffer>> buffer+ ] 2bi ;
200
201 M: object refill
202     [ make-FileArgs dup setup-read ReadFile ]
203     [ drop [ wait-for-file ] [ finish-read ] bi ] 2bi f ;
204
205 M: windows (wait-to-write)
206     [ dup handle>> drain ] with-destructors drop ;
207
208 M: windows (wait-to-read)
209     [ dup handle>> refill ] with-destructors drop ;
210
211 : make-fd-set ( socket -- fd_set )
212     fd_set <struct> swap 1array void* >c-array >>fd_array 1 >>fd_count ;
213
214 : select-sets ( socket event -- read-fds write-fds except-fds )
215     [ make-fd-set ] dip +input+ = [ f f ] [ f swap f ] if ;
216
217 CONSTANT: select-timeval S{ timeval { sec 0 } { usec 1000 } }
218
219 M: windows wait-for-fd
220     [ file>> handle>> 1 swap ] dip select-sets select-timeval
221     select drop yield ;
222
223 : console-app? ( -- ? ) GetConsoleWindow >boolean ;
224
225 M: windows init-stdio
226     console-app?
227     [ init-c-stdio ]
228     [ null-reader null-writer null-writer set-stdio ] if ;
229
230 : open-file ( path access-mode create-mode flags -- handle )
231     [
232         [ share-mode default-security-attributes ] 2dip
233         CreateFile-flags f CreateFileW opened-file
234     ] with-destructors ;
235
236 : open-r/w ( path -- win32-file )
237     flags{ GENERIC_READ GENERIC_WRITE }
238     OPEN_EXISTING 0 open-file ;
239
240 : open-read ( path -- win32-file )
241     GENERIC_READ OPEN_EXISTING 0 open-file 0 >>ptr ;
242
243 : open-write ( path -- win32-file )
244     GENERIC_WRITE CREATE_ALWAYS 0 open-file 0 >>ptr ;
245
246 : (open-append) ( path -- win32-file )
247     GENERIC_WRITE OPEN_ALWAYS 0 open-file ;
248
249 : maybe-create-file ( path -- win32-file ? )
250     ! return true if file was just created
251     flags{ GENERIC_READ GENERIC_WRITE }
252     OPEN_ALWAYS 0 open-file
253     GetLastError ERROR_ALREADY_EXISTS = not ;
254
255 : set-file-pointer ( handle length method -- )
256     [ [ handle>> ] dip d>w/w LONG <ref> ] dip SetFilePointer
257     INVALID_SET_FILE_POINTER = [ "SetFilePointer failed" throw ] when ;
258
259 M: windows (file-reader)
260     open-read <input-port> ;
261
262 M: windows (file-writer)
263     open-write <output-port> ;
264
265 M: windows (file-appender)
266     open-append <output-port> ;
267
268 SYMBOLS: +read-only+ +hidden+ +system+
269 +archive+ +device+ +normal+ +temporary+
270 +sparse-file+ +reparse-point+ +compressed+ +offline+
271 +not-content-indexed+ +encrypted+ ;
272
273 SLOT: attributes
274
275 : read-only? ( file-info -- ? )
276     attributes>> +read-only+ swap member? ;
277
278 : set-file-attributes ( path flags -- )
279     SetFileAttributes win32-error=0/f ;
280
281 : set-file-normal-attribute ( path -- )
282     FILE_ATTRIBUTE_NORMAL set-file-attributes ;
283
284 : win32-file-attributes ( n -- seq )
285     {
286         { +read-only+ $ FILE_ATTRIBUTE_READONLY }
287         { +hidden+ $ FILE_ATTRIBUTE_HIDDEN }
288         { +system+ $ FILE_ATTRIBUTE_SYSTEM }
289         { +directory+ $ FILE_ATTRIBUTE_DIRECTORY }
290         { +archive+ $ FILE_ATTRIBUTE_ARCHIVE }
291         { +device+ $ FILE_ATTRIBUTE_DEVICE }
292         { +normal+ $ FILE_ATTRIBUTE_NORMAL }
293         { +temporary+ $ FILE_ATTRIBUTE_TEMPORARY }
294         { +sparse-file+ $ FILE_ATTRIBUTE_SPARSE_FILE }
295         { +reparse-point+ $ FILE_ATTRIBUTE_REPARSE_POINT }
296         { +compressed+ $ FILE_ATTRIBUTE_COMPRESSED }
297         { +offline+ $ FILE_ATTRIBUTE_OFFLINE }
298         { +not-content-indexed+ $ FILE_ATTRIBUTE_NOT_CONTENT_INDEXED }
299         { +encrypted+ $ FILE_ATTRIBUTE_ENCRYPTED }
300     } [ mask? [ drop f ] unless ] with { } assoc>map sift ;
301
302 : win32-file-type ( n -- symbol )
303     FILE_ATTRIBUTE_DIRECTORY mask? +directory+ +regular-file+ ? ;
304
305 : (set-file-times) ( handle timestamp/f timestamp/f timestamp/f -- )
306     [ timestamp>FILETIME ] tri@
307     SetFileTime win32-error=0/f ;
308
309 M: windows cwd
310     MAX_UNICODE_PATH dup ushort <c-array>
311     [ GetCurrentDirectory win32-error=0/f ] keep alien>native-string ;
312
313 M: windows cd
314     SetCurrentDirectory win32-error=0/f ;
315
316 CONSTANT: unicode-prefix "\\\\?\\"
317
318 M: windows root-directory?
319     {
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 + = ] }
326         [ drop f ]
327     } cond ;
328
329 : prepend-unicode-prefix ( string -- string' )
330     dup unicode-prefix head? [
331         unicode-prefix prepend
332     ] unless ;
333
334 : remove-unicode-prefix ( string -- string' )
335     unicode-prefix ?head drop ;
336
337 TR: normalize-separators "/" "\\" ;
338
339 <PRIVATE
340
341 : unc-path? ( string -- ? )
342     [ "//" head? ] [ "\\\\" head? ] bi or ;
343
344 PRIVATE>
345
346 M: windows canonicalize-path
347     remove-unicode-prefix canonicalize-path* ;
348
349 M: windows canonicalize-drive
350     dup windows-absolute-path? [ ":" split1 [ >upper ] dip ":" glue ] when ;
351
352 M: windows canonicalize-path-full canonicalize-path canonicalize-drive >windows-path ;
353
354 M: windows root-path remove-unicode-prefix root-path* ;
355
356 M: windows relative-path remove-unicode-prefix relative-path* ;
357
358 M: windows normalize-path
359     dup unc-path? [
360         normalize-separators
361     ] [
362         absolute-path
363         normalize-separators
364         prepend-unicode-prefix
365     ] if ;
366
367 <PRIVATE
368
369 : windows-file-size ( path -- size )
370     normalize-path 0 WIN32_FILE_ATTRIBUTE_DATA <struct>
371     [ GetFileAttributesEx win32-error=0/f ] keep
372     [ nFileSizeLow>> ] [ nFileSizeHigh>> ] bi >64bit ;
373
374 PRIVATE>
375
376 M: windows open-append
377     [ dup windows-file-size ] [ drop 0 ] recover
378     [ (open-append) ] dip >>ptr ;
379
380 M: windows home
381     {
382         [ "HOMEDRIVE" os-env "HOMEPATH" os-env append-path ]
383         [ "USERPROFILE" os-env ]
384         [ my-documents ]
385     } 0|| ;
386
387 : STREAM_DATA>out ( WIN32_FIND_STREAM_DATA -- pair/f )
388     [ cStreamName>> alien>native-string ]
389     [ StreamSize>> ] bi 2array ;
390
391 : file-streams-rest ( streams handle -- streams )
392     WIN32_FIND_STREAM_DATA <struct>
393     [ FindNextStream ] 2keep
394     rot zero? [
395         GetLastError ERROR_HANDLE_EOF = [ win32-error ] unless
396         2drop
397     ] [
398         pick push file-streams-rest
399     ] if ;
400
401 : file-streams ( path -- streams )
402     normalize-path
403     FindStreamInfoStandard
404     WIN32_FIND_STREAM_DATA <struct>
405     0
406     [ FindFirstStream ] keepd
407     over INVALID_HANDLE_VALUE = [
408         2drop win32-error f
409     ] [
410         1vector swap file-streams-rest
411     ] if ;
412
413 : alternate-file-streams ( path -- streams )
414     file-streams [ cStreamName>> alien>native-string "::$DATA" = ] reject ;
415
416 : alternate-file-streams? ( path -- ? )
417     alternate-file-streams empty? not ;