]> gitweb.factorcode.org Git - factor.git/blob - basis/io/files/info/unix/unix.factor
Merge commit 'origin/master'
[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 kernel system math math.bitwise strings arrays
4 sequences combinators combinators.short-circuit alien.c-types
5 vocabs.loader calendar calendar.unix io.files.info
6 io.files.types io.backend unix unix.stat unix.time unix.users
7 unix.groups ;
8 IN: io.files.info.unix
9
10 TUPLE: unix-file-system-info < file-system-info
11 block-size preferred-block-size
12 blocks blocks-free blocks-available
13 files files-free files-available
14 name-max flags id ;
15
16 HOOK: new-file-system-info os ( --  file-system-info )
17
18 M: unix new-file-system-info ( -- ) unix-file-system-info new ;
19
20 HOOK: file-system-statfs os ( path -- statfs )
21
22 M: unix file-system-statfs drop f ;
23
24 HOOK: file-system-statvfs os ( path -- statvfs )
25
26 M: unix file-system-statvfs drop f ;
27
28 HOOK: statfs>file-system-info os ( file-system-info statfs -- file-system-info' )
29
30 M: unix statfs>file-system-info drop ;
31
32 HOOK: statvfs>file-system-info os ( file-system-info statvfs -- file-system-info' )
33
34 M: unix statvfs>file-system-info drop ;
35
36 : file-system-calculations ( file-system-info -- file-system-info' )
37     dup [ blocks-available>> ] [ block-size>> ] bi * >>available-space
38     dup [ blocks-free>> ] [ block-size>> ] bi * >>free-space
39     dup [ blocks>> ] [ block-size>> ] bi * >>total-space
40     dup [ total-space>> ] [ free-space>> ] bi - >>used-space ;
41
42 M: unix file-system-info
43     normalize-path
44     [ new-file-system-info ] dip
45     [ file-system-statfs statfs>file-system-info ]
46     [ file-system-statvfs statvfs>file-system-info ] bi
47     file-system-calculations ;
48
49 TUPLE: unix-file-info < file-info uid gid dev ino
50 nlink rdev blocks blocksize ;
51
52 HOOK: new-file-info os ( -- file-info )
53
54 HOOK: stat>file-info os ( stat -- file-info )
55
56 HOOK: stat>type os ( stat -- file-info )
57
58 M: unix file-info ( path -- info )
59     normalize-path file-status stat>file-info ;
60
61 M: unix link-info ( path -- info )
62     normalize-path link-status stat>file-info ;
63
64 M: unix new-file-info ( -- class ) unix-file-info new ;
65
66 M: unix stat>file-info ( stat -- file-info )
67     [ new-file-info ] dip
68     {
69         [ stat>type >>type ]
70         [ stat-st_size >>size ]
71         [ stat-st_mode >>permissions ]
72         [ stat-st_ctimespec timespec>unix-time >>created ]
73         [ stat-st_mtimespec timespec>unix-time >>modified ]
74         [ stat-st_atimespec timespec>unix-time >>accessed ]
75         [ stat-st_uid >>uid ]
76         [ stat-st_gid >>gid ]
77         [ stat-st_dev >>dev ]
78         [ stat-st_ino >>ino ]
79         [ stat-st_nlink >>nlink ]
80         [ stat-st_rdev >>rdev ]
81         [ stat-st_blocks >>blocks ]
82         [ stat-st_blksize >>blocksize ]
83     } cleave ;
84
85 : n>file-type ( n -- type )
86     S_IFMT bitand {
87         { S_IFREG [ +regular-file+ ] }
88         { S_IFDIR [ +directory+ ] }
89         { S_IFCHR [ +character-device+ ] }
90         { S_IFBLK [ +block-device+ ] }
91         { S_IFIFO [ +fifo+ ] }
92         { S_IFLNK [ +symbolic-link+ ] }
93         { S_IFSOCK [ +socket+ ] }
94         [ drop +unknown+ ]
95     } case ;
96
97 M: unix stat>type ( stat -- type )
98     stat-st_mode n>file-type ;
99
100 <PRIVATE
101
102 : stat-mode ( path -- mode )
103     normalize-path file-status stat-st_mode ;
104
105 : chmod-set-bit ( path mask ? -- )
106     [ dup stat-mode ] 2dip
107     [ bitor ] [ unmask ] if chmod io-error ;
108
109 GENERIC# file-mode? 1 ( obj mask -- ? )
110
111 M: integer file-mode? mask? ;
112 M: string file-mode? [ stat-mode ] dip mask? ;
113 M: file-info file-mode? [ permissions>> ] dip mask? ;
114
115 PRIVATE>
116
117 : ch>file-type ( ch -- type )
118     {
119         { CHAR: b [ +block-device+ ] }
120         { CHAR: c [ +character-device+ ] }
121         { CHAR: d [ +directory+ ] }
122         { CHAR: l [ +symbolic-link+ ] }
123         { CHAR: s [ +socket+ ] }
124         { CHAR: p [ +fifo+ ] }
125         { CHAR: - [ +regular-file+ ] }
126         [ drop +unknown+ ]
127     } case ;
128
129 : file-type>ch ( type -- string )
130     {
131         { +block-device+ [ CHAR: b ] }
132         { +character-device+ [ CHAR: c ] }
133         { +directory+ [ CHAR: d ] }
134         { +symbolic-link+ [ CHAR: l ] }
135         { +socket+ [ CHAR: s ] }
136         { +fifo+ [ CHAR: p ] }
137         { +regular-file+ [ CHAR: - ] }
138         [ drop CHAR: - ]
139     } case ;
140
141 : UID           OCT: 0004000 ; inline
142 : GID           OCT: 0002000 ; inline
143 : STICKY        OCT: 0001000 ; inline
144 : USER-ALL      OCT: 0000700 ; inline
145 : USER-READ     OCT: 0000400 ; inline
146 : USER-WRITE    OCT: 0000200 ; inline
147 : USER-EXECUTE  OCT: 0000100 ; inline
148 : GROUP-ALL     OCT: 0000070 ; inline
149 : GROUP-READ    OCT: 0000040 ; inline
150 : GROUP-WRITE   OCT: 0000020 ; inline
151 : GROUP-EXECUTE OCT: 0000010 ; inline
152 : OTHER-ALL     OCT: 0000007 ; inline
153 : OTHER-READ    OCT: 0000004 ; inline
154 : OTHER-WRITE   OCT: 0000002 ; inline
155 : OTHER-EXECUTE OCT: 0000001 ; inline
156
157 : uid? ( obj -- ? ) UID file-mode? ;
158 : gid? ( obj -- ? ) GID file-mode? ;
159 : sticky? ( obj -- ? ) STICKY file-mode? ;
160 : user-read? ( obj -- ? ) USER-READ file-mode? ;
161 : user-write? ( obj -- ? ) USER-WRITE file-mode? ;
162 : user-execute? ( obj -- ? ) USER-EXECUTE file-mode? ;
163 : group-read? ( obj -- ? ) GROUP-READ file-mode? ;
164 : group-write? ( obj -- ? ) GROUP-WRITE file-mode? ;
165 : group-execute? ( obj -- ? ) GROUP-EXECUTE file-mode? ;
166 : other-read? ( obj -- ? ) OTHER-READ file-mode? ;
167 : other-write? ( obj -- ? ) OTHER-WRITE file-mode? ;
168 : other-execute? ( obj -- ? ) OTHER-EXECUTE file-mode? ;
169
170 : any-read? ( obj -- ? )
171     { [ user-read? ] [ group-read? ] [ other-read? ] } 1|| ;
172
173 : any-write? ( obj -- ? )
174     { [ user-write? ] [ group-write? ] [ other-write? ] } 1|| ;
175
176 : any-execute? ( obj -- ? )
177     { [ user-execute? ] [ group-execute? ] [ other-execute? ] } 1|| ;
178
179 : set-uid ( path ? -- ) UID swap chmod-set-bit ;
180 : set-gid ( path ? -- ) GID swap chmod-set-bit ;
181 : set-sticky ( path ? -- ) STICKY swap chmod-set-bit ;
182 : set-user-read ( path ? -- ) USER-READ swap chmod-set-bit ;
183 : set-user-write ( path ? -- ) USER-WRITE swap chmod-set-bit ;
184 : set-user-execute ( path ? -- ) USER-EXECUTE swap chmod-set-bit ;
185 : set-group-read ( path ? -- ) GROUP-READ swap chmod-set-bit ;
186 : set-group-write ( path ? -- ) GROUP-WRITE swap chmod-set-bit ;
187 : set-group-execute ( path ? -- ) GROUP-EXECUTE swap chmod-set-bit ;
188 : set-other-read ( path ? -- ) OTHER-READ swap chmod-set-bit ;
189 : set-other-write ( path ? -- ) OTHER-WRITE swap chmod-set-bit ;
190 : set-other-execute ( path ? -- ) OTHER-EXECUTE swap chmod-set-bit ;
191
192 : set-file-permissions ( path n -- )
193     [ normalize-path ] dip chmod io-error ;
194
195 : file-permissions ( path -- n )
196     normalize-path file-info permissions>> ;
197
198 <PRIVATE
199
200 : make-timeval-array ( array -- byte-array )
201     [ [ "timeval" <c-object> ] unless* ] map concat ;
202
203 : timestamp>timeval ( timestamp -- timeval )
204     unix-1970 time- duration>microseconds make-timeval ;
205
206 : timestamps>byte-array ( timestamps -- byte-array )
207     [ dup [ timestamp>timeval ] when ] map make-timeval-array ;
208
209 PRIVATE>
210
211 : set-file-times ( path timestamps -- )
212     #! set access, write
213     [ normalize-path ] dip
214     timestamps>byte-array utimes io-error ;
215
216 : set-file-access-time ( path timestamp -- )
217     f 2array set-file-times ;
218
219 : set-file-modified-time ( path timestamp -- )
220     f swap 2array set-file-times ;
221
222 : set-file-ids ( path uid gid -- )
223     [ normalize-path ] 2dip
224     [ [ -1 ] unless* ] bi@ chown io-error ;
225
226 GENERIC: set-file-user ( path string/id -- )
227
228 GENERIC: set-file-group ( path string/id -- )
229
230 M: integer set-file-user ( path uid -- )
231     f set-file-ids ;
232
233 M: string set-file-user ( path string -- )
234     user-id f set-file-ids ;
235
236 M: integer set-file-group ( path gid -- )
237     f swap set-file-ids ;
238
239 M: string set-file-group ( path string -- )
240     group-id
241     f swap set-file-ids ;
242
243 : file-user-id ( path -- uid )
244     normalize-path file-info uid>> ;
245
246 : file-username ( path -- string )
247     file-user-id username ;
248
249 : file-group-id ( path -- gid )
250     normalize-path file-info gid>> ;
251
252 : file-group-name ( path -- string )
253     file-group-id group-name ;