Whamcloud - gitweb
Revise DIOCGDINFO (sys/disklabel.h) related FreeBSD comment.
[tools/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", end - start, start));
201
202         if (**cp == '>')
203                 *cp = end;
204         else
205                 (*cp)++;
206
207         *tmp = '\0';
208
209         if (!(tmp = strrchr(end, '<')) || parse_end(&tmp) < 0) {
210                 DBG(DEBUG_READ,
211                     printf("blkid: missing </device> ending: %s\n", end));
212         } else if (tmp)
213                 *tmp = '\0';
214
215         if (end - start <= 1) {
216                 DBG(DEBUG_READ, printf("blkid: empty device name: %s\n", *cp));
217                 return -BLKID_ERR_CACHE;
218         }
219
220         name = blkid_strndup(start, end-start);
221         if (name == NULL)
222                 return -BLKID_ERR_MEM;
223
224         DBG(DEBUG_READ, printf("found dev %s\n", name));
225
226         if (!(*dev = blkid_get_dev(cache, name, BLKID_DEV_CREATE)))
227                 return -BLKID_ERR_MEM;
228
229         free(name);
230         return 1;
231 }
232
233 /*
234  * Extract a tag of the form NAME="value" from the line.
235  */
236 static int parse_token(char **name, char **value, char **cp)
237 {
238         char *end;
239
240         if (!name || !value || !cp)
241                 return -BLKID_ERR_PARAM;
242
243         if (!(*value = strchr(*cp, '=')))
244                 return 0;
245
246         **value = '\0';
247         *name = strip_line(*cp);
248         *value = skip_over_blank(*value + 1);
249
250         if (**value == '"') {
251                 end = strchr(*value + 1, '"');
252                 if (!end) {
253                         DBG(DEBUG_READ,
254                             printf("unbalanced quotes at: %s\n", *value));
255                         *cp = *value;
256                         return -BLKID_ERR_CACHE;
257                 }
258                 (*value)++;
259                 *end = '\0';
260                 end++;
261         } else {
262                 end = skip_over_word(*value);
263                 if (*end) {
264                         *end = '\0';
265                         end++;
266                 }
267         }
268         *cp = end;
269
270         return 1;
271 }
272
273 /*
274  * Extract a tag of the form <NAME>value</NAME> from the line.
275  */
276 /*
277 static int parse_xml(char **name, char **value, char **cp)
278 {
279         char *end;
280
281         if (!name || !value || !cp)
282                 return -BLKID_ERR_PARAM;
283
284         *name = strip_line(*cp);
285
286         if ((*name)[0] != '<' || (*name)[1] == '/')
287                 return 0;
288
289         FIXME: finish this.
290 }
291 */
292
293 /*
294  * Extract a tag from the line.
295  *
296  * Return 1 if a valid tag was found.
297  * Return 0 if no tag found.
298  * Return -ve error code.
299  */
300 static int parse_tag(blkid_cache cache, blkid_dev dev, char **cp)
301 {
302         char *name;
303         char *value;
304         int ret;
305
306         if (!cache || !dev)
307                 return -BLKID_ERR_PARAM;
308
309         if ((ret = parse_token(&name, &value, cp)) <= 0 /* &&
310             (ret = parse_xml(&name, &value, cp)) <= 0 */)
311                 return ret;
312
313         /* Some tags are stored directly in the device struct */
314         if (!strcmp(name, "DEVNO")) 
315                 dev->bid_devno = STRTOULL(value, 0, 0);
316         else if (!strcmp(name, "PRI"))
317                 dev->bid_pri = strtol(value, 0, 0);
318         else if (!strcmp(name, "TIME"))
319                 /* FIXME: need to parse a long long eventually */
320                 dev->bid_time = strtol(value, 0, 0);
321         else
322                 ret = blkid_set_tag(dev, name, value, strlen(value));
323
324         DBG(DEBUG_READ, printf("    tag: %s=\"%s\"\n", name, 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(DEBUG_READ, printf("line: %s\n", 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(DEBUG_READ,
363                     printf("blkid: device %s has no TYPE\n",dev->bid_name));
364                 blkid_free_dev(dev);
365         }
366
367         DBG(DEBUG_READ, blkid_debug_dump_dev(dev));
368
369         return ret;
370 }
371
372 /*
373  * Parse the specified filename, and return the data in the supplied or
374  * a newly allocated cache struct.  If the file doesn't exist, return a
375  * new empty cache struct.
376  */
377 void blkid_read_cache(blkid_cache cache)
378 {
379         FILE *file;
380         char buf[4096];
381         int fd, lineno = 0;
382         struct stat st;
383
384         if (!cache)
385                 return;
386
387         /*
388          * If the file doesn't exist, then we just return an empty
389          * struct so that the cache can be populated.
390          */
391         if ((fd = open(cache->bic_filename, O_RDONLY)) < 0)
392                 return;
393         if (fstat(fd, &st) < 0)
394                 goto errout;
395         if ((st.st_mtime == cache->bic_ftime) ||
396             (cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
397                 DBG(DEBUG_CACHE, printf("skipping re-read of %s\n",
398                                         cache->bic_filename));
399                 goto errout;
400         }
401         
402         DBG(DEBUG_CACHE, printf("reading cache file %s\n",
403                                 cache->bic_filename));
404
405         file = fdopen(fd, "r");
406         if (!file)
407                 goto errout;
408
409         while (fgets(buf, sizeof(buf), file)) {
410                 blkid_dev dev;
411                 unsigned int end;
412
413                 lineno++;
414                 if (buf[0] == 0)
415                         continue;
416                 end = strlen(buf) - 1;
417                 /* Continue reading next line if it ends with a backslash */
418                 while (buf[end] == '\\' && end < sizeof(buf) - 2 &&
419                        fgets(buf + end, sizeof(buf) - end, file)) {
420                         end = strlen(buf) - 1;
421                         lineno++;
422                 }
423
424                 if (blkid_parse_line(cache, &dev, buf) < 0) {
425                         DBG(DEBUG_READ,
426                             printf("blkid: bad format on line %d\n", lineno));
427                         continue;
428                 }
429         }
430         fclose(file);
431
432         /*
433          * Initially we do not need to write out the cache file.
434          */
435         cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
436         cache->bic_ftime = st.st_mtime;
437
438         return;
439 errout:
440         close(fd);
441         return;
442 }
443
444 #ifdef TEST_PROGRAM
445 static void debug_dump_dev(blkid_dev dev)
446 {
447         struct list_head *p;
448
449         if (!dev) {
450                 printf("  dev: NULL\n");
451                 return;
452         }
453
454         printf("  dev: name = %s\n", dev->bid_name);
455         printf("  dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
456         printf("  dev: TIME=\"%lld\"\n", (long long)dev->bid_time);
457         printf("  dev: PRI=\"%d\"\n", dev->bid_pri);
458         printf("  dev: flags = 0x%08X\n", dev->bid_flags);
459
460         list_for_each(p, &dev->bid_tags) {
461                 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
462                 if (tag)
463                         printf("    tag: %s=\"%s\"\n", tag->bit_name, 
464                                tag->bit_val);
465                 else
466                         printf("    tag: NULL\n");
467         }
468         printf("\n");
469 }
470
471 int main(int argc, char**argv)
472 {
473         blkid_cache cache = NULL;
474         int ret;
475
476         blkid_debug_mask = DEBUG_ALL;
477         if (argc > 2) {
478                 fprintf(stderr, "Usage: %s [filename]\n"
479                         "Test parsing of the cache (filename)\n", argv[0]);
480                 exit(1);
481         }
482         if ((ret = blkid_get_cache(&cache, argv[1])) < 0)
483                 fprintf(stderr, "error %d reading cache file %s\n", ret,
484                         argv[1] ? argv[1] : BLKID_CACHE_FILE);
485
486         blkid_put_cache(cache);
487
488         return ret;
489 }
490 #endif