code: mafs

ref: 56bd332994102b4e194819abb5e087a8895452f9
dir: /tests/offsets.c/

View raw version
#include <u.h>
#include <libc.h>

/*
	program to test mafswrite using different offsets and file lengths

	open the fsfile for ORDWR
	write the contents of -s startfile into it
	pwrite the contents of -a addfile at -o offset into it
	read the whole file and write it to stdout
 */

enum {
	Nlen = 128,
};

static void
usage(void)
{
	fprint(2, "usage: offsets [-D] -s startfile -o offset -a addfile fsfile\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	char *file, *startfile, *addfile;
	uvlong offset;
	int fd, sfd, afd, ns, na, nw;
	uchar buf[1024];
	int debug;

	debug = 0;
	startfile = addfile = nil;
	offset = 0;
	ARGBEGIN{
	default:	usage();
	case 'D':	debug++; break;
	case 's':	startfile = ARGF(); break;
	case 'a':	addfile = ARGF(); break;
	case 'o':	offset = atoi(EARGF(usage()));

	}ARGEND

	if(argc != 1)
		usage();

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

	if(file == nil || startfile == nil || addfile == nil || argc != 1)
		usage();

	fd = open(file, ORDWR);
	sfd = open(startfile, OREAD);
	afd = open(addfile, OREAD);

	ns = read(sfd, buf, 1024);
	write(fd, buf, ns);

	memset(buf, 0, 1024);
	na = read(afd, buf, 1024);
	pwrite(fd, buf, na, offset);

	memset(buf, 0, 1024);
	nw = pread(fd, buf, 1024, 0);

	if(debug)
		write(1, buf, nw);
	close(fd); close(sfd); close(afd);
	exits(nil);
}