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