]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/e2p/feature.c
Remove EXT2_FEATURE_RO_COMPAT_BTREE_DIR mention of since it's not
[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_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER,
39 "sparse_super" },
40 { E2P_FEATURE_RO_INCOMPAT, EXT2_FEATURE_RO_COMPAT_LARGE_FILE,
41 "large_file" },
42 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_COMPRESSION,
43 "compression" },
44 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_FILETYPE,
45 "filetype" },
46 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_RECOVER,
47 "needs_recovery" },
48 { E2P_FEATURE_INCOMPAT, EXT3_FEATURE_INCOMPAT_JOURNAL_DEV,
49 "journal_dev" },
50 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
51 "meta_bg" },
52 { 0, 0, 0 },
53 };
54
55 const char *e2p_feature2string(int compat, unsigned int mask)
56 {
57 struct feature *f;
58 static char buf[20];
59 char fchar;
60 int fnum;
61
62 for (f = feature_list; f->string; f++) {
63 if ((compat == f->compat) &&
64 (mask == f->mask))
65 return f->string;
66 }
67 switch (compat) {
68 case E2P_FEATURE_COMPAT:
69 fchar = 'C';
70 break;
71 case E2P_FEATURE_INCOMPAT:
72 fchar = 'I';
73 break;
74 case E2P_FEATURE_RO_INCOMPAT:
75 fchar = 'R';
76 break;
77 default:
78 fchar = '?';
79 break;
80 }
81 for (fnum = 0; mask >>= 1; fnum++);
82 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
83 return buf;
84 }
85
86 int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
87 {
88 struct feature *f;
89 char *eptr;
90 int num;
91
92 for (f = feature_list; f->string; f++) {
93 if (!strcasecmp(string, f->string)) {
94 *compat_type = f->compat;
95 *mask = f->mask;
96 return 0;
97 }
98 }
99 if (strncasecmp(string, "FEATURE_", 8))
100 return 1;
101
102 switch (string[8]) {
103 case 'c':
104 case 'C':
105 *compat_type = E2P_FEATURE_COMPAT;
106 break;
107 case 'i':
108 case 'I':
109 *compat_type = E2P_FEATURE_INCOMPAT;
110 break;
111 case 'r':
112 case 'R':
113 *compat_type = E2P_FEATURE_RO_INCOMPAT;
114 break;
115 default:
116 return 1;
117 }
118 if (string[9] == 0)
119 return 1;
120 num = strtol(string+9, &eptr, 10);
121 if (num > 32 || num < 0)
122 return 1;
123 if (*eptr)
124 return 1;
125 *mask = 1 << num;
126 return 0;
127 }
128
129 static char *skip_over_blanks(char *cp)
130 {
131 while (*cp && isspace(*cp))
132 cp++;
133 return cp;
134 }
135
136 static char *skip_over_word(char *cp)
137 {
138 while (*cp && !isspace(*cp) && *cp != ',')
139 cp++;
140 return cp;
141 }
142
143 /*
144 * Edit a feature set array as requested by the user. The ok_array,
145 * if set, allows the application to limit what features the user is
146 * allowed to set or clear using this function.
147 */
148 int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
149 {
150 char *cp, *buf, *next;
151 int neg;
152 unsigned int mask;
153 int compat_type;
154
155 buf = malloc(strlen(str)+1);
156 if (!buf)
157 return 1;
158 strcpy(buf, str);
159 cp = buf;
160 while (cp && *cp) {
161 neg = 0;
162 cp = skip_over_blanks(cp);
163 next = skip_over_word(cp);
164 if (*next == 0)
165 next = 0;
166 else
167 *next = 0;
168 switch (*cp) {
169 case '-':
170 case '^':
171 neg++;
172 case '+':
173 cp++;
174 break;
175 }
176 if (e2p_string2feature(cp, &compat_type, &mask))
177 return 1;
178 if (ok_array && !(ok_array[compat_type] & mask))
179 return 1;
180 if (neg)
181 compat_array[compat_type] &= ~mask;
182 else
183 compat_array[compat_type] |= mask;
184 cp = next ? next+1 : 0;
185 }
186 return 0;
187 }
188