paul@181 | 1 | /* |
paul@181 | 2 | * mountopts.c --- convert between default mount options and strings |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 2002 Theodore Ts'o <tytso@mit.edu> |
paul@181 | 5 | * |
paul@181 | 6 | * %Begin-Header% |
paul@181 | 7 | * This file may be redistributed under the terms of the GNU Library |
paul@181 | 8 | * General Public License, version 2. |
paul@181 | 9 | * %End-Header% |
paul@181 | 10 | */ |
paul@181 | 11 | |
paul@181 | 12 | #include "config.h" |
paul@181 | 13 | #include <stdio.h> |
paul@181 | 14 | #include <stdlib.h> |
paul@181 | 15 | #include <string.h> |
paul@181 | 16 | #include <strings.h> |
paul@181 | 17 | #include <ctype.h> |
paul@181 | 18 | #include <errno.h> |
paul@181 | 19 | |
paul@181 | 20 | #include "e2p.h" |
paul@181 | 21 | |
paul@181 | 22 | struct mntopt { |
paul@181 | 23 | unsigned int mask; |
paul@181 | 24 | const char *string; |
paul@181 | 25 | }; |
paul@181 | 26 | |
paul@181 | 27 | static struct mntopt mntopt_list[] = { |
paul@181 | 28 | { EXT2_DEFM_DEBUG, "debug" }, |
paul@181 | 29 | { EXT2_DEFM_BSDGROUPS, "bsdgroups" }, |
paul@181 | 30 | { EXT2_DEFM_XATTR_USER, "user_xattr" }, |
paul@181 | 31 | { EXT2_DEFM_ACL, "acl" }, |
paul@181 | 32 | { EXT2_DEFM_UID16, "uid16" }, |
paul@181 | 33 | { EXT3_DEFM_JMODE_DATA, "journal_data" }, |
paul@181 | 34 | { EXT3_DEFM_JMODE_ORDERED, "journal_data_ordered" }, |
paul@181 | 35 | { EXT3_DEFM_JMODE_WBACK, "journal_data_writeback" }, |
paul@181 | 36 | { EXT4_DEFM_NOBARRIER, "nobarrier" }, |
paul@181 | 37 | { EXT4_DEFM_BLOCK_VALIDITY, "block_validity" }, |
paul@181 | 38 | { EXT4_DEFM_DISCARD, "discard"}, |
paul@181 | 39 | { EXT4_DEFM_NODELALLOC, "nodelalloc"}, |
paul@181 | 40 | { 0, 0 }, |
paul@181 | 41 | }; |
paul@181 | 42 | |
paul@181 | 43 | const char *e2p_mntopt2string(unsigned int mask) |
paul@181 | 44 | { |
paul@181 | 45 | struct mntopt *f; |
paul@181 | 46 | static char buf[20]; |
paul@181 | 47 | int fnum; |
paul@181 | 48 | |
paul@181 | 49 | for (f = mntopt_list; f->string; f++) { |
paul@181 | 50 | if (mask == f->mask) |
paul@181 | 51 | return f->string; |
paul@181 | 52 | } |
paul@181 | 53 | for (fnum = 0; mask >>= 1; fnum++); |
paul@181 | 54 | sprintf(buf, "MNTOPT_%d", fnum); |
paul@181 | 55 | return buf; |
paul@181 | 56 | } |
paul@181 | 57 | |
paul@181 | 58 | int e2p_string2mntopt(char *string, unsigned int *mask) |
paul@181 | 59 | { |
paul@181 | 60 | struct mntopt *f; |
paul@181 | 61 | char *eptr; |
paul@181 | 62 | int num; |
paul@181 | 63 | |
paul@181 | 64 | for (f = mntopt_list; f->string; f++) { |
paul@181 | 65 | if (!strcasecmp(string, f->string)) { |
paul@181 | 66 | *mask = f->mask; |
paul@181 | 67 | return 0; |
paul@181 | 68 | } |
paul@181 | 69 | } |
paul@181 | 70 | if (strncasecmp(string, "MNTOPT_", 7)) |
paul@181 | 71 | return 1; |
paul@181 | 72 | |
paul@181 | 73 | if (string[8] == 0) |
paul@181 | 74 | return 1; |
paul@181 | 75 | num = strtol(string+8, &eptr, 10); |
paul@181 | 76 | if (num > 31 || num < 0) |
paul@181 | 77 | return 1; |
paul@181 | 78 | if (*eptr) |
paul@181 | 79 | return 1; |
paul@181 | 80 | *mask = 1 << num; |
paul@181 | 81 | return 0; |
paul@181 | 82 | } |
paul@181 | 83 | |
paul@181 | 84 | static char *skip_over_blanks(char *cp) |
paul@181 | 85 | { |
paul@181 | 86 | while (*cp && isspace(*cp)) |
paul@181 | 87 | cp++; |
paul@181 | 88 | return cp; |
paul@181 | 89 | } |
paul@181 | 90 | |
paul@181 | 91 | static char *skip_over_word(char *cp) |
paul@181 | 92 | { |
paul@181 | 93 | while (*cp && !isspace(*cp) && *cp != ',') |
paul@181 | 94 | cp++; |
paul@181 | 95 | return cp; |
paul@181 | 96 | } |
paul@181 | 97 | |
paul@181 | 98 | /* |
paul@181 | 99 | * Edit a mntopt set array as requested by the user. The ok |
paul@181 | 100 | * parameter, if non-zero, allows the application to limit what |
paul@181 | 101 | * mntopts the user is allowed to set or clear using this function. |
paul@181 | 102 | */ |
paul@181 | 103 | int e2p_edit_mntopts(const char *str, __u32 *mntopts, __u32 ok) |
paul@181 | 104 | { |
paul@181 | 105 | char *cp, *buf, *next; |
paul@181 | 106 | int neg; |
paul@181 | 107 | unsigned int mask; |
paul@181 | 108 | int rc = 0; |
paul@181 | 109 | |
paul@181 | 110 | buf = malloc(strlen(str)+1); |
paul@181 | 111 | if (!buf) |
paul@181 | 112 | return 1; |
paul@181 | 113 | strcpy(buf, str); |
paul@181 | 114 | cp = buf; |
paul@181 | 115 | while (cp && *cp) { |
paul@181 | 116 | neg = 0; |
paul@181 | 117 | cp = skip_over_blanks(cp); |
paul@181 | 118 | next = skip_over_word(cp); |
paul@181 | 119 | if (*next == 0) |
paul@181 | 120 | next = 0; |
paul@181 | 121 | else |
paul@181 | 122 | *next = 0; |
paul@181 | 123 | switch (*cp) { |
paul@181 | 124 | case '-': |
paul@181 | 125 | case '^': |
paul@181 | 126 | neg++; |
paul@181 | 127 | /* fallthrough */ |
paul@181 | 128 | case '+': |
paul@181 | 129 | cp++; |
paul@181 | 130 | break; |
paul@181 | 131 | } |
paul@181 | 132 | if (e2p_string2mntopt(cp, &mask)) { |
paul@181 | 133 | rc = 1; |
paul@181 | 134 | break; |
paul@181 | 135 | } |
paul@181 | 136 | if (ok && !(ok & mask)) { |
paul@181 | 137 | rc = 1; |
paul@181 | 138 | break; |
paul@181 | 139 | } |
paul@181 | 140 | if (mask & EXT3_DEFM_JMODE) |
paul@181 | 141 | *mntopts &= ~EXT3_DEFM_JMODE; |
paul@181 | 142 | if (neg) |
paul@181 | 143 | *mntopts &= ~mask; |
paul@181 | 144 | else |
paul@181 | 145 | *mntopts |= mask; |
paul@181 | 146 | cp = next ? next+1 : 0; |
paul@181 | 147 | } |
paul@181 | 148 | free(buf); |
paul@181 | 149 | return rc; |
paul@181 | 150 | } |