]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/info/windows/windows.factor
cleanup some QUALIFIED: that are no longer needed.
[factor.git] / basis / io / files / info / windows / windows.factor
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
13
14 :: round-up-to ( n multiple -- n' )
15     n multiple rem [
16         n
17     ] [
18         multiple swap - n +
19     ] if-zero ;
20
21 TUPLE: windows-file-info < file-info-tuple attributes ;
22
23 : get-compressed-file-size ( path -- n )
24     { DWORD } [ GetCompressedFileSize ] with-out-parameters
25     over INVALID_FILE_SIZE = [ win32-error-string throw ] [ >64bit ] if ;
26
27 : set-windows-size-on-disk ( file-info path -- file-info )
28     over attributes>> +compressed+ swap member? [
29         get-compressed-file-size
30     ] [
31         drop dup size>> 4096 round-up-to
32     ] if >>size-on-disk ;
33
34 : WIN32_FIND_DATA>file-info ( WIN32_FIND_DATA -- file-info )
35     [ \ windows-file-info new ] dip
36     {
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 ]
44     } cleave ;
45
46 : find-first-file-stat ( path -- WIN32_FIND_DATA )
47     WIN32_FIND_DATA <struct> [
48         FindFirstFile
49         [ INVALID_HANDLE_VALUE = [ win32-error ] when ] keep
50         FindClose win32-error=0/f
51     ] keep ;
52
53 : BY_HANDLE_FILE_INFORMATION>file-info ( HANDLE_FILE_INFORMATION -- file-info )
54     [ \ windows-file-info new ] dip
55     {
56         [ dwFileAttributes>> win32-file-type >>type ]
57         [ dwFileAttributes>> win32-file-attributes >>attributes ]
58         [
59             [ nFileSizeLow>> ]
60             [ nFileSizeHigh>> ] bi >64bit >>size
61         ]
62         [ dwFileAttributes>> >>permissions ]
63         [ ftCreationTime>> FILETIME>timestamp >>created ]
64         [ ftLastWriteTime>> FILETIME>timestamp >>modified ]
65         [ ftLastAccessTime>> FILETIME>timestamp >>accessed ]
66         ! [ nNumberOfLinks>> ]
67         ! [
68           ! [ nFileIndexLow>> ]
69           ! [ nFileIndexHigh>> ] bi >64bit
70         ! ]
71     } cleave ;
72
73 : get-file-information ( handle -- BY_HANDLE_FILE_INFORMATION )
74     [
75         BY_HANDLE_FILE_INFORMATION <struct>
76         [ GetFileInformationByHandle win32-error=0/f ] keep
77     ] keep CloseHandle win32-error=0/f ;
78
79 : get-file-information-stat ( path -- BY_HANDLE_FILE_INFORMATION )
80     dup
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
85     ] [
86         nip
87         get-file-information BY_HANDLE_FILE_INFORMATION>file-info
88     ] if ;
89
90 M: windows file-info ( path -- info )
91     normalize-path
92     [ get-file-information-stat ]
93     [ set-windows-size-on-disk ] bi ;
94
95 M: windows link-info ( path -- info )
96     file-info ;
97
98 : file-executable-type ( path -- executable/f )
99     normalize-path dup
100     0
101     f
102     ! hi is zero means old style executable
103     0 SHGFI_EXETYPE SHGetFileInfoW
104     [
105         file-info drop f
106     ] [
107         nip >lo-hi first2 zero? [
108             {
109                 { 0x5A4D [ +dos-executable+ ] }
110                 { 0x4550 [ +win32-console-executable+ ] }
111                 [ drop f ]
112             } case
113         ] [
114             {
115                 { 0x454C [ +win32-vxd-executable+ ] }
116                 { 0x454E [ +win32-os2-executable+ ] }
117                 { 0x4550 [ +win32-nt-executable+ ] }
118                 [ drop f ]
119             } case
120         ] if
121     ] if-zero ;
122
123 CONSTANT: path-length $[ MAX_PATH 1 + ]
124
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 ]
128     with-out-parameters
129     [ alien>native-string ] 4dip alien>native-string ;
130
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 ;
135
136 : calculate-file-system-info ( file-system-info -- file-system-info' )
137     [ dup [ total-space>> ] [ free-space>> ] bi - >>used-space drop ] keep ;
138
139 TUPLE: win32-file-system-info < file-system-info-tuple max-component flags device-serial ;
140
141 ERROR: not-absolute-path ;
142
143 : root-directory ( string -- string' )
144     unicode-prefix ?head drop
145     dup {
146         [ length 2 >= ]
147         [ second CHAR: : = ]
148         [ first Letter? ]
149     } 1&& [ 2 head "\\" append ] [ not-absolute-path ] if ;
150
151 <PRIVATE
152
153 : (file-system-info) ( path -- file-system-info )
154     dup [ volume-information ] [ file-system-space ] bi
155     \ win32-file-system-info new
156         swap >>free-space
157         swap >>total-space
158         swap >>available-space
159         swap >>type
160         swap >>flags
161         swap >>max-component
162         swap >>device-serial
163         swap >>device-name
164         swap >>mount-point
165     calculate-file-system-info ;
166
167 PRIVATE>
168
169 M: windows file-system-info ( path -- file-system-info )
170     normalize-path root-directory (file-system-info) ;
171
172 CONSTANT: names-buf-length 16384
173
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 ;
178
179 : find-next-volume ( handle -- string/f )
180     { { ushort path-length } }
181     [ path-length FindNextVolume ] with-out-parameters
182     swap 0 = [
183         GetLastError ERROR_NO_MORE_FILES =
184         [ drop f ] [ win32-error-string throw ] if
185     ] [ alien>native-string ] if ;
186
187 : find-volumes ( -- array )
188     find-first-volume
189     [
190         '[
191             [ _ find-next-volume dup ] [ ] produce nip
192             swap prefix
193         ]
194     ] [ '[ _ FindVolumeClose win32-error=0/f ] ] bi [ ] cleanup ;
195
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 )
199     [
200         names-buf-length
201         [ ushort malloc-array &free ] keep
202         0 uint <ref>
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
207     ] with-destructors ;
208
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
214     ] map sift ;
215
216 : file-times ( path -- timestamp timestamp timestamp )
217     [
218         normalize-path open-read &dispose handle>>
219         { FILETIME FILETIME FILETIME }
220         [ GetFileTime win32-error=0/f ]
221         with-out-parameters
222         [ FILETIME>timestamp >local-time ] tri@
223     ] with-destructors ;
224
225 : set-file-times ( path timestamp/f timestamp/f timestamp/f -- )
226     ! timestamp order: creation access write
227     [
228         [
229             normalize-path open-existing &dispose handle>>
230         ] 3dip (set-file-times)
231     ] with-destructors ;
232
233 : set-file-create-time ( path timestamp -- )
234     f f set-file-times ;
235
236 : set-file-access-time ( path timestamp -- )
237     [ f ] dip f set-file-times ;
238
239 : set-file-write-time ( path timestamp -- )
240     [ f f ] dip set-file-times ;
241
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? ;