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 IN: io.files.info.windows
11 :: round-up-to ( n multiple -- n' )
18 TUPLE: windows-file-info < file-info attributes ;
20 : get-compressed-file-size ( path -- n )
21 "DWORD" <c-object> [ GetCompressedFileSize ] keep
22 over INVALID_FILE_SIZE = [
23 win32-error-string throw
28 : set-windows-size-on-disk ( file-info path -- file-info )
29 over attributes>> +compressed+ swap member? [
30 get-compressed-file-size
32 drop dup size>> 4096 round-up-to
35 : WIN32_FIND_DATA>file-info ( WIN32_FIND_DATA -- file-info )
36 [ \ windows-file-info new ] dip
38 [ WIN32_FIND_DATA-dwFileAttributes win32-file-type >>type ]
39 [ WIN32_FIND_DATA-dwFileAttributes win32-file-attributes >>attributes ]
41 [ WIN32_FIND_DATA-nFileSizeLow ]
42 [ WIN32_FIND_DATA-nFileSizeHigh ] bi >64bit >>size
44 [ WIN32_FIND_DATA-dwFileAttributes >>permissions ]
45 [ WIN32_FIND_DATA-ftCreationTime FILETIME>timestamp >>created ]
46 [ WIN32_FIND_DATA-ftLastWriteTime FILETIME>timestamp >>modified ]
47 [ WIN32_FIND_DATA-ftLastAccessTime FILETIME>timestamp >>accessed ]
50 : find-first-file-stat ( path -- WIN32_FIND_DATA )
51 "WIN32_FIND_DATA" <c-object> [
53 [ INVALID_HANDLE_VALUE = [ win32-error ] when ] keep
54 FindClose win32-error=0/f
57 : BY_HANDLE_FILE_INFORMATION>file-info ( HANDLE_FILE_INFORMATION -- file-info )
58 [ \ windows-file-info new ] dip
60 [ dwFileAttributes>> win32-file-type >>type ]
61 [ dwFileAttributes>> win32-file-attributes >>attributes ]
64 [ nFileSizeHigh>> ] bi >64bit >>size
66 [ dwFileAttributes>> >>permissions ]
67 [ ftCreationTime>> FILETIME>timestamp >>created ]
68 [ ftLastWriteTime>> FILETIME>timestamp >>modified ]
69 [ ftLastAccessTime>> FILETIME>timestamp >>accessed ]
70 ! [ nNumberOfLinks>> ]
73 ! [ nFileIndexHigh>> ] bi >64bit
77 : get-file-information ( handle -- BY_HANDLE_FILE_INFORMATION )
79 BY_HANDLE_FILE_INFORMATION <struct>
80 [ GetFileInformationByHandle win32-error=0/f ] keep
81 ] keep CloseHandle win32-error=0/f ;
83 : get-file-information-stat ( path -- BY_HANDLE_FILE_INFORMATION )
85 GENERIC_READ FILE_SHARE_READ f
86 OPEN_EXISTING FILE_FLAG_BACKUP_SEMANTICS f
87 CreateFileW dup INVALID_HANDLE_VALUE = [
88 drop find-first-file-stat WIN32_FIND_DATA>file-info
91 get-file-information BY_HANDLE_FILE_INFORMATION>file-info
94 M: windows file-info ( path -- info )
96 [ get-file-information-stat ]
97 [ set-windows-size-on-disk ] bi ;
99 M: windows link-info ( path -- info )
102 : volume-information ( normalized-path -- volume-name volume-serial max-component flags type )
103 MAX_PATH 1 + [ <byte-array> ] keep
107 MAX_PATH 1 + [ <byte-array> ] keep
108 [ GetVolumeInformation win32-error=0/f ] 7 nkeep
110 [ utf16n alien>string ] 4 ndip
111 utf16n alien>string ;
113 : file-system-space ( normalized-path -- available-space total-space free-space )
114 "ULARGE_INTEGER" <c-object>
115 "ULARGE_INTEGER" <c-object>
116 "ULARGE_INTEGER" <c-object>
117 [ GetDiskFreeSpaceEx win32-error=0/f ] 3keep ;
119 : calculate-file-system-info ( file-system-info -- file-system-info' )
120 [ dup [ total-space>> ] [ free-space>> ] bi - >>used-space drop ] keep ;
122 TUPLE: win32-file-system-info < file-system-info max-component flags device-serial ;
124 ERROR: not-absolute-path ;
126 : root-directory ( string -- string' )
127 unicode-prefix ?head drop
132 } 1&& [ 2 head "\\" append ] [ not-absolute-path ] if ;
134 M: winnt file-system-info ( path -- file-system-info )
135 normalize-path root-directory
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 ;
149 : volume>paths ( string -- array )
150 16384 "ushort" <c-array> tuck dup length
151 0 <uint> dup [ GetVolumePathNamesForVolumeName 0 = ] dip swap [
152 win32-error-string throw
154 *uint "ushort" heap-size * head
155 utf16n alien>string CHAR: \0 split
158 : find-first-volume ( -- string handle )
159 MAX_PATH 1 + [ <byte-array> ] keep
161 FindFirstVolume dup win32-error=0/f
162 [ utf16n alien>string ] dip ;
164 : find-next-volume ( handle -- string/f )
165 MAX_PATH 1 + [ <byte-array> tuck ] keep
167 GetLastError ERROR_NO_MORE_FILES =
168 [ drop f ] [ win32-error-string throw ] if
173 : find-volumes ( -- array )
177 [ _ find-next-volume dup ] [ ] produce nip
180 ] [ '[ _ FindVolumeClose win32-error=0/f ] ] bi [ ] cleanup ;
182 M: winnt file-systems ( -- array )
183 find-volumes [ volume>paths ] map
186 [ drop \ file-system-info new swap >>mount-point ] recover
189 : file-times ( path -- timestamp timestamp timestamp )
191 normalize-path open-read &dispose handle>>
195 [ GetFileTime win32-error=0/f ] 3keep
196 [ FILETIME>timestamp >local-time ] tri@
199 : set-file-times ( path timestamp/f timestamp/f timestamp/f -- )
200 #! timestamp order: creation access write
203 normalize-path open-existing &dispose handle>>
204 ] 3dip (set-file-times)
207 : set-file-create-time ( path timestamp -- )
210 : set-file-access-time ( path timestamp -- )
211 [ f ] dip f set-file-times ;
213 : set-file-write-time ( path timestamp -- )
214 [ f f ] dip set-file-times ;