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.
17 #include <sys/types.h>
26 #include "uuid/uuid.h"
30 #define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
32 /* FIXME: need to support real strtoull here */
33 #define STRTOULL strtoul
41 #define blkid_debug_dump_dev(dev) (debug_dump_dev(dev))
42 static void debug_dump_dev(blkid_dev dev);
48 * <device [<NAME="value"> ...]>device_name</device>
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
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)
60 static char *skip_over_blank(char *cp)
62 while (*cp && isspace(*cp))
67 static char *skip_over_word(char *cp)
72 /* If we see a backslash, skip the next character */
80 if (isspace(ch) || ch == '<' || ch == '>')
87 static char *strip_line(char *line)
91 line = skip_over_blank(line);
93 p = line + strlen(line) - 1;
106 static char *parse_word(char **buf)
114 word = skip_over_blank(word);
115 next = skip_over_word(word);
117 char *end = next - 1;
118 if (*end == '"' || *end == '\'')
124 if (*word == '"' || *word == '\'')
131 * Start parsing a new line from the cache.
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
137 static int parse_start(char **cp)
143 /* Skip comment or blank lines. We can't just NUL the first '#' char,
144 * in case it is inside quotes, or escaped.
146 if (*p == '\0' || *p == '#')
149 if (!strncmp(p, "<device", 7)) {
150 DBG(DEBUG_READ, printf("found device header: %8s\n", p));
160 return -BLKID_ERR_CACHE;
163 /* Consume the remaining XML on the line (cosmetic only) */
164 static int parse_end(char **cp)
166 *cp = skip_over_blank(*cp);
168 if (!strncmp(*cp, "</device>", 9)) {
169 DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
174 return -BLKID_ERR_CACHE;
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>
183 static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
185 char *start, *tmp, *end, *name;
188 if ((ret = parse_start(cp)) <= 0)
191 start = tmp = strchr(*cp, '>');
194 printf("blkid: short line parsing dev: %s\n", *cp));
195 return -BLKID_ERR_CACHE;
197 start = skip_over_blank(start + 1);
198 end = skip_over_word(start);
200 DBG(DEBUG_READ, printf("device should be %*s\n", end - start, start));
209 if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
211 printf("blkid: missing </device> ending: %s\n", end));
215 if (end - start <= 1) {
216 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
217 return -BLKID_ERR_CACHE;
220 name = blkid_strndup(start, end-start);
222 return -BLKID_ERR_MEM;
224 DBG(DEBUG_READ, printf("found dev %s\n", name));
226 if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE)))
227 return -BLKID_ERR_MEM;
234 * Extract a tag of the form NAME="value" from the line.
236 static int parse_token(char **name, char **value, char **cp)
240 if (!name || !value || !cp)
241 return -BLKID_ERR_PARAM;
243 if (!(*value = strchr(*cp, '=')))
247 *name = strip_line(*cp);
248 *value = skip_over_blank(*value + 1);
250 if (**value == '"') {
251 end = strchr(*value + 1, '"');
254 printf("unbalanced quotes at: %s\n", *value));
256 return -BLKID_ERR_CACHE;
262 end = skip_over_word(*value);
274 * Extract a tag of the form <NAME>value</NAME> from the line.
277 static int parse_xml(char **name, char **value, char **cp)
281 if (!name || !value || !cp)
282 return -BLKID_ERR_PARAM;
284 *name = strip_line(*cp);
286 if ((*name)[0] != '<' || (*name)[1] == '/')
294 * Extract a tag from the line.
296 * Return 1 if a valid tag was found.
297 * Return 0 if no tag found.
298 * Return -ve error code.
300 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
307 return -BLKID_ERR_PARAM;
309 if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
310 (ret = parse_xml(&name, &value, cp)) <= 0 */)
313 /* Some tags are stored directly in the device struct */
314 if (!strcmp(name, "DEVNO"))
315 dev->bid_devno = STRTOULL(value, 0, 0);
316 else if (!strcmp(name, "PRI"))
317 dev->bid_pri = strtol(value, 0, 0);
318 else if (!strcmp(name, "TIME"))
319 /* FIXME: need to parse a long long eventually */
320 dev->bid_time = strtol(value, 0, 0);
322 ret = blkid_set_tag(dev, name, value, strlen(value));
324 DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
326 return ret < 0 ? ret : 1;
330 * Parse a single line of data, and return a newly allocated dev struct.
331 * Add the new device to the cache struct, if one was read.
333 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
335 * Returns -ve value on error.
336 * Returns 0 otherwise.
337 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
338 * (e.g. comment lines, unknown XML content, etc).
340 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
345 if (!cache || !dev_p)
346 return -BLKID_ERR_PARAM;
350 DBG(DEBUG_READ, printf("line: %s\n", cp));
352 if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
357 while ((ret = parse_tag(cache, dev, &cp)) > 0) {
361 if (dev->bid_type == NULL) {
363 printf("blkid: device %s has no TYPE\n",dev->bid_name));
367 DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
373 * Parse the specified filename, and return the data in the supplied or
374 * a newly allocated cache struct. If the file doesn't exist, return a
375 * new empty cache struct.
377 void blkid_read_cache(blkid_cache cache)
388 * If the file doesn't exist, then we just return an empty
389 * struct so that the cache can be populated.
391 if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
393 if (fstat(fd, &st) < 0)
395 if ((st.st_mtime == cache->bic_ftime) ||
396 (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
397 DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
398 cache->bic_filename));
402 DBG(DEBUG_CACHE, printf("reading cache file %s\n",
403 cache->bic_filename));
405 file = fdopen(fd, "r");
409 while (fgets(buf, sizeof(buf), file)) {
416 end = strlen(buf) - 1;
417 /* Continue reading next line if it ends with a backslash */
418 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
419 fgets(buf + end, sizeof(buf) - end, file)) {
420 end = strlen(buf) - 1;
424 if (blkid_parse_line(cache, &dev, buf) < 0) {
426 printf("blkid: bad format on line %d\n", lineno));
433 * Initially we do not need to write out the cache file.
435 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
436 cache->bic_ftime = st.st_mtime;
445 static void debug_dump_dev(blkid_dev dev)
450 printf(" dev: NULL\n");
454 printf(" dev: name = %s\n", dev->bid_name);
455 printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
456 printf(" dev: TIME=\"%lld\"\n", (long long)dev->bid_time);
457 printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
458 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
460 list_for_each(p, &dev->bid_tags) {
461 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
463 printf(" tag: %s=\"%s\"\n", tag->bit_name,
466 printf(" tag: NULL\n");
471 int main(int argc, char**argv)
473 blkid_cache cache = NULL;
476 blkid_debug_mask = DEBUG_ALL;
478 fprintf(stderr, "Usage: %s [filename]\n"
479 "Test parsing of the cache (filename)\n", argv[0]);
482 if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
483 fprintf(stderr, "error %d reading cache file %s\n", ret,
484 argv[1] ? argv[1] : BLKID_CACHE_FILE);
486 blkid_put_cache(cache);