From 54b6467013bfbdb3ee606961c02fbff1271ca582 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 19 Sep 2014 15:01:58 +0000 Subject: initial commit --- src/minutils/s6ps_grcache.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/minutils/s6ps_grcache.c (limited to 'src/minutils/s6ps_grcache.c') diff --git a/src/minutils/s6ps_grcache.c b/src/minutils/s6ps_grcache.c new file mode 100644 index 0000000..1fe9380 --- /dev/null +++ b/src/minutils/s6ps_grcache.c @@ -0,0 +1,66 @@ +/* ISC license. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "s6-ps.h" + +static avltree grcache_tree = AVLTREE_ZERO ; +static genalloc grcache_index = GENALLOC_ZERO ; + +int s6ps_grcache_init (void) +{ + avltree_init(&grcache_tree, 5, 3, 8, &left_dtok, &uint_cmp, &grcache_index) ; + return 1 ; +} + +void s6ps_grcache_finish (void) +{ + avltree_free(&grcache_tree) ; + genalloc_free(diuint, &grcache_index) ; +} + +int s6ps_grcache_lookup (stralloc *sa, unsigned int gid) +{ + int wasnull = !satmp.s ; + diuint d = { .left = gid, .right = satmp.len } ; + unsigned int i ; + if (!avltree_search(&grcache_tree, &d.left, &i)) + { + struct group *gr ; + unsigned int n = genalloc_len(diuint, &grcache_index) ; + errno = 0 ; + gr = getgrgid(gid) ; + if (!gr) + { + if (errno) return 0 ; + if (!stralloc_readyplus(&satmp, UINT_FMT + 2)) return 0 ; + stralloc_catb(&satmp, "(", 1) ; + satmp.len += uint_fmt(satmp.s + satmp.len, gid) ; + stralloc_catb(&satmp, ")", 2) ; + } + else if (!stralloc_cats(&satmp, gr->gr_name) || !stralloc_0(&satmp)) return 0 ; + if (!genalloc_append(diuint, &grcache_index, &d)) goto err ; + if (!avltree_insert(&grcache_tree, n)) + { + genalloc_setlen(diuint, &grcache_index, n) ; + goto err ; + } + i = n ; + } + return stralloc_cats(sa, satmp.s + genalloc_s(diuint, &grcache_index)[i].right) ; + err: + { + register int e = errno ; + if (wasnull) stralloc_free(&satmp) ; + else satmp.len = d.right ; + errno = e ; + } + return 0 ; +} -- cgit v1.2.3