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