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 : get-file-information-stat ( path -- BY_HANDLE_FILE_INFORMATION )
81 GENERIC_READ FILE_SHARE_READ f
82 OPEN_EXISTING FILE_FLAG_BACKUP_SEMANTICS f
83 CreateFileW dup INVALID_HANDLE_VALUE = [
84 drop find-first-file-stat WIN32_FIND_DATA>file-info
87 get-file-information BY_HANDLE_FILE_INFORMATION>file-info
90 M: windows file-info ( path -- info )
92 [ get-file-information-stat ]
93 [ set-windows-size-on-disk ] bi ;
95 M: windows link-info ( path -- info )
98 : file-executable-type ( path -- executable/f )
102 ! hi is zero means old style executable
103 0 SHGFI_EXETYPE SHGetFileInfoW
107 nip >lo-hi first2 zero? [
109 { 0x5A4D [ +dos-executable+ ] }
110 { 0x4550 [ +win32-console-executable+ ] }
115 { 0x454C [ +win32-vxd-executable+ ] }
116 { 0x454E [ +win32-os2-executable+ ] }
117 { 0x4550 [ +win32-nt-executable+ ] }
123 CONSTANT: path-length $[ MAX_PATH 1 + ]
125 : volume-information ( normalized-path -- volume-name volume-serial max-component flags type )
126 { { ushort path-length } DWORD DWORD DWORD { ushort path-length } }
127 [ [ path-length ] 4dip path-length GetVolumeInformation win32-error=0/f ]
129 [ alien>native-string ] 4dip alien>native-string ;
131 : file-system-space ( normalized-path -- available-space total-space free-space )
132 { ULARGE_INTEGER ULARGE_INTEGER ULARGE_INTEGER }
133 [ GetDiskFreeSpaceEx win32-error=0/f ]
134 with-out-parameters ;
136 : calculate-file-system-info ( file-system-info -- file-system-info' )
137 [ dup [ total-space>> ] [ free-space>> ] bi - >>used-space drop ] keep ;
139 TUPLE: win32-file-system-info < file-system-info-tuple max-component flags device-serial ;
141 ERROR: not-absolute-path ;
143 : root-directory ( string -- string' )
144 unicode-prefix ?head drop
149 } 1&& [ 2 head "\\" append ] [ not-absolute-path ] if ;
153 : (file-system-info) ( path -- file-system-info )
154 dup [ volume-information ] [ file-system-space ] bi
155 \ win32-file-system-info new
158 swap >>available-space
165 calculate-file-system-info ;
169 M: windows file-system-info ( path -- file-system-info )
170 normalize-path root-directory (file-system-info) ;
172 CONSTANT: names-buf-length 16384
174 : find-first-volume ( -- string handle )
175 { { ushort path-length } }
176 [ path-length FindFirstVolume dup win32-error=0/f ]
177 with-out-parameters alien>native-string swap ;
179 : find-next-volume ( handle -- string/f )
180 { { ushort path-length } }
181 [ path-length FindNextVolume ] with-out-parameters
183 GetLastError ERROR_NO_MORE_FILES =
184 [ drop f ] [ win32-error-string throw ] if
185 ] [ alien>native-string ] if ;
187 : find-volumes ( -- array )
191 [ _ find-next-volume dup ] [ ] produce nip
194 ] [ '[ _ FindVolumeClose win32-error=0/f ] ] bi [ ] cleanup ;
196 ! Windows may return a volume which looks up to path ""
197 ! For now, treat it like there is not a volume here
198 : volume>paths ( string -- array )
201 [ ushort malloc-array &free ] keep
203 [ GetVolumePathNamesForVolumeName win32-error=0/f ] 3keep nip
204 uint deref head but-last-slice
205 { 0 } split-slice harvest
206 [ { } ] [ [ { 0 } append alien>native-string ] map ] if-empty
209 ! Can error with T{ windows-error f 21 "The device is not ready." }
210 ! if there is a D: that is not ready, for instance. Ignore these drives.
211 M: windows file-systems ( -- array )
212 find-volumes [ volume>paths ] map concat [
213 [ (file-system-info) ] [ 2drop f ] recover
216 : file-times ( path -- timestamp timestamp timestamp )
218 normalize-path open-read &dispose handle>>
219 { FILETIME FILETIME FILETIME }
220 [ GetFileTime win32-error=0/f ]
222 [ FILETIME>timestamp >local-time ] tri@
225 : set-file-times ( path timestamp/f timestamp/f timestamp/f -- )
226 ! timestamp order: creation access write
229 normalize-path open-existing &dispose handle>>
230 ] 3dip (set-file-times)
233 : set-file-create-time ( path timestamp -- )
236 : set-file-access-time ( path timestamp -- )
237 [ f ] dip f set-file-times ;
239 : set-file-write-time ( path timestamp -- )
240 [ f f ] dip set-file-times ;
242 M: windows file-readable? file-info >boolean ;
243 M: windows file-writable? file-info attributes>> +read-only+ swap member? not ;
244 M: windows file-executable? file-executable-type windows-executable? ;