2 * read.c - read the blkid cache from disk, to avoid scanning all devices
4 * Copyright (C) 2001, 2003 Theodore Y. Ts'o
5 * Copyright (C) 2001 Andreas Dilger
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
13 #define _XOPEN_SOURCE 600 /* for inclusion of strtoull */
20 #include <sys/types.h>
29 #include "uuid/uuid.h"
32 #define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
34 /* FIXME: need to support real strtoull here */
35 #define STRTOULL strtoul
43 #define blkid_debug_dump_dev(dev) (debug_dump_dev(dev))
44 static void debug_dump_dev(blkid_dev dev);
50 * <device [<NAME="value"> ...]>device_name</device>
52 * The following tags are required for each entry:
53 * <ID="id"> unique (within this file) ID number of this device
54 * <TIME="time"> (ascii time_t) time this entry was last read from disk
55 * <TYPE="type"> (detected) type of filesystem/data for this partition
57 * The following tags may be present, depending on the device contents
58 * <LABEL="label"> (user supplied) label (volume name, etc)
59 * <UUID="uuid"> (generated) universally unique identifier (serial no)
62 static char *skip_over_blank(char *cp)
64 while (*cp && isspace(*cp))
69 static char *skip_over_word(char *cp)
74 /* If we see a backslash, skip the next character */
82 if (isspace(ch) || ch == '<' || ch == '>')
89 static char *strip_line(char *line)
93 line = skip_over_blank(line);
95 p = line + strlen(line) - 1;
108 static char *parse_word(char **buf)
116 word = skip_over_blank(word);
117 next = skip_over_word(word);
119 char *end = next - 1;
120 if (*end == '"' || *end == '\'')
126 if (*word == '"' || *word == '\'')
133 * Start parsing a new line from the cache.
135 * line starts with "<device" return 1 -> continue parsing line
136 * line starts with "<foo", empty, or # return 0 -> skip line
137 * line starts with other, return -BLKID_ERR_CACHE -> error
139 static int parse_start(char **cp)
145 /* Skip comment or blank lines. We can't just NUL the first '#' char,
146 * in case it is inside quotes, or escaped.
148 if (*p == '\0' || *p == '#')
151 if (!strncmp(p, "<device", 7)) {
152 DBG(DEBUG_READ, printf("found device header: %8s\n", p));
162 return -BLKID_ERR_CACHE;
165 /* Consume the remaining XML on the line (cosmetic only) */
166 static int parse_end(char **cp)
168 *cp = skip_over_blank(*cp);
170 if (!strncmp(*cp, "</device>", 9)) {
171 DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
176 return -BLKID_ERR_CACHE;
180 * Allocate a new device struct with device name filled in. Will handle
181 * finding the device on lines of the form:
182 * <device foo=bar>devname</device>
183 * <device>devname<foo>bar</foo></device>
185 static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
187 char *start, *tmp, *end, *name;
190 if ((ret = parse_start(cp)) <= 0)
193 start = tmp = strchr(*cp, '>');
196 printf("blkid: short line parsing dev: %s\n", *cp));
197 return -BLKID_ERR_CACHE;
199 start = skip_over_blank(start + 1);
200 end = skip_over_word(start);
202 DBG(DEBUG_READ, printf("device should be %.*s\n",
203 (int)(end - start), start));
212 if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
214 printf("blkid: missing </device> ending: %s\n", end));
218 if (end - start <= 1) {
219 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
220 return -BLKID_ERR_CACHE;
223 name = blkid_strndup(start, end-start);
225 return -BLKID_ERR_MEM;
227 DBG(DEBUG_READ, printf("found dev %s\n", name));
229 if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE))) {
231 return -BLKID_ERR_MEM;
239 * Extract a tag of the form NAME="value" from the line.
241 static int parse_token(char **name, char **value, char **cp)
245 if (!name || !value || !cp)
246 return -BLKID_ERR_PARAM;
248 if (!(*value = strchr(*cp, '=')))
252 *name = strip_line(*cp);
253 *value = skip_over_blank(*value + 1);
255 if (**value == '"') {
256 end = strchr(*value + 1, '"');
259 printf("unbalanced quotes at: %s\n", *value));
261 return -BLKID_ERR_CACHE;
267 end = skip_over_word(*value);
279 * Extract a tag of the form <NAME>value</NAME> from the line.
282 static int parse_xml(char **name, char **value, char **cp)
286 if (!name || !value || !cp)
287 return -BLKID_ERR_PARAM;
289 *name = strip_line(*cp);
291 if ((*name)[0] != '<' || (*name)[1] == '/')
299 * Extract a tag from the line.
301 * Return 1 if a valid tag was found.
302 * Return 0 if no tag found.
303 * Return -ve error code.
305 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
312 return -BLKID_ERR_PARAM;
314 if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
315 (ret = parse_xml(&name, &value, cp)) <= 0 */)
318 /* Some tags are stored directly in the device struct */
319 if (!strcmp(name, "DEVNO"))
320 dev->bid_devno = STRTOULL(value, 0, 0);
321 else if (!strcmp(name, "PRI"))
322 dev->bid_pri = strtol(value, 0, 0);
323 else if (!strcmp(name, "TIME"))
324 dev->bid_time = STRTOULL(value, 0, 0);
326 ret = blkid_set_tag(dev, name, value, strlen(value));
328 DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
330 return ret < 0 ? ret : 1;
334 * Parse a single line of data, and return a newly allocated dev struct.
335 * Add the new device to the cache struct, if one was read.
337 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
339 * Returns -ve value on error.
340 * Returns 0 otherwise.
341 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
342 * (e.g. comment lines, unknown XML content, etc).
344 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
349 if (!cache || !dev_p)
350 return -BLKID_ERR_PARAM;
354 DBG(DEBUG_READ, printf("line: %s\n", cp));
356 if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
361 while ((ret = parse_tag(cache, dev, &cp)) > 0) {
365 if (dev->bid_type == NULL) {
367 printf("blkid: device %s has no TYPE\n",dev->bid_name));
371 DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
377 * Parse the specified filename, and return the data in the supplied or
378 * a newly allocated cache struct. If the file doesn't exist, return a
379 * new empty cache struct.
381 void blkid_read_cache(blkid_cache cache)
386 #ifdef CONFIG_BLKID_DEBUG
395 * If the file doesn't exist, then we just return an empty
396 * struct so that the cache can be populated.
398 if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
400 if (fstat(fd, &st) < 0)
402 if ((st.st_mtime == cache->bic_ftime) ||
403 (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
404 DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
405 cache->bic_filename));
409 DBG(DEBUG_CACHE, printf("reading cache file %s\n",
410 cache->bic_filename));
412 file = fdopen(fd, "r");
416 while (fgets(buf, sizeof(buf), file)) {
423 end = strlen(buf) - 1;
424 /* Continue reading next line if it ends with a backslash */
425 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
426 fgets(buf + end, sizeof(buf) - end, file)) {
427 end = strlen(buf) - 1;
431 if (blkid_parse_line(cache, &dev, buf) < 0) {
433 printf("blkid: bad format on line %d\n", lineno));
440 * Initially we do not need to write out the cache file.
442 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
443 cache->bic_ftime = st.st_mtime;
452 static void debug_dump_dev(blkid_dev dev)
457 printf(" dev: NULL\n");
461 printf(" dev: name = %s\n", dev->bid_name);
462 printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
463 printf(" dev: TIME=\"%lld\"\n", (long long)dev->bid_time);
464 printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
465 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
467 list_for_each(p, &dev->bid_tags) {
468 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
470 printf(" tag: %s=\"%s\"\n", tag->bit_name,
473 printf(" tag: NULL\n");
478 int main(int argc, char**argv)
480 blkid_cache cache = NULL;
483 blkid_debug_mask = DEBUG_ALL;
485 fprintf(stderr, "Usage: %s [filename]\n"
486 "Test parsing of the cache (filename)\n", argv[0]);
489 if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
490 fprintf(stderr, "error %d reading cache file %s\n", ret,
491 argv[1] ? argv[1] : BLKID_CACHE_FILE);
493 blkid_put_cache(cache);