code: mafs

ref: b3e875d7db45fca0cde9f4efa12872f61afc54d7
dir: /blk.c/

View raw version
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"

void
showdentryhdr(int fd, u8 *buf)
{
	Dentry *d;

	d = (Dentry*)buf;
	// fprint(fd, "name %s\n", d->name);
	fprint(fd, "flags 0x%hhux\n", d->flags);
	fprint(fd, "uid %d\n", d->uid);
	fprint(fd, "gid %d\n", d->gid);
	fprint(fd, "muid %d\n", d->muid);
	fprint(fd, "size %llud\n", d->size);
	fprint(fd, "pdblkno %llud\n", d->pdblkno);
	fprint(fd, "pqpath %llud\n", d->pqpath);
	fprint(fd, "preli %llud\n", d->preli);
	fprint(fd, "mtime %llud\n", d->mtime);
	fprint(fd, "qpath %llud\n", d->qpath);
	fprint(fd, "version %ud\n", d->version);
	fprint(fd, "mode 0%uo 0x%ux\n", d->mode, d->mode);
}
void
showmagic(int fd, u8 *d)
{
	fprint(fd, "%s", ((Dentry*)d)->buf);
}
void
showconfig(int fd, u8 *d)
{
	fprint(fd, "%s", ((Dentry*)d)->buf);
}
void
showsuper(int fd, u8 *d)
{
	Dentry *s;

	s = (Dentry*)d;
	fprint(fd, "qidgen %llud\n", s->qidgen);
	fprint(fd, "fsok %llud\n", s->fsok);
}
void
showdentry(int fd, u8 *buf)
{
	int j, k;
	Dentry *d;
	u64 i, start, end, nunits, *p;
	Name *nm;

	showdentryhdr(fd, buf);
	d = (Dentry*)buf;
	if(d->path == Qpconfig || d->path == Qpconfig0)
		showconfig(fd, buf);
	else if(d->path == Qpsuper || d->path == Qpsuper0)
		showsuper(fd, buf);
	else if(d->path == Qpmagic)
		showmagic(fd, buf);
	else if(d->size <= Ddatasize && (d->mode&DMDIR) == 0 &&
			d->path != Qproot0){
		if(d->preli == 0 && (d->flags&~Fsys) == Fn){
			for(i = 0; i < d->size/sizeof(Name); i++){
				nm = (Name*)d->buf+i;
				fprint(fd, "%llud %hud ", i, nm->namelen);
				for(k = 0; k < Nfirst; k++)
					fprint(fd, "%c", nm->name[k]);
				fprint(fd, " %llud\n", nm->noffset);
			}
		}else if((d->flags&~Fsys) == Fe){
			p = (u64*)d->buf;
			for(i = d->size/(3*sizeof(u64)); i>0; i++){
				start = *p++;
				end = *p++;
				nunits = *p++;
				if(start == 0 && end == 0)
					return;
				fprint(fd, "%llud %llud %llud\n", start, end, nunits);
			}
		}else{
			fprint(fd, "%s", d->buf);
		}
	}else{
		fprint(fd, "direct blocks\n");
		for(j = 0; j<Ndblock; j++)
			fprint(fd, "	%d %llud\n", j, d->dblocks[j]);
		fprint(fd, "indirect blocks\n");
		for(j = 0; j<Niblock; j++)
			fprint(fd, "	%d %llud\n", j, d->iblocks[j]);
	}
}
void
showind(int fd, u8 *buf)
{
	int j;
	Indirect *t;

	t = (Indirect*)buf;
	for(j = 0; j<Nindperblock; j++)
		fprint(fd, "	%d %llud\n", j, t->bufa[j]);
}
void
showdata(int fd, u8 *buf)
{
	Data *da;
	u64 i, start, end, nunits, *p;
	u8 k;
	Name *nm;

	da = (Data*)buf;
	if((da->flags&~Fsys) == Fn){
		/* assuming that the names index will fit in 1048556 bytes */
		/* else, we need to worry about the offset */
		for(i = 0; i < (da->len*Blocksize -Ddataidssize)/sizeof(Name); i++){
			nm = (Name*)da->buf+i;
			fprint(fd, "%llud %hud ", i, nm->namelen);
			for(k = 0; k < Nfirst; k++)
				fprint(fd, "%c", nm->name[k]);
			fprint(fd, " %llud\n", nm->noffset);
		}
	}else if((da->flags&~Fsys) == Fe){
		/* extents file, show extents */
		/* assuming that the extents fit in 1048556 bytes */
		/* else, we need to worry about the offset */
		p = (u64*)da->buf;
		for(i = (da->len*Blocksize -Ddataidssize)/(3*sizeof(u64)); i>0; i++){
			start = *p++;
			end = *p++;
			nunits = *p++;
			if(start == 0 && end == 0)
				return;
			fprint(fd, "%llud %llud %llud\n", start, end, nunits);
		}
	}else{
		fprint(fd, "%s", (s8*)da->buf);
	}
}

void
showblock(int fd, u8 *buf)
{
	u8 tag;
	Data *t;
	Dentry *d;
	Indirect *r;

	tag = buf[0];
	t = (Data*)buf;
	r = (Indirect*)buf;
	d = (Dentry*)buf;
	if(tag == Tblank){
		fprint(fd, "%s\n", tagnames[tag]);
		return;
	}else if(tag == Tdentry){
		fprint(fd, "%s qid.path %llud\n", tagnames[tag], d->path);
		showdentry(fd, buf);
		return;
	}else if(tag == Tdata){
		fprint(fd, "%s qid.path %llud dblkno %llud len %d flags 0x%hhux\n",
			tagnames[tag], ((u64*)buf)[t->len*Nu64perblock -1],
			t->dblkno, t->len, t->flags);
		showdata(fd, buf);
		return;
	}else if(tag < Maxtind)
		fprint(fd, "%s qid.path %llud dblkno %llud\n",
				tagnames[tag], r->pathi, r->dblkno);

	if(tag >= Tind0 && tag < Maxtind)
		showind(fd, buf);
	else
		fprint(fd, "unknown tag type %d\n", tag);
}