]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/read.c
2 * read.c - read the blkid cache from disk, to avoid scanning all devices
4 * Copyright (C) 2001, 2003 Theodore Y. Ts'o
5 * Copyright (C) 2001 Andreas Dilger
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
18 #include <sys/types.h>
29 # ifndef _XOPEN_SOURCE
30 # define _XOPEN_SOURCE 600 /* for inclusion of strtoull */
38 * <device [<NAME="value"> ...]>device_name</device>
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
44 * <TYPE="type"> (detected) type of filesystem/data for this partition
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)
51 static char *skip_over_blank(char *cp
)
53 while (*cp
&& isspace(*cp
))
58 static char *skip_over_word(char *cp
)
63 /* If we see a backslash, skip the next character */
71 if (isspace(ch
) || ch
== '<' || ch
== '>')
78 static char *strip_line(char *line
)
82 line
= skip_over_blank(line
);
84 p
= line
+ strlen(line
) - 1;
97 static char *parse_word(char **buf
)
105 word
= skip_over_blank(word
);
106 next
= skip_over_word(word
);
108 char *end
= next
- 1;
109 if (*end
== '"' || *end
== '\'')
115 if (*word
== '"' || *word
== '\'')
122 * Start parsing a new line from the cache.
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
128 static int parse_start(char **cp
)
134 /* Skip comment or blank lines. We can't just NUL the first '#' char,
135 * in case it is inside quotes, or escaped.
137 if (*p
== '\0' || *p
== '#')
140 if (!strncmp(p
, "<device", 7)) {
141 DBG(READ
, ul_debug("found device header: %8s", p
));
151 return -BLKID_ERR_CACHE
;
154 /* Consume the remaining XML on the line (cosmetic only) */
155 static int parse_end(char **cp
)
157 *cp
= skip_over_blank(*cp
);
159 if (!strncmp(*cp
, "</device>", 9)) {
160 DBG(READ
, ul_debug("found device trailer %9s", *cp
));
165 return -BLKID_ERR_CACHE
;
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>
174 static int parse_dev(blkid_cache cache
, blkid_dev
*dev
, char **cp
)
176 char *start
, *tmp
, *end
, *name
;
179 if ((ret
= parse_start(cp
)) <= 0)
182 start
= tmp
= strchr(*cp
, '>');
184 DBG(READ
, ul_debug("blkid: short line parsing dev: %s", *cp
));
185 return -BLKID_ERR_CACHE
;
187 start
= skip_over_blank(start
+ 1);
188 end
= skip_over_word(start
);
190 DBG(READ
, ul_debug("device should be %*s",
191 (int)(end
- start
), start
));
200 if (!(tmp
= strrchr(end
, '<')) || parse_end(&tmp
) < 0) {
201 DBG(READ
, ul_debug("blkid: missing </device> ending: %s", end
));
205 if (end
- start
<= 1) {
206 DBG(READ
, ul_debug("blkid: empty device name: %s", *cp
));
207 return -BLKID_ERR_CACHE
;
210 name
= strndup(start
, end
- start
);
212 return -BLKID_ERR_MEM
;
214 DBG(READ
, ul_debug("found dev %s", name
));
216 if (!(*dev
= blkid_get_dev(cache
, name
, BLKID_DEV_CREATE
))) {
218 return -BLKID_ERR_MEM
;
226 * Extract a tag of the form NAME="value" from the line.
228 static int parse_token(char **name
, char **value
, char **cp
)
232 if (!name
|| !value
|| !cp
)
233 return -BLKID_ERR_PARAM
;
235 if (!(*value
= strchr(*cp
, '=')))
239 *name
= strip_line(*cp
);
240 *value
= skip_over_blank(*value
+ 1);
242 if (**value
== '"') {
243 char *p
= end
= *value
+ 1;
245 /* convert 'foo\"bar' to 'foo"bar' */
260 DBG(READ
, ul_debug("unbalanced quotes at: %s", *value
));
262 return -BLKID_ERR_CACHE
;
268 end
= skip_over_word(*value
);
280 * Extract a tag from the line.
282 * Return 1 if a valid tag was found.
283 * Return 0 if no tag found.
284 * Return -ve error code.
286 static int parse_tag(blkid_cache cache
, blkid_dev dev
, char **cp
)
293 return -BLKID_ERR_PARAM
;
295 if ((ret
= parse_token(&name
, &value
, cp
)) <= 0)
298 DBG(READ
, ul_debug("tag: %s=\"%s\"", name
, value
));
302 /* Some tags are stored directly in the device struct */
303 if (!strcmp(name
, "DEVNO")) {
304 dev
->bid_devno
= strtoull(value
, NULL
, 0);
307 } else if (!strcmp(name
, "PRI")) {
308 dev
->bid_pri
= strtol(value
, NULL
, 0);
311 } else if (!strcmp(name
, "TIME")) {
314 dev
->bid_time
= strtoull(value
, &end
, 0);
315 if (errno
== 0 && end
&& *end
== '.')
316 dev
->bid_utime
= strtoull(end
+ 1, NULL
, 0);
320 ret
= blkid_set_tag(dev
, name
, value
, strlen(value
));
322 return ret
< 0 ? ret
: 1;
326 * Parse a single line of data, and return a newly allocated dev struct.
327 * Add the new device to the cache struct, if one was read.
329 * Lines are of the form <device [TAG="value" ...]>/dev/foo</device>
331 * Returns -ve value on error.
332 * Returns 0 otherwise.
333 * If a valid device was read, *dev_p is non-NULL, otherwise it is NULL
334 * (e.g. comment lines, unknown XML content, etc).
336 static int blkid_parse_line(blkid_cache cache
, blkid_dev
*dev_p
, char *cp
)
341 if (!cache
|| !dev_p
)
342 return -BLKID_ERR_PARAM
;
346 DBG(READ
, ul_debug("line: %s", cp
));
348 if ((ret
= parse_dev(cache
, dev_p
, &cp
)) <= 0)
353 while ((ret
= parse_tag(cache
, dev
, &cp
)) > 0) {
357 if (dev
->bid_type
== NULL
) {
358 DBG(READ
, ul_debug("blkid: device %s has no TYPE",dev
->bid_name
));
368 * Parse the specified filename, and return the data in the supplied or
369 * a newly allocated cache struct. If the file doesn't exist, return a
370 * new empty cache struct.
372 void blkid_read_cache(blkid_cache cache
)
380 * If the file doesn't exist, then we just return an empty
381 * struct so that the cache can be populated.
383 if ((fd
= open(cache
->bic_filename
, O_RDONLY
|O_CLOEXEC
)) < 0)
385 if (fstat(fd
, &st
) < 0)
387 if ((st
.st_mtime
== cache
->bic_ftime
) ||
388 (cache
->bic_flags
& BLKID_BIC_FL_CHANGED
)) {
389 DBG(CACHE
, ul_debug("skipping re-read of %s",
390 cache
->bic_filename
));
394 DBG(CACHE
, ul_debug("reading cache file %s",
395 cache
->bic_filename
));
397 file
= fdopen(fd
, "r" UL_CLOEXECSTR
);
401 while (fgets(buf
, sizeof(buf
), file
)) {
408 end
= strlen(buf
) - 1;
409 /* Continue reading next line if it ends with a backslash */
410 while (end
< (sizeof(buf
) - 2) && buf
[end
] == '\\' &&
411 fgets(buf
+ end
, sizeof(buf
) - end
, file
)) {
412 end
= strlen(buf
) - 1;
416 if (blkid_parse_line(cache
, &dev
, buf
) < 0) {
417 DBG(READ
, ul_debug("blkid: bad format on line %d", lineno
));
424 * Initially we do not need to write out the cache file.
426 cache
->bic_flags
&= ~BLKID_BIC_FL_CHANGED
;
427 cache
->bic_ftime
= st
.st_mtime
;
436 int main(int argc
, char**argv
)
438 blkid_cache cache
= NULL
;
441 blkid_init_debug(BLKID_DEBUG_ALL
);
443 fprintf(stderr
, "Usage: %s [filename]\n"
444 "Test parsing of the cache (filename)\n", argv
[0]);
447 if ((ret
= blkid_get_cache(&cache
, argv
[1])) < 0)
448 fprintf(stderr
, "error %d reading cache file %s\n", ret
,
449 argv
[1] ? argv
[1] : blkid_get_cache_filename(NULL
));
451 blkid_put_cache(cache
);