OSDN Git Service

Code drop from //branches/cupcake/...@124589
[android-x86/external-e2fsprogs.git] / lib / blkid / read.c
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 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <time.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #if HAVE_ERRNO_H
22 #include <errno.h>
23 #endif
24
25 #include "blkidP.h"
26 #include "uuid/uuid.h"
27
28 #ifdef HAVE_STRTOULL
29 #define __USE_ISOC9X
30 #define STRTOULL strtoull /* defined in stdlib.h if you try hard enough */
31 #else
32 /* FIXME: need to support real strtoull here */
33 #define STRTOULL strtoul
34 #endif
35
36 #if HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39
40 #ifdef TEST_PROGRAM
41 #define blkid_debug_dump_dev(dev)       (debug_dump_dev(dev))
42 static void debug_dump_dev(blkid_dev dev);
43 #endif
44
45 /*
46  * File format:
47  *
48  *      <device [<NAME="value"> ...]>device_name</device>
49  *
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
54  *
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)
58  */
59
60 static char *skip_over_blank(char *cp)
61 {
62         while (*cp && isspace(*cp))
63                 cp++;
64         return cp;
65 }
66
67 static char *skip_over_word(char *cp)
68 {
69         char ch;
70
71         while ((ch = *cp)) {
72                 /* If we see a backslash, skip the next character */
73                 if (ch == '\\') {
74                         cp++;
75                         if (*cp == '\0')
76                                 break;
77                         cp++;
78                         continue;
79                 }
80                 if (isspace(ch) || ch == '<' || ch == '>')
81                         break;
82                 cp++;
83         }
84         return cp;
85 }
86
87 static char *strip_line(char *line)
88 {
89         char    *p;
90
91         line = skip_over_blank(line);
92
93         p = line + strlen(line) - 1;
94
95         while (*line) {
96                 if (isspace(*p))
97                         *p-- = '\0';
98                 else
99                         break;
100         }
101
102         return line;
103 }
104
105 #if 0
106 static char *parse_word(char **buf)
107 {
108         char *word, *next;
109
110         word = *buf;
111         if (*word == '\0')
112                 return NULL;
113
114         word = skip_over_blank(word);
115         next = skip_over_word(word);
116         if (*next) {
117                 char *end = next - 1;
118                 if (*end == '"' || *end == '\'')
119                         *end = '\0';
120                 *next++ = '\0';
121         }
122         *buf = next;
123
124         if (*word == '"' || *word == '\'')
125                 word++;
126         return word;
127 }
128 #endif
129
130 /*
131  * Start parsing a new line from the cache.
132  *
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
136  */
137 static int parse_start(char **cp)
138 {
139         char *p;
140
141         p = strip_line(*cp);
142
143         /* Skip comment or blank lines.  We can't just NUL the first '#' char,
144          * in case it is inside quotes, or escaped.
145          */
146         if (*p == '\0' || *p == '#')
147                 return 0;
148
149         if (!strncmp(p, "<device", 7)) {
150                 DBG(DEBUG_READ, printf("found device header: %8s\n", p));
151                 p += 7;
152
153                 *cp = p;
154                 return 1;
155         }
156
157         if (*p == '<')
158                 return 0;
159
160         return -BLKID_ERR_CACHE;
161 }
162
163 /* Consume the remaining XML on the line (cosmetic only) */
164 static int parse_end(char **cp)
165 {
166         *cp = skip_over_blank(*cp);
167
168         if (!strncmp(*cp, "</device>", 9)) {
169                 DBG(DEBUG_READ, printf("found device trailer %9s\n", *cp));
170                 *cp += 9;
171                 return 0;
172         }
173
174         return -BLKID_ERR_CACHE;
175 }
176
177 /*
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>
182  */
183 static int parse_dev(blkid_cache cache, blkid_dev *dev, char **cp)
184 {
185         char *start, *tmp, *end, *name;
186         int ret;
187
188         if ((ret = parse_start(cp)) <= 0)
189                 return ret;
190
191         start = tmp = strchr(*cp, '>');
192         if (!start) {
193                 DBG(DEBUG_READ,
194                     printf("blkid: short line parsing dev: %s\n", *cp));
195                 return -BLKID_ERR_CACHE;
196         }
197         start = skip_over_blank(start + 1);
198         end = skip_over_word(start);
199
200         DBG(DEBUG_READ, printf("device should be %*s\n",
201                                (int)(end - start), start));
202
203         if (**cp == '>')
204                 *cp = end;
205         else
206                 (*cp)++;
207
208         *tmp = '\0';
209
210         if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
211                 DBG(DEBUG_READ,
212                     printf("blkid: missing </device> ending: %s\n", end));
213         } else if (tmp)
214                 *tmp = '\0';
215
216         if (end - start <= 1) {
217                 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
218                 return -BLKID_ERR_CACHE;
219         }
220
221         name = blkid_strndup(start, end-start);
222         if (name == NULL)
223                 return -BLKID_ERR_MEM;
224
225         DBG(DEBUG_READ, printf("found dev %s\n", name));
226
227         if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE))) {
228                 free(name);
229                 return -BLKID_ERR_MEM;
230         }
231
232         free(name);
233         return 1;
234 }
235
236 /*
237  * Extract a tag of the form NAME="value" from the line.
238  */
239 static int parse_token(char **name, char **value, char **cp)
240 {
241         char *end;
242
243         if (!name || !value || !cp)
244                 return -BLKID_ERR_PARAM;
245
246         if (!(*value = strchr(*cp, '=')))
247                 return 0;
248
249         **value = '\0';
250         *name = strip_line(*cp);
251         *value = skip_over_blank(*value + 1);
252
253         if (**value == '"') {
254                 end = strchr(*value + 1, '"');
255                 if (!end) {
256                         DBG(DEBUG_READ,
257                             printf("unbalanced quotes at: %s\n", *value));
258                         *cp = *value;
259                         return -BLKID_ERR_CACHE;
260                 }
261                 (*value)++;
262                 *end = '\0';
263                 end++;
264         } else {
265                 end = skip_over_word(*value);
266                 if (*end) {
267                         *end = '\0';
268                         end++;
269                 }
270         }
271         *cp = end;
272
273         return 1;
274 }
275
276 /*
277  * Extract a tag of the form <NAME>value</NAME> from the line.
278  */
279 /*
280 static int parse_xml(char **name, char **value, char **cp)
281 {
282         char *end;
283
284         if (!name || !value || !cp)
285                 return -BLKID_ERR_PARAM;
286
287         *name = strip_line(*cp);
288
289         if ((*name)[0] != '<' || (*name)[1] == '/')
290                 return 0;
291
292         FIXME: finish this.
293 }
294 */
295
296 /*
297  * Extract a tag from the line.
298  *
299  * Return 1 if a valid tag was found.
300  * Return 0 if no tag found.
301  * Return -ve error code.
302  */
303 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
304 {
305         char *name;
306         char *value;
307         int ret;
308
309         if (!cache || !dev)
310                 return -BLKID_ERR_PARAM;
311
312         if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
313             (ret = parse_xml(&name, &value, cp)) <= 0 */)
314                 return ret;
315
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                 /* FIXME: need to parse a long long eventually */
323                 dev->bid_time = strtol(value, 0, 0);
324         else
325                 ret = blkid_set_tag(dev, name, value, strlen(value));
326
327         DBG(DEBUG_READ, printf("    tag: %s=\"%s\"\n", name, value));
328
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  */
343 static int blkid_parse_line(blkid_cache cache, blkid_dev *dev_p, char *cp)
344 {
345         blkid_dev dev;
346         int ret;
347
348         if (!cache || !dev_p)
349                 return -BLKID_ERR_PARAM;
350
351         *dev_p = NULL;
352
353         DBG(DEBUG_READ, printf("line: %s\n", cp));
354
355         if ((ret = parse_dev(cache, dev_p, &cp)) <= 0)
356                 return ret;
357
358         dev = *dev_p;
359
360         while ((ret = parse_tag(cache, dev, &cp)) > 0) {
361                 ;
362         }
363
364         if (dev->bid_type == NULL) {
365                 DBG(DEBUG_READ,
366                     printf("blkid: device %s has no TYPE\n",dev->bid_name));
367                 blkid_free_dev(dev);
368         }
369
370         DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
371
372         return ret;
373 }
374
375 /*
376  * 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.
379  */
380 void blkid_read_cache(blkid_cache cache)
381 {
382         FILE *file;
383         char buf[4096];
384         int fd, lineno = 0;
385         struct stat st;
386
387         if (!cache)
388                 return;
389
390         /*
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)
395                 return;
396         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;
403         }
404         
405         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;
411
412         while (fgets(buf, sizeof(buf), file)) {
413                 blkid_dev dev;
414                 unsigned int end;
415
416                 lineno++;
417                 if (buf[0] == 0)
418                         continue;
419                 end = strlen(buf) - 1;
420                 /* Continue reading next line if it ends with a backslash */
421                 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
422                        fgets(buf + end, sizeof(buf) - end, file)) {
423                         end = strlen(buf) - 1;
424                         lineno++;
425                 }
426
427                 if (blkid_parse_line(cache, &dev, buf) < 0) {
428                         DBG(DEBUG_READ,
429                             printf("blkid: bad format on line %d\n", lineno));
430                         continue;
431                 }
432         }
433         fclose(file);
434
435         /*
436          * Initially we do not need to write out the cache file.
437          */
438         cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
439         cache->bic_ftime = st.st_mtime;
440
441         return;
442 errout:
443         close(fd);
444         return;
445 }
446
447 #ifdef TEST_PROGRAM
448 static 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);
458         printf("  dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
459         printf("  dev: TIME=\"%lld\"\n", (long long)dev->bid_time);
460         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)
466                         printf("    tag: %s=\"%s\"\n", tag->bit_name, 
467                                tag->bit_val);
468                 else
469                         printf("    tag: NULL\n");
470         }
471         printf("\n");
472 }
473
474 int main(int argc, char**argv)
475 {
476         blkid_cache cache = NULL;
477         int ret;
478
479         blkid_debug_mask = DEBUG_ALL;
480         if (argc > 2) {
481                 fprintf(stderr, "Usage: %s [filename]\n"
482                         "Test parsing of the cache (filename)\n", argv[0]);
483                 exit(1);
484         }
485         if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
486                 fprintf(stderr, "error %d reading cache file %s\n", ret,
487                         argv[1] ? argv[1] : BLKID_CACHE_FILE);
488
489         blkid_put_cache(cache);
490
491         return ret;
492 }
493 #endif