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.
21 static blkid_tag blkid_new_tag(void)
25 if (!(tag = (blkid_tag) calloc(1, sizeof(struct blkid_struct_tag))))
28 INIT_LIST_HEAD(&tag->bit_tags);
29 INIT_LIST_HEAD(&tag->bit_names);
34 #ifdef CONFIG_BLKID_DEBUG
35 void blkid_debug_dump_tag(blkid_tag tag)
38 printf(" tag: NULL\n");
42 printf(" tag: %s=\"%s\"\n", tag->bit_name, tag->bit_val);
46 void blkid_free_tag(blkid_tag tag)
51 DBG(DEBUG_TAG, printf(" freeing tag %s=%s\n", tag->bit_name,
52 tag->bit_val ? tag->bit_val : "(NULL)"));
53 DBG(DEBUG_TAG, blkid_debug_dump_tag(tag));
55 list_del(&tag->bit_tags); /* list of tags for this device */
56 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,
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;
138 return -BLKID_ERR_PARAM;
140 if (!(val = blkid_strndup(value, vlength)) && value)
141 return -BLKID_ERR_MEM;
144 * Certain common tags are linked directly to the device struct
145 * We need to know what they are before we do anything else because
146 * the function name parameter might get freed later on.
148 if (!strcmp(name, "TYPE"))
149 dev_var = &dev->bid_type;
150 else if (!strcmp(name, "LABEL"))
151 dev_var = &dev->bid_label;
152 else if (!strcmp(name, "UUID"))
153 dev_var = &dev->bid_uuid;
155 t = blkid_find_tag_dev(dev, name);
160 if (!strcmp(t->bit_val, val)) {
161 /* Same thing, exit */
168 /* Existing tag not present, add to device */
169 if (!(t = blkid_new_tag()))
171 t->bit_name = blkid_strdup(name);
175 list_add_tail(&t->bit_tags, &dev->bid_tags);
177 if (dev->bid_cache) {
178 head = blkid_find_head_cache(dev->bid_cache,
181 head = blkid_new_tag();
186 printf(" creating new cache tag head %s\n", name));
187 head->bit_name = blkid_strdup(name);
190 list_add_tail(&head->bit_tags,
191 &dev->bid_cache->bic_tags);
193 list_add_tail(&t->bit_names, &head->bit_names);
197 /* Link common tags directly to the device struct */
202 dev->bid_cache->bic_flags |= BLKID_BIC_FL_CHANGED;
210 blkid_free_tag(head);
211 return -BLKID_ERR_MEM;
216 * Parse a "NAME=value" string. This is slightly different than
217 * parse_token, because that will end an unquoted value at a space, while
218 * this will assume that an unquoted value is the rest of the token (e.g.
219 * if we are passed an already quoted string from the command-line we don't
220 * have to both quote and escape quote so that the quotes make it to
223 * Returns 0 on success, and -1 on failure.
225 int blkid_parse_tag_string(const char *token, char **ret_type, char **ret_val)
227 char *name, *value, *cp;
229 DBG(DEBUG_TAG, printf("trying to parse '%s' as a tag\n", token));
231 if (!token || !(cp = strchr(token, '=')))
234 name = blkid_strdup(token);
237 value = name + (cp - token);
239 if (*value == '"' || *value == '\'') {
241 if (!(cp = strrchr(value, c)))
242 goto errout; /* missing closing quote */
245 value = blkid_strdup(value);
260 * Tag iteration routines for the public libblkid interface.
262 * These routines do not expose the list.h implementation, which are a
263 * contamination of the namespace, and which force us to reveal far, far
264 * too much of our internal implemenation. I'm not convinced I want
265 * to keep list.h in the long term, anyway. It's fine for kernel
266 * programming, but performance is not the #1 priority for this
267 * library, and I really don't like the tradeoff of type-safety for
268 * performance for this application. [tytso:20030125.2007EST]
272 * This series of functions iterate over all tags in a device
274 #define TAG_ITERATE_MAGIC 0x01a5284c
276 struct blkid_struct_tag_iterate {
282 extern blkid_tag_iterate blkid_tag_iterate_begin(blkid_dev dev)
284 blkid_tag_iterate iter;
286 iter = malloc(sizeof(struct blkid_struct_tag_iterate));
288 iter->magic = TAG_ITERATE_MAGIC;
290 iter->p = dev->bid_tags.next;
296 * Return 0 on success, -1 on error
298 extern int blkid_tag_next(blkid_tag_iterate iter,
299 const char **type, const char **value)
305 if (!iter || iter->magic != TAG_ITERATE_MAGIC ||
306 iter->p == &iter->dev->bid_tags)
308 tag = list_entry(iter->p, struct blkid_struct_tag, bit_tags);
309 *type = tag->bit_name;
310 *value = tag->bit_val;
311 iter->p = iter->p->next;
315 extern void blkid_tag_iterate_end(blkid_tag_iterate iter)
317 if (!iter || iter->magic != TAG_ITERATE_MAGIC)
324 * This function returns a device which matches a particular
325 * type/value pair. If there is more than one device that matches the
326 * search specification, it returns the one with the highest priority
327 * value. This allows us to give preference to EVMS or LVM devices.
329 extern blkid_dev blkid_find_dev_with_tag(blkid_cache cache,
339 if (!cache || !type || !value)
342 blkid_read_cache(cache);
344 DBG(DEBUG_TAG, printf("looking for %s=%s in cache\n", type, value));
349 head = blkid_find_head_cache(cache, type);
352 list_for_each(p, &head->bit_names) {
353 blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
356 if (!strcmp(tmp->bit_val, value) &&
357 (tmp->bit_dev->bid_pri > pri) &&
358 !access(tmp->bit_dev->bid_name, F_OK)) {
364 if (dev && !(dev->bid_flags & BLKID_BID_FL_VERIFIED)) {
365 dev = blkid_verify(cache, dev);
366 if (!dev || (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED)))
370 if (!dev && !probe_new) {
371 if (blkid_probe_all_new(cache) < 0)
377 if (!dev && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
378 if (blkid_probe_all(cache) < 0)
393 void usage(char *prog)
395 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask] device "
398 fprintf(stderr, "\tList all tags for a device and exit\n");
402 int main(int argc, char **argv)
404 blkid_tag_iterate iter;
405 blkid_cache cache = NULL;
408 int flags = BLKID_DEV_FIND;
411 char *devname = NULL;
412 char *search_type = NULL;
413 char *search_value = NULL;
414 const char *type, *value;
416 while ((c = getopt (argc, argv, "m:f:")) != EOF)
422 blkid_debug_mask = strtoul (optarg, &tmp, 0);
424 fprintf(stderr, "Invalid debug mask: %s\n",
433 devname = argv[optind++];
435 search_type = argv[optind++];
437 search_value = argv[optind++];
438 if (!devname || (argc != optind))
441 if ((ret = blkid_get_cache(&cache, file)) != 0) {
442 fprintf(stderr, "%s: error creating cache (%d)\n",
447 dev = blkid_get_dev(cache, devname, flags);
449 fprintf(stderr, "%s: Can not find device in blkid cache\n",
454 found = blkid_dev_has_tag(dev, search_type, search_value);
455 printf("Device %s: (%s, %s) %s\n", blkid_dev_devname(dev),
456 search_type, search_value ? search_value : "NULL",
457 found ? "FOUND" : "NOT FOUND");
460 printf("Device %s...\n", blkid_dev_devname(dev));
462 iter = blkid_tag_iterate_begin(dev);
463 while (blkid_tag_next(iter, &type, &value) == 0) {
464 printf("\tTag %s has value %s\n", type, value);
466 blkid_tag_iterate_end(iter);
468 blkid_put_cache(cache);