code: mafs

ref: 5f15f2246d0fdac0fa98a0a910d40827edfaf68f
dir: /updatefrees.c/

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

/*
	update /adm/frees

	disk/unused <{disk/used tests/test.0/disk} 32 | tr -d '	' > /mnt/term/tmp/unused.blocks
	cat /mnt/term/tmp/unused.blocks
	disk/updatefrees tests/test.0/disk /mnt/term/tmp/unused.blocks
 */

int debug = 0;
char *devfile = nil, *freesfile = nil;

static void
usage(void)
{
	fprint(2, "usage: updatefrees [-D] fsfile freecontentfile\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	u64 size, freeblkno;
	s32 nfreesize;
	u8 buf[Rawblocksize], frees[Rawblocksize];
	int fd;
	Content *c;

	ARGBEGIN{
	default:	usage();
	case 'D':	debug++; break;
	}ARGEND

	if(argc != 2)
		usage();

	devfile = argv[0];
	if(devfile == nil)
		sysfatal("no disk file");

	freesfile = strdup(argv[1]);
	if(freesfile == nil)
		sysfatal("no frees file");

	memset(buf,0,Rawblocksize);
	memset(frees, 0, Rawblocksize);

	fd = open(freesfile, OREAD);
	if(fd < 0)
		sysfatal("updatefrees: cannot open freesfile %s\n", freesfile);

	nfreesize = read(fd, frees, Rawblocksize);
	if(nfreesize <= 0)
		sysfatal("updatefrees: nfreesize %d <= 0\n", nfreesize);
	if(nfreesize > Blocksize)
		sysfatal("updatefrees: unsupported nfreesize %d > Blocksize %llud\n",
					nfreesize, Blocksize);
	close(fd);

	freeblkno = atoll((s8*)frees);

	if (access(devfile, AREAD|AWRITE) == -1)
		sysfatal("%s cannot access device", devfile);

	size = devinit(devfile);
	if(size == 0)
		panic("null size %s", devfile);

	if(debug){
		print("Dentry1 size %d Namelen %llud\n", sizeof(Dentry1), Namelen);
		print("Dentry size %d\n", sizeof(Dentry));
	}

	devread(Bdfrees, buf);
	// showblock(1, buf);

	c = (Content*)buf;
	if(c->d.size != nfreesize)
		print("changed size: %llud to %d\n", c->d.size, nfreesize);
	c->d.size = nfreesize;
	if(c->d.dblocks[0] != freeblkno)
		print("changed dblocks[0]: %llud to %llud\n", c->d.dblocks[0], freeblkno);
	c->d.dblocks[0] = freeblkno;

	c = (Content*)frees;
	c->type = Tdata;
	c->path = Qpfrees;

	devwrite(Bdfrees, buf);
	devwrite(freeblkno, frees);

	close(devfd);
	exits(0);
}