summaryrefslogtreecommitdiff
path: root/sys/src/cmd/upas/imap4d/quota.c
blob: 375185d8087701d962567b5891fc51209e97915e (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
#include "imap4d.h"

static int
openpipe(int *pip, char *cmd, char *av[])
{
	int pid, fd[2];

	if(pipe(fd) != 0)
		sysfatal("pipe: %r");
	pid = fork();
	switch(pid){
	case -1:
		return -1;
	case 0:
		close(1);
		dup(fd[1], 1);
		if(fd[1] != 1)
			close(fd[1]);
		if(fd[0] != 0)
			close(fd[0]);
		exec(cmd, av);
		ilog("exec: %r");
		_exits("b0rked");
		return -1;
	default:
		*pip = fd[0];
		close(fd[1]);
		return pid;
	}
}

static int
closepipe(int pid, int fd)
{
	int nz, wpid;
	Waitmsg *w;

	close(fd);
	while(w = wait()){
		nz = !w->msg || !w->msg[0];
		wpid = w->pid;
		free(w);
		if(wpid == pid)
			return nz? 0: -1;
	}
	return -1;
}

static char dupath[Pathlen];
static char *duav[] = { "du", "-s", dupath, 0};

vlong
getquota(void)
{
	char buf[Pathlen + 128], *f[3];
	int fd, pid;

	werrstr("");
	memset(buf, 0, sizeof buf);
	snprint(dupath, sizeof dupath, "%s", mboxdir);
	pid = openpipe(&fd, "/bin/du", duav);
	if(pid == -1)
		return -1;
	if(read(fd, buf, sizeof buf) < 4){
		closepipe(pid, fd);
		return -1;
	}
	if(closepipe(pid, fd) == -1)
		return -1;
	if(getfields(buf, f, 2, 1, "\t") != 2)
		return -1;
	return strtoull(f[0], 0, 0);
}