2003-11-23 17:55:34 +00:00
|
|
|
#include "stdinc.h"
|
|
|
|
|
#include <bio.h>
|
|
|
|
|
|
|
|
|
|
typedef struct Source Source;
|
|
|
|
|
|
|
|
|
|
struct Source
|
|
|
|
|
{
|
|
|
|
|
ulong gen;
|
|
|
|
|
int psize;
|
|
|
|
|
int dsize;
|
|
|
|
|
int dir;
|
|
|
|
|
int active;
|
|
|
|
|
int depth;
|
|
|
|
|
uvlong size;
|
|
|
|
|
uchar score[VtScoreSize];
|
|
|
|
|
int reserved;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int bsize;
|
|
|
|
|
Biobuf *bout;
|
|
|
|
|
VtRoot root;
|
|
|
|
|
int ver;
|
|
|
|
|
int cmp;
|
|
|
|
|
int all;
|
|
|
|
|
int find;
|
|
|
|
|
uchar fscore[VtScoreSize];
|
2004-03-15 01:56:49 +00:00
|
|
|
VtConn *z;
|
2003-11-23 17:55:34 +00:00
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
int vtgetuint16(uchar *p);
|
|
|
|
|
ulong vtgetuint32(uchar *p);
|
|
|
|
|
uvlong vtgetuint48(uchar *p);
|
2003-11-23 17:55:34 +00:00
|
|
|
void usage(void);
|
2004-03-15 01:56:49 +00:00
|
|
|
void readroot(VtRoot*, uchar *score, char *file);
|
|
|
|
|
int dumpdir(Source*, int indent);
|
2004-06-17 03:31:07 +00:00
|
|
|
int timevtread(VtConn*, uchar*, int, void*, int);
|
|
|
|
|
|
|
|
|
|
int mainstacksize = 512*1024;
|
2003-11-23 17:55:34 +00:00
|
|
|
|
|
|
|
|
void
|
2004-03-15 01:56:49 +00:00
|
|
|
threadmain(int argc, char *argv[])
|
2003-11-23 17:55:34 +00:00
|
|
|
{
|
2004-03-15 01:56:49 +00:00
|
|
|
char *host = nil, *pref;
|
2003-11-23 17:55:34 +00:00
|
|
|
uchar score[VtScoreSize];
|
|
|
|
|
Source source;
|
|
|
|
|
char *p;
|
|
|
|
|
int n;
|
2004-06-17 03:31:07 +00:00
|
|
|
uchar buf[VtMaxLumpSize];
|
2003-11-23 17:55:34 +00:00
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
|
case 'h':
|
|
|
|
|
host = ARGF();
|
|
|
|
|
break;
|
|
|
|
|
case 'c':
|
|
|
|
|
cmp++;
|
|
|
|
|
break;
|
|
|
|
|
case 'f':
|
|
|
|
|
find++;
|
|
|
|
|
p = ARGF();
|
2004-03-15 01:56:49 +00:00
|
|
|
if(p == nil || vtparsescore(p, &pref, fscore) < 0 || !pref || strcmp(pref, "vac") != 0)
|
2003-11-23 17:55:34 +00:00
|
|
|
usage();
|
|
|
|
|
break;
|
|
|
|
|
case 'a':
|
|
|
|
|
all = 1;
|
|
|
|
|
break;
|
|
|
|
|
}ARGEND
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
bout = vtmallocz(sizeof(Biobuf));
|
2003-11-23 17:55:34 +00:00
|
|
|
Binit(bout, 1, OWRITE);
|
|
|
|
|
|
|
|
|
|
if(argc > 1)
|
|
|
|
|
usage();
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
fmtinstall('V', vtscorefmt);
|
|
|
|
|
fmtinstall('H', encodefmt);
|
2003-11-23 17:55:34 +00:00
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
z = vtdial(host);
|
2003-11-23 17:55:34 +00:00
|
|
|
if(z == nil)
|
2004-03-15 01:56:49 +00:00
|
|
|
sysfatal("could not connect to server: %r");
|
2003-11-23 17:55:34 +00:00
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
if(vtconnect(z) < 0)
|
|
|
|
|
sysfatal("vtconnect: %r");
|
2003-11-23 17:55:34 +00:00
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
readroot(&root, score, argv[0]);
|
|
|
|
|
bsize = root.blocksize;
|
2003-11-23 17:55:34 +00:00
|
|
|
if(!find) {
|
|
|
|
|
Bprint(bout, "score: %V\n", score);
|
|
|
|
|
Bprint(bout, "name: %s\n", root.name);
|
|
|
|
|
Bprint(bout, "type: %s\n", root.type);
|
|
|
|
|
Bprint(bout, "bsize: %d\n", bsize);
|
|
|
|
|
Bprint(bout, "prev: %V\n", root.prev);
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-29 17:13:24 +00:00
|
|
|
fprint(2, "read...\n");
|
2004-06-17 03:31:07 +00:00
|
|
|
n = timevtread(z, root.score, VtDirType, buf, bsize);
|
2003-11-23 17:55:34 +00:00
|
|
|
if(n < 0)
|
|
|
|
|
sysfatal("could not read root dir");
|
|
|
|
|
|
2004-04-29 17:13:24 +00:00
|
|
|
fprint(2, "...\n");
|
2003-11-23 17:55:34 +00:00
|
|
|
/* fake up top level source */
|
|
|
|
|
memset(&source, 0, sizeof(source));
|
|
|
|
|
memmove(source.score, root.score, VtScoreSize);
|
|
|
|
|
source.psize = bsize;
|
|
|
|
|
source.dsize = bsize;
|
|
|
|
|
source.dir = 1;
|
|
|
|
|
source.active = 1;
|
|
|
|
|
source.depth = 0;
|
|
|
|
|
source.size = n;
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
dumpdir(&source, 0);
|
2003-11-23 17:55:34 +00:00
|
|
|
|
|
|
|
|
Bterm(bout);
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
vthangup(z);
|
|
|
|
|
threadexitsall(0);
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-03-15 01:56:49 +00:00
|
|
|
sourceprint(Source *s, int indent, int entry)
|
2003-11-23 17:55:34 +00:00
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
uvlong size;
|
|
|
|
|
int ne;
|
|
|
|
|
|
|
|
|
|
for(i=0; i<indent; i++)
|
|
|
|
|
Bprint(bout, " ");
|
|
|
|
|
Bprint(bout, "%4d", entry);
|
|
|
|
|
if(s->active) {
|
|
|
|
|
/* dir size in directory entries */
|
|
|
|
|
if(s->dir) {
|
|
|
|
|
ne = s->dsize/VtEntrySize;
|
|
|
|
|
size = ne*(s->size/s->dsize) + (s->size%s->dsize)/VtEntrySize;
|
|
|
|
|
} else
|
|
|
|
|
size = s->size;
|
|
|
|
|
if(cmp) {
|
|
|
|
|
Bprint(bout, ": gen: %lud size: %llud",
|
|
|
|
|
s->gen, size);
|
|
|
|
|
if(!s->dir)
|
|
|
|
|
Bprint(bout, ": %V", s->score);
|
|
|
|
|
} else {
|
|
|
|
|
Bprint(bout, ": gen: %lud psize: %d dsize: %d",
|
|
|
|
|
s->gen, s->psize, s->dsize);
|
|
|
|
|
Bprint(bout, " depth: %d size: %llud: %V",
|
|
|
|
|
s->depth, size, s->score);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(s->reserved)
|
|
|
|
|
Bprint(bout, ": reserved not emtpy");
|
|
|
|
|
}
|
|
|
|
|
Bprint(bout, "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
parse(Source *s, uchar *p)
|
|
|
|
|
{
|
|
|
|
|
VtEntry dir;
|
|
|
|
|
|
|
|
|
|
memset(s, 0, sizeof(*s));
|
2004-03-15 01:56:49 +00:00
|
|
|
if(vtentryunpack(&dir, p, 0) < 0)
|
|
|
|
|
return -1;
|
2003-11-23 17:55:34 +00:00
|
|
|
|
|
|
|
|
if(!(dir.flags & VtEntryActive))
|
2004-03-15 01:56:49 +00:00
|
|
|
return 0;
|
2003-11-23 17:55:34 +00:00
|
|
|
|
|
|
|
|
s->active = 1;
|
|
|
|
|
s->gen = dir.gen;
|
|
|
|
|
s->psize = dir.psize;
|
2004-06-17 03:31:07 +00:00
|
|
|
s->dsize = dir.dsize;
|
2003-11-23 17:55:34 +00:00
|
|
|
s->size = dir.size;
|
|
|
|
|
memmove(s->score, dir.score, VtScoreSize);
|
2004-03-15 01:56:49 +00:00
|
|
|
if((dir.type&~VtTypeDepthMask) == VtDirType)
|
2003-11-23 17:55:34 +00:00
|
|
|
s->dir = 1;
|
2004-03-15 01:56:49 +00:00
|
|
|
s->depth = dir.type&VtTypeDepthMask;
|
|
|
|
|
return 0;
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2004-03-15 01:56:49 +00:00
|
|
|
sourceread(Source *s, ulong block, uchar *p, int n)
|
2003-11-23 17:55:34 +00:00
|
|
|
{
|
2004-03-15 01:56:49 +00:00
|
|
|
uchar *buf;
|
2003-11-23 17:55:34 +00:00
|
|
|
uchar score[VtScoreSize];
|
|
|
|
|
int i, nn, np, type;
|
|
|
|
|
int elem[VtPointerDepth];
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
buf = vtmalloc(VtMaxLumpSize);
|
|
|
|
|
|
2003-11-23 17:55:34 +00:00
|
|
|
memmove(score, s->score, VtScoreSize);
|
|
|
|
|
|
|
|
|
|
np = s->psize/VtScoreSize;
|
|
|
|
|
for(i=0; i<s->depth; i++) {
|
|
|
|
|
elem[i] = block % np;
|
|
|
|
|
block /= np;
|
|
|
|
|
}
|
|
|
|
|
assert(block == 0);
|
|
|
|
|
|
|
|
|
|
for(i=s->depth-1; i>=0; i--) {
|
2004-06-17 03:31:07 +00:00
|
|
|
nn = timevtread(z, score, (s->dir ? VtDirType : VtDataType)+1+i, buf, s->psize);
|
2004-03-15 01:56:49 +00:00
|
|
|
if(nn < 0){
|
|
|
|
|
fprint(2, "vtread %V %d: %r\n", score, (s->dir ? VtDirType : VtDataType)+1+i);
|
|
|
|
|
free(buf);
|
2003-11-23 17:55:34 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
if((elem[i]+1)*VtScoreSize > nn){
|
|
|
|
|
free(buf);
|
2003-11-23 17:55:34 +00:00
|
|
|
return 0;
|
2004-03-15 01:56:49 +00:00
|
|
|
}
|
2003-11-23 17:55:34 +00:00
|
|
|
memmove(score, buf + elem[i]*VtScoreSize, VtScoreSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(s->dir)
|
|
|
|
|
type = VtDirType;
|
|
|
|
|
else
|
|
|
|
|
type = VtDataType;
|
|
|
|
|
|
2004-06-17 03:31:07 +00:00
|
|
|
nn = timevtread(z, score, type, p, n);
|
2004-03-15 01:56:49 +00:00
|
|
|
if(nn < 0){
|
|
|
|
|
fprint(2, "vtread %V %d: %r\n", score, type);
|
|
|
|
|
abort();
|
|
|
|
|
free(buf);
|
2003-11-23 17:55:34 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
free(buf);
|
2003-11-23 17:55:34 +00:00
|
|
|
return nn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-03-15 01:56:49 +00:00
|
|
|
dumpfilecontents(Source *s)
|
2003-11-23 17:55:34 +00:00
|
|
|
{
|
|
|
|
|
int nb, lb, i, n;
|
|
|
|
|
uchar buf[VtMaxLumpSize];
|
|
|
|
|
|
|
|
|
|
nb = (s->size + s->dsize - 1)/s->dsize;
|
|
|
|
|
lb = s->size%s->dsize;
|
|
|
|
|
for(i=0; i<nb; i++) {
|
|
|
|
|
memset(buf, 0, s->dsize);
|
2004-03-15 01:56:49 +00:00
|
|
|
n = sourceread(s, i, buf, s->dsize);
|
2003-11-23 17:55:34 +00:00
|
|
|
if(n < 0) {
|
2004-03-15 01:56:49 +00:00
|
|
|
fprint(2, "could not read block: %d: %r\n", i);
|
2003-11-23 17:55:34 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if(i < nb-1)
|
|
|
|
|
Bwrite(bout, buf, s->dsize);
|
|
|
|
|
else
|
|
|
|
|
Bwrite(bout, buf, lb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-03-15 01:56:49 +00:00
|
|
|
dumpfile(Source *s, int indent)
|
2003-11-23 17:55:34 +00:00
|
|
|
{
|
|
|
|
|
int nb, i, j, n;
|
2004-03-15 01:56:49 +00:00
|
|
|
uchar *buf;
|
2003-11-23 17:55:34 +00:00
|
|
|
uchar score[VtScoreSize];
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
buf = vtmalloc(VtMaxLumpSize);
|
2003-11-23 17:55:34 +00:00
|
|
|
nb = (s->size + s->dsize - 1)/s->dsize;
|
|
|
|
|
for(i=0; i<nb; i++) {
|
|
|
|
|
memset(buf, 0, s->dsize);
|
2004-03-15 01:56:49 +00:00
|
|
|
n = sourceread(s, i, buf, s->dsize);
|
2003-11-23 17:55:34 +00:00
|
|
|
if(n < 0) {
|
2004-03-15 01:56:49 +00:00
|
|
|
fprint(2, "could not read block: %d: %r\n", i);
|
2003-11-23 17:55:34 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for(j=0; j<indent; j++)
|
|
|
|
|
Bprint(bout, " ");
|
2004-03-15 01:56:49 +00:00
|
|
|
sha1(buf, n, score, nil);
|
2003-11-23 17:55:34 +00:00
|
|
|
Bprint(bout, "%4d: size: %ud: %V\n", i, n, score);
|
|
|
|
|
}
|
2004-03-15 01:56:49 +00:00
|
|
|
vtfree(buf);
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2004-03-15 01:56:49 +00:00
|
|
|
dumpdir(Source *s, int indent)
|
2003-11-23 17:55:34 +00:00
|
|
|
{
|
|
|
|
|
int pb, ne, nb, i, j, n, entry;
|
|
|
|
|
Source ss;
|
2004-03-15 01:56:49 +00:00
|
|
|
uchar *buf;
|
2003-11-23 17:55:34 +00:00
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
buf = vtmalloc(VtMaxLumpSize);
|
2003-11-23 17:55:34 +00:00
|
|
|
pb = s->dsize/VtEntrySize;
|
|
|
|
|
ne = pb*(s->size/s->dsize) + (s->size%s->dsize)/VtEntrySize;
|
|
|
|
|
nb = (s->size + s->dsize - 1)/s->dsize;
|
|
|
|
|
for(i=0; i<nb; i++) {
|
|
|
|
|
memset(buf, 0, s->dsize);
|
2004-03-15 01:56:49 +00:00
|
|
|
n = sourceread(s, i, buf, s->dsize);
|
2003-11-23 17:55:34 +00:00
|
|
|
if(n < 0) {
|
2004-03-15 01:56:49 +00:00
|
|
|
fprint(2, "could not read block: %d: %r\n", i);
|
2003-11-23 17:55:34 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for(j=0; j<pb; j++) {
|
|
|
|
|
entry = i*pb + j;
|
|
|
|
|
if(entry >= ne)
|
|
|
|
|
break;
|
|
|
|
|
parse(&ss, buf + j * VtEntrySize);
|
|
|
|
|
|
|
|
|
|
if(!find)
|
2004-03-15 01:56:49 +00:00
|
|
|
sourceprint(&ss, indent, entry);
|
2003-11-23 17:55:34 +00:00
|
|
|
else if(memcmp(ss.score, fscore, VtScoreSize) == 0) {
|
2004-03-15 01:56:49 +00:00
|
|
|
dumpfilecontents(&ss);
|
|
|
|
|
free(buf);
|
|
|
|
|
return -1;
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ss.dir) {
|
2004-03-15 01:56:49 +00:00
|
|
|
if(dumpdir(&ss, indent+1) < 0){
|
|
|
|
|
free(buf);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2003-11-23 17:55:34 +00:00
|
|
|
} else if(all)
|
2004-03-15 01:56:49 +00:00
|
|
|
dumpfile(&ss, indent+1);
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-03-15 01:56:49 +00:00
|
|
|
free(buf);
|
|
|
|
|
return 0;
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
usage(void)
|
|
|
|
|
{
|
|
|
|
|
fprint(2, "%s: [file]\n", argv0);
|
2004-03-15 01:56:49 +00:00
|
|
|
threadexits("usage");
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2004-03-15 01:56:49 +00:00
|
|
|
readroot(VtRoot *root, uchar *score, char *file)
|
2003-11-23 17:55:34 +00:00
|
|
|
{
|
|
|
|
|
int fd;
|
2004-03-15 01:56:49 +00:00
|
|
|
char *pref;
|
|
|
|
|
char buf[VtRootSize];
|
|
|
|
|
int n, nn;
|
2003-11-23 17:55:34 +00:00
|
|
|
|
|
|
|
|
if(file == 0)
|
|
|
|
|
fd = 0;
|
|
|
|
|
else {
|
|
|
|
|
fd = open(file, OREAD);
|
|
|
|
|
if(fd < 0)
|
|
|
|
|
sysfatal("could not open file: %s: %r\n", file);
|
|
|
|
|
}
|
|
|
|
|
n = readn(fd, buf, sizeof(buf)-1);
|
|
|
|
|
if(n < 0)
|
|
|
|
|
sysfatal("read failed: %r\n");
|
2004-03-15 01:56:49 +00:00
|
|
|
if(n==0 || buf[n-1] != '\n')
|
|
|
|
|
sysfatal("not a root file");
|
|
|
|
|
buf[n-1] = 0;
|
2003-11-23 17:55:34 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
2004-03-15 01:56:49 +00:00
|
|
|
if(vtparsescore(buf, &pref, score) < 0){
|
|
|
|
|
sysfatal("not a root file");
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
2004-06-17 03:31:07 +00:00
|
|
|
nn = timevtread(z, score, VtRootType, buf, VtRootSize);
|
2004-03-15 01:56:49 +00:00
|
|
|
if(nn < 0)
|
|
|
|
|
sysfatal("cannot read root %V", score);
|
|
|
|
|
if(vtrootunpack(root, buf) < 0)
|
|
|
|
|
sysfatal("cannot parse root: %r");
|
2003-11-23 17:55:34 +00:00
|
|
|
}
|
2004-06-17 03:31:07 +00:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
timevtread(VtConn *z, uchar *score, int type, void *buf, int nbuf)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
ulong t0, t1;
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
t0 = nsec();
|
|
|
|
|
n = vtread(z, score, type, buf, nbuf);
|
|
|
|
|
t1 = nsec();
|
|
|
|
|
fprint(2, "read %V: %.6f seconds\n", score, (t1-t0)/1.e9);
|
|
|
|
|
return n;
|
|
|
|
|
*/
|
|
|
|
|
return vtread(z, score, type, buf, nbuf);
|
|
|
|
|
}
|