code: mafs

ref: e82f565015fd0b75085d8d2a4f1aaca53e875f64
dir: /config.c/

View raw version
#include "all.h"

/* assuming that the config will not be more than a block */
void
initconfig(u64 dblkno)
{
	Iobuf *buf;
	char *tokens[128], cfg[Blocksize], *nl;
	s32 n, i;

	buf = getbufchk(dblkno, Breadonly, Tdata, Qpconfig);
	if(buf == nil)
		panic("cannot find config file in %llud block\n", dblkno);
	memcpy(cfg, buf->io->buf, Blocksize);
	putbuf(buf);

	n = tokenize(cfg, tokens, 128);
	if(n > 128)
		panic("cannot read config, increase tokens size\n");
	if(n == 0){
		panic("nil config");
	}
	for(i = 0; i<n; i++){
		if(strcmp(tokens[i], "nblocks") == 0){
			if((config.nblocks = atoll(tokens[++i])) == 0)
				panic("mafs: %s initconfig(): invalid nblocks\n", service);
		}else if(strcmp(tokens[i], "size") == 0){
			if((config.size = atoll(tokens[++i])) == 0)
				panic("mafs: %s initconfig(): invalid size\n", service);
		}else if(strcmp(tokens[i], "backup") == 0 &&
					strcmp(tokens[i+1], "config") == 0){
				i++;
				if((config.config.srcbno = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
				i++; /* skip over to */
				if((config.config.dest[0] = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
				if((config.config.dest[1] = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
		}else if(strcmp(tokens[i], "backup") == 0 &&
					strcmp(tokens[i+1], "super") == 0){
				i++;
				if((config.super.srcbno = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
				i++; /* skip over to */
				if((config.super.dest[0] = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
				if((config.super.dest[1] = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
		}else if(strcmp(tokens[i], "backup") == 0 &&
					strcmp(tokens[i+1], "root") == 0){
				i++;
				if((config.root.srcbno = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
				i++; /* skip over to */
				if((config.root.dest[0] = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
				if((config.root.dest[1] = atoll(tokens[++i])) == 0)
					panic("mafs: %s initconfig(): invalid size\n", service);
		}else if(strcmp(tokens[i], "service") == 0){
			snprint(config.service, Namelen,"%s", tokens[++i]);
			if((nl=strchr(config.service, '\n')) != nil)
				*nl = '\0';
		}
	}
	if(chatty9p > 1)
	dprint("config:\n"
			"size %llud\n"
			"nblocks %llud\n"
			"backup config %llud to %llud %llud\n"
			"backup super %llud to %llud %llud\n"
			"backup root %llud to %llud %llud\n",
			"service %s\n",
			config.size,
			config.nblocks,
			config.config.srcbno,
			config.config.dest[0],
			config.config.dest[1],
			config.super.srcbno,
			config.super.dest[0],
			config.super.dest[1],
			config.root.srcbno,
			config.root.dest[0],
			config.root.dest[1],
			config.service);
}

void
writeconfig(u64 bno)
{
	Iobuf *buf;
	s32 n;

	buf = getbufchk(bno, Bwritable, Tdata, Qpconfig);
	if(buf == nil)
		panic("cannot write config");
	n = snprint((s8*)buf->io->buf, Blocksize,
			"size %llud\n"
			"nblocks %llud\n"
			"backup config %llud to %llud %llud\n"
			"backup super %llud to %llud %llud\n"
			"backup root %llud to %llud %llud\n"
			"service %s\n",
			config.size,
			config.nblocks,
			config.config.srcbno,
			config.config.dest[0],
			config.config.dest[1],
			config.super.srcbno,
			config.super.dest[0],
			config.super.dest[1],
			config.root.srcbno,
			config.root.dest[0],
			config.root.dest[1],
			config.service);
	if(n == Blocksize)
		panic("config needs more blocks");
	if(chatty9p > 1)
		dprint("config:\n%s", buf->io->buf);
	putbuf(buf);
}