]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/hgfs/info.c
upas/fs: fix more locking bugs, remove debugging clutter, remove planb mbox code
[plan9front.git] / sys / src / cmd / hgfs / info.c
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include "dat.h"
5 #include "fns.h"
6
7 Revinfo*
8 loadrevinfo(Revlog *changelog, int rev)
9 {
10         char buf[BUFSZ], *p, *e;
11         int fd, line, eof, inmsg, n;
12         Revinfo *ri;
13         vlong off;
14
15         if((fd = revlogopentemp(changelog, rev)) < 0)
16                 return nil;
17
18         off = fmetaheader(fd);
19         seek(fd, off, 0);
20
21         ri = malloc(sizeof(*ri));
22         memset(ri, 0, sizeof(*ri));
23
24         memmove(ri->chash, changelog->map[rev].hash, HASHSZ);
25
26         eof = 0;
27         line = 0;
28         inmsg = 0;
29         p = buf;
30         e = buf + BUFSZ;
31         while(eof == 0){
32                 if((n = read(fd, p, e - p)) < 0)
33                         break;
34                 if(n == 0){
35                         eof = 1;
36                         *p = '\n';
37                         n++;
38                 }
39                 p += n;
40                 while((p > buf) && (e = memchr(buf, '\n', p - buf))){
41                         *e++ = 0;
42
43                         switch(line++){
44                         case 0:
45                                 hex2hash(buf, ri->mhash);
46                                 break;
47                         case 1:
48                                 ri->who = strdup(buf);
49                                 break;
50                         case 2:
51                                 ri->when = strtol(buf, nil, 10);
52                                 break;
53                         case 3:
54                                 ri->logoff = off;
55                         default:
56                                 if(!inmsg){
57                                         if(*buf == 0){
58                                                 ri->loglen = off - ri->logoff;
59                                                 inmsg = 1;
60                                         }
61                                 } else {
62                                         n = ri->why ? strlen(ri->why) : 0;
63                                         ri->why = realloc(ri->why, n + strlen(buf)+2);
64                                         if(n > 0) ri->why[n++] = '\n';
65                                         strcpy(ri->why + n, buf);
66                                 }
67                         }
68                         n = e - buf;
69                         p -= n;
70                         if(p > buf)
71                                 memmove(buf, e, p - buf);
72                         off += n;
73                 }
74                 e = buf + BUFSZ;
75         }
76         close(fd);
77
78         return ri;
79 }