2 * tag.c - allocation/initialization/free routines for tag structs
4 * Copyright (C) 2001 Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
19 static blkid_tag blkid_new_tag(void)
23 if (!(tag = (blkid_tag) calloc(1, sizeof(struct blkid_struct_tag))))
26 INIT_LIST_HEAD(&tag->bit_tags);
27 INIT_LIST_HEAD(&tag->bit_names);
32 #ifdef CONFIG_BLKID_DEBUG
33 void blkid_debug_dump_tag(blkid_tag tag)
36 printf(" tag: NULL\n");
40 printf(" tag: %s=\"%s\"\n", tag->bit_name, tag->bit_val);
44 void blkid_free_tag(blkid_tag tag)
49 DBG(DEBUG_TAG, printf(" freeing tag %s=%s\n", tag->bit_name,
50 tag->bit_val ? tag->bit_val : "(NULL)"));
51 DBG(DEBUG_TAG, blkid_debug_dump_tag(tag));
53 list_del(&tag->bit_tags); /* list of tags for this device */
54 list_del(&tag->bit_names); /* list of tags with this type */
65 * Find the desired tag on a device. If value is NULL, then the
66 * first such tag is returned, otherwise return only exact tag if found.
68 blkid_tag blkid_find_tag_dev(blkid_dev dev, const char *type)
75 list_for_each(p, &dev->bid_tags) {
76 blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
79 if (!strcmp(tmp->bit_name, type))
85 extern int blkid_dev_has_tag(blkid_dev dev, const char *type,
90 if (!dev || !type || !value)
93 tag = blkid_find_tag_dev(dev, type);
96 if (!tag || strcmp(tag->bit_val, value))
102 * Find the desired tag type in the cache.
103 * We return the head tag for this tag type.
105 static blkid_tag blkid_find_head_cache(blkid_cache cache, const char *type)
107 blkid_tag head = NULL, tmp;
113 list_for_each(p, &cache->bic_tags) {
114 tmp = list_entry(p, struct blkid_struct_tag, bit_tags);
115 if (!strcmp(tmp->bit_name, type)) {
117 printf(" found cache tag head %s\n", type));
126 * Set a tag on an existing device.
128 * If value is NULL, then delete the tagsfrom the device.
130 int blkid_set_tag(blkid_dev dev, const char *name,
131 const char *value, const int vlength)
133 blkid_tag t = 0, head = 0;
137 return -BLKID_ERR_PARAM;
139 if (!(val = blkid_strndup(value, vlength)) && value)
140 return -BLKID_ERR_MEM;
141 t = blkid_find_tag_dev(dev, name);
146 if (!strcmp(t->bit_val, val)) {
147 /* Same thing, exit */
154 /* Existing tag not present, add to device */
155 if (!(t = blkid_new_tag()))
157 t->bit_name = blkid_strdup(name);
161 list_add_tail(&t->bit_tags, &dev->bid_tags);
163 if (dev->bid_cache) {
164 head = blkid_find_head_cache(dev->bid_cache,
167 head = blkid_new_tag();
172 printf(" creating new cache tag head %s\n", name));
173 head->bit_name = blkid_strdup(name);
176 list_add_tail(&head->bit_tags,
177 &dev->bid_cache->bic_tags);
179 list_add_tail(&t->bit_names, &head->bit_names);
183 /* Link common tags directly to the device struct */
184 if (!strcmp(name, "TYPE"))
186 else if (!strcmp(name, "LABEL"))
187 dev->bid_label = val;
188 else if (!strcmp(name, "UUID"))
192 dev->bid_cache->bic_flags |= BLKID_BIC_FL_CHANGED;
201 blkid_free_tag(head);
202 return -BLKID_ERR_MEM;
207 * Parse a "NAME=value" string. This is slightly different than
208 * parse_token, because that will end an unquoted value at a space, while
209 * this will assume that an unquoted value is the rest of the token (e.g.
210 * if we are passed an already quoted string from the command-line we don't
211 * have to both quote and escape quote so that the quotes make it to
214 * Returns 0 on success, and -1 on failure.
216 int blkid_parse_tag_string(const char *token, char **ret_type, char **ret_val)
218 char *name, *value, *cp;
220 DBG(DEBUG_TAG, printf("trying to parse '%s' as a tag\n", token));
222 if (!token || !(cp = strchr(token, '=')))
225 name = blkid_strdup(token);
228 value = name + (cp - token);
230 if (*value == '"' || *value == '\'') {
232 if (!(cp = strrchr(value, c)))
233 goto errout; /* missing closing quote */
236 value = blkid_strdup(value);
251 * Tag iteration routines for the public libblkid interface.
253 * These routines do not expose the list.h implementation, which are a
254 * contamination of the namespace, and which force us to reveal far, far
255 * too much of our internal implemenation. I'm not convinced I want
256 * to keep list.h in the long term, anyway. It's fine for kernel
257 * programming, but performance is not the #1 priority for this
258 * library, and I really don't like the tradeoff of type-safety for
259 * performance for this application. [tytso:20030125.2007EST]
263 * This series of functions iterate over all tags in a device
265 #define TAG_ITERATE_MAGIC 0x01a5284c
267 struct blkid_struct_tag_iterate {
273 extern blkid_tag_iterate blkid_tag_iterate_begin(blkid_dev dev)
275 blkid_tag_iterate iter;
277 iter = malloc(sizeof(struct blkid_struct_tag_iterate));
279 iter->magic = TAG_ITERATE_MAGIC;
281 iter->p = dev->bid_tags.next;
287 * Return 0 on success, -1 on error
289 extern int blkid_tag_next(blkid_tag_iterate iter,
290 const char **type, const char **value)
296 if (!iter || iter->magic != TAG_ITERATE_MAGIC ||
297 iter->p == &iter->dev->bid_tags)
299 tag = list_entry(iter->p, struct blkid_struct_tag, bit_tags);
300 *type = tag->bit_name;
301 *value = tag->bit_val;
302 iter->p = iter->p->next;
306 extern void blkid_tag_iterate_end(blkid_tag_iterate iter)
308 if (!iter || iter->magic != TAG_ITERATE_MAGIC)
315 * This function returns a device which matches a particular
316 * type/value pair. If there is more than one device that matches the
317 * search specification, it returns the one with the highest priority
318 * value. This allows us to give preference to EVMS or LVM devices.
320 * XXX there should also be an interface which uses an iterator so we
321 * can get all of the devices which match a type/value search parameter.
323 extern blkid_dev blkid_find_dev_with_tag(blkid_cache cache,
332 if (!cache || !type || !value)
335 blkid_read_cache(cache);
337 DBG(DEBUG_TAG, printf("looking for %s=%s in cache\n", type, value));
342 head = blkid_find_head_cache(cache, type);
345 list_for_each(p, &head->bit_names) {
346 blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
349 if (!strcmp(tmp->bit_val, value) &&
350 tmp->bit_dev->bid_pri > pri) {
356 if (dev && !(dev->bid_flags & BLKID_BID_FL_VERIFIED)) {
357 dev = blkid_verify(cache, dev);
358 if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
362 if (!dev && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
363 if (blkid_probe_all(cache) < 0)
378 void usage(char *prog)
380 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask] device "
383 fprintf(stderr, "\tList all tags for a device and exit\n", prog);
387 int main(int argc, char **argv)
389 blkid_tag_iterate iter;
390 blkid_cache cache = NULL;
393 int flags = BLKID_DEV_FIND;
396 char *devname = NULL;
397 char *search_type = NULL;
398 char *search_value = NULL;
399 const char *type, *value;
401 while ((c = getopt (argc, argv, "m:f:")) != EOF)
407 blkid_debug_mask = strtoul (optarg, &tmp, 0);
409 fprintf(stderr, "Invalid debug mask: %d\n",
418 devname = argv[optind++];
420 search_type = argv[optind++];
422 search_value = argv[optind++];
423 if (!devname || (argc != optind))
426 if ((ret = blkid_get_cache(&cache, file)) != 0) {
427 fprintf(stderr, "%s: error creating cache (%d)\n",
432 dev = blkid_get_dev(cache, devname, flags);
434 fprintf(stderr, "%s: Can not find device in blkid cache\n");
438 found = blkid_dev_has_tag(dev, search_type, search_value);
439 printf("Device %s: (%s, %s) %s\n", blkid_dev_devname(dev),
440 search_type, search_value ? search_value : "NULL",
441 found ? "FOUND" : "NOT FOUND");
444 printf("Device %s...\n", blkid_dev_devname(dev));
446 iter = blkid_tag_iterate_begin(dev);
447 while (blkid_tag_next(iter, &type, &value) == 0) {
448 printf("\tTag %s has value %s\n", type, value);
450 blkid_tag_iterate_end(iter);
452 blkid_put_cache(cache);