blob: ca2798f052f867f45c95e7815f3e980a3c453374 [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
2 * read.c - read the blkid cache from disk, to avoid scanning all devices
3 *
Theodore Ts'o50b380b2003-02-12 23:51:21 -05004 * Copyright (C) 2001, 2003 Theodore Y. Ts'o
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05005 * 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
Andreas Dilger864b8d42008-08-24 20:37:39 -040013#define _XOPEN_SOURCE 600 /* for inclusion of strtoull */
14
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050015#include <stdio.h>
16#include <ctype.h>
17#include <string.h>
18#include <time.h>
Theodore Ts'o79dd2342003-02-22 17:15:20 -050019#include <sys/types.h>
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050020#include <sys/stat.h>
Theodore Ts'o79dd2342003-02-22 17:15:20 -050021#include <fcntl.h>
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050022#include <unistd.h>
23#if HAVE_ERRNO_H
24#include <errno.h>
25#endif
26
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050027#include "blkidP.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050028#include "uuid/uuid.h"
29
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050030#ifdef HAVE_STRTOULL
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050031#define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
32#else
33/* FIXME: need to support real strtoull here */
34#define STRTOULL strtoul
35#endif
36
37#if HAVE_STDLIB_H
38#include <stdlib.h>
39#endif
40
Theodore Ts'o55080a72005-06-27 13:54:08 -040041#ifdef TEST_PROGRAM
42#define blkid_debug_dump_dev(dev) (debug_dump_dev(dev))
43static void debug_dump_dev(blkid_dev dev);
44#endif
45
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050046/*
47 * File format:
48 *
49 * <device [<NAME="value"> ...]>device_name</device>
50 *
51 * The following tags are required for each entry:
52 * <ID="id"> unique (within this file) ID number of this device
53 * <TIME="time"> (ascii time_t) time this entry was last read from disk
54 * <TYPE="type"> (detected) type of filesystem/data for this partition
55 *
56 * The following tags may be present, depending on the device contents
57 * <LABEL="label"> (user supplied) label (volume name, etc)
58 * <UUID="uuid"> (generated) universally unique identifier (serial no)
59 */
60
61static char *skip_over_blank(char *cp)
62{
63 while (*cp && isspace(*cp))
64 cp++;
65 return cp;
66}
67
68static char *skip_over_word(char *cp)
69{
70 char ch;
71
72 while ((ch = *cp)) {
73 /* If we see a backslash, skip the next character */
74 if (ch == '\\') {
75 cp++;
76 if (*cp == '\0')
77 break;
78 cp++;
79 continue;
80 }
81 if (isspace(ch) || ch == '<' || ch == '>')
82 break;
83 cp++;
84 }
85 return cp;
86}
87
88static char *strip_line(char *line)
89{
90 char *p;
91
92 line = skip_over_blank(line);
93
94 p = line + strlen(line) - 1;
95
96 while (*line) {
97 if (isspace(*p))
98 *p-- = '\0';
99 else
100 break;
101 }
102
103 return line;
104}
105
106#if 0
107static char *parse_word(char **buf)
108{
109 char *word, *next;
110
111 word = *buf;
112 if (*word == '\0')
113 return NULL;
114
115 word = skip_over_blank(word);
116 next = skip_over_word(word);
117 if (*next) {
118 char *end = next - 1;
119 if (*end == '"' || *end == '\'')
120 *end = '\0';
121 *next++ = '\0';
122 }
123 *buf = next;
124
125 if (*word == '"' || *word == '\'')
126 word++;
127 return word;
128}
129#endif
130
131/*
132 * Start parsing a new line from the cache.
133 *
134 * line starts with "<device" return 1 -> continue parsing line
135 * line starts with "<foo", empty, or # return 0 -> skip line
136 * line starts with other, return -BLKID_ERR_CACHE -> error
137 */
138static int parse_start(char **cp)
139{
140 char *p;
141
142 p = strip_line(*cp);
143
144 /* Skip comment or blank lines. We can't just NUL the first '#' char,
145 * in case it is inside quotes, or escaped.
146 */
147 if (*p == '\0' || *p == '#')
148 return 0;
149
150 if (!strncmp(p, "<device", 7)) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500151 DBG(DEBUG_READ, printf("found device header: %8s\n", p));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500152 p += 7;
153
154 *cp = p;
155 return 1;
156 }
157
158 if (*p == '<')
159 return 0;
160
161 return -BLKID_ERR_CACHE;
162}
163
164/* Consume the remaining XML on the line (cosmetic only) */
165static int parse_end(char **cp)
166{
167 *cp = skip_over_blank(*cp);
168
169 if (!strncmp(*cp, "</device>", 9)) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500170 DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500171 *cp += 9;
172 return 0;
173 }
174
175 return -BLKID_ERR_CACHE;
176}
177
178/*
179 * Allocate a new device struct with device name filled in. Will handle
180 * finding the device on lines of the form:
181 * <device foo=bar>devname</device>
182 * <device>devname<foo>bar</foo></device>
183 */
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500184static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500185{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500186 char *start, *tmp, *end, *name;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500187 int ret;
188
189 if ((ret = parse_start(cp)) <= 0)
190 return ret;
191
192 start = tmp = strchr(*cp, '>');
193 if (!start) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500194 DBG(DEBUG_READ,
195 printf("blkid: short line parsing dev: %s\n", *cp));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500196 return -BLKID_ERR_CACHE;
197 }
198 start = skip_over_blank(start + 1);
199 end = skip_over_word(start);
200
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400201 DBG(DEBUG_READ, printf("device should be %*s\n",
202 (int)(end - start), start));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500203
204 if (**cp == '>')
205 *cp = end;
206 else
207 (*cp)++;
208
209 *tmp = '\0';
210
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500211 if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
212 DBG(DEBUG_READ,
213 printf("blkid: missing </device> ending: %s\n", end));
214 } else if (tmp)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500215 *tmp = '\0';
216
217 if (end - start <= 1) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500218 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500219 return -BLKID_ERR_CACHE;
220 }
221
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500222 name = blkid_strndup(start, end-start);
223 if (name == NULL)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500224 return -BLKID_ERR_MEM;
225
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500226 DBG(DEBUG_READ, printf("found dev %s\n", name));
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500227
Brian Behlendorfe0a700d2007-03-23 22:55:59 -0400228 if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE))) {
229 free(name);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500230 return -BLKID_ERR_MEM;
Brian Behlendorfe0a700d2007-03-23 22:55:59 -0400231 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500232
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500233 free(name);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500234 return 1;
235}
236
237/*
238 * Extract a tag of the form NAME="value" from the line.
239 */
240static 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 == '"') {
255 end = strchr(*value + 1, '"');
256 if (!end) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500257 DBG(DEBUG_READ,
258 printf("unbalanced quotes at: %s\n", *value));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500259 *cp = *value;
260 return -BLKID_ERR_CACHE;
261 }
262 (*value)++;
263 *end = '\0';
264 end++;
265 } else {
266 end = skip_over_word(*value);
267 if (*end) {
268 *end = '\0';
269 end++;
270 }
271 }
272 *cp = end;
273
274 return 1;
275}
276
277/*
278 * Extract a tag of the form <NAME>value</NAME> from the line.
279 */
280/*
281static int parse_xml(char **name, char **value, char **cp)
282{
283 char *end;
284
285 if (!name || !value || !cp)
286 return -BLKID_ERR_PARAM;
287
288 *name = strip_line(*cp);
289
290 if ((*name)[0] != '<' || (*name)[1] == '/')
291 return 0;
292
293 FIXME: finish this.
294}
295*/
296
297/*
298 * Extract a tag from the line.
299 *
300 * Return 1 if a valid tag was found.
301 * Return 0 if no tag found.
302 * Return -ve error code.
303 */
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500304static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500305{
306 char *name;
307 char *value;
308 int ret;
309
310 if (!cache || !dev)
311 return -BLKID_ERR_PARAM;
312
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500313 if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
314 (ret = parse_xml(&name, &value, cp)) <= 0 */)
315 return ret;
316
317 /* Some tags are stored directly in the device struct */
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400318 if (!strcmp(name, "DEVNO"))
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500319 dev->bid_devno = STRTOULL(value, 0, 0);
Theodore Ts'oce72b862003-02-14 01:31:45 -0500320 else if (!strcmp(name, "PRI"))
321 dev->bid_pri = strtol(value, 0, 0);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500322 else if (!strcmp(name, "TIME"))
Andreas Dilger864b8d42008-08-24 20:37:39 -0400323 dev->bid_time = STRTOULL(value, 0, 0);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500324 else
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500325 ret = blkid_set_tag(dev, name, value, strlen(value));
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500326
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500327 DBG(DEBUG_READ, printf(" tag: %s=\"%s\"\n", name, value));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500328
329 return ret < 0 ? ret : 1;
330}
331
332/*
333 * Parse a single line of data, and return a newly allocated dev struct.
334 * Add the new device to the cache struct, if one was read.
335 *
336 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
337 *
338 * Returns -ve value on error.
339 * Returns 0 otherwise.
340 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
341 * (e.g. comment lines, unknown XML content, etc).
342 */
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500343static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500344{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500345 blkid_dev dev;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500346 int ret;
347
348 if (!cache || !dev_p)
349 return -BLKID_ERR_PARAM;
350
351 *dev_p = NULL;
352
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500353 DBG(DEBUG_READ, printf("line: %s\n", cp));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500354
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500355 if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500356 return ret;
357
358 dev = *dev_p;
359
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500360 while ((ret = parse_tag(cache, dev, &cp)) > 0) {
361 ;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500362 }
363
364 if (dev->bid_type == NULL) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500365 DBG(DEBUG_READ,
366 printf("blkid: device %s has no TYPE\n",dev->bid_name));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500367 blkid_free_dev(dev);
368 }
369
Theodore Ts'o78c7d0e2005-05-07 14:22:38 -0400370 DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500371
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500372 return ret;
373}
374
375/*
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500376 * Parse the specified filename, and return the data in the supplied or
377 * a newly allocated cache struct. If the file doesn't exist, return a
378 * new empty cache struct.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500379 */
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500380void blkid_read_cache(blkid_cache cache)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500381{
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500382 FILE *file;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500383 char buf[4096];
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500384 int fd, lineno = 0;
385 struct stat st;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500386
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500387 if (!cache)
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500388 return;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500389
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500390 /*
391 * If the file doesn't exist, then we just return an empty
392 * struct so that the cache can be populated.
393 */
394 if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500395 return;
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500396 if (fstat(fd, &st) < 0)
397 goto errout;
398 if ((st.st_mtime == cache->bic_ftime) ||
399 (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
400 DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
401 cache->bic_filename));
402 goto errout;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500403 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400404
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500405 DBG(DEBUG_CACHE, printf("reading cache file %s\n",
406 cache->bic_filename));
407
408 file = fdopen(fd, "r");
409 if (!file)
410 goto errout;
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500411
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500412 while (fgets(buf, sizeof(buf), file)) {
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500413 blkid_dev dev;
Theodore Ts'o54434922003-12-07 01:28:50 -0500414 unsigned int end;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500415
416 lineno++;
Theodore Ts'o54434922003-12-07 01:28:50 -0500417 if (buf[0] == 0)
418 continue;
419 end = strlen(buf) - 1;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500420 /* Continue reading next line if it ends with a backslash */
421 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500422 fgets(buf + end, sizeof(buf) - end, file)) {
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500423 end = strlen(buf) - 1;
424 lineno++;
425 }
426
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500427 if (blkid_parse_line(cache, &dev, buf) < 0) {
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500428 DBG(DEBUG_READ,
429 printf("blkid: bad format on line %d\n", lineno));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500430 continue;
431 }
432 }
Theodore Ts'o1b510f52004-05-15 17:27:43 -0400433 fclose(file);
434
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500435 /*
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500436 * Initially we do not need to write out the cache file.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500437 */
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500438 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
439 cache->bic_ftime = st.st_mtime;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500440
Theodore Ts'oed1b33e2003-03-01 19:29:01 -0500441 return;
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500442errout:
443 close(fd);
444 return;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500445}
446
447#ifdef TEST_PROGRAM
Theodore Ts'o55080a72005-06-27 13:54:08 -0400448static void debug_dump_dev(blkid_dev dev)
449{
450 struct list_head *p;
451
452 if (!dev) {
453 printf(" dev: NULL\n");
454 return;
455 }
456
457 printf(" dev: name = %s\n", dev->bid_name);
Matthias Andree12a829d2006-05-30 01:48:51 +0200458 printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
459 printf(" dev: TIME=\"%lld\"\n", (long long)dev->bid_time);
Theodore Ts'o55080a72005-06-27 13:54:08 -0400460 printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
461 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
462
463 list_for_each(p, &dev->bid_tags) {
464 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
465 if (tag)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400466 printf(" tag: %s=\"%s\"\n", tag->bit_name,
Theodore Ts'o55080a72005-06-27 13:54:08 -0400467 tag->bit_val);
468 else
469 printf(" tag: NULL\n");
470 }
471 printf("\n");
472}
473
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500474int main(int argc, char**argv)
475{
Theodore Ts'o7a603aa2003-01-26 01:54:39 -0500476 blkid_cache cache = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500477 int ret;
478
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500479 blkid_debug_mask = DEBUG_ALL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500480 if (argc > 2) {
481 fprintf(stderr, "Usage: %s [filename]\n"
482 "Test parsing of the cache (filename)\n", argv[0]);
483 exit(1);
484 }
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500485 if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500486 fprintf(stderr, "error %d reading cache file %s\n", ret,
487 argv[1] ? argv[1] : BLKID_CACHE_FILE);
488
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500489 blkid_put_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500490
491 return ret;
492}
493#endif