9front - general discussion about 9front
 help / color / mirror / Atom feed
* Re: [9front] hgfs(4) loadrevinfo patch
@ 2019-12-09  0:55 cinap_lenrek
  0 siblings, 0 replies; 4+ messages in thread
From: cinap_lenrek @ 2019-12-09  0:55 UTC (permalink / raw)
  To: 9front

see how many lines are deleted now! excellent work. applying.

--
cinap


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9front] hgfs(4) loadrevinfo patch
@ 2019-12-08 10:09 Alex Musolino
  0 siblings, 0 replies; 4+ messages in thread
From: Alex Musolino @ 2019-12-08 10:09 UTC (permalink / raw)
  To: 9front

> I'll send an updated patch tomorrow night.

Okay, here it is.

diff -r aca41046f6ee sys/src/cmd/hgfs/info.c
--- a/sys/src/cmd/hgfs/info.c	Mon Dec 02 14:50:53 2019 -0800
+++ b/sys/src/cmd/hgfs/info.c	Sun Dec 08 20:38:42 2019 +1030
@@ -1,16 +1,18 @@
 #include <u.h>
 #include <libc.h>
 #include <thread.h>
+#include <bio.h>
 #include "dat.h"
 #include "fns.h"
 
 Revinfo*
 loadrevinfo(Revlog *changelog, int rev)
 {
-	char buf[BUFSZ], *p, *e;
-	int fd, line, eof, inmsg, n;
+	int c, fd;
+	char *line;
 	Revinfo *ri;
 	vlong off;
+	Biobuf *buf;
 
 	if((fd = revlogopentemp(changelog, rev)) < 0)
 		return nil;
@@ -21,59 +23,56 @@
 	ri = malloc(sizeof(*ri));
 	memset(ri, 0, sizeof(*ri));
 
+	ri->logoff = off;
 	memmove(ri->chash, changelog->map[rev].hash, HASHSZ);
 
-	eof = 0;
-	line = 0;
-	inmsg = 0;
-	p = buf;
-	e = buf + BUFSZ;
-	while(eof == 0){
-		if((n = read(fd, p, e - p)) < 0)
-			break;
-		if(n == 0){
-			eof = 1;
-			*p = '\n';
-			n++;
+	buf = Bfdopen(fd, OREAD);
+	line = Brdstr(buf, '\n', 1);
+	if(line == nil)
+		goto Error;
+	hex2hash(line, ri->mhash);
+	free(line);
+
+	line = Brdstr(buf, '\n', 1);
+	if(line == nil)
+		goto Error;
+	ri->who = line;
+
+	line = Brdstr(buf, '\n', 1);
+	if(line == nil)
+		goto Error;
+	ri->when = strtol(line, nil, 10);
+	free(line);
+
+	ri->logoff = Boffset(buf);
+
+	for(;;){
+		if((c = Bgetc(buf)) < 0)
+			goto Error;
+		if(c == '\n'){
+			if((c = Bgetc(buf)) < 0)
+				goto Error;
+			if(c == '\n')
+				break;
 		}
-		p += n;
-		while((p > buf) && (e = memchr(buf, '\n', p - buf))){
-			*e++ = 0;
+	}
 
-			switch(line++){
-			case 0:
-				hex2hash(buf, ri->mhash);
-				break;
-			case 1:
-				ri->who = strdup(buf);
-				break;
-			case 2:
-				ri->when = strtol(buf, nil, 10);
-				break;
-			case 3:
-				ri->logoff = off;
-			default:
-				if(!inmsg){
-					if(*buf == 0){
-						ri->loglen = off - ri->logoff;
-						inmsg = 1;
-					}
-				} else {
-					n = ri->why ? strlen(ri->why) : 0;
-					ri->why = realloc(ri->why, n + strlen(buf)+2);
-					if(n > 0) ri->why[n++] = '\n';
-					strcpy(ri->why + n, buf);
-				}
-			}
-			n = e - buf;
-			p -= n;
-			if(p > buf)
-				memmove(buf, e, p - buf);
-			off += n;
-		}
-		e = buf + BUFSZ;
-	}
+	ri->loglen = Boffset(buf) - ri->logoff - 1;
+
+	line = Brdstr(buf, '\0', 1);
+	if(line == nil)
+		goto Error;
+	ri->why = line;
+
+	Bterm(buf);
 	close(fd);
 
 	return ri;
+Error:
+	Bterm(buf);
+	close(fd);
+	free(ri->who);
+	free(ri->why);
+	free(ri);
+	return nil;
 }


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9front] hgfs(4) loadrevinfo patch
@ 2019-12-07  2:27 Alex Musolino
  0 siblings, 0 replies; 4+ messages in thread
From: Alex Musolino @ 2019-12-07  2:27 UTC (permalink / raw)
  To: 9front

> a good start, but why not just use Boffset() to get logoff/loglen?

Because Blength comes before Boffset in the man page.

> also, to find the "\n\n", just do a simple state machine:

Yeah, this becomes the obvious way when you use Boffset instead
of Blength; no need to keep track of all the bytes you're skipping.

I'll send an updated patch tomorrow night.

--
Cheers,
Alex Musolino


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [9front] hgfs(4) loadrevinfo patch
@ 2019-12-07  2:10 cinap_lenrek
  0 siblings, 0 replies; 4+ messages in thread
From: cinap_lenrek @ 2019-12-07  2:10 UTC (permalink / raw)
  To: 9front

a good start, but why not just use Boffset() to get logoff/loglen?

also, to find the "\n\n", just do a simple state machine:

for(;;){
	if((c = Bgetc(bio)) == -1)
		goto Error;
	if(c == '\n){
		if((c = Bgetc(bio)) == -1)
			goto Error;
		if(c == '\n')
			break;		// we'r at the end of the log. take Boffset() and subtract to get length
	}
}

--
cinap


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-12-09  0:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-09  0:55 [9front] hgfs(4) loadrevinfo patch cinap_lenrek
  -- strict thread matches above, loose matches on Subject: below --
2019-12-08 10:09 Alex Musolino
2019-12-07  2:27 Alex Musolino
2019-12-07  2:10 cinap_lenrek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).