10 #ifdef HAVE_BLKID_BLKID_H
11 #include <blkid/blkid.h>
17 #include "canonicalize.h"
18 #include "pathnames.h"
21 static blkid_cache blcache;
23 #ifdef HAVE_BLKID_EVALUATE_TAG
24 /* ask kernel developers why we need such ugly open() method... */
26 open_device(const char *devname)
31 int fd = open(devname, O_RDONLY);
34 if (errno != ENOMEDIUM)
36 if (retries >= CRDOM_NOMEDIUM_RETRIES)
47 * Parses NAME=value, returns -1 on parse error, 0 success. The success is also
48 * when the 'spec' doesn't contain name=value pair (because the spec could be
49 * a devname too). In particular case the pointer 'name' is set to NULL.
52 fsprobe_parse_spec(const char *spec, char **name, char **value)
57 if (strchr(spec, '='))
58 return blkid_parse_tag_string(spec, name, value);
64 fsprobe_get_devname_by_spec(const char *spec)
70 if (fsprobe_parse_spec(spec, &name, &value) != 0)
71 return NULL; /* parse error */
75 if (!strcmp(name,"LABEL"))
76 nspec = fsprobe_get_devname_by_label(value);
77 else if (!strcmp(name,"UUID"))
78 nspec = fsprobe_get_devname_by_uuid(value);
85 return canonicalize_path(spec);
95 fsprobe_known_fstype(const char *fstype)
97 return blkid_known_fstype(fstype);
100 #ifdef HAVE_BLKID_EVALUATE_TAG
102 * libblkid from util-linux-ng
105 static blkid_probe blprobe;
111 blkid_free_probe(blprobe);
113 blkid_put_cache(blcache);
116 /* returns device LABEL, UUID, FSTYPE, ... by low-level
120 fsprobe_get_value(const char *name, const char *devname)
123 const char *data = NULL;
125 if (!devname || !name)
127 fd = open_device(devname);
131 blprobe = blkid_new_probe();
134 if (blkid_probe_set_device(blprobe, fd, 0, 0))
136 if (blkid_probe_set_request(blprobe, BLKID_PROBREQ_LABEL |
137 BLKID_PROBREQ_UUID | BLKID_PROBREQ_TYPE ))
139 if (blkid_do_safeprobe(blprobe))
141 if (blkid_probe_lookup_value(blprobe, name, &data, NULL))
145 return data ? strdup((char *) data) : NULL;
149 fsprobe_get_label_by_devname(const char *devname)
151 return fsprobe_get_value("LABEL", devname);
155 fsprobe_get_uuid_by_devname(const char *devname)
157 return fsprobe_get_value("UUID", devname);
161 fsprobe_get_fstype_by_devname(const char *devname)
163 return fsprobe_get_value("TYPE", devname);
167 fsprobe_get_devname_by_uuid(const char *uuid)
169 return blkid_evaluate_tag("UUID", uuid, &blcache);
173 fsprobe_get_devname_by_label(const char *label)
175 return blkid_evaluate_tag("LABEL", label, &blcache);
178 #else /* !HAVE_BLKID_EVALUATE_TAG */
181 * Classic libblkid (from e2fsprogs) without blkid_evaluate_tag()
184 #define BLKID_EMPTY_CACHE "/dev/null"
190 blkid_put_cache(blcache);
194 fsprobe_get_devname_by_uuid(const char *uuid)
197 blkid_get_cache(&blcache, NULL);
199 return blkid_get_devname(blcache, "UUID", uuid);
203 fsprobe_get_devname_by_label(const char *label)
206 blkid_get_cache(&blcache, NULL);
208 return blkid_get_devname(blcache, "LABEL", label);
212 fsprobe_get_fstype_by_devname(const char *devname)
218 return blkid_get_tag_value(blcache, "TYPE", devname);
220 /* The cache is not initialized yet. Use empty cache rather than waste
221 * time with /etc/blkid.tab. It seems that probe FS is faster than
222 * parse the cache file. -- kzak (17-May-2007)
224 blkid_get_cache(&c, BLKID_EMPTY_CACHE);
225 tp = blkid_get_tag_value(c, "TYPE", devname);
232 fsprobe_get_label_by_devname(const char *devname)
235 blkid_get_cache(&blcache, NULL);
237 return blkid_get_tag_value(blcache, "LABEL", devname);
241 fsprobe_get_uuid_by_devname(const char *devname)
244 blkid_get_cache(&blcache, NULL);
246 return blkid_get_tag_value(blcache, "UUID", devname);
249 #endif /* !HAVE_BLKID_EVALUATE_TAG */