+/**
+Mounts a device if it was not mounted, or unmounts if it was.
+*/
+int toggle_device(Device *device, char *out_buf, int out_size)
+{
+ int umount = !!device->mount_point;
+ char mount_point[1024];
+ 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;
+
+ /* 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);
+
+ pid = fork();
+ if(pid==0)
+ {
+ /* Child process */
+
+ /* Complete construction of pmount call */
+ int last;
+
+ pmount_argv[0] = "pmount";
+
+ last = 0;
+ while(pmount_argv[++last]);
+
+ pmount_argv[last] = device->node;
+ pmount_argv[last+1] = mount_point+7;
+ pmount_argv[last+2] = NULL;
+
+ if(verbosity>=1)
+ {
+ if(umount)
+ printf("Running pumount %s\n", device->node);
+ else
+ {
+ int i = 0;
+ printf("Running pmount");
+ while(pmount_argv[++i])
+ printf(" %s", pmount_argv[i]);
+ printf("\n");
+ }
+ }
+
+ close(pipe_fd[0]);
+ dup2(pipe_fd[1], 1);
+ dup2(pipe_fd[1], 2);
+
+ if(umount)
+ execl("/usr/bin/pumount", "pumount", device->node, NULL);
+ else
+ execvp("/usr/bin/pmount", pmount_argv);
+ _exit(1);
+ }
+ else if(pid<0)
+ return -1;
+
+ /* Parent process */
+
+ close(pipe_fd[1]);
+ FD_ZERO(&fds);
+ FD_SET(pipe_fd[0], &fds);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 200000;
+
+ while(1)
+ {
+ /* The write fd for the pipe may be inherited by a fuse server
+ process and stay open indefinitely. */
+ if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
+ {
+ int len;
+
+ len = read(pipe_fd[0], out_buf+pos, out_size-pos-1);
+ if(len<=0)
+ break;
+ pos += len;
+ }
+ else if(waitpid(pid, &status, 0))
+ {
+ pid = 0;
+ break;
+ }
+ }
+
+ close(pipe_fd[0]);
+ if(pid)
+ waitpid(pid, &status, 0);
+
+ out_buf[pos] = 0;
+
+ if(verbosity>=1)
+ {
+ if(WIFEXITED(status))
+ {
+ if(WEXITSTATUS(status))
+ printf("Command exited successfully\n");
+ else
+ printf("Command exited with status %d\n", WEXITSTATUS(status));
+ }
+ else if(WIFSIGNALED(status))
+ printf("Command terminated with signal %d\n", WTERMSIG(status));
+ else
+ printf("Command exited with unknown result %04X\n", status);
+ }
+
+ if(!WIFEXITED(status) || WEXITSTATUS(status))
+ return -1;
+
+ if(umount)
+ {
+ free(device->mount_point);
+ device->mount_point = NULL;
+ }
+ else
+ device->mount_point = strdup(mount_point);
+
+ return 0;
+}
+
+/**
+Refreshes both the internal devices array and the GUI list.
+*/
+int refresh_devices(GuiContext *context, int umount)
+{
+ GtkListStore *store;
+ GtkTreeSelection *selection;
+ int n_listed;
+ time_t latest;
+ int i;
+ GtkTreeIter iter;
+
+ store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(context->list)));
+ gtk_list_store_clear(store);
+
+ free_devices(context->devices);
+
+ context->devices = get_devices();
+ if(!context->devices)
+ return 0;
+
+ selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(context->list));
+
+ /* Populate the list with devices in appropriate state. */
+ n_listed = 0;
+ latest = 0;
+ for(i=0; context->devices[i].node; ++i)
+ {
+ Device *dev;
+
+ dev = &context->devices[i];
+ if(umount<0 || !dev->mount_point==!umount)
+ {
+ gtk_list_store_append(store, &iter);
+ gtk_list_store_set(store, &iter,
+ 0, dev->description,
+ 1, dev,
+ 2, !!dev->mount_point,
+ 3, dev->mount_point,
+ -1);
+ if(dev->time>latest)
+ {
+ /* Pre-select the device that appeared on the system most recently. */
+ latest = dev->time;
+ gtk_tree_selection_select_iter(selection, &iter);
+ }
+
+ ++n_listed;
+ }
+ }
+
+ return n_listed;
+}
+
+/**
+Handles an automatic refresh of the device list in response to inotify events.
+*/
+gboolean refresh_devices_idle(gpointer data)
+{
+ GuiContext *context = (GuiContext *)data;
+ int n_listed;
+
+ n_listed = refresh_devices(context, -1);
+ context->refresh_pending = 0;
+
+ if(context->autohide)
+ {
+ if(n_listed)
+ gtk_widget_show_all(context->window);
+ else
+ gtk_widget_hide(context->window);
+ }
+
+ return FALSE;
+}
+
+/**
+Callback for selection in the device list changing. Updates the action button
+label according to device status.
+*/
+void selection_changed(GtkTreeSelection *selection, gpointer user_data)
+{
+ GuiContext *context = (GuiContext *)user_data;
+ GtkTreeIter iter;
+ GtkTreeModel *model;
+ Device *device;
+
+ if(!gtk_tree_selection_get_selected(selection, &model, &iter))
+ return;
+
+ gtk_tree_model_get(model, &iter, 1, &device, -1);
+ gtk_button_set_label(GTK_BUTTON(context->button), (device->mount_point ? "Unmount" : "Mount"));
+}
+
+/**
+Callback for activating a row in the device list. Mounts or unmounts the
+device depending on operating mode.
+*/