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>
24 #include "../uuid/uuid.h"
28 #define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
30 /* FIXME: need to support real strtoull here */
31 #define STRTOULL strtoul
39 * <device [<NAME="value"> ...]>device_name</device>
41 * The following tags are required for each entry:
42 * <ID="id"> unique (within this file) ID number of this device
43 * <TIME="time"> (ascii time_t) time this entry was last read from disk
44 * <TYPE="type"> (detected) type of filesystem/data for this partition
46 * The following tags may be present, depending on the device contents
47 * <LABEL="label"> (user supplied) label (volume name, etc)
48 * <UUID="uuid"> (generated) universally unique identifier (serial no)
51 static char *skip_over_blank(char *cp)
53 while (*cp && isspace(*cp))
58 static char *skip_over_word(char *cp)
63 /* If we see a backslash, skip the next character */
71 if (isspace(ch) || ch == '<' || ch == '>')
78 static char *strip_line(char *line)
82 line = skip_over_blank(line);
84 p = line + strlen(line) - 1;
97 static char *parse_word(char **buf)
105 word = skip_over_blank(word);
106 next = skip_over_word(word);
108 char *end = next - 1;
109 if (*end == '"' || *end == '\'')
115 if (*word == '"' || *word == '\'')
122 * Start parsing a new line from the cache.
124 * line starts with "<device" return 1 -> continue parsing line
125 * line starts with "<foo", empty, or # return 0 -> skip line
126 * line starts with other, return -BLKID_ERR_CACHE -> error
128 static int parse_start(char **cp)
134 /* Skip comment or blank lines. We can't just NUL the first '#' char,
135 * in case it is inside quotes, or escaped.
137 if (*p == '\0' || *p == '#')
140 if (!strncmp(p, "<device", 7)) {
141 DBG(DEBUG_READ, printf("found device header: %8s\n", p));
151 return -BLKID_ERR_CACHE;
154 /* Consume the remaining XML on the line (cosmetic only) */
155 static int parse_end(char **cp)
157 *cp = skip_over_blank(*cp);
159 if (!strncmp(*cp, "</device>", 9)) {
160 DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
165 return -BLKID_ERR_CACHE;
169 * Allocate a new device struct with device name filled in. Will handle
170 * finding the device on lines of the form:
171 * <device foo=bar>devname</device>
172 * <device>devname<foo>bar</foo></device>
174 static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
176 char *start, *tmp, *end, *name;
179 if ((ret = parse_start(cp)) <= 0)
182 start = tmp = strchr(*cp, '>');
185 printf("blkid: short line parsing dev: %s\n", *cp));
186 return -BLKID_ERR_CACHE;
188 start = skip_over_blank(start + 1);
189 end = skip_over_word(start);
191 DBG(DEBUG_READ, printf("device should be %*s\n", end - start, start));
200 if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
202 printf("blkid: missing </device> ending: %s\n", end));
206 if (end - start <= 1) {
207 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
208 return -BLKID_ERR_CACHE;
211 name = blkid_strndup(start, end-start);
213 return -BLKID_ERR_MEM;
215 DBG(DEBUG_READ, printf("found dev %s\n", name));
217 if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE)))
218 return -BLKID_ERR_MEM;
225 * Extract a tag of the form NAME="value" from the line.
227 static int parse_token(char **name, char **value, char **cp)
231 if (!name || !value || !cp)
232 return -BLKID_ERR_PARAM;
234 if (!(*value = strchr(*cp, '=')))
238 *name = strip_line(*cp);
239 *value = skip_over_blank(*value + 1);
241 if (**value == '"') {
242 end = strchr(*value + 1, '"');
245 printf("unbalanced quotes at: %s\n", *value));
247 return -BLKID_ERR_CACHE;
253 end = skip_over_word(*value);
265 * Extract a tag of the form <NAME>value</NAME> from the line.
268 static int parse_xml(char **name, char **value, char **cp)
272 if (!name || !value || !cp)
273 return -BLKID_ERR_PARAM;
275 *name = strip_line(*cp);
277 if ((*name)[0] != '<' || (*name)[1] == '/')
285 * Extract a tag from the line.
287 * Return 1 if a valid tag was found.
288 * Return 0 if no tag found.
289 * Return -ve error code.
291 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
298 return -BLKID_ERR_PARAM;
300 if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
301 (ret = parse_xml(&name, &value, cp)) <= 0 */)
304 /* Some tags are stored directly in the device struct */
305 if (!strcmp(name, "DEVNO"))
306 dev->bid_devno = STRTOULL(value, 0, 0);
307 else if (!strcmp(name, "PRI"))
308 dev->bid_pri = strtol(value, 0, 0);
309 else if (!strcmp(name, "TIME"))
310 /* FIXME: need to parse a long long eventually */
311 dev->bid_time = strtol(value, 0, 0);
313 ret = blkid_set_tag(dev, name, value, strlen(value));
315 DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
317 return ret < 0 ? ret : 1;
321 * Parse a single line of data, and return a newly allocated dev struct.
322 * Add the new device to the cache struct, if one was read.
324 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
326 * Returns -ve value on error.
327 * Returns 0 otherwise.
328 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
329 * (e.g. comment lines, unknown XML content, etc).
331 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
336 if (!cache || !dev_p)
337 return -BLKID_ERR_PARAM;
341 DBG(DEBUG_READ, printf("line: %s\n", cp));
343 if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
348 while ((ret = parse_tag(cache, dev, &cp)) > 0) {
352 if (dev->bid_type == NULL) {
354 printf("blkid: device %s has no TYPE\n",dev->bid_name));
358 DEB_DUMP_DEV(DEBUG_READ, dev);
364 * Parse the specified filename, and return the data in the supplied or
365 * a newly allocated cache struct. If the file doesn't exist, return a
366 * new empty cache struct.
368 void blkid_read_cache(blkid_cache cache)
379 * If the file doesn't exist, then we just return an empty
380 * struct so that the cache can be populated.
382 if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
384 if (fstat(fd, &st) < 0)
386 if ((st.st_mtime == cache->bic_ftime) ||
387 (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
388 DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
389 cache->bic_filename));
393 DBG(DEBUG_CACHE, printf("reading cache file %s\n",
394 cache->bic_filename));
396 file = fdopen(fd, "r");
400 while (fgets(buf, sizeof(buf), file)) {
407 end = strlen(buf) - 1;
408 /* Continue reading next line if it ends with a backslash */
409 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
410 fgets(buf + end, sizeof(buf) - end, file)) {
411 end = strlen(buf) - 1;
415 if (blkid_parse_line(cache, &dev, buf) < 0) {
417 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);