Debian Codegraph @

libblkid/src/read.c

raw
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 /*
36 * File format:
37 *
38 * <device [<NAME="value"> ...]>device_name</device>
39 *
40 * The following tags are required for each entry:
41 * <ID="id"> unique (within this file) ID number of this device
42 * <TIME="sec.usec"> (time_t and suseconds_t) time this entry was last
43 * read from disk
44 * <TYPE="type"> (detected) type of filesystem/data for this partition
45 *
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)
49 */
50
51 static char *skip_over_blank(char *cp)
52 {
53 while (*cp && isspace(*cp))
54 cp++;
55 return cp;
56 }
57
58 static char *skip_over_word(char *cp)
59 {
60 char ch;
61
62 while ((ch = *cp)) {
63 /* If we see a backslash, skip the next character */
64 if (ch == '\\') {
65 cp++;
66 if (*cp == '\0')
67 break;
68 cp++;
69 continue;
70 }
71 if (isspace(ch) || ch == '<' || ch == '>')
72 break;
73 cp++;
74 }
75 return cp;
76 }
77
78 static char *strip_line(char *line)
79 {
80 char *p;
81
82 line = skip_over_blank(line);
83
84 p = line + strlen(line) - 1;
85
86 while (*line) {
87 if (isspace(*p))
88 *p-- = '\0';
89 else
90 break;
91 }
92
93 return line;
94 }
95
96 #if 0
97 static char *parse_word(char **buf)
98 {
99 char *word, *next;
100
101 word = *buf;
102 if (*word == '\0')
103 return NULL;
104
105 word = skip_over_blank(word);
106 next = skip_over_word(word);
107 if (*next) {
108 char *end = next - 1;
109 if (*end == '"' || *end == '\'')
110 *end = '\0';
111 *next++ = '\0';
112 }
113 *buf = next;
114
115 if (*word == '"' || *word == '\'')
116 word++;
117 return word;
118 }
119 #endif
120
121 /*
122 * Start parsing a new line from the cache.
123 *
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
127 */
128 static int parse_start(char **cp)
129 {
130 char *p;
131
132 p = strip_line(*cp);
133
134 /* Skip comment or blank lines. We can't just NUL the first '#' char,
135 * in case it is inside quotes, or escaped.
136 */
137 if (*p == '\0' || *p == '#')
138 return 0;
139
140 if (!strncmp(p, "<device", 7)) {
141 DBG(READ, ul_debug("found device header: %8s", p));
142 p += 7;
143
144 *cp = p;
145 return 1;
146 }
147
148 if (*p == '<')
149 return 0;
150
151 return -BLKID_ERR_CACHE;
152 }
153
154 /* Consume the remaining XML on the line (cosmetic only) */
155 static int parse_end(char **cp)
156 {
157 *cp = skip_over_blank(*cp);
158
159 if (!strncmp(*cp, "</device>", 9)) {
160 DBG(READ, ul_debug("found device trailer %9s", *cp));
161 *cp += 9;
162 return 0;
163 }
164
165 return -BLKID_ERR_CACHE;
166 }
167
168 /*
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>
173 */
174 static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
175 {
176 char *start, *tmp, *end, *name;
177 int ret;
178
179 if ((ret = parse_start(cp)) <= 0)
180 return ret;
181
182 start = tmp = strchr(*cp, '>');
183 if (!start) {
184 DBG(READ, ul_debug("blkid: short line parsing dev: %s", *cp));
185 return -BLKID_ERR_CACHE;
186 }
187 start = skip_over_blank(start + 1);
188 end = skip_over_word(start);
189
190 ON_DBG(READ, {
191 char c = *end;
192 *end = '\0';
193 ul_debug("device should be %s", start);
194 *end = c;
195 });
196
197 if (**cp == '>')
198 *cp = end;
199 else
200 (*cp)++;
201
202 *tmp = '\0';
203
204 if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
205 DBG(READ, ul_debug("blkid: missing </device> ending: %s", end));
206 } else if (tmp)
207 *tmp = '\0';
208
209 if (end - start <= 1) {
210 DBG(READ, ul_debug("blkid: empty device name: %s", *cp));
211 return -BLKID_ERR_CACHE;
212 }
213
214 name = strndup(start, end - start);
215 if (name == NULL)
216 return -BLKID_ERR_MEM;
217
218 DBG(READ, ul_debug("found dev %s", name));
219
220 if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE))) {
221 free(name);
222 return -BLKID_ERR_MEM;
223 }
224
225 free(name);
226 return 1;
227 }
228
229 /*
230 * Extract a tag of the form NAME="value" from the line.
231 */
232 static int parse_token(char **name, char **value, char **cp)
233 {
234 char *end;
235
236 if (!name || !value || !cp)
237 return -BLKID_ERR_PARAM;
238
239 if (!(*value = strchr(*cp, '=')))
240 return 0;
241
242 **value = '\0';
243 *name = strip_line(*cp);
244 *value = skip_over_blank(*value + 1);
245
246 if (**value == '"') {
247 char *p = end = *value + 1;
248
249 /* convert 'foo\"bar' to 'foo"bar' */
250 while (*p) {
251 if (*p == '\\') {
252 p++;
253 *end = *p;
254 } else {
255 *end = *p;
256 if (*p == '"')
257 break;
258 }
259 p++;
260 end++;
261 }
262
263 if (*end != '"') {
264 DBG(READ, ul_debug("unbalanced quotes at: %s", *value));
265 *cp = *value;
266 return -BLKID_ERR_CACHE;
267 }
268 (*value)++;
269 *end = '\0';
270 end = ++p;
271 } else {
272 end = skip_over_word(*value);
273 if (*end) {
274 *end = '\0';
275 end++;
276 }
277 }
278 *cp = end;
279
280 return 1;
281 }
282
283 /*
284 * Extract a tag from the line.
285 *
286 * Return 1 if a valid tag was found.
287 * Return 0 if no tag found.
288 * Return -ve error code.
289 */
290 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
291 {
292 char *name = NULL;
293 char *value = NULL;
294 int ret;
295
296 if (!cache || !dev)
297 return -BLKID_ERR_PARAM;
298
299 if ((ret = parse_token(&name, &value, cp)) <= 0)
300 return ret;
301
302 DBG(READ, ul_debug("tag: %s=\"%s\"", name, value));
303
304 errno = 0;
305
306 /* Some tags are stored directly in the device struct */
307 if (!strcmp(name, "DEVNO")) {
308 dev->bid_devno = strtoull(value, NULL, 0);
309 if (errno)
310 return -errno;
311 } else if (!strcmp(name, "PRI")) {
312 dev->bid_pri = strtol(value, NULL, 0);
313 if (errno)
314 return -errno;
315 } else if (!strcmp(name, "TIME")) {
316 char *end = NULL;
317
318 dev->bid_time = strtoull(value, &end, 0);
319 if (errno == 0 && end && *end == '.')
320 dev->bid_utime = strtoull(end + 1, NULL, 0);
321 if (errno)
322 return -errno;
323 } else
324 ret = blkid_set_tag(dev, name, value, strlen(value));
325
326 return ret < 0 ? ret : 1;
327 }
328
329 /*
330 * Parse a single line of data, and return a newly allocated dev struct.
331 * Add the new device to the cache struct, if one was read.
332 *
333 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
334 *
335 * Returns -ve value on error.
336 * Returns 0 otherwise.
337 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
338 * (e.g. comment lines, unknown XML content, etc).
339 */
340 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
341 {
342 blkid_dev dev;
343 int ret;
344
345 if (!cache || !dev_p)
346 return -BLKID_ERR_PARAM;
347
348 *dev_p = NULL;
349
350 DBG(READ, ul_debug("line: %s", cp));
351
352 if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
353 return ret;
354
355 dev = *dev_p;
356
357 while ((ret = parse_tag(cache, dev, &cp)) > 0) {
358 ;
359 }
360
361 if (dev->bid_type == NULL) {
362 DBG(READ, ul_debug("blkid: device %s has no TYPE",dev->bid_name));
363 blkid_free_dev(dev);
364 goto done;
365 }
366
367 done:
368 return ret;
369 }
370
371 /*
372 * Parse the specified filename, and return the data in the supplied or
373 * a newly allocated cache struct. If the file doesn't exist, return a
374 * new empty cache struct.
375 */
376 void blkid_read_cache(blkid_cache cache)
377 {
378 FILE *file;
379 char buf[4096];
380 int fd, lineno = 0;
381 struct stat st;
382
383 /*
384 * If the file doesn't exist, then we just return an empty
385 * struct so that the cache can be populated.
386 */
387 if ((fd = open(cache->bic_filename, O_RDONLY|O_CLOEXEC)) < 0)
388 return;
389 if (fstat(fd, &st) < 0)
390 goto errout;
391 if ((st.st_mtime == cache->bic_ftime) ||
392 (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
393 DBG(CACHE, ul_debug("skipping re-read of %s",
394 cache->bic_filename));
395 goto errout;
396 }
397
398 DBG(CACHE, ul_debug("reading cache file %s",
399 cache->bic_filename));
400
401 file = fdopen(fd, "r" UL_CLOEXECSTR);
402 if (!file)
403 goto errout;
404
405 while (fgets(buf, sizeof(buf), file)) {
406 blkid_dev dev;
407 unsigned int end;
408
409 lineno++;
410 if (buf[0] == 0)
411 continue;
412 end = strlen(buf) - 1;
413 /* Continue reading next line if it ends with a backslash */
414 while (end < (sizeof(buf) - 2) && buf[end] == '\\' &&
415 fgets(buf + end, sizeof(buf) - end, file)) {
416 end = strlen(buf) - 1;
417 lineno++;
418 }
419
420 if (blkid_parse_line(cache, &dev, buf) < 0) {
421 DBG(READ, ul_debug("blkid: bad format on line %d", lineno));
422 continue;
423 }
424 }
425 fclose(file);
426
427 /*
428 * Initially we do not need to write out the cache file.
429 */
430 cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
431 cache->bic_ftime = st.st_mtime;
432
433 return;
434 errout:
435 close(fd);
436 }
437
438 #ifdef TEST_PROGRAM
439
440 int main(int argc, char**argv)
441 {
442 blkid_cache cache = NULL;
443 int ret;
444
445 blkid_init_debug(BLKID_DEBUG_ALL);
446 if (argc > 2) {
447 fprintf(stderr, "Usage: %s [filename]\n"
448 "Test parsing of the cache (filename)\n", argv[0]);
449 exit(1);
450 }
451 if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
452 fprintf(stderr, "error %d reading cache file %s\n", ret,
453 argv[1] ? argv[1] : blkid_get_cache_filename(NULL));
454
455 blkid_put_cache(cache);
456
457 return ret;
458 }
459 #endif