shithub: 9pro

Download patch

ref: 1a0e97f78fb7f3cdcb442ffe76d360d3c81c4b26
parent: f0aebf0557848dab8edec8a8cd9e03998cc3f462
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Dec 24 08:01:50 EST 2019

store uid->name and gid->name in memory

--- a/9pex.c
+++ b/9pex.c
@@ -52,6 +52,12 @@
 	C9tag tag;
 }Tag;
 
+typedef struct
+{
+	char *name;
+	uint32_t id;
+}Id;
+
 static char *t2s[] = {
 	[Tversion-Tversion] = "Tversion",
 	[Tauth-Tversion] = "Tauth",
@@ -96,6 +102,8 @@
 static uint8_t *rdbuf;
 static uint8_t *wrbuf;
 static uint32_t wroff, wrend, wrbufsz;
+static Id *uids, *gids;
+static int numuids, numgids;
 
 __attribute__ ((format (printf, 1, 2)))
 static void
@@ -190,8 +198,6 @@
 	return -1;
 }
 
-
-
 static void
 stat2qid(struct stat *st, C9qid *qid, uint32_t *iounit)
 {
@@ -202,7 +208,7 @@
 	fmt = st->st_mode & S_IFMT;
 	if (fmt == S_IFDIR)
 		qid->type |= C9qtdir;
-	if (fmt == S_IFCHR || fmt == S_IFCHR || fmt == S_IFSOCK || fmt == S_IFIFO)
+	if ((st->st_mode & 0222) != 0 && (fmt == S_IFCHR || fmt == S_IFCHR || fmt == S_IFSOCK || fmt == S_IFIFO))
 		qid->type |= C9qtappend;
 	if (iounit != NULL)
 		*iounit = st->st_blksize;
@@ -403,13 +409,26 @@
 uid2str(uid_t uid, char **err)
 {
 	struct passwd *p;
+	Id *newuids;
+	int i;
 
-	if ((p = getpwuid(uid)) == NULL)
+	for (i = 0; i < numuids; i++) {
+		if (uids[i].id == uid)
+			return uids[i].name;
+	}
+	if ((p = getpwuid(uid)) == NULL) {
 		*err = strerror(errno);
-	else
-		return p->pw_name;
+		return NULL;
+	}
+	if ((newuids = realloc(uids, sizeof(*uids)*(numuids+1))) == NULL) {
+		*err = Enomem;
+		return NULL;
+	}
+	uids = newuids;
+	uids[numuids].id = uid;
+	uids[numuids].name = strdup(p->pw_name);
 
-	return NULL;
+	return uids[numuids++].name;
 }
 
 static char *
@@ -416,13 +435,26 @@
 gid2str(gid_t gid, char **err)
 {
 	struct group *g;
+	Id *newgids;
+	int i;
 
-	if ((g = getgrgid(gid)) == NULL)
+	for (i = 0; i < numgids; i++) {
+		if (gids[i].id == gid)
+			return gids[i].name;
+	}
+	if ((g = getgrgid(gid)) == NULL) {
 		*err = strerror(errno);
-	else
-		return g->gr_name;
+		return NULL;
+	}
+	if ((newgids = realloc(gids, sizeof(*gids)*(numgids+1))) == NULL) {
+		*err = Enomem;
+		return NULL;
+	}
+	gids = newgids;
+	gids[numgids].id = gid;
+	gids[numgids].name = strdup(g->gr_name);
 
-	return NULL;
+	return gids[numgids++].name;
 }
 
 static int
@@ -787,6 +819,10 @@
 	numfids = 0;
 	tags = NULL;
 	numtags = 0;
+	uids = NULL;
+	numuids = 0;
+	gids = NULL;
+	numgids = 0;
 
 	memset(&ctx, 0, sizeof(ctx));
 	ctx.msize = 64*1024;
@@ -838,6 +874,13 @@
 			free(f);
 		}
 	}
+
+	for (i = 0; i < numuids; i++)
+		free(uids[i].name);
+	free(uids);
+	for (i = 0; i < numgids; i++)
+		free(gids[i].name);
+	free(gids);
 
 	memset(wrbuf, 0, ctx.msize);
 	free(wrbuf);