1 /* vi: set sw=4 ts=4: */
3 * read.c - read the blkid cache from disk, to avoid scanning all devices
5 * Copyright (C) 2001, 2003 Theodore Y. Ts'o
6 * Copyright (C) 2001 Andreas Dilger
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
18 #include <sys/types.h>
25 #include "../uuid/uuid.h"
29 #define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
31 /* FIXME: need to support real strtoull here */
32 #define STRTOULL strtoul
38 #define blkid_debug_dump_dev(dev) (debug_dump_dev(dev))
39 static void debug_dump_dev(blkid_dev dev);
45 * <device [<NAME="value"> ...]>device_name</device>
47 * The following tags are required for each entry:
48 * <ID="id"> unique (within this file) ID number of this device
49 * <TIME="time"> (ascii time_t) time this entry was last read from disk
50 * <TYPE="type"> (detected) type of filesystem/data for this partition
52 * The following tags may be present, depending on the device contents
53 * <LABEL="label"> (user supplied) label (volume name, etc)
54 * <UUID="uuid"> (generated) universally unique identifier (serial no)
57 static char *skip_over_blank(char *cp)
59 while (*cp && isspace(*cp))
64 static char *skip_over_word(char *cp)
69 /* If we see a backslash, skip the next character */
77 if (isspace(ch) || ch == '<' || ch == '>')
84 static char *strip_line(char *line)
88 line = skip_over_blank(line);
90 p = line + strlen(line) - 1;
103 * Start parsing a new line from the cache.
105 * line starts with "<device" return 1 -> continue parsing line
106 * line starts with "<foo", empty, or # return 0 -> skip line
107 * line starts with other, return -BLKID_ERR_CACHE -> error
109 static int parse_start(char **cp)
115 /* Skip comment or blank lines. We can't just NUL the first '#' char,
116 * in case it is inside quotes, or escaped.
118 if (*p == '\0' || *p == '#')
121 if (!strncmp(p, "<device", 7)) {
122 DBG(DEBUG_READ, printf("found device header: %8s\n", p));
132 return -BLKID_ERR_CACHE;
135 /* Consume the remaining XML on the line (cosmetic only) */
136 static int parse_end(char **cp)
138 *cp = skip_over_blank(*cp);
140 if (!strncmp(*cp, "</device>", 9)) {
141 DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
146 return -BLKID_ERR_CACHE;
150 * Allocate a new device struct with device name filled in. Will handle
151 * finding the device on lines of the form:
152 * <device foo=bar>devname</device>
153 * <device>devname<foo>bar</foo></device>
155 static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
157 char *start, *tmp, *end, *name;
160 if ((ret = parse_start(cp)) <= 0)
163 start = tmp = strchr(*cp, '>');
166 printf("blkid: short line parsing dev: %s\n", *cp));
167 return -BLKID_ERR_CACHE;
169 start = skip_over_blank(start + 1);
170 end = skip_over_word(start);
172 DBG(DEBUG_READ, printf("device should be %*s\n", end - start, start));
181 if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
183 printf("blkid: missing </device> ending: %s\n", end));
187 if (end - start <= 1) {
188 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
189 return -BLKID_ERR_CACHE;
192 name = blkid_strndup(start, end-start);
194 return -BLKID_ERR_MEM;
196 DBG(DEBUG_READ, printf("found dev %s\n", name));
198 if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE)))
199 return -BLKID_ERR_MEM;
206 * Extract a tag of the form NAME="value" from the line.
208 static int parse_token(char **name, char **value, char **cp)
212 if (!name || !value || !cp)
213 return -BLKID_ERR_PARAM;
215 if (!(*value = strchr(*cp, '=')))
219 *name = strip_line(*cp);
220 *value = skip_over_blank(*value + 1);
222 if (**value == '"') {
223 end = strchr(*value + 1, '"');
226 printf("unbalanced quotes at: %s\n", *value));
228 return -BLKID_ERR_CACHE;
234 end = skip_over_word(*value);
246 * Extract a tag of the form <NAME>value</NAME> from the line.
249 static int parse_xml(char **name, char **value, char **cp)
253 if (!name || !value || !cp)
254 return -BLKID_ERR_PARAM;
256 *name = strip_line(*cp);
258 if ((*name)[0] != '<' || (*name)[1] == '/')
266 * Extract a tag from the line.
268 * Return 1 if a valid tag was found.
269 * Return 0 if no tag found.
270 * Return -ve error code.
272 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
279 return -BLKID_ERR_PARAM;
281 if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
282 (ret = parse_xml(&name, &value, cp)) <= 0 */)
285 /* Some tags are stored directly in the device struct */
286 if (!strcmp(name, "DEVNO"))
287 dev->bid_devno = STRTOULL(value, 0, 0);
288 else if (!strcmp(name, "PRI"))
289 dev->bid_pri = strtol(value, 0, 0);
290 else if (!strcmp(name, "TIME"))
291 /* FIXME: need to parse a long long eventually */
292 dev->bid_time = strtol(value, 0, 0);
294 ret = blkid_set_tag(dev, name, value, strlen(value));
296 DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
298 return ret < 0 ? ret : 1;
302 * Parse a single line of data, and return a newly allocated dev struct.
303 * Add the new device to the cache struct, if one was read.
305 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
307 * Returns -ve value on error.
308 * Returns 0 otherwise.
309 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
310 * (e.g. comment lines, unknown XML content, etc).
312 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
317 if (!cache || !dev_p)
318 return -BLKID_ERR_PARAM;
322 DBG(DEBUG_READ, printf("line: %s\n", cp));
324 if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
329 while ((ret = parse_tag(cache, dev, &cp)) > 0) {
333 if (dev->bid_type == NULL) {
335 printf("blkid: device %s has no TYPE\n",dev->bid_name));
339 DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
345 * Parse the specified filename, and return the data in the supplied or
346 * a newly allocated cache struct. If the file doesn't exist, return a
347 * new empty cache struct.
349 void blkid_read_cache(blkid_cache cache)
360 * If the file doesn't exist, then we just return an empty
361 * struct so that the cache can be populated.
363 if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
365 if (fstat(fd, &st) < 0)
367 if ((st.st_mtime == cache->bic_ftime) ||
368 (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
369 DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
370 cache->bic_filename));
374 DBG(DEBUG_CACHE, printf("reading cache file %s\n",
375 cache->bic_filename));
377 file = fdopen(fd, "r");
381 while (fgets(buf, sizeof(buf), file)) {
388 end = strlen(buf) - 1;
389 /* Continue reading next line if it ends with a backslash */
390 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
391 fgets(buf + end, sizeof(buf) - end, file)) {
392 end = strlen(buf) - 1;
396 if (blkid_parse_line(cache, &dev, buf) < 0) {
398 printf("blkid: bad format on line %d\n", lineno));
405 * Initially we do not need to write out the cache file.
407 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
408 cache->bic_ftime = st.st_mtime;
416 static void debug_dump_dev(blkid_dev dev)
421 printf(" dev: NULL\n");
425 printf(" dev: name = %s\n", dev->bid_name);
426 printf(" dev: DEVNO=\"0x%0llx\"\n", dev->bid_devno);
427 printf(" dev: TIME=\"%lu\"\n", dev->bid_time);
428 printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
429 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
431 list_for_each(p, &dev->bid_tags) {
432 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
434 printf(" tag: %s=\"%s\"\n", tag->bit_name,
437 printf(" tag: NULL\n");
442 int main(int argc, char**argv)
444 blkid_cache cache = NULL;
447 blkid_debug_mask = DEBUG_ALL;
449 fprintf(stderr, "Usage: %s [filename]\n"
450 "Test parsing of the cache (filename)\n", argv[0]);
453 if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
454 fprintf(stderr, "error %d reading cache file %s\n", ret,
455 argv[1] ? argv[1] : BLKID_CACHE_FILE);
457 blkid_put_cache(cache);