blob: 0a9058384d6fc7b8dc88798be1cc727a9f1d4dae [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
2 * feature.c --- convert between features and strings
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00004 * Copyright (C) 1999 Theodore Ts'o <tytso@mit.edu>
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00005 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00006 * This file can be redistributed under the terms of the GNU Library General
7 * Public License
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00008 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00009 */
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
19struct feature {
20 int compat;
21 unsigned int mask;
22 const char *string;
23};
24
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000025static const struct feature feature_list[] = {
Mike Frysingerd89e6292005-04-24 05:07:59 +000026 { 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, EXT3_FEATURE_INCOMPAT_EXTENTS,
51 "extents" },
52 { E2P_FEATURE_INCOMPAT, EXT2_FEATURE_INCOMPAT_META_BG,
53 "meta_bg" },
54 { 0, 0, 0 },
55};
56
57const char *e2p_feature2string(int compat, unsigned int mask)
58{
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000059 const struct feature *f;
Mike Frysingerd89e6292005-04-24 05:07:59 +000060 static char buf[20];
Mike Frysinger85cffcc2005-06-11 00:08:50 +000061 char fchar;
62 int fnum;
Mike Frysingerd89e6292005-04-24 05:07:59 +000063
64 for (f = feature_list; f->string; f++) {
65 if ((compat == f->compat) &&
66 (mask == f->mask))
67 return f->string;
68 }
69 switch (compat) {
Mike Frysinger85cffcc2005-06-11 00:08:50 +000070 case E2P_FEATURE_COMPAT:
Mike Frysingerd89e6292005-04-24 05:07:59 +000071 fchar = 'C';
72 break;
73 case E2P_FEATURE_INCOMPAT:
74 fchar = 'I';
75 break;
76 case E2P_FEATURE_RO_INCOMPAT:
77 fchar = 'R';
78 break;
79 default:
80 fchar = '?';
81 break;
82 }
83 for (fnum = 0; mask >>= 1; fnum++);
Mike Frysinger85cffcc2005-06-11 00:08:50 +000084 sprintf(buf, "FEATURE_%c%d", fchar, fnum);
Mike Frysingerd89e6292005-04-24 05:07:59 +000085 return buf;
86}
87
88int e2p_string2feature(char *string, int *compat_type, unsigned int *mask)
89{
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000090 const struct feature *f;
Mike Frysinger85cffcc2005-06-11 00:08:50 +000091 char *eptr;
92 int num;
Mike Frysingerd89e6292005-04-24 05:07:59 +000093
94 for (f = feature_list; f->string; f++) {
95 if (!strcasecmp(string, f->string)) {
96 *compat_type = f->compat;
97 *mask = f->mask;
98 return 0;
99 }
100 }
101 if (strncasecmp(string, "FEATURE_", 8))
102 return 1;
103
104 switch (string[8]) {
105 case 'c':
106 case 'C':
107 *compat_type = E2P_FEATURE_COMPAT;
108 break;
109 case 'i':
110 case 'I':
111 *compat_type = E2P_FEATURE_INCOMPAT;
112 break;
113 case 'r':
114 case 'R':
115 *compat_type = E2P_FEATURE_RO_INCOMPAT;
116 break;
117 default:
118 return 1;
119 }
120 if (string[9] == 0)
121 return 1;
122 num = strtol(string+9, &eptr, 10);
123 if (num > 32 || num < 0)
124 return 1;
125 if (*eptr)
126 return 1;
127 *mask = 1 << num;
128 return 0;
129}
130
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000131static inline char *skip_over_blanks(char *cp)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000132{
133 while (*cp && isspace(*cp))
134 cp++;
135 return cp;
136}
137
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000138static inline char *skip_over_word(char *cp)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000139{
140 while (*cp && !isspace(*cp) && *cp != ',')
141 cp++;
142 return cp;
143}
144
145/*
146 * Edit a feature set array as requested by the user. The ok_array,
147 * if set, allows the application to limit what features the user is
148 * allowed to set or clear using this function.
149 */
150int e2p_edit_feature(const char *str, __u32 *compat_array, __u32 *ok_array)
151{
152 char *cp, *buf, *next;
153 int neg;
154 unsigned int mask;
155 int compat_type;
156
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +0000157 buf = bb_xstrdup(str);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000158 cp = buf;
159 while (cp && *cp) {
160 neg = 0;
161 cp = skip_over_blanks(cp);
162 next = skip_over_word(cp);
163 if (*next == 0)
164 next = 0;
165 else
166 *next = 0;
167 switch (*cp) {
168 case '-':
169 case '^':
170 neg++;
171 case '+':
172 cp++;
173 break;
174 }
175 if (e2p_string2feature(cp, &compat_type, &mask))
176 return 1;
177 if (ok_array && !(ok_array[compat_type] & mask))
178 return 1;
179 if (neg)
180 compat_array[compat_type] &= ~mask;
181 else
182 compat_array[compat_type] |= mask;
183 cp = next ? next+1 : 0;
184 }
185 return 0;
186}