shithub: riscv

Download patch

ref: b4b4b6cd631e4627f8e581d20fc70d5d13f4de18
parent: 70cc0a7b44ad70929a00565b03327296d2e64c52
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Aug 1 17:46:39 EDT 2022

hjfs: do not truncate lines on /adm/users write

--- a/sys/src/cmd/hjfs/auth.c
+++ b/sys/src/cmd/hjfs/auth.c
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <bio.h>
 #include <thread.h>
 #include "dat.h"
 #include "fns.h"
@@ -184,14 +185,23 @@
 	return -1;
 }
 
+static int
+Bchanwrite(Biobufhdr *b, void *p, long n)
+{
+	Dir d;
+	if(chanstat(b->aux, &d) < 0)
+		return -1;
+	return chanwrite(b->aux, p, n, d.length);
+}
+
 int
 userssave(Fs *fs, Chan *ch)
 {
 	User *u, *v;
 	int nu, i;
-	char buf[512], ubuf[USERLEN], *p, *e;
-	uvlong off;
-	
+	char ubuf[USERLEN], *uname;
+	Biobuf buf;
+
 	rlock(&fs->udatal);
 	u = fs->udata;
 	if(u == nil){
@@ -199,31 +209,30 @@
 		nu = nelem(udef);
 	}else
 		nu = fs->nudata;
-	off = 0;
+
+	Binit(&buf, 2, OWRITE);
+	if(ch != nil)
+		Biofn(&buf, Bchanwrite);
+	buf.aux = ch;
+
 	for(v = u; v < u + nu; v++){
-		p = buf;
-		e = buf + sizeof(buf);
-		p = seprint(p, e, "%d:%s:", v->uid, v->name);
-		if(v->lead != NOUID)
-			p = strecpy(p, e, uid2name(fs, v->lead, ubuf));
-		if(p < e)
-			*p++ = ':';
+		uname = v->lead == NOUID ? "" : uid2name(fs, v->lead, ubuf);
+		if(Bprint(&buf, "%d:%s:%s:", v->uid, v->name, uname) < 0)
+			goto err;
 		for(i = 0; i < v->nmemb; i++){
 			if(v->memb[i] == NOUID)
 				continue;
-			if(p < e && i > 0)
-				*p++ = ',';
-			p = strecpy(p, e, uid2name(fs, v->memb[i], ubuf));
+			uname = uid2name(fs, v->memb[i], ubuf);
+			if(Bprint(&buf, "%s%s", i < 1 ? "" : ",", uname) < 0)
+				goto err;
 		}
-		*p++ = '\n';
-		if(ch == nil)
-			write(2, buf, p - buf);
-		else if(chanwrite(ch, buf, p - buf, off) < p - buf)
+		if(Bputc(&buf, '\n') < 0)
 			goto err;
-		off += p - buf;
 	}
-	runlock(&fs->udatal);
-	return 0;
+	if(Bterm(&buf) == 0){
+		runlock(&fs->udatal);
+		return 0;
+	}
 err:
 	runlock(&fs->udatal);
 	return -1;