]> git.tdb.fi Git - pmount-gui.git/commitdiff
Generate a unique mount point name
authorMikko Rasa <tdb@tdb.fi>
Sat, 29 Aug 2015 16:19:56 +0000 (19:19 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 29 Aug 2015 16:44:24 +0000 (19:44 +0300)
It may happen that two devices have the same label.

main.c

diff --git a/main.c b/main.c
index e5b644024648727747d9fdd564ff1d5770448df1..5aa961130cd80890b732ea6be857b1a23547c8a1 100644 (file)
--- a/main.c
+++ b/main.c
@@ -7,6 +7,7 @@
 #include <fcntl.h>
 #include <dirent.h>
 #include <mntent.h>
+#include <errno.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/select.h>
@@ -635,19 +636,38 @@ int toggle_device(Device *device, char *out_buf, int out_size)
 {
        int umount = !!device->mount_point;
        char mount_point[1024];
-       int len;
        int pos = 0;
        int status = 0;
        fd_set fds;
        struct timeval timeout;
        int pid;
        int pipe_fd[2];
+       char suffix = 0;
 
        out_buf[0] = 0;
 
-       len = snprintf(mount_point, sizeof(mount_point), "/media/%s", device->label);
-       if(len>=(int)sizeof(mount_point))
-               return -1;
+       /* Find a mount point that does not exist yet. */
+       while(1)
+       {
+               int len;
+
+               len = snprintf(mount_point, sizeof(mount_point), "/media/%s", device->label);
+               if(len+2>=(int)sizeof(mount_point))
+                       return -1;
+
+               if(suffix)
+                       len += snprintf(mount_point+len, sizeof(mount_point)-len, "_%c", suffix);
+
+               if(access(mount_point, F_OK)<0 && errno==ENOENT)
+                       break;
+
+               if(suffix==0)
+                       suffix = '1';
+               else if(suffix<'9')
+                       ++suffix;
+               else
+                       return -1;
+       }
 
        pipe(pipe_fd);