]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/groups/groups.factor
92cf9f258fbf115bddf49cc8c1b1d876eecd1418
[factor.git] / basis / unix / groups / groups.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 assocs byte-arrays
4 classes.struct combinators continuations fry grouping
5 io.encodings.utf8 kernel math math.parser namespaces sequences
6 strings unix unix.ffi unix.users unix.utilities ;
7 IN: unix.groups
8
9 TUPLE: group id name passwd members ;
10
11 SYMBOL: group-cache
12
13 GENERIC: group-struct ( obj -- group/f )
14
15 <PRIVATE
16
17 : group-members ( group-struct -- seq )
18     gr_mem>> utf8 alien>strings ;
19
20 : (group-struct) ( id -- group-struct id group-struct byte-array length void* )
21     [ unix.ffi:group <struct> ] dip over 4096
22     [ <byte-array> ] keep f void* <ref> ;
23
24 : check-group-struct ( group-struct ptr -- group-struct/f )
25     void* deref [ drop f ] unless ;
26
27 M: integer group-struct ( id -- group/f )
28     (group-struct)
29     [ [ unix.ffi:getgrgid_r ] unix-system-call drop ] keep
30     check-group-struct ;
31
32 M: string group-struct ( string -- group/f )
33     (group-struct)
34     [ [ unix.ffi:getgrnam_r ] unix-system-call drop ] keep
35     check-group-struct ;
36
37 : group-struct>group ( group-struct -- group )
38     [ \ group new ] dip
39     {
40         [ gr_name>> >>name ]
41         [ gr_passwd>> >>passwd ]
42         [ gr_gid>> >>id ]
43         [ group-members >>members ]
44     } cleave ;
45
46 PRIVATE>
47
48 : group-name ( id -- string )
49     dup group-cache get [
50         ?at [ name>> ] [ number>string ] if
51     ] [
52         group-struct [ gr_name>> ] [ f ] if*
53     ] if*
54     [ ] [ number>string ] ?if ;
55
56 : group-id ( string -- id/f )
57     group-struct dup [ gr_gid>> ] when ;
58
59 ERROR: no-group string ;
60
61 : ?group-id ( string -- id )
62     dup group-struct [ nip gr_gid>> ] [ no-group ] if* ;
63
64 <PRIVATE
65
66 : >groups ( byte-array n -- groups )
67     [ 4 grouping:group ] dip head-slice [ uint deref group-name ] map ;
68
69 : (user-groups) ( string -- seq )
70     dup user-passwd [
71         gid>> 64 [ 4 * <byte-array> ] keep
72         int <ref> [ [ unix.ffi:getgrouplist ] unix-system-call drop ] 2keep
73         int deref >groups
74     ] [
75         drop { }
76     ] if* ;
77
78 PRIVATE>
79
80 GENERIC: user-groups ( string/id -- seq )
81
82 M: string user-groups ( string -- seq )
83     (user-groups) ;
84
85 M: integer user-groups ( id -- seq )
86     user-name (user-groups) ;
87
88 : all-groups ( -- seq )
89     [ unix.ffi:getgrent dup ] [ group-struct>group ] produce nip
90     endgrent ;
91
92 : all-group-names ( -- seq )
93     all-groups [ name>> ] map ;
94
95 : <group-cache> ( -- assoc )
96     all-groups [ [ id>> ] keep ] H{ } map>assoc ;
97
98 : with-group-cache ( quot -- )
99     [ <group-cache> group-cache ] dip with-variable ; inline
100
101 : real-group-id ( -- id ) unix.ffi:getgid ; inline
102
103 : real-group-name ( -- string ) real-group-id group-name ; inline
104
105 : effective-group-id ( -- string ) unix.ffi:getegid ; inline
106
107 : effective-group-name ( -- string )
108     effective-group-id group-name ; inline
109
110 : group-exists? ( name/id -- ? ) group-id >boolean ;
111
112 GENERIC: set-real-group ( obj -- )
113
114 GENERIC: set-effective-group ( obj -- )
115
116 : (with-real-group) ( string/id quot -- )
117     '[ _ set-real-group @ ]
118     real-group-id '[ _ set-real-group ] finally ; inline
119
120 : with-real-group ( string/id/f quot -- )
121     over [ (with-real-group) ] [ nip call ] if ; inline
122
123 : (with-effective-group) ( string/id quot -- )
124     '[ _ set-effective-group @ ]
125     effective-group-id '[ _ set-effective-group ] finally ; inline
126
127 : with-effective-group ( string/id/f quot -- )
128     over [ (with-effective-group) ] [ nip call ] if ; inline
129
130 <PRIVATE
131
132 : (set-real-group) ( id -- )
133     [ unix.ffi:setgid ] unix-system-call drop ; inline
134
135 : (set-effective-group) ( id -- )
136     [ unix.ffi:setegid ] unix-system-call drop ; inline
137
138 PRIVATE>
139
140 M: integer set-real-group ( id -- )
141     (set-real-group) ;
142
143 M: string set-real-group ( string -- )
144     ?group-id (set-real-group) ;
145
146 M: integer set-effective-group ( id -- )
147     (set-effective-group) ;
148
149 M: string set-effective-group ( string -- )
150     ?group-id (set-effective-group) ;