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