Upload Tizen:Base source
[framework/base/util-linux-ng.git] / shlibs / blkid / src / tag.c
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 = (blkid_tag) calloc(1, sizeof(struct blkid_struct_tag))))
25                 return NULL;
26
27         INIT_LIST_HEAD(&tag->bit_tags);
28         INIT_LIST_HEAD(&tag->bit_names);
29
30         return tag;
31 }
32
33 #ifdef CONFIG_BLKID_DEBUG
34 void blkid_debug_dump_tag(blkid_tag tag)
35 {
36         if (!tag) {
37                 printf("    tag: NULL\n");
38                 return;
39         }
40
41         printf("    tag: %s=\"%s\"\n", tag->bit_name, tag->bit_val);
42 }
43 #endif
44
45 void blkid_free_tag(blkid_tag tag)
46 {
47         if (!tag)
48                 return;
49
50         DBG(DEBUG_TAG, printf("    freeing tag %s=%s\n", tag->bit_name,
51                    tag->bit_val ? tag->bit_val : "(NULL)"));
52         DBG(DEBUG_TAG, blkid_debug_dump_tag(tag));
53
54         list_del(&tag->bit_tags);       /* list of tags for this device */
55         list_del(&tag->bit_names);      /* list of tags with this type */
56
57         free(tag->bit_name);
58         free(tag->bit_val);
59
60         free(tag);
61 }
62
63 /*
64  * Find the desired tag on a device.  If value is NULL, then the
65  * first such tag is returned, otherwise return only exact tag if found.
66  */
67 blkid_tag blkid_find_tag_dev(blkid_dev dev, const char *type)
68 {
69         struct list_head *p;
70
71         if (!dev || !type)
72                 return NULL;
73
74         list_for_each(p, &dev->bid_tags) {
75                 blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
76                                            bit_tags);
77
78                 if (!strcmp(tmp->bit_name, type))
79                         return tmp;
80         }
81         return NULL;
82 }
83
84 extern int blkid_dev_has_tag(blkid_dev dev, const char *type,
85                              const char *value)
86 {
87         blkid_tag               tag;
88
89         if (!dev || !type)
90                 return -1;
91
92         tag = blkid_find_tag_dev(dev, type);
93         if (!value)
94                 return (tag != NULL);
95         if (!tag || strcmp(tag->bit_val, value))
96                 return 0;
97         return 1;
98 }
99
100 /*
101  * Find the desired tag type in the cache.
102  * We return the head tag for this tag type.
103  */
104 static blkid_tag blkid_find_head_cache(blkid_cache cache, const char *type)
105 {
106         blkid_tag head = NULL, tmp;
107         struct list_head *p;
108
109         if (!cache || !type)
110                 return NULL;
111
112         list_for_each(p, &cache->bic_tags) {
113                 tmp = list_entry(p, struct blkid_struct_tag, bit_tags);
114                 if (!strcmp(tmp->bit_name, type)) {
115                         DBG(DEBUG_TAG,
116                             printf("    found cache tag head %s\n", type));
117                         head = tmp;
118                         break;
119                 }
120         }
121         return head;
122 }
123
124 /*
125  * Set a tag on an existing device.
126  *
127  * If value is NULL, then delete the tagsfrom the device.
128  */
129 int blkid_set_tag(blkid_dev dev, const char *name,
130                   const char *value, const int vlength)
131 {
132         blkid_tag       t = 0, head = 0;
133         char            *val = 0;
134         char            **dev_var = 0;
135
136         if (!dev || !name)
137                 return -BLKID_ERR_PARAM;
138
139         if (!(val = blkid_strndup(value, vlength)) && value)
140                 return -BLKID_ERR_MEM;
141
142         /*
143          * Certain common tags are linked directly to the device struct
144          * We need to know what they are before we do anything else because
145          * the function name parameter might get freed later on.
146          */
147         if (!strcmp(name, "TYPE"))
148                 dev_var = &dev->bid_type;
149         else if (!strcmp(name, "LABEL"))
150                 dev_var = &dev->bid_label;
151         else if (!strcmp(name, "UUID"))
152                 dev_var = &dev->bid_uuid;
153
154         t = blkid_find_tag_dev(dev, name);
155         if (!value) {
156                 if (t)
157                         blkid_free_tag(t);
158         } else if (t) {
159                 if (!strcmp(t->bit_val, val)) {
160                         /* Same thing, exit */
161                         free(val);
162                         return 0;
163                 }
164                 free(t->bit_val);
165                 t->bit_val = val;
166         } else {
167                 /* Existing tag not present, add to device */
168                 if (!(t = blkid_new_tag()))
169                         goto errout;
170                 t->bit_name = blkid_strdup(name);
171                 t->bit_val = val;
172                 t->bit_dev = dev;
173
174                 list_add_tail(&t->bit_tags, &dev->bid_tags);
175
176                 if (dev->bid_cache) {
177                         head = blkid_find_head_cache(dev->bid_cache,
178                                                      t->bit_name);
179                         if (!head) {
180                                 head = blkid_new_tag();
181                                 if (!head)
182                                         goto errout;
183
184                                 DBG(DEBUG_TAG,
185                                     printf("    creating new cache tag head %s\n", name));
186                                 head->bit_name = blkid_strdup(name);
187                                 if (!head->bit_name)
188                                         goto errout;
189                                 list_add_tail(&head->bit_tags,
190                                               &dev->bid_cache->bic_tags);
191                         }
192                         list_add_tail(&t->bit_names, &head->bit_names);
193                 }
194         }
195
196         /* Link common tags directly to the device struct */
197         if (dev_var)
198                 *dev_var = val;
199
200         if (dev->bid_cache)
201                 dev->bid_cache->bic_flags |= BLKID_BIC_FL_CHANGED;
202         return 0;
203
204 errout:
205         if (t)
206                 blkid_free_tag(t);
207         else
208                 free(val);
209         if (head)
210                 blkid_free_tag(head);
211         return -BLKID_ERR_MEM;
212 }
213
214
215 /*
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
221  * us).
222  *
223  * Returns 0 on success, and -1 on failure.
224  */
225 int blkid_parse_tag_string(const char *token, char **ret_type, char **ret_val)
226 {
227         char *name, *value, *cp;
228
229         DBG(DEBUG_TAG, printf("trying to parse '%s' as a tag\n", token));
230
231         if (!token || !(cp = strchr(token, '=')))
232                 return -1;
233
234         name = blkid_strdup(token);
235         if (!name)
236                 return -1;
237         value = name + (cp - token);
238         *value++ = '\0';
239         if (*value == '"' || *value == '\'') {
240                 char c = *value++;
241                 if (!(cp = strrchr(value, c)))
242                         goto errout; /* missing closing quote */
243                 *cp = '\0';
244         }
245         value = blkid_strdup(value);
246         if (!value)
247                 goto errout;
248
249         *ret_type = name;
250         *ret_val = value;
251
252         return 0;
253
254 errout:
255         free(name);
256         return -1;
257 }
258
259 /*
260  * Tag iteration routines for the public libblkid interface.
261  *
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]
269  */
270
271 /*
272  * This series of functions iterate over all tags in a device
273  */
274 #define TAG_ITERATE_MAGIC       0x01a5284c
275
276 struct blkid_struct_tag_iterate {
277         int                     magic;
278         blkid_dev               dev;
279         struct list_head        *p;
280 };
281
282 extern blkid_tag_iterate blkid_tag_iterate_begin(blkid_dev dev)
283 {
284         blkid_tag_iterate       iter;
285
286         iter = malloc(sizeof(struct blkid_struct_tag_iterate));
287         if (iter) {
288                 iter->magic = TAG_ITERATE_MAGIC;
289                 iter->dev = dev;
290                 iter->p = dev->bid_tags.next;
291         }
292         return (iter);
293 }
294
295 /*
296  * Return 0 on success, -1 on error
297  */
298 extern int blkid_tag_next(blkid_tag_iterate iter,
299                           const char **type, const char **value)
300 {
301         blkid_tag tag;
302
303         *type = 0;
304         *value = 0;
305         if (!iter || iter->magic != TAG_ITERATE_MAGIC ||
306             iter->p == &iter->dev->bid_tags)
307                 return -1;
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;
312         return 0;
313 }
314
315 extern void blkid_tag_iterate_end(blkid_tag_iterate iter)
316 {
317         if (!iter || iter->magic != TAG_ITERATE_MAGIC)
318                 return;
319         iter->magic = 0;
320         free(iter);
321 }
322
323 /*
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.
328  */
329 extern blkid_dev blkid_find_dev_with_tag(blkid_cache cache,
330                                          const char *type,
331                                          const char *value)
332 {
333         blkid_tag       head;
334         blkid_dev       dev;
335         int             pri;
336         struct list_head *p;
337         int             probe_new = 0;
338
339         if (!cache || !type || !value)
340                 return NULL;
341
342         blkid_read_cache(cache);
343
344         DBG(DEBUG_TAG, printf("looking for %s=%s in cache\n", type, value));
345
346 try_again:
347         pri = -1;
348         dev = 0;
349         head = blkid_find_head_cache(cache, type);
350
351         if (head) {
352                 list_for_each(p, &head->bit_names) {
353                         blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
354                                                    bit_names);
355
356                         if (!strcmp(tmp->bit_val, value) &&
357                             (tmp->bit_dev->bid_pri > pri) &&
358                             !access(tmp->bit_dev->bid_name, F_OK)) {
359                                 dev = tmp->bit_dev;
360                                 pri = dev->bid_pri;
361                         }
362                 }
363         }
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)))
367                         goto try_again;
368         }
369
370         if (!dev && !probe_new) {
371                 if (blkid_probe_all_new(cache) < 0)
372                         return NULL;
373                 probe_new++;
374                 goto try_again;
375         }
376
377         if (!dev && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
378                 if (blkid_probe_all(cache) < 0)
379                         return NULL;
380                 goto try_again;
381         }
382         return dev;
383 }
384
385 #ifdef TEST_PROGRAM
386 #ifdef HAVE_GETOPT_H
387 #include <getopt.h>
388 #else
389 extern char *optarg;
390 extern int optind;
391 #endif
392
393 void usage(char *prog)
394 {
395         fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask] device "
396                 "[type value]\n",
397                 prog);
398         fprintf(stderr, "\tList all tags for a device and exit\n");
399         exit(1);
400 }
401
402 int main(int argc, char **argv)
403 {
404         blkid_tag_iterate       iter;
405         blkid_cache             cache = NULL;
406         blkid_dev               dev;
407         int                     c, ret, found;
408         int                     flags = BLKID_DEV_FIND;
409         char                    *tmp;
410         char                    *file = NULL;
411         char                    *devname = NULL;
412         char                    *search_type = NULL;
413         char                    *search_value = NULL;
414         const char              *type, *value;
415
416         while ((c = getopt (argc, argv, "m:f:")) != EOF)
417                 switch (c) {
418                 case 'f':
419                         file = optarg;
420                         break;
421                 case 'm':
422                 {
423                         int mask = strtoul (optarg, &tmp, 0);
424                         if (*tmp) {
425                                 fprintf(stderr, "Invalid debug mask: %s\n",
426                                         optarg);
427                                 exit(1);
428                         }
429                         blkid_init_debug(mask);
430                         break;
431                 }
432                 case '?':
433                         usage(argv[0]);
434                 }
435         if (argc > optind)
436                 devname = argv[optind++];
437         if (argc > optind)
438                 search_type = argv[optind++];
439         if (argc > optind)
440                 search_value = argv[optind++];
441         if (!devname || (argc != optind))
442                 usage(argv[0]);
443
444         if ((ret = blkid_get_cache(&cache, file)) != 0) {
445                 fprintf(stderr, "%s: error creating cache (%d)\n",
446                         argv[0], ret);
447                 exit(1);
448         }
449
450         dev = blkid_get_dev(cache, devname, flags);
451         if (!dev) {
452                 fprintf(stderr, "%s: Can not find device in blkid cache\n",
453                         devname);
454                 exit(1);
455         }
456         if (search_type) {
457                 found = blkid_dev_has_tag(dev, search_type, search_value);
458                 printf("Device %s: (%s, %s) %s\n", blkid_dev_devname(dev),
459                        search_type, search_value ? search_value : "NULL",
460                        found ? "FOUND" : "NOT FOUND");
461                 return(!found);
462         }
463         printf("Device %s...\n", blkid_dev_devname(dev));
464
465         iter = blkid_tag_iterate_begin(dev);
466         while (blkid_tag_next(iter, &type, &value) == 0) {
467                 printf("\tTag %s has value %s\n", type, value);
468         }
469         blkid_tag_iterate_end(iter);
470
471         blkid_put_cache(cache);
472         return (0);
473 }
474 #endif