xref: /inferno-os/liblogfs/groupset.c (revision 28942ead413418b56c5be78e8c4c400881fba72e)
1 #include "logfsos.h"
2 #include "logfs.h"
3 #include "local.h"
4 
5 struct GroupSet {
6 	int maxentries;
7 	int nentries;
8 	Group **entry;
9 };
10 
11 char *
logfsgroupsetnew(GroupSet ** gsp)12 logfsgroupsetnew(GroupSet **gsp)
13 {
14 	GroupSet *gs = logfsrealloc(nil, sizeof(*gs));
15 	if(gs == nil)
16 		return Enomem;
17 	gs->entry = logfsrealloc(nil, sizeof(Group *));
18 	if(gs->entry == nil) {
19 		logfsfreemem(gs);
20 		return Enomem;
21 	}
22 	gs->maxentries = 1;		/* most groups have one member */
23 	gs->nentries = 0;
24 	*gsp = gs;
25 	return nil;
26 }
27 
28 void
logfsgroupsetfree(GroupSet ** gsp)29 logfsgroupsetfree(GroupSet **gsp)
30 {
31 	GroupSet *gs = *gsp;
32 	if(gs) {
33 		logfsfreemem(gs->entry);
34 		logfsfreemem(gs);
35 		*gsp = nil;
36 	}
37 }
38 
39 int
logfsgroupsetadd(GroupSet * gs,Group * g)40 logfsgroupsetadd(GroupSet *gs, Group *g)
41 {
42 	int x;
43 	for(x = 0; x < gs->nentries; x++)
44 		if(gs->entry[x] == g)
45 			return 1;
46 	if(gs->nentries >= gs->maxentries) {
47 		Group **ne = logfsrealloc(gs->entry, sizeof(Group*)*(gs->maxentries * 2));
48 		if(ne == nil)
49 			return 0;
50 		gs->entry = ne;
51 		gs->maxentries *= 2;
52 	}
53 	gs->entry[gs->nentries++] = g;
54 	return 1;
55 }
56 
57 int
logfsgroupsetremove(GroupSet * gs,Group * g)58 logfsgroupsetremove(GroupSet *gs, Group *g)
59 {
60 	int x;
61 	for(x = 0; x < gs->nentries; x++)
62 		if(gs->entry[x] == g)
63 			break;
64 	if(x == gs->nentries)
65 		return 0;
66 	gs->nentries--;
67 	memmove(&gs->entry[x], &gs->entry[x + 1], sizeof(Group *) * (gs->nentries - x));
68 	return 1;
69 }
70 
71 int
logfsgroupsetwalk(GroupSet * gs,LOGFSGROUPSETWALKFN * func,void * magic)72 logfsgroupsetwalk(GroupSet *gs, LOGFSGROUPSETWALKFN *func, void *magic)
73 {
74 	int x;
75 	for(x = 0; x < gs->nentries; x++) {
76 		int rv = (*func)(magic, gs->entry[x]);
77 		if(rv <= 0)
78 			return rv;
79 	}
80 	return 1;
81 }
82 
83 int
logfsgroupsetismember(GroupSet * gs,Group * g)84 logfsgroupsetismember(GroupSet *gs, Group *g)
85 {
86 	int x;
87 	for(x = 0; x < gs->nentries; x++)
88 		if(gs->entry[x] == g)
89 			return 1;
90 	return 0;
91 }
92