]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/read.c
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.
18 #include <sys/types.h>
29 # ifndef _XOPEN_SOURCE
30 # define _XOPEN_SOURCE 600 /* for inclusion of strtoull */
36 #define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
38 /* FIXME: need to support real strtoull here */
39 #define STRTOULL strtoul
43 #define blkid_debug_dump_dev(dev) (debug_dump_dev(dev))
44 static void debug_dump_dev(blkid_dev dev
);
50 * <device [<NAME="value"> ...]>device_name</device>
52 * The following tags are required for each entry:
53 * <ID="id"> unique (within this file) ID number of this device
54 * <TIME="sec.usec"> (time_t and suseconds_t) time this entry was last
56 * <TYPE="type"> (detected) type of filesystem/data for this partition
58 * The following tags may be present, depending on the device contents
59 * <LABEL="label"> (user supplied) label (volume name, etc)
60 * <UUID="uuid"> (generated) universally unique identifier (serial no)
63 static char *skip_over_blank(char *cp
)
65 while (*cp
&& isspace(*cp
))
70 static char *skip_over_word(char *cp
)
75 /* If we see a backslash, skip the next character */
83 if (isspace(ch
) || ch
== '<' || ch
== '>')
90 static char *strip_line(char *line
)
94 line
= skip_over_blank(line
);
96 p
= line
+ strlen(line
) - 1;
109 static char *parse_word(char **buf
)
117 word
= skip_over_blank(word
);
118 next
= skip_over_word(word
);
120 char *end
= next
- 1;
121 if (*end
== '"' || *end
== '\'')
127 if (*word
== '"' || *word
== '\'')
134 * Start parsing a new line from the cache.
136 * line starts with "<device" return 1 -> continue parsing line
137 * line starts with "<foo", empty, or # return 0 -> skip line
138 * line starts with other, return -BLKID_ERR_CACHE -> error
140 static int parse_start(char **cp
)
146 /* Skip comment or blank lines. We can't just NUL the first '#' char,
147 * in case it is inside quotes, or escaped.
149 if (*p
== '\0' || *p
== '#')
152 if (!strncmp(p
, "<device", 7)) {
153 DBG(READ
, ul_debug("found device header: %8s", p
));
163 return -BLKID_ERR_CACHE
;
166 /* Consume the remaining XML on the line (cosmetic only) */
167 static int parse_end(char **cp
)
169 *cp
= skip_over_blank(*cp
);
171 if (!strncmp(*cp
, "</device>", 9)) {
172 DBG(READ
, ul_debug("found device trailer %9s", *cp
));
177 return -BLKID_ERR_CACHE
;
181 * Allocate a new device struct with device name filled in. Will handle
182 * finding the device on lines of the form:
183 * <device foo=bar>devname</device>
184 * <device>devname<foo>bar</foo></device>
186 static int parse_dev(blkid_cache cache
, blkid_dev
*dev
, char **cp
)
188 char *start
, *tmp
, *end
, *name
;
191 if ((ret
= parse_start(cp
)) <= 0)
194 start
= tmp
= strchr(*cp
, '>');
196 DBG(READ
, ul_debug("blkid: short line parsing dev: %s", *cp
));
197 return -BLKID_ERR_CACHE
;
199 start
= skip_over_blank(start
+ 1);
200 end
= skip_over_word(start
);
202 DBG(READ
, ul_debug("device should be %*s",
203 (int)(end
- start
), start
));
212 if (!(tmp
= strrchr(end
, '<')) || parse_end(&tmp
) < 0) {
213 DBG(READ
, ul_debug("blkid: missing </device> ending: %s", end
));
217 if (end
- start
<= 1) {
218 DBG(READ
, ul_debug("blkid: empty device name: %s", *cp
));
219 return -BLKID_ERR_CACHE
;
222 name
= strndup(start
, end
- start
);
224 return -BLKID_ERR_MEM
;
226 DBG(READ
, ul_debug("found dev %s", name
));
228 if (!(*dev
= blkid_get_dev(cache
, name
, BLKID_DEV_CREATE
))) {
230 return -BLKID_ERR_MEM
;
238 * Extract a tag of the form NAME="value" from the line.
240 static int parse_token(char **name
, char **value
, char **cp
)
244 if (!name
|| !value
|| !cp
)
245 return -BLKID_ERR_PARAM
;
247 if (!(*value
= strchr(*cp
, '=')))
251 *name
= strip_line(*cp
);
252 *value
= skip_over_blank(*value
+ 1);
254 if (**value
== '"') {
255 char *p
= end
= *value
+ 1;
257 /* convert 'foo\"bar' to 'foo"bar' */
272 DBG(READ
, ul_debug("unbalanced quotes at: %s", *value
));
274 return -BLKID_ERR_CACHE
;
280 end
= skip_over_word(*value
);
292 * Extract a tag of the form <NAME>value</NAME> from the line.
295 static int parse_xml(char **name, char **value, char **cp)
299 if (!name || !value || !cp)
300 return -BLKID_ERR_PARAM;
302 *name = strip_line(*cp);
304 if ((*name)[0] != '<' || (*name)[1] == '/')
312 * Extract a tag from the line.
314 * Return 1 if a valid tag was found.
315 * Return 0 if no tag found.
316 * Return -ve error code.
318 static int parse_tag(blkid_cache cache
, blkid_dev dev
, char **cp
)
325 return -BLKID_ERR_PARAM
;
327 if ((ret
= parse_token(&name
, &value
, cp
)) <= 0 /* &&
328 (ret = parse_xml(&name, &value, cp)) <= 0 */)
331 /* Some tags are stored directly in the device struct */
332 if (!strcmp(name
, "DEVNO"))
333 dev
->bid_devno
= STRTOULL(value
, 0, 0);
334 else if (!strcmp(name
, "PRI"))
335 dev
->bid_pri
= strtol(value
, 0, 0);
336 else if (!strcmp(name
, "TIME")) {
338 dev
->bid_time
= STRTOULL(value
, &end
, 0);
339 if (end
&& *end
== '.')
340 dev
->bid_utime
= STRTOULL(end
+ 1, 0, 0);
342 ret
= blkid_set_tag(dev
, name
, value
, strlen(value
));
344 DBG(READ
, ul_debug(" tag: %s=\"%s\"", name
, value
));
346 return ret
< 0 ? ret
: 1;
350 * Parse a single line of data, and return a newly allocated dev struct.
351 * Add the new device to the cache struct, if one was read.
353 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
355 * Returns -ve value on error.
356 * Returns 0 otherwise.
357 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
358 * (e.g. comment lines, unknown XML content, etc).
360 static int blkid_parse_line(blkid_cache cache
, blkid_dev
*dev_p
, char *cp
)
365 if (!cache
|| !dev_p
)
366 return -BLKID_ERR_PARAM
;
370 DBG(READ
, ul_debug("line: %s", cp
));
372 if ((ret
= parse_dev(cache
, dev_p
, &cp
)) <= 0)
377 while ((ret
= parse_tag(cache
, dev
, &cp
)) > 0) {
381 if (dev
->bid_type
== NULL
) {
382 DBG(READ
, ul_debug("blkid: device %s has no TYPE",dev
->bid_name
));
387 DBG(READ
, blkid_debug_dump_dev(dev
));
394 * Parse the specified filename, and return the data in the supplied or
395 * a newly allocated cache struct. If the file doesn't exist, return a
396 * new empty cache struct.
398 void blkid_read_cache(blkid_cache cache
)
409 * If the file doesn't exist, then we just return an empty
410 * struct so that the cache can be populated.
412 if ((fd
= open(cache
->bic_filename
, O_RDONLY
|O_CLOEXEC
)) < 0)
414 if (fstat(fd
, &st
) < 0)
416 if ((st
.st_mtime
== cache
->bic_ftime
) ||
417 (cache
->bic_flags
& BLKID_BIC_FL_CHANGED
)) {
418 DBG(CACHE
, ul_debug("skipping re-read of %s",
419 cache
->bic_filename
));
423 DBG(CACHE
, ul_debug("reading cache file %s",
424 cache
->bic_filename
));
426 file
= fdopen(fd
, "r" UL_CLOEXECSTR
);
430 while (fgets(buf
, sizeof(buf
), file
)) {
437 end
= strlen(buf
) - 1;
438 /* Continue reading next line if it ends with a backslash */
439 while (end
< (sizeof(buf
) - 2) && buf
[end
] == '\\' &&
440 fgets(buf
+ end
, sizeof(buf
) - end
, file
)) {
441 end
= strlen(buf
) - 1;
445 if (blkid_parse_line(cache
, &dev
, buf
) < 0) {
446 DBG(READ
, ul_debug("blkid: bad format on line %d", lineno
));
453 * Initially we do not need to write out the cache file.
455 cache
->bic_flags
&= ~BLKID_BIC_FL_CHANGED
;
456 cache
->bic_ftime
= st
.st_mtime
;
465 static void debug_dump_dev(blkid_dev dev
)
470 printf(" dev: NULL\n");
474 printf(" dev: name = %s\n", dev
->bid_name
);
475 printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev
->bid_devno
);
476 printf(" dev: TIME=\"%ld.%ld\"\n", (long)dev
->bid_time
, (long)dev
->bid_utime
);
477 printf(" dev: PRI=\"%d\"\n", dev
->bid_pri
);
478 printf(" dev: flags = 0x%08X\n", dev
->bid_flags
);
480 list_for_each(p
, &dev
->bid_tags
) {
481 blkid_tag tag
= list_entry(p
, struct blkid_struct_tag
, bit_tags
);
483 printf(" tag: %s=\"%s\"\n", tag
->bit_name
,
486 printf(" tag: NULL\n");
491 int main(int argc
, char**argv
)
493 blkid_cache cache
= NULL
;
496 blkid_init_debug(BLKID_DEBUG_ALL
);
498 fprintf(stderr
, "Usage: %s [filename]\n"
499 "Test parsing of the cache (filename)\n", argv
[0]);
502 if ((ret
= blkid_get_cache(&cache
, argv
[1])) < 0)
503 fprintf(stderr
, "error %d reading cache file %s\n", ret
,
504 argv
[1] ? argv
[1] : blkid_get_cache_filename(NULL
));
506 blkid_put_cache(cache
);