]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/info/windows/windows.factor
99eccd1c0f7c94da2bee3515a2c3a3acaf3cdfb0
[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: accessors alien.c-types alien.data alien.strings ascii
4 calendar classes.struct combinators combinators.short-circuit
5 continuations destructors fry io.backend io.files.info
6 io.files.windows kernel libc literals locals math sequences
7 splitting system windows.errors windows.kernel32 windows.shell32
8 windows.time windows.types ;
9 IN: io.files.info.windows
10
11 :: round-up-to ( n multiple -- n' )
12     n multiple rem [
13         n
14     ] [
15         multiple swap - n +
16     ] if-zero ;
17
18 TUPLE: windows-file-info < file-info-tuple attributes ;
19
20 : get-compressed-file-size ( path -- n )
21     { DWORD } [ GetCompressedFileSize ] with-out-parameters
22     over INVALID_FILE_SIZE = [ win32-error ] [ >64bit ] if ;
23
24 : set-windows-size-on-disk ( file-info path -- file-info )
25     over attributes>> +compressed+ swap member? [
26         get-compressed-file-size
27     ] [
28         drop dup size>> 4096 round-up-to
29     ] if >>size-on-disk ;
30
31 : WIN32_FIND_DATA>file-info ( WIN32_FIND_DATA -- file-info )
32     [ \ windows-file-info new ] dip
33     {
34         [ dwFileAttributes>> win32-file-type >>type ]
35         [ dwFileAttributes>> win32-file-attributes >>attributes ]
36         [ [ nFileSizeLow>> ] [ nFileSizeHigh>> ] bi >64bit >>size ]
37         [ dwFileAttributes>> >>permissions ]
38         [ ftCreationTime>> FILETIME>timestamp >>created ]
39         [ ftLastWriteTime>> FILETIME>timestamp >>modified ]
40         [ ftLastAccessTime>> FILETIME>timestamp >>accessed ]
41     } cleave ;
42
43 : find-first-file-stat ( path -- WIN32_FIND_DATA )
44     WIN32_FIND_DATA <struct> [
45         FindFirstFile check-invalid-handle
46         FindClose win32-error=0/f
47     ] keep ;
48
49 : BY_HANDLE_FILE_INFORMATION>file-info ( HANDLE_FILE_INFORMATION -- file-info )
50     [ \ windows-file-info new ] dip
51     {
52         [ dwFileAttributes>> win32-file-type >>type ]
53         [ dwFileAttributes>> win32-file-attributes >>attributes ]
54         [
55             [ nFileSizeLow>> ]
56             [ nFileSizeHigh>> ] bi >64bit >>size
57         ]
58         [ dwFileAttributes>> >>permissions ]
59         [ ftCreationTime>> FILETIME>timestamp >>created ]
60         [ ftLastWriteTime>> FILETIME>timestamp >>modified ]
61         [ ftLastAccessTime>> FILETIME>timestamp >>accessed ]
62         ! [ nNumberOfLinks>> ]
63         ! [
64           ! [ nFileIndexLow>> ]
65           ! [ nFileIndexHigh>> ] bi >64bit
66         ! ]
67     } cleave ;
68
69 : get-file-information ( handle -- BY_HANDLE_FILE_INFORMATION )
70     [
71         BY_HANDLE_FILE_INFORMATION <struct>
72         [ GetFileInformationByHandle win32-error=0/f ] keep
73     ] keep CloseHandle win32-error=0/f ;
74
75 : valid-handle? ( handle -- boolean )
76     INVALID_HANDLE_VALUE = not ; inline
77
78 : open-read-handle ( path -- handle/f )
79     ! Parameters of CreateFileW here should match those in open-read.
80     GENERIC_READ share-mode f
81     OPEN_EXISTING 0 CreateFile-flags f
82     CreateFileW [ valid-handle? ] keep f ? ;
83
84 : get-file-information-stat ( path -- file-info )
85     dup open-read-handle dup [
86         nip
87         get-file-information BY_HANDLE_FILE_INFORMATION>file-info
88     ] [
89         drop find-first-file-stat WIN32_FIND_DATA>file-info
90     ] if ;
91
92 M: windows file-info ( path -- info )
93     normalize-path
94     [ get-file-information-stat ]
95     [ set-windows-size-on-disk ] bi ;
96
97 M: windows link-info ( path -- info )
98     file-info ;
99
100 : file-executable-type ( path -- executable/f )
101     normalize-path dup
102     0
103     f
104     ! hi is zero means old style executable
105     0 SHGFI_EXETYPE SHGetFileInfoW
106     [
107         file-info drop f
108     ] [
109         nip >lo-hi first2 zero? [
110             {
111                 { 0x5A4D [ +dos-executable+ ] }
112                 { 0x4550 [ +win32-console-executable+ ] }
113                 [ drop f ]
114             } case
115         ] [
116             {
117                 { 0x454C [ +win32-vxd-executable+ ] }
118                 { 0x454E [ +win32-os2-executable+ ] }
119                 { 0x4550 [ +win32-nt-executable+ ] }
120                 [ drop f ]
121             } case
122         ] if
123     ] if-zero ;
124
125 CONSTANT: path-length $[ MAX_PATH 1 + ]
126
127 : volume-information ( normalized-path -- volume-name volume-serial max-component flags type )
128     { { ushort path-length } DWORD DWORD DWORD { ushort path-length } }
129     [ [ path-length ] 4dip path-length GetVolumeInformation win32-error=0/f ]
130     with-out-parameters
131     [ alien>native-string ] 4dip alien>native-string ;
132
133 : file-system-space ( normalized-path -- available-space total-space free-space )
134     { ULARGE_INTEGER ULARGE_INTEGER ULARGE_INTEGER }
135     [ GetDiskFreeSpaceEx win32-error=0/f ]
136     with-out-parameters ;
137
138 : calculate-file-system-info ( file-system-info -- file-system-info' )
139     [ dup [ total-space>> ] [ free-space>> ] bi - >>used-space drop ] keep ;
140
141 TUPLE: win32-file-system-info < file-system-info-tuple max-component flags device-serial ;
142
143 ERROR: not-absolute-path ;
144
145 : root-directory ( string -- string' )
146     unicode-prefix ?head drop
147     dup {
148         [ length 2 >= ]
149         [ second CHAR: : = ]
150         [ first Letter? ]
151     } 1&& [ 2 head "\\" append ] [ not-absolute-path ] if ;
152
153 <PRIVATE
154
155 : (file-system-info) ( path -- file-system-info )
156     dup [ volume-information ] [ file-system-space ] bi
157     \ win32-file-system-info new
158         swap >>free-space
159         swap >>total-space
160         swap >>available-space
161         swap >>type
162         swap >>flags
163         swap >>max-component
164         swap >>device-serial
165         swap >>device-name
166         swap >>mount-point
167     calculate-file-system-info ;
168
169 PRIVATE>
170
171 M: windows file-system-info ( path -- file-system-info )
172     normalize-path root-directory (file-system-info) ;
173
174 CONSTANT: names-buf-length 16384
175
176 : find-first-volume ( -- string handle )
177     { { ushort path-length } }
178     [ path-length FindFirstVolume dup win32-error=0/f ]
179     with-out-parameters alien>native-string swap ;
180
181 : find-next-volume ( handle -- string/f )
182     { { ushort path-length } }
183     [ path-length FindNextVolume ] with-out-parameters
184     swap 0 = [
185         GetLastError ERROR_NO_MORE_FILES =
186         [ drop f ] [ win32-error ] if
187     ] [ alien>native-string ] if ;
188
189 : find-volumes ( -- array )
190     find-first-volume
191     [
192         '[
193             [ _ find-next-volume dup ] [ ] produce nip
194             swap prefix
195         ]
196     ] [ '[ _ FindVolumeClose win32-error=0/f ] ] bi finally ;
197
198 ! Windows may return a volume which looks up to path ""
199 ! For now, treat it like there is not a volume here
200 : (volume>paths) ( string -- array )
201     [
202         names-buf-length
203         [ ushort malloc-array &free ] keep
204         0 uint <ref>
205         [ GetVolumePathNamesForVolumeName win32-error=0/f ] 3keep nip
206         uint deref head but-last-slice
207         { 0 } split-slice harvest
208         [ { } ] [ [ { 0 } append alien>native-string ] map ] if-empty
209     ] with-destructors ;
210
211 ! Suppress T{ windows-error f 2 "The system cannot find the file specified." }
212 : volume>paths ( string -- array/f )
213     '[ _ (volume>paths) ] [
214         { [ windows-error? ] [ n>> ERROR_FILE_NOT_FOUND = ] } 1&&
215     ] ignore-error/f ;
216
217 ! Can error with T{ windows-error f 21 "The device is not ready." }
218 ! if there is a D: that is not ready, for instance. Ignore these drives.
219 M: windows file-systems ( -- array )
220     find-volumes [ volume>paths ] map concat [
221         [ (file-system-info) ] [ 2drop f ] recover
222     ] map sift ;
223
224 : file-times ( path -- timestamp timestamp timestamp )
225     [
226         normalize-path open-read &dispose handle>>
227         { FILETIME FILETIME FILETIME }
228         [ GetFileTime win32-error=0/f ]
229         with-out-parameters
230         [ FILETIME>timestamp >local-time ] tri@
231     ] with-destructors ;
232
233 : set-file-times ( path timestamp/f timestamp/f timestamp/f -- )
234     ! timestamp order: creation access write
235     [
236         [
237             normalize-path open-r/w &dispose handle>>
238         ] 3dip (set-file-times)
239     ] with-destructors ;
240
241 : set-file-create-time ( path timestamp -- )
242     f f set-file-times ;
243
244 : set-file-access-time ( path timestamp -- )
245     [ f ] dip f set-file-times ;
246
247 : set-file-write-time ( path timestamp -- )
248     [ f f ] dip set-file-times ;
249
250 M: windows file-readable?
251     normalize-path open-read-handle
252     dup [ CloseHandle win32-error=0/f ] when* >boolean ;
253
254 M: windows file-writable? file-info attributes>> +read-only+ swap member? not ;
255 M: windows file-executable? file-executable-type windows-executable? ;