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.
25 static blkid_tag blkid_new_tag(void)
29 if (!(tag = (blkid_tag) calloc(1, sizeof(struct blkid_struct_tag))))
32 INIT_LIST_HEAD(&tag->bit_tags);
33 INIT_LIST_HEAD(&tag->bit_names);
38 void blkid_free_tag(blkid_tag tag)
43 DBG(printf(" freeing tag %s=%s\n", tag->bit_name,
44 tag->bit_val ? tag->bit_val : "(NULL)"));
47 list_del(&tag->bit_tags); /* list of tags for this device */
48 list_del(&tag->bit_names); /* list of tags with this type */
59 * Find the desired tag on a device. If value is NULL, then the
60 * first such tag is returned, otherwise return only exact tag if found.
62 blkid_tag blkid_find_tag_dev(blkid_dev dev, const char *type)
69 list_for_each(p, &dev->bid_tags) {
70 blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
73 if (!strcmp(tmp->bit_name, type))
80 * Find the desired tag type in the cache.
81 * We return the head tag for this tag type.
83 static blkid_tag blkid_find_head_cache(blkid_cache cache, const char *type)
85 blkid_tag head = NULL, tmp;
91 list_for_each(p, &cache->bic_tags) {
92 tmp = list_entry(p, struct blkid_struct_tag, bit_tags);
93 if (!strcmp(tmp->bit_name, type)) {
94 DBG(printf(" found cache tag head %s\n", type));
103 * Set a tag on an existing device.
105 * If replace is non-zero, blkid_set_tag() will replace the existing
106 * tag with the specified value. Otherwise, it will add the specified
109 * If value is NULL, then delete all tags with that name from the
112 int blkid_set_tag(blkid_dev dev, const char *name,
113 const char *value, const int vlength, int replace)
115 blkid_tag t = 0, head = 0;
119 return -BLKID_ERR_PARAM;
122 t = blkid_find_tag_dev(dev, name);
123 val = blkid_strndup(value, vlength);
134 if (!strcmp(t->bit_val, val)) {
135 /* Same thing, exit */
144 dev->bid_flags |= BLKID_BID_FL_MTYPE;
147 /* Existing tag not present, add to device */
151 t->bit_name = blkid_strdup(name);
155 list_add_tail(&t->bit_tags, &dev->bid_tags);
157 if (dev->bid_cache) {
158 head = blkid_find_head_cache(dev->bid_cache, t->bit_name);
160 head = blkid_new_tag();
164 DBG(printf(" creating new cache tag head %s\n",
166 head->bit_name = blkid_strdup(name);
169 list_add_tail(&head->bit_tags,
170 &dev->bid_cache->bic_tags);
172 list_add_tail(&t->bit_names, &head->bit_names);
176 /* Link common tags directly to the device struct */
177 if (!strcmp(name, "TYPE") && (!val || !dev->bid_type))
179 else if (!strcmp(name, "LABEL"))
180 dev->bid_label = val;
181 else if (!strcmp(name, "UUID"))
185 dev->bid_cache->bic_flags |= BLKID_BIC_FL_CHANGED;
194 blkid_free_tag(head);
195 return -BLKID_ERR_MEM;
200 * Parse a "NAME=value" string. This is slightly different than
201 * parse_token, because that will end an unquoted value at a space, while
202 * this will assume that an unquoted value is the rest of the token (e.g.
203 * if we are passed an already quoted string from the command-line we don't
204 * have to both quote and escape quote so that the quotes make it to
207 * Returns 0 on success, and -1 on failure.
209 int blkid_parse_tag_string(const char *token, char **ret_type, char **ret_val)
211 char *name, *value, *cp;
213 DBG(printf("trying to parse '%s' as a tag\n", token));
215 if (!token || !(cp = strchr(token, '=')))
218 name = blkid_strdup(token);
221 value = name + (cp - token);
223 if (*value == '"' || *value == '\'') {
225 if (!(cp = strrchr(value, c)))
226 goto errout; /* missing closing quote */
229 value = blkid_strdup(value);
244 * Tag iteration routines for the public libblkid interface.
246 * These routines do not expose the list.h implementation, which are a
247 * contamination of the namespace, and which force us to reveal far, far
248 * too much of our internal implemenation. I'm not convinced I want
249 * to keep list.h in the long term, anyway. It's fine for kernel
250 * programming, but performance is not the #1 priority for this
251 * library, and I really don't like the tradeoff of type-safety for
252 * performance for this application. [tytso:20030125.2007EST]
256 * This series of functions iterate over all tags in a device
258 #define TAG_ITERATE_MAGIC 0x01a5284c
260 struct blkid_struct_tag_iterate {
266 extern blkid_tag_iterate blkid_tag_iterate_begin(blkid_dev dev)
268 blkid_tag_iterate iter;
270 iter = malloc(sizeof(struct blkid_struct_tag_iterate));
272 iter->magic = TAG_ITERATE_MAGIC;
274 iter->p = dev->bid_tags.next;
280 * Return 0 on success, -1 on error
282 extern int blkid_tag_next(blkid_tag_iterate iter,
283 const char **type, const char **value)
289 if (!iter || iter->magic != TAG_ITERATE_MAGIC ||
290 iter->p == &iter->dev->bid_tags)
292 tag = list_entry(iter->p, struct blkid_struct_tag, bit_tags);
293 *type = tag->bit_name;
294 *value = tag->bit_val;
295 iter->p = iter->p->next;
299 extern void blkid_tag_iterate_end(blkid_tag_iterate iter)
301 if (!iter || iter->magic != TAG_ITERATE_MAGIC)
308 * This function returns a device which matches a particular
309 * type/value pair. If there is more than one device that matches the
310 * search specification, it returns the one with the highest priority
311 * value. This allows us to give preference to EVMS or LVM devices.
313 * XXX there should also be an interface which uses an iterator so we
314 * can get all of the devices which match a type/value search parameter.
316 extern blkid_dev blkid_find_dev_with_tag(blkid_cache cache,
320 blkid_tag head, found;
325 if (!cache || !type || !value)
328 DBG(printf("looking for %s=%s in cache\n", type, value));
333 head = blkid_find_head_cache(cache, type);
336 list_for_each(p, &head->bit_names) {
337 blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
340 if (!strcmp(tmp->bit_val, value) &&
341 tmp->bit_dev->bid_pri > pri) {
343 dev = found->bit_dev;
348 dev = blkid_verify_devname(cache, dev);
349 if (dev && strcmp(found->bit_val, value))
352 if ((!head || !dev) && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
353 blkid_probe_all(cache);