1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: byte-arrays math io.backend io.files.info
4 io.files.windows kernel windows.kernel32
5 windows.time windows.types windows accessors alien.c-types
6 combinators generalizations system alien.strings
7 sequences splitting windows.errors fry
8 continuations destructors calendar ascii
9 combinators.short-circuit literals locals classes.struct
10 specialized-arrays alien.data libc windows.shell32 ;
11 SPECIALIZED-ARRAY: ushort
12 IN: io.files.info.windows
14 :: round-up-to ( n multiple -- n' )
21 TUPLE: windows-file-info < file-info-tuple attributes ;
23 : get-compressed-file-size ( path -- n )
24 { DWORD } [ GetCompressedFileSize ] with-out-parameters
25 over INVALID_FILE_SIZE = [ win32-error-string throw ] [ >64bit ] if ;
27 : set-windows-size-on-disk ( file-info path -- file-info )
28 over attributes>> +compressed+ swap member? [
29 get-compressed-file-size
31 drop dup size>> 4096 round-up-to
34 : WIN32_FIND_DATA>file-info ( WIN32_FIND_DATA -- file-info )
35 [ \ windows-file-info new ] dip
37 [ dwFileAttributes>> win32-file-type >>type ]
38 [ dwFileAttributes>> win32-file-attributes >>attributes ]
39 [ [ nFileSizeLow>> ] [ nFileSizeHigh>> ] bi >64bit >>size ]
40 [ dwFileAttributes>> >>permissions ]
41 [ ftCreationTime>> FILETIME>timestamp >>created ]
42 [ ftLastWriteTime>> FILETIME>timestamp >>modified ]
43 [ ftLastAccessTime>> FILETIME>timestamp >>accessed ]
46 : find-first-file-stat ( path -- WIN32_FIND_DATA )
47 WIN32_FIND_DATA <struct> [
49 [ INVALID_HANDLE_VALUE = [ win32-error ] when ] keep
50 FindClose win32-error=0/f
53 : BY_HANDLE_FILE_INFORMATION>file-info ( HANDLE_FILE_INFORMATION -- file-info )
54 [ \ windows-file-info new ] dip
56 [ dwFileAttributes>> win32-file-type >>type ]
57 [ dwFileAttributes>> win32-file-attributes >>attributes ]
60 [ nFileSizeHigh>> ] bi >64bit >>size
62 [ dwFileAttributes>> >>permissions ]
63 [ ftCreationTime>> FILETIME>timestamp >>created ]
64 [ ftLastWriteTime>> FILETIME>timestamp >>modified ]
65 [ ftLastAccessTime>> FILETIME>timestamp >>accessed ]
66 ! [ nNumberOfLinks>> ]
69 ! [ nFileIndexHigh>> ] bi >64bit
73 : get-file-information ( handle -- BY_HANDLE_FILE_INFORMATION )
75 BY_HANDLE_FILE_INFORMATION <struct>
76 [ GetFileInformationByHandle win32-error=0/f ] keep
77 ] keep CloseHandle win32-error=0/f ;
79 : valid-handle? ( handle -- boolean )
80 INVALID_HANDLE_VALUE = not ; inline
82 : open-read-handle ( path -- handle/f )
83 ! Parameters of CreateFileW here should match those in open-read.
84 GENERIC_READ share-mode f
85 OPEN_EXISTING 0 CreateFile-flags f
86 CreateFileW [ valid-handle? ] keep f ? ;
88 : get-file-information-stat ( path -- file-info )
89 dup open-read-handle dup [
91 get-file-information BY_HANDLE_FILE_INFORMATION>file-info
93 drop find-first-file-stat WIN32_FIND_DATA>file-info
96 M: windows file-info ( path -- info )
98 [ get-file-information-stat ]
99 [ set-windows-size-on-disk ] bi ;
101 M: windows link-info ( path -- info )
104 : file-executable-type ( path -- executable/f )
108 ! hi is zero means old style executable
109 0 SHGFI_EXETYPE SHGetFileInfoW
113 nip >lo-hi first2 zero? [
115 { 0x5A4D [ +dos-executable+ ] }
116 { 0x4550 [ +win32-console-executable+ ] }
121 { 0x454C [ +win32-vxd-executable+ ] }
122 { 0x454E [ +win32-os2-executable+ ] }
123 { 0x4550 [ +win32-nt-executable+ ] }
129 CONSTANT: path-length $[ MAX_PATH 1 + ]
131 : volume-information ( normalized-path -- volume-name volume-serial max-component flags type )
132 { { ushort path-length } DWORD DWORD DWORD { ushort path-length } }
133 [ [ path-length ] 4dip path-length GetVolumeInformation win32-error=0/f ]
135 [ alien>native-string ] 4dip alien>native-string ;
137 : file-system-space ( normalized-path -- available-space total-space free-space )
138 { ULARGE_INTEGER ULARGE_INTEGER ULARGE_INTEGER }
139 [ GetDiskFreeSpaceEx win32-error=0/f ]
140 with-out-parameters ;
142 : calculate-file-system-info ( file-system-info -- file-system-info' )
143 [ dup [ total-space>> ] [ free-space>> ] bi - >>used-space drop ] keep ;
145 TUPLE: win32-file-system-info < file-system-info-tuple max-component flags device-serial ;
147 ERROR: not-absolute-path ;
149 : root-directory ( string -- string' )
150 unicode-prefix ?head drop
155 } 1&& [ 2 head "\\" append ] [ not-absolute-path ] if ;
159 : (file-system-info) ( path -- file-system-info )
160 dup [ volume-information ] [ file-system-space ] bi
161 \ win32-file-system-info new
164 swap >>available-space
171 calculate-file-system-info ;
175 M: windows file-system-info ( path -- file-system-info )
176 normalize-path root-directory (file-system-info) ;
178 CONSTANT: names-buf-length 16384
180 : find-first-volume ( -- string handle )
181 { { ushort path-length } }
182 [ path-length FindFirstVolume dup win32-error=0/f ]
183 with-out-parameters alien>native-string swap ;
185 : find-next-volume ( handle -- string/f )
186 { { ushort path-length } }
187 [ path-length FindNextVolume ] with-out-parameters
189 GetLastError ERROR_NO_MORE_FILES =
190 [ drop f ] [ win32-error-string throw ] if
191 ] [ alien>native-string ] if ;
193 : find-volumes ( -- array )
197 [ _ find-next-volume dup ] [ ] produce nip
200 ] [ '[ _ FindVolumeClose win32-error=0/f ] ] bi [ ] cleanup ;
202 ! Windows may return a volume which looks up to path ""
203 ! For now, treat it like there is not a volume here
204 : (volume>paths) ( string -- array )
207 [ ushort malloc-array &free ] keep
209 [ GetVolumePathNamesForVolumeName win32-error=0/f ] 3keep nip
210 uint deref head but-last-slice
211 { 0 } split-slice harvest
212 [ { } ] [ [ { 0 } append alien>native-string ] map ] if-empty
215 ! Suppress T{ windows-error f 2 "The system cannot find the file specified." }
216 : volume>paths ( string -- array )
217 [ (volume>paths) ] curry
219 dup { [ windows-error? ] [ n>> ERROR_FILE_NOT_FOUND = ] } 1&&
220 [ drop { } ] [ rethrow ] if
223 ! Can error with T{ windows-error f 21 "The device is not ready." }
224 ! if there is a D: that is not ready, for instance. Ignore these drives.
225 M: windows file-systems ( -- array )
226 find-volumes [ volume>paths ] map concat [
227 [ (file-system-info) ] [ 2drop f ] recover
230 : file-times ( path -- timestamp timestamp timestamp )
232 normalize-path open-read &dispose handle>>
233 { FILETIME FILETIME FILETIME }
234 [ GetFileTime win32-error=0/f ]
236 [ FILETIME>timestamp >local-time ] tri@
239 : set-file-times ( path timestamp/f timestamp/f timestamp/f -- )
240 ! timestamp order: creation access write
243 normalize-path open-r/w &dispose handle>>
244 ] 3dip (set-file-times)
247 : set-file-create-time ( path timestamp -- )
250 : set-file-access-time ( path timestamp -- )
251 [ f ] dip f set-file-times ;
253 : set-file-write-time ( path timestamp -- )
254 [ f f ] dip set-file-times ;
256 M: windows file-readable?
257 normalize-path open-read-handle
258 dup [ CloseHandle win32-error=0/f ] when* >boolean ;
260 M: windows file-writable? file-info attributes>> +read-only+ swap member? not ;
261 M: windows file-executable? file-executable-type windows-executable? ;