/* * Locate a device name from a token (NAME=value string), or (name, value) * pair. In the case of a token, value is ignored. If the "token" is not * of the form "NAME=value" and there is no value given, then it is assumed * to be the actual devname and a copy is returned. */ char *blkid_get_devname(blkid_cache cache, const char *token, const char *value) { blkid_dev dev; blkid_cache c = cache; char *t = 0, *v = 0; char *ret = NULL; if (!token) return NULL; if (!cache) { if (blkid_get_cache(&c, NULL) < 0) return NULL; } DBG(DEBUG_RESOLVE, printf("looking for %s%s%s %s\n", token, value ? "=" : "", value ? value : "", cache ? "in cache" : "from disk")); if (!value) { if (!strchr(token, '=')) { ret = blkid_strdup(token); goto out; } blkid_parse_tag_string(token, &t, &v); if (!t || !v) goto out; token = t; value = v; } dev = blkid_find_dev_with_tag(c, token, value); if (!dev) goto out; ret = blkid_strdup(blkid_dev_devname(dev)); out: if (t) free(t); if (v) free(v); if (!cache) { blkid_put_cache(c); } return (ret); }
/* * Find a tagname (e.g. LABEL or UUID) on a specific device. */ char *blkid_get_tag_value(blkid_cache cache, const char *tagname, const char *devname) { blkid_tag found; blkid_dev dev; blkid_cache c = cache; char *ret = NULL; DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname)); if (!devname) return NULL; if (!cache) { if (blkid_get_cache(&c, NULL) < 0) return NULL; } if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) && (found = blkid_find_tag_dev(dev, tagname))) ret = blkid_strdup(found->bit_val); if (!cache) blkid_put_cache(c); return ret; }
/* * This function adds an entry to the directory list */ static void add_to_dirlist(const char *name, struct dir_list **list) { struct dir_list *dp; dp = xmalloc(sizeof(struct dir_list)); dp->name = blkid_strdup(name); dp->next = *list; *list = dp; }
/** * blkid_evaluate_tag: * @token: token name (e.g "LABEL" or "UUID") * @value: token data * @cache: pointer to cache (or NULL when you don't want to re-use the cache) * * Returns: allocated string with a device name. */ char *blkid_evaluate_tag(const char *token, const char *value, blkid_cache *cache) { struct blkid_config *conf = NULL; char *t = NULL, *v = NULL; char *ret = NULL; int i; if (!token) return NULL; if (!cache || !*cache) blkid_init_debug(0); DBG(DEBUG_EVALUATE, printf("evaluating %s%s%s\n", token, value ? "=" : "", value ? value : "")); if (!value) { if (!strchr(token, '=')) { ret = blkid_strdup(token); goto out; } blkid_parse_tag_string(token, &t, &v); if (!t || !v) goto out; token = t; value = v; } conf = blkid_read_config(NULL); if (!conf) goto out; for (i = 0; i < conf->nevals; i++) { if (conf->eval[i] == BLKID_EVAL_UDEV) ret = evaluate_by_udev(token, value, conf->uevent); else if (conf->eval[i] == BLKID_EVAL_SCAN) ret = evaluate_by_scan(token, value, cache, conf); if (ret) break; } DBG(DEBUG_EVALUATE, printf("%s=%s evaluated as %s\n", token, value, ret)); out: blkid_free_config(conf); free(t); free(v); return ret; }
/* * This function adds an entry to the directory list */ static void add_to_dirlist(const char *name, struct dir_list **list) { struct dir_list *dp; dp = malloc(sizeof(struct dir_list)); if (!dp) return; dp->name = blkid_strdup(name); if (!dp->name) { free(dp); return; } dp->next = *list; *list = dp; }
/* * This function adds an entry to the directory list */ static void add_to_dirlist(const char *dir, const char *subdir, struct dir_list **list) { struct dir_list *dp; dp = malloc(sizeof(struct dir_list)); if (!dp) return; dp->name = subdir ? blkid_strconcat(dir, "/", subdir) : blkid_strdup(dir); if (!dp->name) { free(dp); return; } dp->next = *list; *list = dp; }
void blkid__scan_dir(char *dirname, dev_t devno, struct dir_list **list, char **devname) { DIR *dir; struct dirent *dp; char path[1024]; int dirlen; struct stat st; if ((dir = opendir(dirname)) == NULL) return; dirlen = strlen(dirname) + 2; while ((dp = readdir(dir)) != 0) { if (dirlen + strlen(dp->d_name) >= sizeof(path)) continue; if (dp->d_name[0] == '.' && ((dp->d_name[1] == 0) || ((dp->d_name[1] == '.') && (dp->d_name[2] == 0)))) continue; sprintf(path, "%s/%s", dirname, dp->d_name); if (stat(path, &st) < 0) continue; if (S_ISBLK(st.st_mode) && st.st_rdev == devno) { *devname = blkid_strdup(path); DBG(DEBUG_DEVNO, printf("found 0x%llx at %s (%p)\n", (long long)devno, path, *devname)); break; } if (list && S_ISDIR(st.st_mode) && !lstat(path, &st) && S_ISDIR(st.st_mode)) add_to_dirlist(path, list); } closedir(dir); return; }