blob: a4f3c64cb5790f63c8b23556c6198230795f15bb [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
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
19struct feature {
20 int compat;
21 unsigned int mask;
22 const char *string;
23};
24
25static 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, 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{
Mike Frysinger85cffcc2005-06-11 00:08:50 +000059 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{
Mike Frysinger85cffcc2005-06-11 00:08:50 +000090 struct feature *f;
91 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
Mike Frysingerd1a9d572005-06-11 22:25:27 +0000157 buf = xmalloc(strlen(str)+1);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000158 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}