Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * mountopts.c --- convert between default mount options and strings |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 3 | * |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 4 | * Copyright (C) 2002 Theodore Ts'o <tytso@mit.edu> |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 5 | * |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 6 | * This file can be redistributed under the terms of the GNU Library General |
| 7 | * Public License |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 8 | * |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 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 mntopt { |
| 20 | unsigned int mask; |
| 21 | const char *string; |
| 22 | }; |
| 23 | |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 24 | static const struct mntopt mntopt_list[] = { |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 25 | { EXT2_DEFM_DEBUG, "debug" }, |
| 26 | { EXT2_DEFM_BSDGROUPS, "bsdgroups" }, |
| 27 | { EXT2_DEFM_XATTR_USER, "user_xattr" }, |
| 28 | { EXT2_DEFM_ACL, "acl" }, |
| 29 | { EXT2_DEFM_UID16, "uid16" }, |
| 30 | { EXT3_DEFM_JMODE_DATA, "journal_data" }, |
| 31 | { EXT3_DEFM_JMODE_ORDERED, "journal_data_ordered" }, |
| 32 | { EXT3_DEFM_JMODE_WBACK, "journal_data_writeback" }, |
| 33 | { 0, 0 }, |
| 34 | }; |
| 35 | |
| 36 | const char *e2p_mntopt2string(unsigned int mask) |
| 37 | { |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 38 | const struct mntopt *f; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 39 | static char buf[20]; |
| 40 | int fnum; |
| 41 | |
| 42 | for (f = mntopt_list; f->string; f++) { |
| 43 | if (mask == f->mask) |
| 44 | return f->string; |
| 45 | } |
| 46 | for (fnum = 0; mask >>= 1; fnum++); |
| 47 | sprintf(buf, "MNTOPT_%d", fnum); |
| 48 | return buf; |
| 49 | } |
| 50 | |
| 51 | int e2p_string2mntopt(char *string, unsigned int *mask) |
| 52 | { |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 53 | const struct mntopt *f; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 54 | char *eptr; |
| 55 | int num; |
| 56 | |
| 57 | for (f = mntopt_list; f->string; f++) { |
| 58 | if (!strcasecmp(string, f->string)) { |
| 59 | *mask = f->mask; |
| 60 | return 0; |
| 61 | } |
| 62 | } |
| 63 | if (strncasecmp(string, "MNTOPT_", 8)) |
| 64 | return 1; |
| 65 | |
| 66 | if (string[8] == 0) |
| 67 | return 1; |
| 68 | num = strtol(string+8, &eptr, 10); |
| 69 | if (num > 32 || num < 0) |
| 70 | return 1; |
| 71 | if (*eptr) |
| 72 | return 1; |
| 73 | *mask = 1 << num; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static char *skip_over_blanks(char *cp) |
| 78 | { |
| 79 | while (*cp && isspace(*cp)) |
| 80 | cp++; |
| 81 | return cp; |
| 82 | } |
| 83 | |
| 84 | static char *skip_over_word(char *cp) |
| 85 | { |
| 86 | while (*cp && !isspace(*cp) && *cp != ',') |
| 87 | cp++; |
| 88 | return cp; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Edit a mntopt set array as requested by the user. The ok |
| 93 | * parameter, if non-zero, allows the application to limit what |
| 94 | * mntopts the user is allowed to set or clear using this function. |
| 95 | */ |
| 96 | int e2p_edit_mntopts(const char *str, __u32 *mntopts, __u32 ok) |
| 97 | { |
| 98 | char *cp, *buf, *next; |
| 99 | int neg; |
| 100 | unsigned int mask; |
| 101 | |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 102 | buf = bb_xstrdup(str); |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 103 | cp = buf; |
| 104 | while (cp && *cp) { |
| 105 | neg = 0; |
| 106 | cp = skip_over_blanks(cp); |
| 107 | next = skip_over_word(cp); |
| 108 | if (*next == 0) |
| 109 | next = 0; |
| 110 | else |
| 111 | *next = 0; |
| 112 | switch (*cp) { |
| 113 | case '-': |
| 114 | case '^': |
| 115 | neg++; |
| 116 | case '+': |
| 117 | cp++; |
| 118 | break; |
| 119 | } |
| 120 | if (e2p_string2mntopt(cp, &mask)) |
| 121 | return 1; |
| 122 | if (ok && !(ok & mask)) |
| 123 | return 1; |
| 124 | if (mask & EXT3_DEFM_JMODE) |
| 125 | *mntopts &= ~EXT3_DEFM_JMODE; |
| 126 | if (neg) |
| 127 | *mntopts &= ~mask; |
| 128 | else |
| 129 | *mntopts |= mask; |
| 130 | cp = next ? next+1 : 0; |
| 131 | } |
| 132 | return 0; |
| 133 | } |