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",
201 (int)(end - start), start));
210 if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
212 printf("blkid: missing </device> ending: %s\n", end));
216 if (end - start <= 1) {
217 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
218 return -BLKID_ERR_CACHE;
221 name = blkid_strndup(start, end-start);
223 return -BLKID_ERR_MEM;
225 DBG(DEBUG_READ, printf("found dev %s\n", name));
227 if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE))) {
229 return -BLKID_ERR_MEM;
237 * Extract a tag of the form NAME="value" from the line.
239 static int parse_token(char **name, char **value, char **cp)
243 if (!name || !value || !cp)
244 return -BLKID_ERR_PARAM;
246 if (!(*value = strchr(*cp, '=')))
250 *name = strip_line(*cp);
251 *value = skip_over_blank(*value + 1);
253 if (**value == '"') {
254 end = strchr(*value + 1, '"');
257 printf("unbalanced quotes at: %s\n", *value));
259 return -BLKID_ERR_CACHE;
265 end = skip_over_word(*value);
277 * Extract a tag of the form <NAME>value</NAME> from the line.
280 static int parse_xml(char **name, char **value, char **cp)
284 if (!name || !value || !cp)
285 return -BLKID_ERR_PARAM;
287 *name = strip_line(*cp);
289 if ((*name)[0] != '<' || (*name)[1] == '/')
297 * Extract a tag from the line.
299 * Return 1 if a valid tag was found.
300 * Return 0 if no tag found.
301 * Return -ve error code.
303 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
310 return -BLKID_ERR_PARAM;
312 if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
313 (ret = parse_xml(&name, &value, cp)) <= 0 */)
316 /* Some tags are stored directly in the device struct */
317 if (!strcmp(name, "DEVNO"))
318 dev->bid_devno = STRTOULL(value, 0, 0);
319 else if (!strcmp(name, "PRI"))
320 dev->bid_pri = strtol(value, 0, 0);
321 else if (!strcmp(name, "TIME"))
322 /* FIXME: need to parse a long long eventually */
323 dev->bid_time = strtol(value, 0, 0);
325 ret = blkid_set_tag(dev, name, value, strlen(value));
327 DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
329 return ret < 0 ? ret : 1;
333 * Parse a single line of data, and return a newly allocated dev struct.
334 * Add the new device to the cache struct, if one was read.
336 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
338 * Returns -ve value on error.
339 * Returns 0 otherwise.
340 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
341 * (e.g. comment lines, unknown XML content, etc).
343 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
348 if (!cache || !dev_p)
349 return -BLKID_ERR_PARAM;
353 DBG(DEBUG_READ, printf("line: %s\n", cp));
355 if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
360 while ((ret = parse_tag(cache, dev, &cp)) > 0) {
364 if (dev->bid_type == NULL) {
366 printf("blkid: device %s has no TYPE\n",dev->bid_name));
370 DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
376 * Parse the specified filename, and return the data in the supplied or
377 * a newly allocated cache struct. If the file doesn't exist, return a
378 * new empty cache struct.
380 void blkid_read_cache(blkid_cache cache)
391 * If the file doesn't exist, then we just return an empty
392 * struct so that the cache can be populated.
394 if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
396 if (fstat(fd, &st) < 0)
398 if ((st.st_mtime == cache->bic_ftime) ||
399 (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
400 DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
401 cache->bic_filename));
405 DBG(DEBUG_CACHE, printf("reading cache file %s\n",
406 cache->bic_filename));
408 file = fdopen(fd, "r");
412 while (fgets(buf, sizeof(buf), file)) {
419 end = strlen(buf) - 1;
420 /* Continue reading next line if it ends with a backslash */
421 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
422 fgets(buf + end, sizeof(buf) - end, file)) {
423 end = strlen(buf) - 1;
427 if (blkid_parse_line(cache, &dev, buf) < 0) {
429 printf("blkid: bad format on line %d\n", lineno));
436 * Initially we do not need to write out the cache file.
438 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
439 cache->bic_ftime = st.st_mtime;
448 static void debug_dump_dev(blkid_dev dev)
453 printf(" dev: NULL\n");
457 printf(" dev: name = %s\n", dev->bid_name);
458 printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
459 printf(" dev: TIME=\"%lld\"\n", (long long)dev->bid_time);
460 printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
461 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
463 list_for_each(p, &dev->bid_tags) {
464 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
466 printf(" tag: %s=\"%s\"\n", tag->bit_name,
469 printf(" tag: NULL\n");
474 int main(int argc, char**argv)
476 blkid_cache cache = NULL;
479 blkid_debug_mask = DEBUG_ALL;
481 fprintf(stderr, "Usage: %s [filename]\n"
482 "Test parsing of the cache (filename)\n", argv[0]);
485 if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
486 fprintf(stderr, "error %d reading cache file %s\n", ret,
487 argv[1] ? argv[1] : BLKID_CACHE_FILE);
489 blkid_put_cache(cache);