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