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 io.files.windows.nt kernel windows.kernel32
5 windows.time windows accessors alien.c-types combinators
6 generalizations system alien.strings io.encodings.utf16n
7 sequences splitting windows.errors fry continuations destructors
8 calendar ascii combinators.short-circuit locals classes.struct
9 specialized-arrays alien.data ;
10 SPECIALIZED-ARRAY: ushort
11 IN: io.files.info.windows
13 :: round-up-to ( n multiple -- n' )
20 TUPLE: windows-file-info < file-info attributes ;
22 : get-compressed-file-size ( path -- n )
23 DWORD <c-object> [ GetCompressedFileSize ] keep
24 over INVALID_FILE_SIZE = [
25 win32-error-string throw
30 : set-windows-size-on-disk ( file-info path -- file-info )
31 over attributes>> +compressed+ swap member? [
32 get-compressed-file-size
34 drop dup size>> 4096 round-up-to
37 : WIN32_FIND_DATA>file-info ( WIN32_FIND_DATA -- file-info )
38 [ \ windows-file-info new ] dip
40 [ dwFileAttributes>> win32-file-type >>type ]
41 [ dwFileAttributes>> win32-file-attributes >>attributes ]
42 [ [ nFileSizeLow>> ] [ nFileSizeHigh>> ] bi >64bit >>size ]
43 [ dwFileAttributes>> >>permissions ]
44 [ ftCreationTime>> FILETIME>timestamp >>created ]
45 [ ftLastWriteTime>> FILETIME>timestamp >>modified ]
46 [ ftLastAccessTime>> FILETIME>timestamp >>accessed ]
49 : find-first-file-stat ( path -- WIN32_FIND_DATA )
50 WIN32_FIND_DATA <struct> [
52 [ INVALID_HANDLE_VALUE = [ win32-error ] when ] keep
53 FindClose win32-error=0/f
56 : BY_HANDLE_FILE_INFORMATION>file-info ( HANDLE_FILE_INFORMATION -- file-info )
57 [ \ windows-file-info new ] dip
59 [ dwFileAttributes>> win32-file-type >>type ]
60 [ dwFileAttributes>> win32-file-attributes >>attributes ]
63 [ nFileSizeHigh>> ] bi >64bit >>size
65 [ dwFileAttributes>> >>permissions ]
66 [ ftCreationTime>> FILETIME>timestamp >>created ]
67 [ ftLastWriteTime>> FILETIME>timestamp >>modified ]
68 [ ftLastAccessTime>> FILETIME>timestamp >>accessed ]
69 ! [ nNumberOfLinks>> ]
72 ! [ nFileIndexHigh>> ] bi >64bit
76 : get-file-information ( handle -- BY_HANDLE_FILE_INFORMATION )
78 BY_HANDLE_FILE_INFORMATION <struct>
79 [ GetFileInformationByHandle win32-error=0/f ] keep
80 ] keep CloseHandle win32-error=0/f ;
82 : get-file-information-stat ( path -- BY_HANDLE_FILE_INFORMATION )
84 GENERIC_READ FILE_SHARE_READ f
85 OPEN_EXISTING FILE_FLAG_BACKUP_SEMANTICS f
86 CreateFileW dup INVALID_HANDLE_VALUE = [
87 drop find-first-file-stat WIN32_FIND_DATA>file-info
90 get-file-information BY_HANDLE_FILE_INFORMATION>file-info
93 M: windows file-info ( path -- info )
95 [ get-file-information-stat ]
96 [ set-windows-size-on-disk ] bi ;
98 M: windows link-info ( path -- info )
101 : volume-information ( normalized-path -- volume-name volume-serial max-component flags type )
102 MAX_PATH 1 + [ <ushort-array> ] keep
106 MAX_PATH 1 + [ <ushort-array> ] keep
107 [ GetVolumeInformation win32-error=0/f ] 7 nkeep
109 [ utf16n alien>string ] 4 ndip
110 utf16n alien>string ;
112 : file-system-space ( normalized-path -- available-space total-space free-space )
113 ULARGE_INTEGER <c-object>
114 ULARGE_INTEGER <c-object>
115 ULARGE_INTEGER <c-object>
116 [ GetDiskFreeSpaceEx win32-error=0/f ] 3keep ;
118 : calculate-file-system-info ( file-system-info -- file-system-info' )
119 [ dup [ total-space>> ] [ free-space>> ] bi - >>used-space drop ] keep ;
121 TUPLE: win32-file-system-info < file-system-info max-component flags device-serial ;
123 ERROR: not-absolute-path ;
125 : root-directory ( string -- string' )
126 unicode-prefix ?head drop
131 } 1&& [ 2 head "\\" append ] [ not-absolute-path ] if ;
135 : (file-system-info) ( path -- file-system-info )
136 dup [ volume-information ] [ file-system-space ] bi
137 \ win32-file-system-info new
138 swap *ulonglong >>free-space
139 swap *ulonglong >>total-space
140 swap *ulonglong >>available-space
143 swap *uint >>max-component
144 swap *uint >>device-serial
147 calculate-file-system-info ;
151 M: winnt file-system-info ( path -- file-system-info )
152 normalize-path root-directory (file-system-info) ;
154 :: volume>paths ( string -- array )
155 16384 :> names-buf-length
156 names-buf-length <ushort-array> :> names
157 0 <uint> :> names-length
159 string names names-buf-length names-length GetVolumePathNamesForVolumeName :> ret
161 ret win32-error-string throw
163 names names-length *uint ushort heap-size * head
164 utf16n alien>string CHAR: \0 split
167 : find-first-volume ( -- string handle )
168 MAX_PATH 1 + [ <ushort-array> ] keep
170 FindFirstVolume dup win32-error=0/f
171 [ utf16n alien>string ] dip ;
173 :: find-next-volume ( handle -- string/f )
174 MAX_PATH 1 + :> buf-length
175 buf-length <ushort-array> :> buf
177 handle buf buf-length FindNextVolume :> ret
179 GetLastError ERROR_NO_MORE_FILES =
180 [ f ] [ win32-error-string throw ] if
182 buf utf16n alien>string
185 : find-volumes ( -- array )
189 [ _ find-next-volume dup ] [ ] produce nip
192 ] [ '[ _ FindVolumeClose win32-error=0/f ] ] bi [ ] cleanup ;
194 M: winnt file-systems ( -- array )
195 find-volumes [ volume>paths ] map
197 [ (file-system-info) ]
198 [ drop \ file-system-info new swap >>mount-point ] recover
201 : file-times ( path -- timestamp timestamp timestamp )
203 normalize-path open-read &dispose handle>>
207 [ GetFileTime win32-error=0/f ] 3keep
208 [ FILETIME>timestamp >local-time ] tri@
211 : set-file-times ( path timestamp/f timestamp/f timestamp/f -- )
212 #! timestamp order: creation access write
215 normalize-path open-existing &dispose handle>>
216 ] 3dip (set-file-times)
219 : set-file-create-time ( path timestamp -- )
222 : set-file-access-time ( path timestamp -- )
223 [ f ] dip f set-file-times ;
225 : set-file-write-time ( path timestamp -- )
226 [ f f ] dip set-file-times ;