henesyinit from p9fd787fda5 years agoHistory
989 lines · 15.5 KB · C
RawDownload
#include "stdinc.h"#include "vac.h"#include "dat.h"#include "fns.h"#include "error.h" static int	getString(char **s, uchar **p, int *n);static int	metaBlockUnpack(MetaBlock *mb, uchar *p, int n);static uchar 	*metaBlockEntry(MetaBlock *mb, int i, int *n);static int	vacDirUnpack(VacDir *dir, uchar *p, int n);static int	vacFileAquire(VacFile *vf);static void	vacFileRelease(VacFile *vf); voidvacDirCleanup(VacDir *dir){	vtMemFree(dir->elem);	dir->elem = nil;	vtMemFree(dir->uid);	dir->uid = nil;	vtMemFree(dir->gid);	dir->gid = nil;	vtMemFree(dir->mid);	dir->mid = nil;} voidvacDirCopy(VacDir *dst, VacDir *src){	*dst = *src;	dst->elem = vtStrDup(src->elem);	dst->uid = vtStrDup(src->uid);	dst->gid = vtStrDup(src->gid);	dst->mid = vtStrDup(src->mid);}  static intvacDirUnpack(VacDir *dir, uchar *p, int n){	int t, nn; 	memset(dir, 0, sizeof(VacDir)); 	/* magic */	if(n < 4 || vtGetUint32(p) != DirMagic)		goto Err;	p += 4;	n -= 4; 	/* version */	if(n < 2)		goto Err;	dir->version = vtGetUint16(p);	if(dir->version < 7 || dir->version > 8)		goto Err;	p += 2;	n -= 2;	 	/* elem */	if(!getString(&dir->elem, &p, &n))		goto Err; 	/* entry  */	if(n < 4)		goto Err;	dir->entry = vtGetUint32(p);	p += 4;	n -= 4; 	/* size is gotten from DirEntry */ 	/* qid */	if(n < 8)		goto Err;	dir->qid = vtGetUint64(p);	p += 8;	n -= 8; 	/* replacement */	if(dir->version == 7) {		if(n < VtScoreSize)			goto Err;		p += VtScoreSize;		n -= VtScoreSize;	}		/* uid */	if(!getString(&dir->uid, &p, &n))		goto Err; 	/* gid */	if(!getString(&dir->gid, &p, &n))		goto Err; 	/* mid */	if(!getString(&dir->mid, &p, &n))		goto Err; 	if(n < 5*4)		goto Err;	dir->mtime = vtGetUint32(p);	dir->mcount = vtGetUint32(p+4);	dir->ctime = vtGetUint32(p+8);	dir->atime = vtGetUint32(p+12);	dir->mode = vtGetUint32(p+16);	p += 5*4;	n -= 5*4; 	/* optional meta data */	while(n > 0) {		if(n < 3)			goto Err;		t = p[0];		nn = vtGetUint16(p+1);		p += 3;		n -= 3;		if(n < nn)			goto Err;		switch(t) {		case DirPlan9Entry:			if(dir->plan9 || nn != 12)				goto Err;			dir->plan9 = 1;			dir->p9path = vtGetUint64(p);			dir->p9version = vtGetUint32(p+8);			break;		}		p += nn;		n -= nn;	} 	return 1;Err:	vtSetError(EBadMeta);	vacDirCleanup(dir);	return 0;} static intmetaBlockUnpack(MetaBlock *mb, uchar *p, int n){		mb->buf = p; 	if(n == 0) {		memset(mb, 0, sizeof(MetaBlock));		return 1;	} 	if(n < MetaHeaderSize) {		vtSetError("truncated meta block 1");		return 0;	}	if(vtGetUint32(p) != MetaMagic) {		vtSetError("bad meta block magic");		return 0;	}	mb->size = vtGetUint16(p+4);	mb->free = vtGetUint16(p+6);	mb->maxindex = vtGetUint16(p+8);	mb->nindex = vtGetUint16(p+10);	if(mb->size > n) {		vtSetError("bad meta block size");		return 0;	}	p += MetaHeaderSize;	n -= MetaHeaderSize; 	USED(p);	if(n < mb->maxindex*MetaIndexSize) { 		vtSetError("truncated meta block 2");		return 0;	}	return 1;}  static uchar *metaBlockEntry(MetaBlock *mb, int i, int *n){	uchar *p;	int eo, en; 	if(i < 0 || i >= mb->nindex) {		vtSetError("bad meta entry index");		return 0;	} 	p = mb->buf + MetaHeaderSize + i*MetaIndexSize;	eo = vtGetUint16(p);	en = vtGetUint16(p+2); 	if(eo < MetaHeaderSize + mb->maxindex*MetaIndexSize) {		vtSetError("corrupted entry in meta block");		return 0;	} 	if(eo+en > mb->size) { 		vtSetError("truncated meta block 3");		return 0;	}		p = mb->buf + eo;		/* make sure entry looks ok and includes an elem name */	if(en < 8 || vtGetUint32(p) != DirMagic || en < 8 + vtGetUint16(p+6)) {		vtSetError("corrupted meta block entry");		return 0;	} 	if(n != nil)		*n = en;	return p;} /* assumes a small amount of checking has been done in metaBlockEntry */static intmetaEntryCmp(uchar *p, char *s){	int n; 	p += 6;	n = vtGetUint16(p);	p += 2;	while(n > 0) {		if(*s == 0)			return -1;		if(*p < (uchar)*s)			return -1;		if(*p > (uchar)*s)			return 1;		p++;		s++;		n--;	}	return *s != 0;}  static intdirLookup(Source *meta, char *elem, VacDir *vd){	int i, j, nb, n;	MetaBlock mb;	uchar *p;	Lump *u;	int b, t, x; 	u = nil;	nb = (sourceGetSize(meta) + meta->dsize - 1)/meta->dsize;	for(i=0; i<nb; i++) {		u = sourceGetLump(meta, i, 1, 1);		if(u == nil)			continue;		if(!metaBlockUnpack(&mb, u->data, u->size))			goto Err;		if(0) {		for(j=0; j<mb.nindex; j++) {			p = metaBlockEntry(&mb, j, &n);			if(p == 0)				goto Err;			if(metaEntryCmp(p, elem) != 0)				continue;			if(!vacDirUnpack(vd, p, n))				goto Err;			lumpDecRef(u, 1);			return 1;		}		} else {		/* binary search within block */		b = 0;		t = mb.nindex;		while(b < t) {			j = (b+t)>>1;			p = metaBlockEntry(&mb, j, &n);			if(p == 0)				goto Err;			x = metaEntryCmp(p, elem); 			if(x == 0) {				if(!vacDirUnpack(vd, p, n))					goto Err;				lumpDecRef(u, 1);				return 1;			}					if(x < 0)				b = j+1;			else /* x > 0 */				t = j;		}		}				lumpDecRef(u, 1);		u = nil;	}	vtSetError("file does not exist");Err:	lumpDecRef(u, 1);	return 0;} static VacFile *vacFileAlloc(VacFS *fs){	VacFile *vf; 	vf = vtMemAllocZ(sizeof(VacFile));	vf->lk = vtLockAlloc();	vf->ref = 1;	vf->fs = fs;	return vf;}  VacFile *vacFileRoot(VacFS *fs, uchar *score){	Lump *u, *v;	Source *r0, *r1, *r2;	MetaBlock mb;	uchar *q;	int nn;	VacFile *root = nil; 	r0 = nil;	r1 = nil;	r2 = nil;	v = nil; 	u = cacheGetLump(fs->cache, score, VtDirType, fs->bsize);	if(u == nil)		goto Err;	if(!fs->readOnly) {		v = cacheAllocLump(fs->cache, VtDirType, fs->bsize, 1);		if(v == nil) {			vtUnlock(u->lk);			goto Err;		}		v->gen = u->gen;		v->size = u->size;		v->state = LumpActive;		memmove(v->data, u->data, v->size);		lumpDecRef(u, 1);		u = v;		v = nil;	}	vtUnlock(u->lk);	r0 = sourceAlloc(fs->cache, u, 0, fs->readOnly);	if(r0 == nil)		goto Err;	r1 = sourceAlloc(fs->cache, u, 1, fs->readOnly);	if(r1 == nil)		goto Err;	r2 = sourceAlloc(fs->cache, u, 2, fs->readOnly);	if(r2 == nil)		goto Err;	lumpDecRef(u, 0); 	root = vacFileAlloc(fs);	root->up = root;	root->source = r0;	r0 = nil; USED(r0);	root->msource = r1;	r1 = nil; USED(r1); 	u = sourceGetLump(r2, 0, 1, 0);	if(u == nil)		goto Err; 	if(!metaBlockUnpack(&mb, u->data, u->size))		goto Err; 	q = metaBlockEntry(&mb, 0, &nn);	if(q == nil)		goto Err;	if(!vacDirUnpack(&root->dir, q, nn))		goto Err;	lumpDecRef(u, 0);	sourceFree(r2); 	return root;Err:	lumpDecRef(u, 0);	lumpDecRef(v, 0);	if(r0)		sourceFree(r0);	if(r1)		sourceFree(r1);	if(r2)		sourceFree(r2);	if(root)		vacFileDecRef(root); 	return nil;}  VacFile *vacFileWalk(VacFile *vf, char *elem){	VacFile *nvf = nil; 	vtLock(vf->lk);	if(elem[0] == 0) {		vtSetError("illegal path element");		goto Err;	} 	if(vf->removed) {		vtSetError(ERemoved);		goto Err;	} 	if(!(vf->dir.mode & ModeDir)) {		vtSetError("not a directory");		goto Err;	} 	if(strcmp(elem, ".") == 0) {		vtUnlock(vf->lk);		return vacFileIncRef(vf);	} 	if(strcmp(elem, "..") == 0) {		vtUnlock(vf->lk);		return vacFileIncRef(vf->up);	} 	for(nvf = vf->down; nvf; nvf=nvf->next) {		vtLock(nvf->lk);		if(strcmp(elem, nvf->dir.elem) == 0 && !nvf->removed) {			nvf = vacFileIncRef(nvf);			vtUnlock(nvf->lk);			vtUnlock(vf->lk);			return nvf;		}		vtUnlock(nvf->lk);	} 	nvf = vacFileAlloc(vf->fs);	if(!dirLookup(vf->msource, elem, &nvf->dir))		goto Err;	nvf->source = sourceOpen(vf->source, nvf->dir.entry, vf->fs->readOnly);	if(nvf->source == nil)		goto Err;	if(nvf->dir.mode & ModeDir) {		nvf->msource = sourceOpen(vf->source, nvf->dir.entry+1, vf->fs->readOnly);		if(nvf->msource == nil)			goto Err;	}	nvf->next = vf->down;	vf->down = nvf;	nvf->up = vf;	vtUnlock(vf->lk);	vacFileIncRef(vf);//fprint(2, "opening %s\n", nvf->dir.elem);	return nvf;Err:	vtUnlock(vf->lk);	if(nvf) {		vacDirCleanup(&nvf->dir);		sourceFree(nvf->source);		sourceFree(nvf->msource);		vtLockFree(nvf->lk);		vtMemFree(nvf);	}	return nil;} VacFile *vacFileOpen(VacFS *fs, char *path){	VacFile *vf, *nvf;	char *p, elem[VtMaxStringSize];	int n; 	vf = fs->root;	vacFileIncRef(vf);	while(*path != 0) {		for(p = path; *p && *p != '/'; p++)			;		n = p - path;		if(n > 0) {			if(n > VtMaxStringSize) {				vtSetError("path element too long");				goto Err;			}			memmove(elem, path, n);			elem[n] = 0;			nvf = vacFileWalk(vf, elem);			if(nvf == nil)				goto Err;			vacFileDecRef(vf);			vf = nvf;		}		if(*p == '/')			p++;		path = p;	}

Lines 1–500 of 989.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t