- basename = strrchr(dev_path, '/');
- if (basename)
- basename++;
- else
- basename = dev_path;
-
- /* create a unique mountpoint */
- dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename));
- sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename);
-
- if (!mkdtemp(dir)) {
- pb_log("failed to create temporary directory in %s: %s",
- TMP_DIR, strerror(errno));
- goto out;
+ dir = mountpoint_for_device(dev_path);
+
+ if (stat(dir, &statbuf)) {
+ if (mkdir(dir, 0755)) {
+ pb_log("couldn't create directory %s: %s\n",
+ dir, strerror(errno));
+ goto out;
+ }
+ } else {
+ if (!S_ISDIR(statbuf.st_mode)) {
+ pb_log("mountpoint %s exists, "
+ "but isn't a directory\n", dir);
+ goto out;
+ }