• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/src/router/busybox-1.x/e2fsprogs/old_e2fsprogs/blkid/
1/* vi: set sw=4 ts=4: */
2/*
3 * read.c - read the blkid cache from disk, to avoid scanning all devices
4 *
5 * Copyright (C) 2001, 2003 Theodore Y. Ts'o
6 * Copyright (C) 2001 Andreas Dilger
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 * %End-Header%
12 */
13
14#include <stdio.h>
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <fcntl.h>
21#include <unistd.h>
22#include <errno.h>
23
24#include "blkidP.h"
25#include "../uuid/uuid.h"
26
27#ifdef HAVE_STRTOULL
28#define __USE_ISOC9X
29#define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
30#else
31#define STRTOULL strtoul
32#endif
33
34#include <stdlib.h>
35
36#ifdef TEST_PROGRAM
37#define blkid_debug_dump_dev(dev)  (debug_dump_dev(dev))
38static void debug_dump_dev(blkid_dev dev);
39#endif
40
41/*
42 * File format:
43 *
44 *	<device [<NAME="value"> ...]>device_name</device>
45 *
46 *	The following tags are required for each entry:
47 *	<ID="id">	unique (within this file) ID number of this device
48 *	<TIME="time">	(ascii time_t) time this entry was last read from disk
49 *	<TYPE="type">	(detected) type of filesystem/data for this partition
50 *
51 *	The following tags may be present, depending on the device contents
52 *	<LABEL="label">	(user supplied) label (volume name, etc)
53 *	<UUID="uuid">	(generated) universally unique identifier (serial no)
54 */
55
56static char *skip_over_blank(char *cp)
57{
58	while (*cp && isspace(*cp))
59		cp++;
60	return cp;
61}
62
63static char *skip_over_word(char *cp)
64{
65	char ch;
66
67	while ((ch = *cp)) {
68		/* If we see a backslash, skip the next character */
69		if (ch == '\\') {
70			cp++;
71			if (*cp == '\0')
72				break;
73			cp++;
74			continue;
75		}
76		if (isspace(ch) || ch == '<' || ch == '>')
77			break;
78		cp++;
79	}
80	return cp;
81}
82
83static char *strip_line(char *line)
84{
85	char	*p;
86
87	line = skip_over_blank(line);
88
89	p = line + strlen(line) - 1;
90
91	while (*line) {
92		if (isspace(*p))
93			*p-- = '\0';
94		else
95			break;
96	}
97
98	return line;
99}
100
101/*
102 * Start parsing a new line from the cache.
103 *
104 * line starts with "<device" return 1 -> continue parsing line
105 * line starts with "<foo", empty, or # return 0 -> skip line
106 * line starts with other, return -BLKID_ERR_CACHE -> error
107 */
108static int parse_start(char **cp)
109{
110	char *p;
111
112	p = strip_line(*cp);
113
114	/* Skip comment or blank lines.  We can't just NUL the first '#' char,
115	 * in case it is inside quotes, or escaped.
116	 */
117	if (*p == '\0' || *p == '#')
118		return 0;
119
120	if (!strncmp(p, "<device", 7)) {
121		DBG(DEBUG_READ, printf("found device header: %8s\n", p));
122		p += 7;
123
124		*cp = p;
125		return 1;
126	}
127
128	if (*p == '<')
129		return 0;
130
131	return -BLKID_ERR_CACHE;
132}
133
134/* Consume the remaining XML on the line (cosmetic only) */
135static int parse_end(char **cp)
136{
137	*cp = skip_over_blank(*cp);
138
139	if (!strncmp(*cp, "</device>", 9)) {
140		DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
141		*cp += 9;
142		return 0;
143	}
144
145	return -BLKID_ERR_CACHE;
146}
147
148/*
149 * Allocate a new device struct with device name filled in.  Will handle
150 * finding the device on lines of the form:
151 * <device foo=bar>devname</device>
152 * <device>devname<foo>bar</foo></device>
153 */
154static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
155{
156	char *start, *tmp, *end, *name;
157	int ret;
158
159	if ((ret = parse_start(cp)) <= 0)
160		return ret;
161
162	start = tmp = strchr(*cp, '>');
163	if (!start) {
164		DBG(DEBUG_READ,
165		    printf("blkid: short line parsing dev: %s\n", *cp));
166		return -BLKID_ERR_CACHE;
167	}
168	start = skip_over_blank(start + 1);
169	end = skip_over_word(start);
170
171	DBG(DEBUG_READ, printf("device should be %*s\n", end - start, start));
172
173	if (**cp == '>')
174		*cp = end;
175	else
176		(*cp)++;
177
178	*tmp = '\0';
179
180	if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
181		DBG(DEBUG_READ,
182		    printf("blkid: missing </device> ending: %s\n", end));
183	} else if (tmp)
184		*tmp = '\0';
185
186	if (end - start <= 1) {
187		DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
188		return -BLKID_ERR_CACHE;
189	}
190
191	name = blkid_strndup(start, end-start);
192	if (name == NULL)
193		return -BLKID_ERR_MEM;
194
195	DBG(DEBUG_READ, printf("found dev %s\n", name));
196
197	if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE)))
198		return -BLKID_ERR_MEM;
199
200	free(name);
201	return 1;
202}
203
204/*
205 * Extract a tag of the form NAME="value" from the line.
206 */
207static int parse_token(char **name, char **value, char **cp)
208{
209	char *end;
210
211	if (!name || !value || !cp)
212		return -BLKID_ERR_PARAM;
213
214	if (!(*value = strchr(*cp, '=')))
215		return 0;
216
217	**value = '\0';
218	*name = strip_line(*cp);
219	*value = skip_over_blank(*value + 1);
220
221	if (**value == '"') {
222		end = strchr(*value + 1, '"');
223		if (!end) {
224			DBG(DEBUG_READ,
225			    printf("unbalanced quotes at: %s\n", *value));
226			*cp = *value;
227			return -BLKID_ERR_CACHE;
228		}
229		(*value)++;
230		*end = '\0';
231		end++;
232	} else {
233		end = skip_over_word(*value);
234		if (*end) {
235			*end = '\0';
236			end++;
237		}
238	}
239	*cp = end;
240
241	return 1;
242}
243
244/*
245 * Extract a tag of the form <NAME>value</NAME> from the line.
246 */
247
248/*
249 * Extract a tag from the line.
250 *
251 * Return 1 if a valid tag was found.
252 * Return 0 if no tag found.
253 * Return -ve error code.
254 */
255static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
256{
257	char *name;
258	char *value;
259	int ret;
260
261	if (!cache || !dev)
262		return -BLKID_ERR_PARAM;
263
264	if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
265	    (ret = parse_xml(&name, &value, cp)) <= 0 */)
266		return ret;
267
268	/* Some tags are stored directly in the device struct */
269	if (!strcmp(name, "DEVNO"))
270		dev->bid_devno = STRTOULL(value, 0, 0);
271	else if (!strcmp(name, "PRI"))
272		dev->bid_pri = strtol(value, 0, 0);
273	else if (!strcmp(name, "TIME"))
274		dev->bid_time = strtol(value, 0, 0);
275	else
276		ret = blkid_set_tag(dev, name, value, strlen(value));
277
278	DBG(DEBUG_READ, printf("    tag: %s=\"%s\"\n", name, value));
279
280	return ret < 0 ? ret : 1;
281}
282
283/*
284 * Parse a single line of data, and return a newly allocated dev struct.
285 * Add the new device to the cache struct, if one was read.
286 *
287 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
288 *
289 * Returns -ve value on error.
290 * Returns 0 otherwise.
291 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
292 * (e.g. comment lines, unknown XML content, etc).
293 */
294static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
295{
296	blkid_dev dev;
297	int ret;
298
299	if (!cache || !dev_p)
300		return -BLKID_ERR_PARAM;
301
302	*dev_p = NULL;
303
304	DBG(DEBUG_READ, printf("line: %s\n", cp));
305
306	if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
307		return ret;
308
309	dev = *dev_p;
310
311	while ((ret = parse_tag(cache, dev, &cp)) > 0) {
312		;
313	}
314
315	if (dev->bid_type == NULL) {
316		DBG(DEBUG_READ,
317		    printf("blkid: device %s has no TYPE\n",dev->bid_name));
318		blkid_free_dev(dev);
319	}
320
321	DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
322
323	return ret;
324}
325
326/*
327 * Parse the specified filename, and return the data in the supplied or
328 * a newly allocated cache struct.  If the file doesn't exist, return a
329 * new empty cache struct.
330 */
331void blkid_read_cache(blkid_cache cache)
332{
333	FILE *file;
334	char buf[4096];
335	int fd, lineno = 0;
336	struct stat st;
337
338	if (!cache)
339		return;
340
341	/*
342	 * If the file doesn't exist, then we just return an empty
343	 * struct so that the cache can be populated.
344	 */
345	if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
346		return;
347	if (fstat(fd, &st) < 0)
348		goto errout;
349	if ((st.st_mtime == cache->bic_ftime) ||
350	    (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
351		DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
352					cache->bic_filename));
353		goto errout;
354	}
355
356	DBG(DEBUG_CACHE, printf("reading cache file %s\n",
357				cache->bic_filename));
358
359	file = fdopen(fd, "r");
360	if (!file)
361		goto errout;
362
363	while (fgets(buf, sizeof(buf), file)) {
364		blkid_dev dev;
365		unsigned int end;
366
367		lineno++;
368		if (buf[0] == 0)
369			continue;
370		end = strlen(buf) - 1;
371		/* Continue reading next line if it ends with a backslash */
372		while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
373		       fgets(buf + end, sizeof(buf) - end, file)) {
374			end = strlen(buf) - 1;
375			lineno++;
376		}
377
378		if (blkid_parse_line(cache, &dev, buf) < 0) {
379			DBG(DEBUG_READ,
380			    printf("blkid: bad format on line %d\n", lineno));
381			continue;
382		}
383	}
384	fclose(file);
385
386	/*
387	 * Initially we do not need to write out the cache file.
388	 */
389	cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
390	cache->bic_ftime = st.st_mtime;
391
392	return;
393errout:
394	close(fd);
395}
396
397#ifdef TEST_PROGRAM
398static void debug_dump_dev(blkid_dev dev)
399{
400	struct list_head *p;
401
402	if (!dev) {
403		printf("  dev: NULL\n");
404		return;
405	}
406
407	printf("  dev: name = %s\n", dev->bid_name);
408	printf("  dev: DEVNO=\"0x%0llx\"\n", dev->bid_devno);
409	printf("  dev: TIME=\"%lu\"\n", dev->bid_time);
410	printf("  dev: PRI=\"%d\"\n", dev->bid_pri);
411	printf("  dev: flags = 0x%08X\n", dev->bid_flags);
412
413	list_for_each(p, &dev->bid_tags) {
414		blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
415		if (tag)
416			printf("    tag: %s=\"%s\"\n", tag->bit_name,
417			       tag->bit_val);
418		else
419			printf("    tag: NULL\n");
420	}
421	puts("");
422}
423
424int main(int argc, char**argv)
425{
426	blkid_cache cache = NULL;
427	int ret;
428
429	blkid_debug_mask = DEBUG_ALL;
430	if (argc > 2) {
431		fprintf(stderr, "Usage: %s [filename]\n"
432			"Test parsing of the cache (filename)\n", argv[0]);
433		exit(1);
434	}
435	if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
436		fprintf(stderr, "error %d reading cache file %s\n", ret,
437			argv[1] ? argv[1] : BLKID_CACHE_FILE);
438
439	blkid_put_cache(cache);
440
441	return ret;
442}
443#endif
444