aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vbackup/vcat.c
blob: 0346963b5c021681a69fad9a1143e164d52e370d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <u.h>
#include <libc.h>
#include <venti.h>
#include <diskfs.h>
#include <thread.h>

void
usage(void)
{
	fprint(2, "usage: vcat [-z] score >diskfile\n");
	threadexitsall("usage");
}

void
threadmain(int argc, char **argv)
{
	extern int nfilereads;
	char *pref;
	int zerotoo;
	uchar score[VtScoreSize];
	u8int *zero;
	u32int i;
	u32int n;
	Block *b;
	Disk *disk;
	Fsys *fsys;
	VtCache *c;
	VtConn *z;

	zerotoo = 0;
	ARGBEGIN{
	case 'z':
		zerotoo = 1;
		break;
	default:
		usage();
	}ARGEND

	if(argc != 1)
		usage();

	fmtinstall('V', vtscorefmt);

	if(vtparsescore(argv[0], &pref, score) < 0)
		sysfatal("bad score '%s'", argv[0]);
	if((z = vtdial(nil)) == nil)
		sysfatal("vtdial: %r");
	if(vtconnect(z) < 0)
		sysfatal("vtconnect: %r");
	if((c = vtcachealloc(z, 16384, 32)) == nil)
		sysfatal("vtcache: %r");
	if((disk = diskopenventi(c, score)) == nil)
		sysfatal("diskopenventi: %r");
	if((fsys = fsysopen(disk)) == nil)
		sysfatal("fsysopen: %r");

	zero = emalloc(fsys->blocksize);
	fprint(2, "%d blocks total\n", fsys->nblock);
	n = 0;
	for(i=0; i<fsys->nblock; i++){
		if((b = fsysreadblock(fsys, i)) != nil){
			if(pwrite(1, b->data, fsys->blocksize,
			    (u64int)fsys->blocksize*i) != fsys->blocksize)
				fprint(2, "error writing block %lud: %r\n", i);
			n++;
			blockput(b);
		}else if(zerotoo || i==fsys->nblock-1)
			if(pwrite(1, zero, fsys->blocksize,
			    (u64int)fsys->blocksize*i) != fsys->blocksize)
				fprint(2, "error writing block %lud: %r\n", i);
	}
	fprint(2, "%d blocks in use, %d file reads\n", n, nfilereads);
	threadexitsall(nil);
}