aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vbackup/diskftp.c
blob: 33d278b5ab99a354d30cb2f998fbe65e382034b2 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <sunrpc.h>
#include <nfs3.h>
#include <diskfs.h>

int debug;

void
usage(void)
{
	fprint(2, "usage: fsview fspartition cmd\n");
	fprint(2, "cmd is:\n");
	fprint(2, "\tcat file\n");
	fprint(2, "\tls dir\n");
	fprint(2, "\tstat file\n");
	threadexitsall("usage");
}

void
printattr(Nfs3Attr *attr)
{
	Fmt fmt;
	char buf[256];

	fmtfdinit(&fmt, 2, buf, sizeof buf);
	nfs3attrprint(&fmt, attr);
	fmtfdflush(&fmt);
	fprint(2, "\n");
}

char buf[8192];

void
x(int ok)
{
	if(ok != Nfs3Ok){
		nfs3errstr(ok);
		sysfatal("%r");
	}
}

void
threadmain(int argc, char **argv)
{
	char *p, *q;
	u32int n;
	Disk *disk;
	Fsys *fsys;
	Nfs3Handle h;
	SunAuthUnix au;
	Nfs3Attr attr;
	u64int offset;
	u1int eof;
	uchar *data;
	char *link;

	ARGBEGIN{
	case 'd':
		debug = 1;
		break;
	default:
		usage();
	}ARGEND

	if(argc != 3)
		usage();

	if((disk = diskopenfile(argv[0])) == nil)
		sysfatal("diskopen: %r");
	if((disk = diskcache(disk, 16384, 16)) == nil)
		sysfatal("diskcache: %r");
	if((fsys = fsysopen(disk)) == nil)
		sysfatal("fsysopen: %r");

	allowall = 1;
	memset(&au, 0, sizeof au);

	/* walk */
	if(debug) fprint(2, "get root...");
	x(fsysroot(fsys, &h));
	p = argv[2];
	while(*p){
		while(*p == '/')
			p++;
		if(*p == 0)
			break;
		q = strchr(p, '/');
		if(q){
			*q = 0;
			q++;
		}else
			q = "";
		if(debug) fprint(2, "walk %s...", p);
		x(fsyslookup(fsys, &au, &h, p, &h));
		p = q;
	}

	if(debug) fprint(2, "getattr...");
	x(fsysgetattr(fsys, &au, &h, &attr));
	printattr(&attr);

	/* do the op */
	if(strcmp(argv[1], "cat") == 0){
		switch(attr.type){
		case Nfs3FileReg:
		case Nfs3FileDir:
			offset = 0;
			for(;;){
				x(fsysreadfile(fsys, &au, &h, sizeof buf, offset, &data, &n, &eof));
				if(n){
					write(1, data, n);
					free(data);
					offset += n;
				}
				if(eof)
					break;
			}
			break;
		case Nfs3FileSymlink:
			x(fsysreadlink(fsys, &au, &h, &link));
			print("%s\n", link);
			break;
		default:
			print("cannot cat: not file, not link\n");
			break;
		}
	}else if(strcmp(argv[1], "ls") == 0){
		/* not implemented */
	}else if(strcmp(argv[1], "stat") == 0){
		/* already done */
	}
	threadexitsall(nil);
}