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
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 <ushort-array> tuck dup length
156 0 <uint> dup [ GetVolumePathNamesForVolumeName 0 = ] dip swap [
157 win32-error-string throw
159 *uint "ushort" heap-size * head
160 utf16n alien>string CHAR: \0 split
163 : find-first-volume ( -- string handle )
164 MAX_PATH 1 + [ <ushort-array> ] keep
166 FindFirstVolume dup win32-error=0/f
167 [ utf16n alien>string ] dip ;
169 : find-next-volume ( handle -- string/f )
170 MAX_PATH 1 + [ <ushort-array> tuck ] keep
172 GetLastError ERROR_NO_MORE_FILES =
173 [ drop f ] [ win32-error-string throw ] if
178 : find-volumes ( -- array )
182 [ _ find-next-volume dup ] [ ] produce nip
185 ] [ '[ _ FindVolumeClose win32-error=0/f ] ] bi [ ] cleanup ;
187 M: winnt file-systems ( -- array )
188 find-volumes [ volume>paths ] map
190 [ (file-system-info) ]
191 [ drop \ file-system-info new swap >>mount-point ] recover
194 : file-times ( path -- timestamp timestamp timestamp )
196 normalize-path open-read &dispose handle>>
200 [ GetFileTime win32-error=0/f ] 3keep
201 [ FILETIME>timestamp >local-time ] tri@
204 : set-file-times ( path timestamp/f timestamp/f timestamp/f -- )
205 #! timestamp order: creation access write
208 normalize-path open-existing &dispose handle>>
209 ] 3dip (set-file-times)
212 : set-file-create-time ( path timestamp -- )
215 : set-file-access-time ( path timestamp -- )
216 [ f ] dip f set-file-times ;
218 : set-file-write-time ( path timestamp -- )
219 [ f f ] dip set-file-times ;