]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/e2p/feature.c
Merge branch 'maint'
[thirdparty/e2fsprogs.git] / lib / e2p / feature.c
1 /*
2 * feature.c --- convert between features and strings
3 *
4 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
5 *
6 * This file can be redistributed under the terms of the GNU Library General
7 * Public License
8 *
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <errno.h>
16
17 #include "e2p.h"
18
19 struct feature {
20 int compat;
21 unsigned int mask;
22 const char *string;
23 };
24
25 static struct feature feature_list[] = {
26 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_PREALLOC,
27 "dir_prealloc" },
28 { E2P_FEATURE_COMPAT, EXT3_FEATURE_COMPAT_HAS_JOURNAL,
29 "has_journal" },
30 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_IMAGIC_INODES,
31 "imagic_inodes" },
32 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_EXT_ATTR,
33 "ext_attr" },
34 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_DIR_INDEX,
35 "dir_index" },
36 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_RESIZE_INODE,
37 "resize_inode" },
38 { E2P_FEATURE_COMPAT, EXT2_FEATURE_COMPAT_LAZY_BG,
39 "lazy_bg" },
40
41 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
42 "sparse_super" },
43 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
44 "large_file" },
45 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_HUGE_FILE,
46 "huge_file" },
47 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
48 "uninit_bg" },
49 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_GDT_CSUM,
50 "uninit_groups" },
51 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_DIR_NLINK,
52 "dir_nlink" },
53 { E2P_FEATURE_RO_INCOMPAT, EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE,
54 "extra_isize" },
55
56 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
57 "compression" },
58 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
59 "filetype" },
60 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
61 "needs_recovery" },
62 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
63 "journal_dev" },
64 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
65 "extent" },
66 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_EXTENTS,
67 "extents" },
68 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
69 "meta_bg" },
70 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_64BIT,
71 "64bit" },
72 { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG,
73 "flex_bg"},
74 { 0, 0, 0 },
75 };
76
77 const char *e2p_feature2string(int compat, unsigned int mask)
78 {
79 struct feature *f;
80 static char buf[20];
81 char fchar;
82 int fnum;
83
84 for (f = feature_list; f->string; f++) {
85 if ((compat == f->compat) &&
86 (mask == f->mask))
87 return f->string;
88 }
89 switch (compat) {
90 case E2P_FEATURE_COMPAT:
91 fchar = 'C';
92 break;
93 case E2P_FEATURE_INCOMPAT:
94 fchar = 'I';
95 break;
96 case E2P_FEATURE_RO_INCOMPAT:
97 fchar = 'R';
98 break;
99 default:
100 fchar = '?';
101 break;
102 }
103 for (fnum = 0; mask >>= 1; fnum++);
104 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
105 return buf;
106 }
107
108 int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
109 {
110 struct feature *f;
111 char *eptr;
112 int num;
113
114 for (f = feature_list; f->string; f++) {
115 if (!strcasecmp(string, f->string)) {
116 *compat_type = f->compat;
117 *mask = f->mask;
118 return 0;
119 }
120 }
121 if (strncasecmp(string, "FEATURE_", 8))
122 return 1;
123
124 switch (string[8]) {
125 case 'c':
126 case 'C':
127 *compat_type = E2P_FEATURE_COMPAT;
128 break;
129 case 'i':
130 case 'I':
131 *compat_type = E2P_FEATURE_INCOMPAT;
132 break;
133 case 'r':
134 case 'R':
135 *compat_type = E2P_FEATURE_RO_INCOMPAT;
136 break;
137 default:
138 return 1;
139 }
140 if (string[9] == 0)
141 return 1;
142 num = strtol(string+9, &eptr, 10);
143 if (num > 32 || num < 0)
144 return 1;
145 if (*eptr)
146 return 1;
147 *mask = 1 << num;
148 return 0;
149 }
150
151 static char *skip_over_blanks(char *cp)
152 {
153 while (*cp && isspace(*cp))
154 cp++;
155 return cp;
156 }
157
158 static char *skip_over_word(char *cp)
159 {
160 while (*cp && !isspace(*cp) && *cp != ',')
161 cp++;
162 return cp;
163 }
164
165 /*
166 * Edit a feature set array as requested by the user. The ok_array,
167 * if set, allows the application to limit what features the user is
168 * allowed to set or clear using this function. If clear_ok_array is set,
169 * then use it tell whether or not it is OK to clear a filesystem feature.
170 */
171 int e2p_edit_feature2(const char *str, __u32 *compat_array, __u32 *ok_array,
172 __u32 *clear_ok_array, int *type_err,
173 unsigned int *mask_err)
174 {
175 char *cp, *buf, *next;
176 int neg;
177 unsigned int mask;
178 int compat_type;
179 int rc = 0;
180
181 if (!clear_ok_array)
182 clear_ok_array = ok_array;
183
184 if (type_err)
185 *type_err = 0;
186 if (mask_err)
187 *mask_err = 0;
188
189 buf = malloc(strlen(str)+1);
190 if (!buf)
191 return 1;
192 strcpy(buf, str);
193 for (cp = buf; cp && *cp; cp = next ? next+1 : 0) {
194 neg = 0;
195 cp = skip_over_blanks(cp);
196 next = skip_over_word(cp);
197
198 if (*next == 0)
199 next = 0;
200 else
201 *next = 0;
202
203 if ((strcasecmp(cp, "none") == 0) ||
204 (strcasecmp(cp, "clear") == 0)) {
205 compat_array[0] = 0;
206 compat_array[1] = 0;
207 compat_array[2] = 0;
208 continue;
209 }
210
211 switch (*cp) {
212 case '-':
213 case '^':
214 neg++;
215 case '+':
216 cp++;
217 break;
218 }
219 if (e2p_string2feature(cp, &compat_type, &mask)) {
220 rc = 1;
221 break;
222 }
223 if (neg) {
224 if (clear_ok_array &&
225 !(clear_ok_array[compat_type] & mask)) {
226 rc = 1;
227 if (type_err)
228 *type_err = (compat_type |
229 E2P_FEATURE_NEGATE_FLAG);
230 if (mask_err)
231 *mask_err = mask;
232 break;
233 }
234 compat_array[compat_type] &= ~mask;
235 } else {
236 if (ok_array && !(ok_array[compat_type] & mask)) {
237 rc = 1;
238 if (type_err)
239 *type_err = compat_type;
240 if (mask_err)
241 *mask_err = mask;
242 break;
243 }
244 compat_array[compat_type] |= mask;
245 }
246 }
247 free(buf);
248 return rc;
249 }
250
251 int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
252 {
253 return e2p_edit_feature2(str, compat_array, ok_array, 0, 0, 0);
254 }