summaryrefslogtreecommitdiffstats
path: root/lib/e2p/mntopts.c
blob: 7d0ae9caece8f1fd8877a77c8426419d795669df (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
/*
 * mountopts.c --- convert between default mount options and strings
 *
 * Copyright (C) 2002  Theodore Ts'o <tytso@mit.edu>
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Library
 * General Public License, version 2.
 * %End-Header%
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <errno.h>

#include "e2p.h"

struct mntopt {
	unsigned int	mask;
	const char	*string;
};

static struct mntopt mntopt_list[] = {
	{ EXT2_DEFM_DEBUG,	"debug" },
	{ EXT2_DEFM_BSDGROUPS,	"bsdgroups" },
	{ EXT2_DEFM_XATTR_USER,	"user_xattr" },
	{ EXT2_DEFM_ACL,	"acl" },
	{ EXT2_DEFM_UID16,	"uid16" },
	{ EXT3_DEFM_JMODE_DATA, "journal_data" },
	{ EXT3_DEFM_JMODE_ORDERED, "journal_data_ordered" },
	{ EXT3_DEFM_JMODE_WBACK, "journal_data_writeback" },
	{ EXT4_DEFM_NOBARRIER,	"nobarrier" },
	{ EXT4_DEFM_BLOCK_VALIDITY, "block_validity" },
	{ EXT4_DEFM_DISCARD,	"discard"},
	{ EXT4_DEFM_NODELALLOC,	"nodelalloc"},
	{ 0, 0 },
};

const char *e2p_mntopt2string(unsigned int mask)
{
	struct mntopt  *f;
	static char buf[20];
	int	fnum;

	for (f = mntopt_list; f->string; f++) {
		if (mask == f->mask)
			return f->string;
	}
	for (fnum = 0; mask >>= 1; fnum++);
	sprintf(buf, "MNTOPT_%d", fnum);
	return buf;
}

int e2p_string2mntopt(char *string, unsigned int *mask)
{
	struct mntopt  *f;
	char		*eptr;
	int		num;

	for (f = mntopt_list; f->string; f++) {
		if (!strcasecmp(string, f->string)) {
			*mask = f->mask;
			return 0;
		}
	}
	if (strncasecmp(string, "MNTOPT_", 7))
		return 1;

	if (string[8] == 0)
		return 1;
	num = strtol(string+8, &eptr, 10);
	if (num > 31 || num < 0)
		return 1;
	if (*eptr)
		return 1;
	*mask = 1 << num;
	return 0;
}

static char *skip_over_blanks(char *cp)
{
	while (*cp && isspace(*cp))
		cp++;
	return cp;
}

static char *skip_over_word(char *cp)
{
	while (*cp && !isspace(*cp) && *cp != ',')
		cp++;
	return cp;
}

/*
 * Edit a mntopt set array as requested by the user.  The ok
 * parameter, if non-zero, allows the application to limit what
 * mntopts the user is allowed to set or clear using this function.
 */
int e2p_edit_mntopts(const char *str, __u32 *mntopts, __u32 ok)
{
	char	*cp, *buf, *next;
	int	neg;
	unsigned int	mask;
	int	rc = 0;

	buf = malloc(strlen(str)+1);
	if (!buf)
		return 1;
	strcpy(buf, str);
	cp = buf;
	while (cp && *cp) {
		neg = 0;
		cp = skip_over_blanks(cp);
		next = skip_over_word(cp);
		if (*next == 0)
			next = 0;
		else
			*next = 0;
		switch (*cp) {
		case '-':
		case '^':
			neg++;
			/* fallthrough */
		case '+':
			cp++;
			break;
		}
		if (e2p_string2mntopt(cp, &mask)) {
			rc = 1;
			break;
		}
		if (ok && !(ok & mask)) {
			rc = 1;
			break;
		}
		if (mask & EXT3_DEFM_JMODE)
			*mntopts &= ~EXT3_DEFM_JMODE;
		if (neg)
			*mntopts &= ~mask;
		else
			*mntopts |= mask;
		cp = next ? next+1 : 0;
	}
	free(buf);
	return rc;
}