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.
13
#define _XOPEN_SOURCE 600 /* for inclusion of strtoull */
19
#include <sys/types.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
dev->bid_time = STRTOULL(value, 0, 0);
324
ret = blkid_set_tag(dev, name, value, strlen(value));
326
DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
328
return ret < 0 ? ret : 1;
332
* Parse a single line of data, and return a newly allocated dev struct.
333
* Add the new device to the cache struct, if one was read.
335
* Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
337
* Returns -ve value on error.
338
* Returns 0 otherwise.
339
* If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
340
* (e.g. comment lines, unknown XML content, etc).
342
static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
347
if (!cache || !dev_p)
348
return -BLKID_ERR_PARAM;
352
DBG(DEBUG_READ, printf("line: %s\n", cp));
354
if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
359
while ((ret = parse_tag(cache, dev, &cp)) > 0) {
363
if (dev->bid_type == NULL) {
365
printf("blkid: device %s has no TYPE\n",dev->bid_name));
369
DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
375
* Parse the specified filename, and return the data in the supplied or
376
* a newly allocated cache struct. If the file doesn't exist, return a
377
* new empty cache struct.
379
void blkid_read_cache(blkid_cache cache)
390
* If the file doesn't exist, then we just return an empty
391
* struct so that the cache can be populated.
393
if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
395
if (fstat(fd, &st) < 0)
397
if ((st.st_mtime == cache->bic_ftime) ||
398
(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
399
DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
400
cache->bic_filename));
404
DBG(DEBUG_CACHE, printf("reading cache file %s\n",
405
cache->bic_filename));
407
file = fdopen(fd, "r");
411
while (fgets(buf, sizeof(buf), file)) {
418
end = strlen(buf) - 1;
419
/* Continue reading next line if it ends with a backslash */
420
while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
421
fgets(buf + end, sizeof(buf) - end, file)) {
422
end = strlen(buf) - 1;
426
if (blkid_parse_line(cache, &dev, buf) < 0) {
428
printf("blkid: bad format on line %d\n", lineno));
435
* Initially we do not need to write out the cache file.
437
cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
438
cache->bic_ftime = st.st_mtime;
447
static void debug_dump_dev(blkid_dev dev)
452
printf(" dev: NULL\n");
456
printf(" dev: name = %s\n", dev->bid_name);
457
printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
458
printf(" dev: TIME=\"%lld\"\n", (long long)dev->bid_time);
459
printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
460
printf(" dev: flags = 0x%08X\n", dev->bid_flags);
462
list_for_each(p, &dev->bid_tags) {
463
blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
465
printf(" tag: %s=\"%s\"\n", tag->bit_name,
468
printf(" tag: NULL\n");
473
int main(int argc, char**argv)
475
blkid_cache cache = NULL;
478
blkid_init_debug(DEBUG_ALL);
480
fprintf(stderr, "Usage: %s [filename]\n"
481
"Test parsing of the cache (filename)\n", argv[0]);
484
if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
485
fprintf(stderr, "error %d reading cache file %s\n", ret,
486
argv[1] ? argv[1] : BLKID_CACHE_FILE);
488
blkid_put_cache(cache);