bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * read.c - read the blkid cache from disk, to avoid scanning all devices |
| 3 | * |
| 4 | * Copyright (C) 2001, 2003 Theodore Y. Ts'o |
| 5 | * Copyright (C) 2001 Andreas Dilger |
| 6 | * |
| 7 | * %Begin-Header% |
| 8 | * This file may be redistributed under the terms of the |
| 9 | * GNU Lesser General Public License. |
| 10 | * %End-Header% |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <ctype.h> |
| 16 | #include <string.h> |
| 17 | #include <time.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <unistd.h> |
| 22 | #ifdef HAVE_ERRNO_H |
| 23 | #include <errno.h> |
| 24 | #endif |
| 25 | |
| 26 | #include "blkidP.h" |
| 27 | |
| 28 | #ifdef HAVE_STDLIB_H |
| 29 | # ifndef _XOPEN_SOURCE |
| 30 | # define _XOPEN_SOURCE 600 /* for inclusion of strtoull */ |
| 31 | # endif |
| 32 | # include <stdlib.h> |
| 33 | #endif |
| 34 | |
| 35 | #ifdef HAVE_STRTOULL |
| 36 | #define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */ |
| 37 | #else |
| 38 | /* FIXME: need to support real strtoull here */ |
| 39 | #define STRTOULL strtoul |
| 40 | #endif |
| 41 | |
| 42 | #ifdef TEST_PROGRAM |
| 43 | #define blkid_debug_dump_dev(dev) (debug_dump_dev(dev)) |
| 44 | static void debug_dump_dev(blkid_dev dev); |
| 45 | #endif |
| 46 | |
| 47 | /* |
| 48 | * File format: |
| 49 | * |
| 50 | * <device [<NAME="value"> ...]>device_name</device> |
| 51 | * |
| 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 |
| 55 | * read from disk |
| 56 | * <TYPE="type"> (detected) type of filesystem/data for this partition |
| 57 | * |
| 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) |
| 61 | */ |
| 62 | |
| 63 | static char *skip_over_blank(char *cp) |
| 64 | { |
| 65 | while (*cp && isspace(*cp)) |
| 66 | cp++; |
| 67 | return cp; |
| 68 | } |
| 69 | |
| 70 | static char *skip_over_word(char *cp) |
| 71 | { |
| 72 | char ch; |
| 73 | |
| 74 | while ((ch = *cp)) { |
| 75 | /* If we see a backslash, skip the next character */ |
| 76 | if (ch == '\\') { |
| 77 | cp++; |
| 78 | if (*cp == '\0') |
| 79 | break; |
| 80 | cp++; |
| 81 | continue; |
| 82 | } |
| 83 | if (isspace(ch) || ch == '<' || ch == '>') |
| 84 | break; |
| 85 | cp++; |
| 86 | } |
| 87 | return cp; |
| 88 | } |
| 89 | |
| 90 | static char *strip_line(char *line) |
| 91 | { |
| 92 | char *p; |
| 93 | |
| 94 | line = skip_over_blank(line); |
| 95 | |
| 96 | p = line + strlen(line) - 1; |
| 97 | |
| 98 | while (*line) { |
| 99 | if (isspace(*p)) |
| 100 | *p-- = '\0'; |
| 101 | else |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | return line; |
| 106 | } |
| 107 | |
| 108 | #if 0 |
| 109 | static char *parse_word(char **buf) |
| 110 | { |
| 111 | char *word, *next; |
| 112 | |
| 113 | word = *buf; |
| 114 | if (*word == '\0') |
| 115 | return NULL; |
| 116 | |
| 117 | word = skip_over_blank(word); |
| 118 | next = skip_over_word(word); |
| 119 | if (*next) { |
| 120 | char *end = next - 1; |
| 121 | if (*end == '"' || *end == '\'') |
| 122 | *end = '\0'; |
| 123 | *next++ = '\0'; |
| 124 | } |
| 125 | *buf = next; |
| 126 | |
| 127 | if (*word == '"' || *word == '\'') |
| 128 | word++; |
| 129 | return word; |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | /* |
| 134 | * Start parsing a new line from the cache. |
| 135 | * |
| 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 |
| 139 | */ |
| 140 | static int parse_start(char **cp) |
| 141 | { |
| 142 | char *p; |
| 143 | |
| 144 | p = strip_line(*cp); |
| 145 | |
| 146 | /* Skip comment or blank lines. We can't just NUL the first '#' char, |
| 147 | * in case it is inside quotes, or escaped. |
| 148 | */ |
| 149 | if (*p == '\0' || *p == '#') |
| 150 | return 0; |
| 151 | |
| 152 | if (!strncmp(p, "<device", 7)) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 153 | DBG(READ, ul_debug("found device header: %8s", p)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 154 | p += 7; |
| 155 | |
| 156 | *cp = p; |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | if (*p == '<') |
| 161 | return 0; |
| 162 | |
| 163 | return -BLKID_ERR_CACHE; |
| 164 | } |
| 165 | |
| 166 | /* Consume the remaining XML on the line (cosmetic only) */ |
| 167 | static int parse_end(char **cp) |
| 168 | { |
| 169 | *cp = skip_over_blank(*cp); |
| 170 | |
| 171 | if (!strncmp(*cp, "</device>", 9)) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 172 | DBG(READ, ul_debug("found device trailer %9s", *cp)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 173 | *cp += 9; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | return -BLKID_ERR_CACHE; |
| 178 | } |
| 179 | |
| 180 | /* |
| 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> |
| 185 | */ |
| 186 | static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp) |
| 187 | { |
| 188 | char *start, *tmp, *end, *name; |
| 189 | int ret; |
| 190 | |
| 191 | if ((ret = parse_start(cp)) <= 0) |
| 192 | return ret; |
| 193 | |
| 194 | start = tmp = strchr(*cp, '>'); |
| 195 | if (!start) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 196 | DBG(READ, ul_debug("blkid: short line parsing dev: %s", *cp)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 197 | return -BLKID_ERR_CACHE; |
| 198 | } |
| 199 | start = skip_over_blank(start + 1); |
| 200 | end = skip_over_word(start); |
| 201 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 202 | DBG(READ, ul_debug("device should be %*s", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 203 | (int)(end - start), start)); |
| 204 | |
| 205 | if (**cp == '>') |
| 206 | *cp = end; |
| 207 | else |
| 208 | (*cp)++; |
| 209 | |
| 210 | *tmp = '\0'; |
| 211 | |
| 212 | if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 213 | DBG(READ, ul_debug("blkid: missing </device> ending: %s", end)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 214 | } else if (tmp) |
| 215 | *tmp = '\0'; |
| 216 | |
| 217 | if (end - start <= 1) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 218 | DBG(READ, ul_debug("blkid: empty device name: %s", *cp)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 219 | return -BLKID_ERR_CACHE; |
| 220 | } |
| 221 | |
| 222 | name = strndup(start, end - start); |
| 223 | if (name == NULL) |
| 224 | return -BLKID_ERR_MEM; |
| 225 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 226 | DBG(READ, ul_debug("found dev %s", name)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 227 | |
| 228 | if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE))) { |
| 229 | free(name); |
| 230 | return -BLKID_ERR_MEM; |
| 231 | } |
| 232 | |
| 233 | free(name); |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Extract a tag of the form NAME="value" from the line. |
| 239 | */ |
| 240 | static int parse_token(char **name, char **value, char **cp) |
| 241 | { |
| 242 | char *end; |
| 243 | |
| 244 | if (!name || !value || !cp) |
| 245 | return -BLKID_ERR_PARAM; |
| 246 | |
| 247 | if (!(*value = strchr(*cp, '='))) |
| 248 | return 0; |
| 249 | |
| 250 | **value = '\0'; |
| 251 | *name = strip_line(*cp); |
| 252 | *value = skip_over_blank(*value + 1); |
| 253 | |
| 254 | if (**value == '"') { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 255 | char *p = end = *value + 1; |
| 256 | |
| 257 | /* convert 'foo\"bar' to 'foo"bar' */ |
| 258 | while (*p) { |
| 259 | if (*p == '\\') { |
| 260 | p++; |
| 261 | *end = *p; |
| 262 | } else { |
| 263 | *end = *p; |
| 264 | if (*p == '"') |
| 265 | break; |
| 266 | } |
| 267 | p++; |
| 268 | end++; |
| 269 | } |
| 270 | |
| 271 | if (*end != '"') { |
| 272 | DBG(READ, ul_debug("unbalanced quotes at: %s", *value)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 273 | *cp = *value; |
| 274 | return -BLKID_ERR_CACHE; |
| 275 | } |
| 276 | (*value)++; |
| 277 | *end = '\0'; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 278 | end = ++p; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 279 | } else { |
| 280 | end = skip_over_word(*value); |
| 281 | if (*end) { |
| 282 | *end = '\0'; |
| 283 | end++; |
| 284 | } |
| 285 | } |
| 286 | *cp = end; |
| 287 | |
| 288 | return 1; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Extract a tag of the form <NAME>value</NAME> from the line. |
| 293 | */ |
| 294 | /* |
| 295 | static int parse_xml(char **name, char **value, char **cp) |
| 296 | { |
| 297 | char *end; |
| 298 | |
| 299 | if (!name || !value || !cp) |
| 300 | return -BLKID_ERR_PARAM; |
| 301 | |
| 302 | *name = strip_line(*cp); |
| 303 | |
| 304 | if ((*name)[0] != '<' || (*name)[1] == '/') |
| 305 | return 0; |
| 306 | |
| 307 | FIXME: finish this. |
| 308 | } |
| 309 | */ |
| 310 | |
| 311 | /* |
| 312 | * Extract a tag from the line. |
| 313 | * |
| 314 | * Return 1 if a valid tag was found. |
| 315 | * Return 0 if no tag found. |
| 316 | * Return -ve error code. |
| 317 | */ |
| 318 | static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp) |
| 319 | { |
| 320 | char *name = NULL; |
| 321 | char *value = NULL; |
| 322 | int ret; |
| 323 | |
| 324 | if (!cache || !dev) |
| 325 | return -BLKID_ERR_PARAM; |
| 326 | |
| 327 | if ((ret = parse_token(&name, &value, cp)) <= 0 /* && |
| 328 | (ret = parse_xml(&name, &value, cp)) <= 0 */) |
| 329 | return ret; |
| 330 | |
| 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")) { |
| 337 | char *end = NULL; |
| 338 | dev->bid_time = STRTOULL(value, &end, 0); |
| 339 | if (end && *end == '.') |
| 340 | dev->bid_utime = STRTOULL(end + 1, 0, 0); |
| 341 | } else |
| 342 | ret = blkid_set_tag(dev, name, value, strlen(value)); |
| 343 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 344 | DBG(READ, ul_debug(" tag: %s=\"%s\"", name, value)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 345 | |
| 346 | return ret < 0 ? ret : 1; |
| 347 | } |
| 348 | |
| 349 | /* |
| 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. |
| 352 | * |
| 353 | * Lines are of the form <device [TAG="value" ...]>/dev/foo</device> |
| 354 | * |
| 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). |
| 359 | */ |
| 360 | static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp) |
| 361 | { |
| 362 | blkid_dev dev; |
| 363 | int ret; |
| 364 | |
| 365 | if (!cache || !dev_p) |
| 366 | return -BLKID_ERR_PARAM; |
| 367 | |
| 368 | *dev_p = NULL; |
| 369 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 370 | DBG(READ, ul_debug("line: %s", cp)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 371 | |
| 372 | if ((ret = parse_dev(cache, dev_p, &cp)) <= 0) |
| 373 | return ret; |
| 374 | |
| 375 | dev = *dev_p; |
| 376 | |
| 377 | while ((ret = parse_tag(cache, dev, &cp)) > 0) { |
| 378 | ; |
| 379 | } |
| 380 | |
| 381 | if (dev->bid_type == NULL) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 382 | DBG(READ, ul_debug("blkid: device %s has no TYPE",dev->bid_name)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 383 | blkid_free_dev(dev); |
| 384 | goto done; |
| 385 | } |
| 386 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 387 | DBG(READ, blkid_debug_dump_dev(dev)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 388 | |
| 389 | done: |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | /* |
| 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. |
| 397 | */ |
| 398 | void blkid_read_cache(blkid_cache cache) |
| 399 | { |
| 400 | FILE *file; |
| 401 | char buf[4096]; |
| 402 | int fd, lineno = 0; |
| 403 | struct stat st; |
| 404 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 405 | /* |
| 406 | * If the file doesn't exist, then we just return an empty |
| 407 | * struct so that the cache can be populated. |
| 408 | */ |
| 409 | if ((fd = open(cache->bic_filename, O_RDONLY|O_CLOEXEC)) < 0) |
| 410 | return; |
| 411 | if (fstat(fd, &st) < 0) |
| 412 | goto errout; |
| 413 | if ((st.st_mtime == cache->bic_ftime) || |
| 414 | (cache->bic_flags & BLKID_BIC_FL_CHANGED)) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 415 | DBG(CACHE, ul_debug("skipping re-read of %s", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 416 | cache->bic_filename)); |
| 417 | goto errout; |
| 418 | } |
| 419 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 420 | DBG(CACHE, ul_debug("reading cache file %s", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 421 | cache->bic_filename)); |
| 422 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 423 | file = fdopen(fd, "r" UL_CLOEXECSTR); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 424 | if (!file) |
| 425 | goto errout; |
| 426 | |
| 427 | while (fgets(buf, sizeof(buf), file)) { |
| 428 | blkid_dev dev; |
| 429 | unsigned int end; |
| 430 | |
| 431 | lineno++; |
| 432 | if (buf[0] == 0) |
| 433 | continue; |
| 434 | end = strlen(buf) - 1; |
| 435 | /* Continue reading next line if it ends with a backslash */ |
| 436 | while (end < (sizeof(buf) - 2) && buf[end] == '\\' && |
| 437 | fgets(buf + end, sizeof(buf) - end, file)) { |
| 438 | end = strlen(buf) - 1; |
| 439 | lineno++; |
| 440 | } |
| 441 | |
| 442 | if (blkid_parse_line(cache, &dev, buf) < 0) { |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 443 | DBG(READ, ul_debug("blkid: bad format on line %d", lineno)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 444 | continue; |
| 445 | } |
| 446 | } |
| 447 | fclose(file); |
| 448 | |
| 449 | /* |
| 450 | * Initially we do not need to write out the cache file. |
| 451 | */ |
| 452 | cache->bic_flags &= ~BLKID_BIC_FL_CHANGED; |
| 453 | cache->bic_ftime = st.st_mtime; |
| 454 | |
| 455 | return; |
| 456 | errout: |
| 457 | close(fd); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | #ifdef TEST_PROGRAM |
| 462 | static void debug_dump_dev(blkid_dev dev) |
| 463 | { |
| 464 | struct list_head *p; |
| 465 | |
| 466 | if (!dev) { |
| 467 | printf(" dev: NULL\n"); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | printf(" dev: name = %s\n", dev->bid_name); |
| 472 | printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno); |
| 473 | printf(" dev: TIME=\"%ld.%ld\"\n", (long)dev->bid_time, (long)dev->bid_utime); |
| 474 | printf(" dev: PRI=\"%d\"\n", dev->bid_pri); |
| 475 | printf(" dev: flags = 0x%08X\n", dev->bid_flags); |
| 476 | |
| 477 | list_for_each(p, &dev->bid_tags) { |
| 478 | blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags); |
| 479 | if (tag) |
| 480 | printf(" tag: %s=\"%s\"\n", tag->bit_name, |
| 481 | tag->bit_val); |
| 482 | else |
| 483 | printf(" tag: NULL\n"); |
| 484 | } |
| 485 | printf("\n"); |
| 486 | } |
| 487 | |
| 488 | int main(int argc, char**argv) |
| 489 | { |
| 490 | blkid_cache cache = NULL; |
| 491 | int ret; |
| 492 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 493 | blkid_init_debug(BLKID_DEBUG_ALL); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 494 | if (argc > 2) { |
| 495 | fprintf(stderr, "Usage: %s [filename]\n" |
| 496 | "Test parsing of the cache (filename)\n", argv[0]); |
| 497 | exit(1); |
| 498 | } |
| 499 | if ((ret = blkid_get_cache(&cache, argv[1])) < 0) |
| 500 | fprintf(stderr, "error %d reading cache file %s\n", ret, |
| 501 | argv[1] ? argv[1] : blkid_get_cache_filename(NULL)); |
| 502 | |
| 503 | blkid_put_cache(cache); |
| 504 | |
| 505 | return ret; |
| 506 | } |
| 507 | #endif |