summaryrefslogtreecommitdiff
path: root/package/aufs2-util/src/br.c
blob: 6451c120e6828ec8d0b86d88b7ffa0bc3406ddea (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2005-2009 Junjiro Okajima
 *
 * This program, aufs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#define _GNU_SOURCE /* strndup */

#include <sys/stat.h>
#include <sys/types.h>
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <linux/aufs_type.h>
#include "au_util.h"

static int by_opts(char ***br, int *nbr, char *bropt)
{
	char *p, **a;
	int l;

	/* bropts is placed at the end of mnt_opts */
	errno = EINVAL;
	//puts(bropt);
	if (strchr(bropt, ','))
		AuFin("%s", bropt);

	l = strlen(bropt);
	p = malloc(l + 2);
	if (!p)
		AuFin("malloc");
	memcpy(p, bropt, l + 1);
	bropt = p;
	bropt[l + 1] = 0; /* end marker */

	*nbr = 1;
	while (1) {
		p = strchr(p + 1, ':');
		if (!p)
			break;
		*p = 0;
		(*nbr)++;
	}

	a = malloc(sizeof(a) * (*nbr + 1));
	if (!a)
		AuFin("malloc");

	*br = a;
	*a++ = bropt;
	p = bropt;
	while (*p) {
		p += strlen(p) + 1;
		*a++ = p;
	}
	*--a = NULL;
	/* don't free bropt */

	return 0;
}

#ifdef DEBUG
#define SiPathPrefix	"/tmp/aufs/si_"
#define BufSiz		4
#else
#define SiPathPrefix	"/sys/fs/aufs/si_"
#define BufSiz		BUFSIZ
#endif

static int by_sysfs(char ***br, int *nbr, char *siopt)
{
	int err, i, l, sz;
	char buf[BufSiz], path[] = SiPathPrefix "1234567890123456/br32767";
	char *p, *end, **a, *q;
	FILE *fp;

	errno = EINVAL;
	end = strchr(siopt, ',');
	if (end)
		i = end - siopt;
	else
		i = strlen(siopt);

	strncpy(path + sizeof(SiPathPrefix) - 1, siopt, i);
	p = path + sizeof(SiPathPrefix) - 1 + i;
	strcpy(p, "/br");
	p += 3; /* "/br" */
	*nbr = 0;
	err = 0;
	while (!err) {
		sprintf(p, "%d", (*nbr)++);
		err = access(path, F_OK);
	}

	a = malloc(sizeof(*br) * *nbr);
	if (!a)
		AuFin("malloc");

	(*nbr)--;
	*br = a;
	for (i = 0; i < *nbr; i++) {
		sprintf(p, "%d", i);
		fp = fopen(path, "r");
		if (!fp)
			AuFin("%s", path);
		if (fgets(buf, sizeof(buf), fp) != buf)
			AuFin("%s", path);
		l = strlen(buf);
		if (l < 1)
			AuFin("internal error, %d", l);

		q = strndup(buf, l - 1);
		if (buf[l - 1] != '\n') {
			/* a branch path with crazy length */
			/* stat(2) for sysfs is meaningless */
			sz = sizeof(buf);
			do {
				free(q);
				sz <<= 1;
				q = malloc(sz);
				if (!q)
					AuFin("malloc");
				rewind(fp);
				if (fgets(q, sz, fp) != q)
					AuFin("%s", path);
				l = strlen(q);
			} while (q[l - 1] != '\n');
			q[l - 1] = 0;
		}

		*a++ = q;
		/* don't free q */
		fclose(fp); /* ignore */
	}
	*a = NULL;

	return 0;
}

#define BrOpt	",br:"
#define SiOpt	"si"
int au_br(char ***br, int *nbr, struct mntent *ent)
{
	char *p;

	*nbr = 0;
	p = strstr(ent->mnt_opts, BrOpt);
	if (p)
		return by_opts(br, nbr, p + sizeof(BrOpt) - 1);
	p = hasmntopt(ent, SiOpt);
	if (p)
		return by_sysfs(br, nbr, p + sizeof(SiOpt));

	/* broken opts */
	AuFin("internal error, %s", ent->mnt_opts);
	return -1; /* never reach here */
}