From: Mikko Rasa Date: Sat, 29 Aug 2015 16:19:56 +0000 (+0300) Subject: Generate a unique mount point name X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0a8aff086f409621047d9dd62ffee7ae0a819c96;p=pmount-gui.git Generate a unique mount point name It may happen that two devices have the same label. --- diff --git a/main.c b/main.c index e5b6440..5aa9611 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -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);