1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
/*
* parse_opt.c -- mount option string parsing helpers
*
* Copyright (C) 2007 Oracle. All rights reserved.
* Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*
*/
/*
* Converting a C string containing mount options to a data object
* and manipulating that object is cleaner in C than manipulating
* the C string itself. This is similar to the way Python handles
* string manipulation.
*
* The current implementation uses a linked list as the data object
* since lists are simple, and we don't need to worry about more
* than ten or twenty options at a time.
*
* Hopefully the interface is abstract enough that the underlying
* data structure can be replaced if needed without changing the API.
*/
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "parse_opt.h"
#include "token.h"
struct mount_option {
struct mount_option *next, *prev;
char *keyword;
char *value;
};
struct mount_options {
struct mount_option *head, *tail;
unsigned int count;
};
static struct mount_option *option_create(char *str)
{
struct mount_option *option;
char *opteq;
if (!str)
return NULL;
option = malloc(sizeof(*option));
if (!option)
return NULL;
option->next = NULL;
option->prev = NULL;
opteq = strchr(str, '=');
if (opteq) {
option->keyword = strndup(str, opteq - str);
if (!option->keyword)
goto fail;
option->value = strdup(opteq + 1);
if (!option->value) {
free(option->keyword);
goto fail;
}
} else {
option->keyword = strdup(str);
if (!option->keyword)
goto fail;
option->value = NULL;
}
return option;
fail:
free(option);
return NULL;
}
static void option_destroy(struct mount_option *option)
{
free(option->keyword);
free(option->value);
free(option);
}
static void options_init(struct mount_options *options)
{
options->head = options->tail = NULL;
options->count = 0;
}
static struct mount_options *options_create(void)
{
struct mount_options *options;
options = malloc(sizeof(*options));
if (options)
options_init(options);
return options;
}
static int options_empty(struct mount_options *options)
{
return options->count == 0;
}
static void options_tail_insert(struct mount_options *options,
struct mount_option *option)
{
struct mount_option *prev = options->tail;
option->next = NULL;
option->prev = prev;
if (prev)
prev->next = option;
else
options->head = option;
options->tail = option;
options->count++;
}
static void options_delete(struct mount_options *options,
struct mount_option *option)
{
struct mount_option *prev = option->prev;
struct mount_option *next = option->next;
if (!options_empty(options)) {
if (prev)
prev->next = option->next;
if (next)
next->prev = option->prev;
if (options->head == option)
options->head = option->next;
if (options->tail == option)
options->tail = prev;
options->count--;
option_destroy(option);
}
}
/**
* po_destroy - deallocate a group of mount options
* @options: pointer to mount options to free
*
*/
void po_destroy(struct mount_options *options)
{
if (options) {
while (!options_empty(options))
options_delete(options, options->head);
free(options);
}
}
/**
* po_split - split options string into group of options
* @options: pointer to C string containing zero or more comma-delimited options
*
* Convert our mount options string to a list to make it easier
* to adjust the options as we go. This is just an exercise in
* lexical parsing -- this function doesn't pay attention to the
* meaning of the options themselves.
*
* Returns a new group of mount options if successful; otherwise NULL
* is returned if some failure occurred.
*/
struct mount_options *po_split(char *str)
{
struct mount_options *options;
struct tokenizer_state *tstate;
char *opt;
if (!str)
return options_create();
options = options_create();
if (options) {
tstate = init_tokenizer(str, ',');
for (opt = next_token(tstate); opt; opt = next_token(tstate)) {
struct mount_option *option = option_create(opt);
free(opt);
if (!option)
goto fail;
options_tail_insert(options, option);
}
if (tokenizer_error(tstate))
goto fail;
end_tokenizer(tstate);
}
return options;
fail:
end_tokenizer(tstate);
po_destroy(options);
return NULL;
}
/**
* po_replace - replace mount options in one mount_options object with another
* @target: pointer to previously instantiated object to replace
* @source: pointer to object containing source mount options
*
* Side effect: the object referred to by source is emptied.
*/
void po_replace(struct mount_options *target, struct mount_options *source)
{
if (target) {
while (!options_empty(target))
options_delete(target, target->head);
if (source) {
target->head = source->head;
target->tail = source->tail;
target->count = source->count;
options_init(source);
}
}
}
/**
* po_join - recombine group of mount options into a C string
* @options: pointer to mount options to recombine
* @str: handle on string to replace (input and output)
*
* Convert our mount options object back into a string that the
* rest of the world can use.
*
* Returns 1 if the string was successfully created; otherwise
* zero. Upon return, @string contains the address of a
* replacement C string containing a comma-delimited list of
* mount options and values; or the passed-in string is freed
* and NULL is returned if some failure occurred.
*/
int po_join(struct mount_options *options, char **str)
{
size_t len = 0;
struct mount_option *option;
if (!str || !options)
return PO_FAILED;
free(*str);
*str = NULL;
if (options_empty(options)) {
*str = strdup("");
return *str ? PO_SUCCEEDED : PO_FAILED;
}
for (option = options->head; option; option = option->next) {
len += strlen(option->keyword);
if (option->value)
len +=strlen(option->value) + 1; /* equals sign */
if (option->next)
len++; /* comma */
}
len++; /* NULL on the end */
*str = malloc(len);
if (!*str)
return PO_FAILED;
*str[0] = '\0';
for (option = options->head; option; option = option->next) {
strcat(*str, option->keyword);
if (option->value) {
strcat(*str, "=");
strcat(*str, option->value);
}
if (option->next)
strcat(*str, ",");
}
return PO_SUCCEEDED;
}
/**
* po_append - concatenate an option onto a group of options
* @options: pointer to mount options
* @option: pointer to a C string containing the option to add
*
* Returns 1 if the list was successfully concatenated; otherwise
* zero.
*/
int po_append(struct mount_options *options, char *str)
{
struct mount_option *option = option_create(str);
if (option) {
options_tail_insert(options, option);
return PO_SUCCEEDED;
}
return PO_FAILED;
}
/**
* po_contains - check for presense of an option in a group
* @options: pointer to mount options
* @keyword: pointer to a C string containing option keyword for which to search
*
* Returns 1 if the option is present in the list; otherwise zero.
*/
int po_contains(struct mount_options *options, char *keyword)
{
struct mount_option *option;
if (options && keyword) {
for (option = options->head; option; option = option->next)
if (strcmp(option->keyword, keyword) == 0)
return PO_FOUND;
}
return PO_NOT_FOUND;
}
/**
* po_get - return the value of the rightmost instance of an option
* @options: pointer to mount options
* @keyword: pointer to a C string containing option keyword for which to search
*
* If multiple instances of the same option are present in a mount option
* list, the rightmost instance is always the effective one.
*
* Returns pointer to C string containing the value of the option.
* Returns NULL if the option isn't found, or if the option doesn't
* have a value.
*/
char *po_get(struct mount_options *options, char *keyword)
{
struct mount_option *option;
if (options && keyword) {
for (option = options->tail; option; option = option->prev)
if (strcmp(option->keyword, keyword) == 0)
return option->value;
}
return NULL;
}
/**
* po_rightmost - determine the relative position of two options
* @options: pointer to mount options
* @key1: pointer to a C string containing an option keyword
* @key2: pointer to a C string containing another option keyword
*
* The kernel parses the mount option string from left to right.
* If an option is specified more than once (for example, "intr"
* and "nointr", the rightmost option is the last to be parsed,
* and it therefore takes precedence over previous similar options.
*
* This function can be used to determine which of two similar
* options will be the one to take effect.
*
* Returns 1 if key2 is rightmost or key1 is not present.
* Returns -1 if key1 is rightmost or key2 is not present.
* Returns 0 if neither key is present.
*/
int po_rightmost(struct mount_options *options, char *key1, char *key2)
{
struct mount_option *option;
if (options) {
for (option = options->tail; option; option = option->prev) {
if (key2 && strcmp(option->keyword, key2) == 0)
return PO_KEY2_RIGHTMOST;
if (key1 && strcmp(option->keyword, key1) == 0)
return PO_KEY1_RIGHTMOST;
}
}
return PO_NOT_FOUND;
}
/**
* po_remove_all - remove instances of an option from a group
* @options: pointer to mount options
* @keyword: pointer to a C string containing an option keyword to remove
*
* Returns 1 if the option was found and removed; passed-in list is
* truncated upon return; otherwise zero.
*/
int po_remove_all(struct mount_options *options, char *keyword)
{
struct mount_option *option, *next;
int found = PO_NOT_FOUND;
if (options && keyword) {
for (option = options->head; option; option = next) {
next = option->next;
if (strcmp(option->keyword, keyword) == 0) {
options_delete(options, option);
found = PO_FOUND;
}
}
}
return found;
}
|