summaryrefslogtreecommitdiff
path: root/sys/src/cmd/webfsget.c
blob: 906d7d1cbbcc3640bfb31b7f5b32fd068a802c76 (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
/* Example of how to use webfs */
#include <u.h>
#include <libc.h>

void
xfer(int from, int to)
{
	char buf[12*1024];
	int n;

	while((n = read(from, buf, sizeof buf)) > 0)
		if(write(to, buf, n) < 0)
			sysfatal("write failed: %r");
	if(n < 0)
		sysfatal("read failed: %r");
}

void
usage(void)
{
	fprint(2, "usage: webfsget [-b baseurl] [-m mtpt] [-p postbody] url\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	int conn, ctlfd, fd, n;
	char buf[128], *base, *mtpt, *post, *url;

	mtpt = "/mnt/web";
	post = nil;
	base = nil;
	ARGBEGIN{
	default:
		usage();
	case 'b':
		base = EARGF(usage());
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 'p':
		post = EARGF(usage());
		break;
	}ARGEND;

	if (argc != 1) 
		usage();

	url = argv[0];
 
	snprint(buf, sizeof buf, "%s/clone", mtpt);
	if((ctlfd = open(buf, ORDWR)) < 0)
		sysfatal("couldn't open %s: %r", buf);
	if((n = read(ctlfd, buf, sizeof buf-1)) < 0)
		sysfatal("reading clone: %r");
	if(n == 0)
		sysfatal("short read on clone");
	buf[n] = '\0';
	conn = atoi(buf);

	if(base)
		if(fprint(ctlfd, "baseurl %s", base) < 0)
			sysfatal("baseurl ctl write: %r");

	if(fprint(ctlfd, "url %s", url) <= 0)
		sysfatal("get ctl write: %r");

	if(post){
		snprint(buf, sizeof buf, "%s/%d/postbody", mtpt, conn);
		if((fd = open(buf, OWRITE)) < 0)
			sysfatal("open %s: %r", buf);
		if(write(fd, post, strlen(post)) < 0)
			sysfatal("post write failed: %r");
		close(fd);
	}

	snprint(buf, sizeof buf, "%s/%d/body", mtpt, conn);
	if((fd = open(buf, OREAD)) < 0)
		sysfatal("open %s: %r", buf);

	xfer(fd, 1);
	exits(nil);
}