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