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
43 * <device [<NAME="value"> ...]>device_name</device>
45 * The following tags are required for each entry:
46 * <ID="id"> unique (within this file) ID number of this device
47 * <TIME="time"> (ascii time_t) time this entry was last read from disk
48 * <TYPE="type"> (detected) type of filesystem/data for this partition
50 * The following tags may be present, depending on the device contents
51 * <LABEL="label"> (user supplied) label (volume name, etc)
52 * <UUID="uuid"> (generated) universally unique identifier (serial no)
55 static char *skip_over_blank(char *cp)
57 while (*cp && isspace(*cp))
62 static char *skip_over_word(char *cp)
67 /* If we see a backslash, skip the next character */
75 if (isspace(ch) || ch == '<' || ch == '>')
82 static char *strip_line(char *line)
86 line = skip_over_blank(line);
88 p = line + strlen(line) - 1;
101 static char *parse_word(char **buf)
109 word = skip_over_blank(word);
110 next = skip_over_word(word);
112 char *end = next - 1;
113 if (*end == '"' || *end == '\'')
119 if (*word == '"' || *word == '\'')
126 * Start parsing a new line from the cache.
128 * line starts with "<device" return 1 -> continue parsing line
129 * line starts with "<foo", empty, or # return 0 -> skip line
130 * line starts with other, return -BLKID_ERR_CACHE -> error
132 static int parse_start(char **cp)
138 /* Skip comment or blank lines. We can't just NUL the first '#' char,
139 * in case it is inside quotes, or escaped.
141 if (*p == '\0' || *p == '#')
144 if (!strncmp(p, "<device", 7)) {
145 DBG(DEBUG_READ, printf("found device header: %8s\n", p));
155 return -BLKID_ERR_CACHE;
158 /* Consume the remaining XML on the line (cosmetic only) */
159 static int parse_end(char **cp)
161 *cp = skip_over_blank(*cp);
163 if (!strncmp(*cp, "</device>", 9)) {
164 DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
169 return -BLKID_ERR_CACHE;
173 * Allocate a new device struct with device name filled in. Will handle
174 * finding the device on lines of the form:
175 * <device foo=bar>devname</device>
176 * <device>devname<foo>bar</foo></device>
178 static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
180 char *start, *tmp, *end, *name;
183 if ((ret = parse_start(cp)) <= 0)
186 start = tmp = strchr(*cp, '>');
189 printf("blkid: short line parsing dev: %s\n", *cp));
190 return -BLKID_ERR_CACHE;
192 start = skip_over_blank(start + 1);
193 end = skip_over_word(start);
195 DBG(DEBUG_READ, printf("device should be %*s\n", end - start, start));
204 if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
206 printf("blkid: missing </device> ending: %s\n", end));
210 if (end - start <= 1) {
211 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
212 return -BLKID_ERR_CACHE;
215 name = blkid_strndup(start, end-start);
217 return -BLKID_ERR_MEM;
219 DBG(DEBUG_READ, printf("found dev %s\n", name));
221 if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE)))
222 return -BLKID_ERR_MEM;
229 * Extract a tag of the form NAME="value" from the line.
231 static int parse_token(char **name, char **value, char **cp)
235 if (!name || !value || !cp)
236 return -BLKID_ERR_PARAM;
238 if (!(*value = strchr(*cp, '=')))
242 *name = strip_line(*cp);
243 *value = skip_over_blank(*value + 1);
245 if (**value == '"') {
246 end = strchr(*value + 1, '"');
249 printf("unbalanced quotes at: %s\n", *value));
251 return -BLKID_ERR_CACHE;
257 end = skip_over_word(*value);
269 * Extract a tag of the form <NAME>value</NAME> from the line.
272 static int parse_xml(char **name, char **value, char **cp)
276 if (!name || !value || !cp)
277 return -BLKID_ERR_PARAM;
279 *name = strip_line(*cp);
281 if ((*name)[0] != '<' || (*name)[1] == '/')
289 * Extract a tag from the line.
291 * Return 1 if a valid tag was found.
292 * Return 0 if no tag found.
293 * Return -ve error code.
295 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
302 return -BLKID_ERR_PARAM;
304 if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
305 (ret = parse_xml(&name, &value, cp)) <= 0 */)
308 /* Some tags are stored directly in the device struct */
309 if (!strcmp(name, "DEVNO"))
310 dev->bid_devno = STRTOULL(value, 0, 0);
311 else if (!strcmp(name, "PRI"))
312 dev->bid_pri = strtol(value, 0, 0);
313 else if (!strcmp(name, "TIME"))
314 /* FIXME: need to parse a long long eventually */
315 dev->bid_time = strtol(value, 0, 0);
317 ret = blkid_set_tag(dev, name, value, strlen(value));
319 DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
321 return ret < 0 ? ret : 1;
325 * Parse a single line of data, and return a newly allocated dev struct.
326 * Add the new device to the cache struct, if one was read.
328 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
330 * Returns -ve value on error.
331 * Returns 0 otherwise.
332 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
333 * (e.g. comment lines, unknown XML content, etc).
335 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
340 if (!cache || !dev_p)
341 return -BLKID_ERR_PARAM;
345 DBG(DEBUG_READ, printf("line: %s\n", cp));
347 if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
352 while ((ret = parse_tag(cache, dev, &cp)) > 0) {
356 if (dev->bid_type == NULL) {
358 printf("blkid: device %s has no TYPE\n",dev->bid_name));
362 DEB_DUMP_DEV(DEBUG_READ, dev);
368 * Parse the specified filename, and return the data in the supplied or
369 * a newly allocated cache struct. If the file doesn't exist, return a
370 * new empty cache struct.
372 void blkid_read_cache(blkid_cache cache)
383 * If the file doesn't exist, then we just return an empty
384 * struct so that the cache can be populated.
386 if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
388 if (fstat(fd, &st) < 0)
390 if ((st.st_mtime == cache->bic_ftime) ||
391 (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
392 DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
393 cache->bic_filename));
397 DBG(DEBUG_CACHE, printf("reading cache file %s\n",
398 cache->bic_filename));
400 file = fdopen(fd, "r");
404 while (fgets(buf, sizeof(buf), file)) {
407 int end = strlen(buf) - 1;
410 /* Continue reading next line if it ends with a backslash */
411 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
412 fgets(buf + end, sizeof(buf) - end, file)) {
413 end = strlen(buf) - 1;
417 if (blkid_parse_line(cache, &dev, buf) < 0) {
419 printf("blkid: bad format on line %d\n", lineno));
424 * Initially we do not need to write out the cache file.
426 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
427 cache->bic_ftime = st.st_mtime;
436 int main(int argc, char**argv)
438 blkid_cache cache = NULL;
441 blkid_debug_mask = DEBUG_ALL;
443 fprintf(stderr, "Usage: %s [filename]\n"
444 "Test parsing of the cache (filename)\n", argv[0]);
447 if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
448 fprintf(stderr, "error %d reading cache file %s\n", ret,
449 argv[1] ? argv[1] : BLKID_CACHE_FILE);
451 blkid_put_cache(cache);