-const char *mountpoint_for_device(const char *dev_path)
-{
- int i;
- const char *basename;
-
- /* shorten '/dev/foo' to 'foo' */
- basename = strrchr(dev_path, '/');
- if (basename)
- basename++;
- else
- basename = dev_path;
-
- /* check existing entries in the map */
- for (i = 0; (i < DEVICE_MAP_SIZE) && device_map[i].dev; i++)
- if (!strcmp(device_map[i].dev, basename))
- return device_map[i].mnt;
-
- if (i == DEVICE_MAP_SIZE)
- return NULL;
-
- device_map[i].dev = strdup(dev_path);
- asprintf(&device_map[i].mnt, "%s/%s", TMP_DIR, basename);
- return device_map[i].mnt;
+ if (!*dir)
+ return 0;
+
+ if (!stat(dir, &statbuf)) {
+ if (!S_ISDIR(statbuf.st_mode)) {
+ pb_log("%s: %s exists, but isn't a directory\n",
+ __func__, dir);
+ return -1;
+ }
+ return 0;
+ }
+
+ str = strdup(dir);
+ sep = strchr(*str == '/' ? str + 1 : str, '/');
+
+ while (1) {
+
+ /* terminate the path at sep */
+ if (sep)
+ *sep = '\0';
+ pb_log("mkdir(%s)\n", str);
+
+ if (mkdir(str, mode) && errno != EEXIST) {
+ pb_log("mkdir(%s): %s\n", str, strerror(errno));
+ return -1;
+ }
+
+ if (!sep)
+ break;
+
+ /* reset dir to the full path */
+ strcpy(str, dir);
+ sep = strchr(sep + 1, '/');
+ }
+
+ free(str);
+
+ return 0;