]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/hgfs/hgdb.c
hgfs: add wip debug command
[plan9front.git] / sys / src / cmd / hgfs / hgdb.c
1 /* hg debug stuff, just dumps dirstate database right now */
2
3 #include <u.h>
4 #include <libc.h>
5 #include <thread.h>
6 #include "dat.h"
7 #include "fns.h"
8
9 char dothg[MAXPATH];
10
11 void
12 main(int argc, char *argv[])
13 {
14         char buf[MAXPATH];
15         uchar hdr[1+4+4+4+4];
16         int n, fd;
17
18         ARGBEGIN {
19         } ARGEND;
20
21         if(getdothg(dothg, *argv) < 0)
22                 sysfatal("can't find .hg: %r");
23
24         snprint(buf, sizeof(buf), "%s/dirstate", dothg);
25         if((fd = open(buf, OREAD)) < 0)
26                 sysfatal("can't open dirstate: %r");
27
28         if(seek(fd, 0x28LL, 0) != 0x28LL)
29                 sysfatal("can't seek dirstate: %r");
30
31         for(;;){
32                 char state;
33                 int mode, len;
34                 vlong size;
35                 long mtime;
36                 
37                 if((n = read(fd, hdr, sizeof(hdr))) == 0)
38                         break;
39                 if(n < 0)
40                         sysfatal("read error: %r");
41                 if(n < sizeof(hdr))
42                         sysfatal("dirstate truncated");
43
44                 state = hdr[0];
45                 mode = hdr[4] | hdr[3]<<8 | hdr[2]<<16 | hdr[1]<<24;
46                 size = hdr[8] | hdr[7]<<8 | hdr[6]<<16 | hdr[5]<<24;
47                 mtime = hdr[12] | hdr[11]<<8 | hdr[10]<<16 | hdr[9]<<24;
48                 len = hdr[16] | hdr[15]<<8 | hdr[14]<<16 | hdr[13]<<24;
49                 USED(mtime);
50
51                 if(len >= sizeof(buf))
52                         sysfatal("invalid name length %d", len);
53
54                 n = read(fd, buf, len);
55                 if(n < 0)
56                         sysfatal("read error: %r");
57                 if(n < len)
58                         sysfatal("dirstate name truncated");
59                 buf[n] = 0;
60
61
62                 print("%c\t%o\t%lld\t%s\n", state, mode, size, buf);
63         }
64
65         exits(0);
66 }