Debian Codegraph @

libblkid/src/tag.c

raw
1 /*
2 * tag.c - allocation/initialization/free routines for tag structs
3 *
4 * Copyright (C) 2001 Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
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 <unistd.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17
18 #include "blkidP.h"
19
20 static blkid_tag blkid_new_tag(void)
21 {
22 blkid_tag tag;
23
24 if (!(tag = calloc(1, sizeof(struct blkid_struct_tag))))
25 return NULL;
26
27 DBG(TAG, ul_debugobj(tag, "alloc"));
28 INIT_LIST_HEAD(&tag->bit_tags);
29 INIT_LIST_HEAD(&tag->bit_names);
30
31 return tag;
32 }
33
34 void blkid_free_tag(blkid_tag tag)
35 {
36 if (!tag)
37 return;
38
39 DBG(TAG, ul_debugobj(tag, "freeing tag %s (%s)", tag->bit_name, tag->bit_val));
40
41 list_del(&tag->bit_tags); /* list of tags for this device */
42 list_del(&tag->bit_names); /* list of tags with this type */
43
44 free(tag->bit_name);
45 free(tag->bit_val);
46
47 free(tag);
48 }
49
50 /*
51 * Find the desired tag on a device. If value is NULL, then the
52 * first such tag is returned, otherwise return only exact tag if found.
53 */
54 blkid_tag blkid_find_tag_dev(blkid_dev dev, const char *type)
55 {
56 struct list_head *p;
57
58 list_for_each(p, &dev->bid_tags) {
59 blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
60 bit_tags);
61
62 if (!strcmp(tmp->bit_name, type))
63 return tmp;
64 }
65 return NULL;
66 }
67
68 int blkid_dev_has_tag(blkid_dev dev, const char *type,
69 const char *value)
70 {
71 blkid_tag tag;
72
73 tag = blkid_find_tag_dev(dev, type);
74 if (!value)
75 return (tag != NULL);
76 if (!tag || strcmp(tag->bit_val, value) != 0)
77 return 0;
78 return 1;
79 }
80
81 /*
82 * Find the desired tag type in the cache.
83 * We return the head tag for this tag type.
84 */
85 static blkid_tag blkid_find_head_cache(blkid_cache cache, const char *type)
86 {
87 blkid_tag head = NULL, tmp;
88 struct list_head *p;
89
90 if (!cache || !type)
91 return NULL;
92
93 list_for_each(p, &cache->bic_tags) {
94 tmp = list_entry(p, struct blkid_struct_tag, bit_tags);
95 if (!strcmp(tmp->bit_name, type)) {
96 DBG(TAG, ul_debug("found cache tag head %s", type));
97 head = tmp;
98 break;
99 }
100 }
101 return head;
102 }
103
104 /*
105 * Set a tag on an existing device.
106 *
107 * If value is NULL, then delete the tags from the device.
108 */
109 int blkid_set_tag(blkid_dev dev, const char *name,
110 const char *value, const int vlength)
111 {
112 blkid_tag t = NULL, head = NULL;
113 char *val = NULL;
114 char **dev_var = NULL;
115
116 if (value && !(val = strndup(value, vlength)))
117 return -BLKID_ERR_MEM;
118
119 /*
120 * Certain common tags are linked directly to the device struct
121 * We need to know what they are before we do anything else because
122 * the function name parameter might get freed later on.
123 */
124 if (!strcmp(name, "TYPE"))
125 dev_var = &dev->bid_type;
126 else if (!strcmp(name, "LABEL"))
127 dev_var = &dev->bid_label;
128 else if (!strcmp(name, "UUID"))
129 dev_var = &dev->bid_uuid;
130
131 t = blkid_find_tag_dev(dev, name);
132 if (!value) {
133 if (t)
134 blkid_free_tag(t);
135 } else if (t) {
136 if (!strcmp(t->bit_val, val)) {
137 /* Same thing, exit */
138 free(val);
139 return 0;
140 }
141 DBG(TAG, ul_debugobj(t, "update (%s) '%s' -> '%s'", t->bit_name, t->bit_val, val));
142 free(t->bit_val);
143 t->bit_val = val;
144 } else {
145 /* Existing tag not present, add to device */
146 if (!(t = blkid_new_tag()))
147 goto errout;
148 t->bit_name = strdup(name);
149 t->bit_val = val;
150 t->bit_dev = dev;
151
152 DBG(TAG, ul_debugobj(t, "setting (%s) '%s'", t->bit_name, t->bit_val));
153 list_add_tail(&t->bit_tags, &dev->bid_tags);
154
155 if (dev->bid_cache) {
156 head = blkid_find_head_cache(dev->bid_cache,
157 t->bit_name);
158 if (!head) {
159 head = blkid_new_tag();
160 if (!head)
161 goto errout;
162
163 DBG(TAG, ul_debugobj(head, "creating new cache tag head %s", name));
164 head->bit_name = strdup(name);
165 if (!head->bit_name)
166 goto errout;
167 list_add_tail(&head->bit_tags,
168 &dev->bid_cache->bic_tags);
169 }
170 list_add_tail(&t->bit_names, &head->bit_names);
171 }
172 }
173
174 /* Link common tags directly to the device struct */
175 if (dev_var)
176 *dev_var = val;
177
178 if (dev->bid_cache)
179 dev->bid_cache->bic_flags |= BLKID_BIC_FL_CHANGED;
180 return 0;
181
182 errout:
183 if (t)
184 blkid_free_tag(t);
185 else
186 free(val);
187 if (head)
188 blkid_free_tag(head);
189 return -BLKID_ERR_MEM;
190 }
191
192
193 /*
194 * Parse a "NAME=value" string. This is slightly different than
195 * parse_token, because that will end an unquoted value at a space, while
196 * this will assume that an unquoted value is the rest of the token (e.g.
197 * if we are passed an already quoted string from the command-line we don't
198 * have to both quote and escape quote so that the quotes make it to
199 * us).
200 *
201 * Returns 0 on success, and -1 on failure.
202 */
203 int blkid_parse_tag_string(const char *token, char **ret_type, char **ret_val)
204 {
205 char *name, *value;
206 const char *eq;
207
208 DBG(TAG, ul_debug("trying to parse '%s' as a tag", token));
209
210 if (!token || !(eq = strchr(token, '=')))
211 return -1;
212
213 name = strdup(token);
214 if (!name)
215 return -1;
216 value = name + (eq - token);
217 *value++ = '\0';
218 if (*value == '"' || *value == '\'') {
219 char c = *value++;
220 char *cp = strrchr(value, c);
221 if (!cp)
222 goto errout; /* missing closing quote */
223 *cp = '\0';
224 }
225
226 if (ret_val) {
227 value = *value ? strdup(value) : NULL;
228 if (!value)
229 goto errout;
230 *ret_val = value;
231 }
232
233 if (ret_type)
234 *ret_type = name;
235 else
236 free(name);
237
238 return 0;
239
240 errout:
241 DBG(TAG, ul_debug("parse error: '%s'", token));
242 free(name);
243 return -1;
244 }
245
246 /*
247 * Tag iteration routines for the public libblkid interface.
248 *
249 * These routines do not expose the list.h implementation, which are a
250 * contamination of the namespace, and which force us to reveal far, far
251 * too much of our internal implementation. I'm not convinced I want
252 * to keep list.h in the long term, anyway. It's fine for kernel
253 * programming, but performance is not the #1 priority for this
254 * library, and I really don't like the trade-off of type-safety for
255 * performance for this application. [tytso:20030125.2007EST]
256 */
257
258 /*
259 * This series of functions iterate over all tags in a device
260 */
261 #define TAG_ITERATE_MAGIC 0x01a5284c
262
263 struct blkid_struct_tag_iterate {
264 int magic;
265 blkid_dev dev;
266 struct list_head *p;
267 };
268
269 blkid_tag_iterate blkid_tag_iterate_begin(blkid_dev dev)
270 {
271 blkid_tag_iterate iter;
272
273 if (!dev) {
274 errno = EINVAL;
275 return NULL;
276 }
277
278 iter = malloc(sizeof(struct blkid_struct_tag_iterate));
279 if (iter) {
280 iter->magic = TAG_ITERATE_MAGIC;
281 iter->dev = dev;
282 iter->p = dev->bid_tags.next;
283 }
284 return (iter);
285 }
286
287 /*
288 * Return 0 on success, -1 on error
289 */
290 int blkid_tag_next(blkid_tag_iterate iter,
291 const char **type, const char **value)
292 {
293 blkid_tag tag;
294
295 if (!type || !value ||
296 !iter || iter->magic != TAG_ITERATE_MAGIC ||
297 iter->p == &iter->dev->bid_tags)
298 return -1;
299
300 *type = NULL;
301 *value = NULL;
302 tag = list_entry(iter->p, struct blkid_struct_tag, bit_tags);
303 *type = tag->bit_name;
304 *value = tag->bit_val;
305 iter->p = iter->p->next;
306 return 0;
307 }
308
309 void blkid_tag_iterate_end(blkid_tag_iterate iter)
310 {
311 if (!iter || iter->magic != TAG_ITERATE_MAGIC)
312 return;
313 iter->magic = 0;
314 free(iter);
315 }
316
317 /*
318 * This function returns a device which matches a particular
319 * type/value pair. If there is more than one device that matches the
320 * search specification, it returns the one with the highest priority
321 * value. This allows us to give preference to EVMS or LVM devices.
322 */
323 blkid_dev blkid_find_dev_with_tag(blkid_cache cache,
324 const char *type,
325 const char *value)
326 {
327 blkid_tag head;
328 blkid_dev dev;
329 int pri;
330 struct list_head *p;
331 int probe_new = 0, probe_all = 0;
332
333 if (!cache || !type || !value)
334 return NULL;
335
336 blkid_read_cache(cache);
337
338 DBG(TAG, ul_debug("looking for tag %s=%s in cache", type, value));
339
340 try_again:
341 pri = -1;
342 dev = NULL;
343 head = blkid_find_head_cache(cache, type);
344
345 if (head) {
346 list_for_each(p, &head->bit_names) {
347 blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
348 bit_names);
349
350 if (!strcmp(tmp->bit_val, value) &&
351 (tmp->bit_dev->bid_pri > pri) &&
352 !access(tmp->bit_dev->bid_name, F_OK)) {
353 dev = tmp->bit_dev;
354 pri = dev->bid_pri;
355 }
356 }
357 }
358 if (dev && !(dev->bid_flags & BLKID_BID_FL_VERIFIED)) {
359 dev = blkid_verify(cache, dev);
360 if (!dev || dev->bid_flags & BLKID_BID_FL_VERIFIED)
361 goto try_again;
362 }
363
364 if (!dev && !probe_new) {
365 if (blkid_probe_all_new(cache) < 0)
366 return NULL;
367 probe_new++;
368 goto try_again;
369 }
370
371 if (!dev && !probe_all
372 && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
373 if (blkid_probe_all(cache) < 0)
374 return NULL;
375 probe_all++;
376 goto try_again;
377 }
378 return dev;
379 }
380
381 #ifdef TEST_PROGRAM
382 #ifdef HAVE_GETOPT_H
383 #include <getopt.h>
384 #else
385 extern char *optarg;
386 extern int optind;
387 #endif
388
389 static void __attribute__((__noreturn__)) usage(char *prog)
390 {
391 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask] device "
392 "[type value]\n",
393 prog);
394 fprintf(stderr, "\tList all tags for a device and exit\n");
395 exit(1);
396 }
397
398 int main(int argc, char **argv)
399 {
400 blkid_tag_iterate iter;
401 blkid_cache cache = NULL;
402 blkid_dev dev;
403 int c, ret, found;
404 int flags = BLKID_DEV_FIND;
405 char *tmp;
406 char *file = NULL;
407 char *devname = NULL;
408 char *search_type = NULL;
409 char *search_value = NULL;
410 const char *type, *value;
411
412 while ((c = getopt (argc, argv, "m:f:")) != EOF)
413 switch (c) {
414 case 'f':
415 file = optarg;
416 break;
417 case 'm':
418 {
419 int mask = strtoul (optarg, &tmp, 0);
420 if (*tmp) {
421 fprintf(stderr, "Invalid debug mask: %s\n",
422 optarg);
423 exit(1);
424 }
425 blkid_init_debug(mask);
426 break;
427 }
428 case '?':
429 usage(argv[0]);
430 }
431 if (argc > optind)
432 devname = argv[optind++];
433 if (argc > optind)
434 search_type = argv[optind++];
435 if (argc > optind)
436 search_value = argv[optind++];
437 if (!devname || (argc != optind))
438 usage(argv[0]);
439
440 if ((ret = blkid_get_cache(&cache, file)) != 0) {
441 fprintf(stderr, "%s: error creating cache (%d)\n",
442 argv[0], ret);
443 exit(1);
444 }
445
446 dev = blkid_get_dev(cache, devname, flags);
447 if (!dev) {
448 fprintf(stderr, "%s: cannot find device in blkid cache\n",
449 devname);
450 exit(1);
451 }
452 if (search_type) {
453 found = blkid_dev_has_tag(dev, search_type, search_value);
454 printf("Device %s: (%s, %s) %s\n", blkid_dev_devname(dev),
455 search_type, search_value ? search_value : "NULL",
456 found ? "FOUND" : "NOT FOUND");
457 return(!found);
458 }
459 printf("Device %s...\n", blkid_dev_devname(dev));
460
461 iter = blkid_tag_iterate_begin(dev);
462 while (blkid_tag_next(iter, &type, &value) == 0) {
463 printf("\tTag %s has value %s\n", type, value);
464 }
465 blkid_tag_iterate_end(iter);
466
467 blkid_put_cache(cache);
468 return (0);
469 }
470 #endif