]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/info/unix/unix.factor
use radix literals
[factor.git] / basis / io / files / info / unix / unix.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 arrays calendar
4 calendar.unix classes.struct combinators
5 combinators.short-circuit io.backend io.directories
6 io.files.info io.files.types kernel literals math math.bitwise
7 sequences specialized-arrays strings system unix unix.ffi
8 unix.groups unix.stat unix.time unix.users vocabs ;
9 IN: io.files.info.unix
10 SPECIALIZED-ARRAY: timeval
11
12 TUPLE: unix-file-system-info < file-system-info
13 block-size preferred-block-size
14 blocks blocks-free blocks-available
15 files files-free files-available
16 name-max flags id ;
17
18 HOOK: new-file-system-info os ( --  file-system-info )
19
20 M: unix new-file-system-info unix-file-system-info new ;
21
22 HOOK: file-system-statfs os ( path -- statfs )
23
24 M: unix file-system-statfs drop f ;
25
26 HOOK: file-system-statvfs os ( path -- statvfs )
27
28 M: unix file-system-statvfs drop f ;
29
30 HOOK: statfs>file-system-info os ( file-system-info statfs -- file-system-info' )
31
32 M: unix statfs>file-system-info drop ;
33
34 HOOK: statvfs>file-system-info os ( file-system-info statvfs -- file-system-info' )
35
36 M: unix statvfs>file-system-info drop ;
37
38 : file-system-calculations ( file-system-info -- file-system-info' )
39     dup [ blocks-available>> ] [ block-size>> ] bi * >>available-space
40     dup [ blocks-free>> ] [ block-size>> ] bi * >>free-space
41     dup [ blocks>> ] [ block-size>> ] bi * >>total-space
42     dup [ total-space>> ] [ free-space>> ] bi - >>used-space ;
43
44 M: unix file-system-info
45     normalize-path
46     [ new-file-system-info ] dip
47     [ file-system-statfs statfs>file-system-info ]
48     [ file-system-statvfs statvfs>file-system-info ] bi
49     file-system-calculations ;
50
51 TUPLE: unix-file-info < file-info uid gid dev ino
52 nlink rdev blocks blocksize ;
53
54 HOOK: new-file-info os ( -- file-info )
55
56 HOOK: stat>file-info os ( stat -- file-info )
57
58 HOOK: stat>type os ( stat -- file-info )
59
60 M: unix file-info ( path -- info )
61     normalize-path file-status stat>file-info ;
62
63 M: unix link-info ( path -- info )
64     normalize-path link-status stat>file-info ;
65
66 M: unix new-file-info ( -- class ) unix-file-info new ;
67
68 CONSTANT: standard-unix-block-size 512
69
70 M: unix stat>file-info ( stat -- file-info )
71     [ new-file-info ] dip
72     {
73         [ stat>type >>type ]
74         [ st_size>> >>size ]
75         [ st_mode>> >>permissions ]
76         [ st_ctimespec>> timespec>unix-time >>created ]
77         [ st_mtimespec>> timespec>unix-time >>modified ]
78         [ st_atimespec>> timespec>unix-time >>accessed ]
79         [ st_uid>> >>uid ]
80         [ st_gid>> >>gid ]
81         [ st_dev>> >>dev ]
82         [ st_ino>> >>ino ]
83         [ st_nlink>> >>nlink ]
84         [ st_rdev>> >>rdev ]
85         [ st_blocks>> >>blocks ]
86         [ st_blksize>> >>blocksize ]
87         [ drop dup blocks>> standard-unix-block-size * >>size-on-disk ]
88     } cleave ;
89
90 : n>file-type ( n -- type )
91     S_IFMT bitand {
92         { S_IFREG [ +regular-file+ ] }
93         { S_IFDIR [ +directory+ ] }
94         { S_IFCHR [ +character-device+ ] }
95         { S_IFBLK [ +block-device+ ] }
96         { S_IFIFO [ +fifo+ ] }
97         { S_IFLNK [ +symbolic-link+ ] }
98         { S_IFSOCK [ +socket+ ] }
99         [ drop +unknown+ ]
100     } case ;
101
102 M: unix stat>type ( stat -- type )
103     st_mode>> n>file-type ;
104
105 <PRIVATE
106
107 : stat-mode ( path -- mode )
108     normalize-path file-status st_mode>> ;
109
110 : chmod-set-bit ( path mask ? -- )
111     [ dup stat-mode ] 2dip
112     [ bitor ] [ unmask ] if [ chmod ] unix-system-call drop ;
113
114 GENERIC# file-mode? 1 ( obj mask -- ? )
115
116 M: integer file-mode? mask? ;
117 M: string file-mode? [ stat-mode ] dip mask? ;
118 M: file-info file-mode? [ permissions>> ] dip mask? ;
119
120 PRIVATE>
121
122 CONSTANT: UID           0o0004000
123 CONSTANT: GID           0o0002000
124 CONSTANT: STICKY        0o0001000
125 CONSTANT: USER-ALL      0o0000700
126 CONSTANT: USER-READ     0o0000400
127 CONSTANT: USER-WRITE    0o0000200
128 CONSTANT: USER-EXECUTE  0o0000100
129 CONSTANT: GROUP-ALL     0o0000070
130 CONSTANT: GROUP-READ    0o0000040
131 CONSTANT: GROUP-WRITE   0o0000020
132 CONSTANT: GROUP-EXECUTE 0o0000010
133 CONSTANT: OTHER-ALL     0o0000007
134 CONSTANT: OTHER-READ    0o0000004
135 CONSTANT: OTHER-WRITE   0o0000002
136 CONSTANT: OTHER-EXECUTE 0o0000001
137 CONSTANT: ALL-READ      0o0000444
138 CONSTANT: ALL-WRITE     0o0000222
139 CONSTANT: ALL-EXECUTE   0o0000111
140
141 : uid? ( obj -- ? ) UID file-mode? ;
142 : gid? ( obj -- ? ) GID file-mode? ;
143 : sticky? ( obj -- ? ) STICKY file-mode? ;
144 : user-read? ( obj -- ? ) USER-READ file-mode? ;
145 : user-write? ( obj -- ? ) USER-WRITE file-mode? ;
146 : user-execute? ( obj -- ? ) USER-EXECUTE file-mode? ;
147 : group-read? ( obj -- ? ) GROUP-READ file-mode? ;
148 : group-write? ( obj -- ? ) GROUP-WRITE file-mode? ;
149 : group-execute? ( obj -- ? ) GROUP-EXECUTE file-mode? ;
150 : other-read? ( obj -- ? ) OTHER-READ file-mode? ;
151 : other-write? ( obj -- ? ) OTHER-WRITE file-mode? ;
152 : other-execute? ( obj -- ? ) OTHER-EXECUTE file-mode? ;
153
154 : any-read? ( obj -- ? )
155     { [ user-read? ] [ group-read? ] [ other-read? ] } 1|| ;
156
157 : any-write? ( obj -- ? )
158     { [ user-write? ] [ group-write? ] [ other-write? ] } 1|| ;
159
160 : any-execute? ( obj -- ? )
161     { [ user-execute? ] [ group-execute? ] [ other-execute? ] } 1|| ;
162
163 : set-uid ( path ? -- ) UID swap chmod-set-bit ;
164 : set-gid ( path ? -- ) GID swap chmod-set-bit ;
165 : set-sticky ( path ? -- ) STICKY swap chmod-set-bit ;
166 : set-user-read ( path ? -- ) USER-READ swap chmod-set-bit ;
167 : set-user-write ( path ? -- ) USER-WRITE swap chmod-set-bit ;
168 : set-user-execute ( path ? -- ) USER-EXECUTE swap chmod-set-bit ;
169 : set-group-read ( path ? -- ) GROUP-READ swap chmod-set-bit ;
170 : set-group-write ( path ? -- ) GROUP-WRITE swap chmod-set-bit ;
171 : set-group-execute ( path ? -- ) GROUP-EXECUTE swap chmod-set-bit ;
172 : set-other-read ( path ? -- ) OTHER-READ swap chmod-set-bit ;
173 : set-other-write ( path ? -- ) OTHER-WRITE swap chmod-set-bit ;
174 : set-other-execute ( path ? -- ) OTHER-EXECUTE swap chmod-set-bit ;
175
176 : set-file-permissions ( path n -- )
177     [ normalize-path ] dip [ chmod ] unix-system-call drop ;
178
179 : file-permissions ( path -- n )
180     normalize-path file-info permissions>> ;
181
182 : add-file-permissions ( path n -- )
183     over file-permissions bitor set-file-permissions ;
184
185 : remove-file-permissions ( path n -- )
186     over file-permissions [ bitnot ] dip bitand set-file-permissions ;
187
188 M: unix copy-file-and-info ( from to -- )
189     [ copy-file ] [ swap file-permissions set-file-permissions ] 2bi ;
190
191 <PRIVATE
192
193 : timestamp>timeval ( timestamp -- timeval )
194     unix-1970 time- duration>microseconds make-timeval ;
195
196 : timestamps>byte-array ( timestamps -- byte-array )
197     [ [ timestamp>timeval ] [ \ timeval <struct> ] if* ] map
198     timeval >c-array ;
199
200 PRIVATE>
201
202 : set-file-times ( path timestamps -- )
203     #! set access, write
204     [ normalize-path ] dip
205     timestamps>byte-array [ utimes ] unix-system-call drop ;
206
207 : set-file-access-time ( path timestamp -- )
208     f 2array set-file-times ;
209
210 : set-file-modified-time ( path timestamp -- )
211     f swap 2array set-file-times ;
212
213 : set-file-ids ( path uid gid -- )
214     [ normalize-path ] 2dip [ -1 or ] bi@
215     [ chown ] unix-system-call drop ;
216
217 GENERIC: set-file-user ( path string/id -- )
218
219 GENERIC: set-file-group ( path string/id -- )
220
221 M: integer set-file-user ( path uid -- )
222     f set-file-ids ;
223
224 M: string set-file-user ( path string -- )
225     user-id f set-file-ids ;
226
227 M: integer set-file-group ( path gid -- )
228     f swap set-file-ids ;
229
230 M: string set-file-group ( path string -- )
231     group-id
232     f swap set-file-ids ;
233
234 : file-user-id ( path -- uid )
235     normalize-path file-info uid>> ;
236
237 : file-user-name ( path -- string )
238     file-user-id user-name ;
239
240 : file-group-id ( path -- gid )
241     normalize-path file-info gid>> ;
242
243 : file-group-name ( path -- string )
244     file-group-id group-name ;
245
246 : ch>file-type ( ch -- type )
247     {
248         { CHAR: b [ +block-device+ ] }
249         { CHAR: c [ +character-device+ ] }
250         { CHAR: d [ +directory+ ] }
251         { CHAR: l [ +symbolic-link+ ] }
252         { CHAR: s [ +socket+ ] }
253         { CHAR: p [ +fifo+ ] }
254         { CHAR: - [ +regular-file+ ] }
255         [ drop +unknown+ ]
256     } case ;
257
258 : file-type>ch ( type -- ch )
259     {
260         { +block-device+ [ CHAR: b ] }
261         { +character-device+ [ CHAR: c ] }
262         { +directory+ [ CHAR: d ] }
263         { +symbolic-link+ [ CHAR: l ] }
264         { +socket+ [ CHAR: s ] }
265         { +fifo+ [ CHAR: p ] }
266         { +regular-file+ [ CHAR: - ] }
267         [ drop CHAR: - ]
268     } case ;
269
270 <PRIVATE
271
272 : file-type>executable ( directory-entry -- string )
273     name>> any-execute? "*" "" ? ;
274
275 PRIVATE>
276
277 : file-type>trailing ( directory-entry -- string )
278     dup type>>
279     {
280         { +directory+ [ drop "/" ] }
281         { +symbolic-link+ [ drop "@" ] }
282         { +fifo+ [ drop "|" ] }
283         { +socket+ [ drop "=" ] }
284         { +whiteout+ [ drop "%" ] }
285         { +unknown+ [ file-type>executable ] }
286         { +regular-file+ [ file-type>executable ] }
287         [ drop file-type>executable ]
288     } case ;
289
290 "io.files.info.unix." os name>> append require