]> git.tdb.fi Git - pmount-gui.git/blob - main.c
ae08cbb7cda87de794b03d673e40a6de691e721e
[pmount-gui.git] / main.c
1 /* Required for strdup, snprintf, getopt */
2 #define _XOPEN_SOURCE 500
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <dirent.h>
9 #include <mntent.h>
10 #include <sys/stat.h>
11 #include <sys/wait.h>
12 #include <sys/select.h>
13 #include <gtk/gtk.h>
14 #include <gdk/gdkkeysyms.h>
15
16 typedef struct sProperty
17 {
18         char *name;
19         char *value;
20 } Property;
21
22 typedef struct sDevice
23 {
24         char *node;
25         char *label;
26         char *description;
27         int mounted;
28         time_t time;
29 } Device;
30
31 int verbosity = 0;
32 char *post_mount_command = NULL;
33
34 /**
35 Parses a string of the form name=value and places the components in a Property
36 structure.  Returns 0 on success, or -1 if the string wasn't a valid property.
37 */
38 int parse_property(char *str, int size, Property *prop)
39 {
40         int equals = -1;
41         int i;
42
43         for(i=0; (equals<0 && i<size); ++i)
44                 if(str[i]=='=')
45                         equals = i;
46
47         if(equals<0)
48                 return -1;
49
50         prop->name = malloc(equals+1);
51         strncpy(prop->name, str, equals);
52         prop->name[equals] = 0;
53
54         prop->value = malloc(size-equals);
55         strncpy(prop->value, str+equals+1, size-equals-1);
56         prop->value[size-equals-1] = 0;
57
58         return 0;
59 }
60
61 /**
62 Retrieves all properties associated with a /dev node.  The returned array is
63 terminated with an entry containing NULL values.  Use free_properties to free
64 the array.
65 */
66 Property *get_device_properties(char *node)
67 {
68         int pid;
69         int pipe_fd[2];
70         char *buf;
71         int bufsize;
72         int pos = 0;
73         int eof = 0;
74         Property *props = NULL;
75         int n_props = 0;
76
77         pipe(pipe_fd);
78
79         pid = fork();
80         if(pid==0)
81         {
82                 /* Child process */
83                 if(verbosity>=2)
84                         printf("Running udevadm info -q property -n \"%s\"\n", node);
85
86                 close(pipe_fd[0]);
87                 dup2(pipe_fd[1], 1);
88
89                 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
90                 _exit(1);
91         }
92         else if(pid<0)
93         {
94                 close(pipe_fd[0]);
95                 close(pipe_fd[1]);
96
97                 return NULL;
98         }
99
100         /* Parent process */
101         close(pipe_fd[1]);
102
103         bufsize = 256;
104         buf = (char *)malloc(bufsize);
105
106         while(1)
107         {
108                 int newline;
109                 int i;
110                 Property prop;
111
112                 if(!eof)
113                 {
114                         int len;
115
116                         len = read(pipe_fd[0], buf+pos, bufsize-pos);
117                         if(len==0)
118                                 eof = 1;
119                         else if(len==-1)
120                                 break;
121                         pos += len;
122                 }
123
124                 newline = -1;
125                 for(i=0; (newline<0 && i<pos); ++i)
126                         if(buf[i]=='\n')
127                                 newline = i;
128
129                 if(newline<0)
130                 {
131                         if(eof)
132                                 break;
133
134                         /* There was no newline in the buffer but there is more output to
135                         be read.  Try again with a larger buffer. */
136                         bufsize *= 2;
137                         buf = (char *)realloc(buf, bufsize);
138                         continue;
139                 }
140
141                 if(parse_property(buf, newline, &prop)==0)
142                 {
143                         /* Reserve space for a sentinel value as well. */
144                         props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
145                         props[n_props] = prop;
146                         ++n_props;
147
148                         memmove(buf, buf+newline+1, pos-newline-1);
149                         pos -= newline+1;
150                 }
151                 else
152                         break;
153         }
154
155         free(buf);
156
157         if(props)
158         {
159                 /* Terminate the array with NULL pointers. */
160                 props[n_props].name = NULL;
161                 props[n_props].value = NULL;
162         }
163
164         waitpid(pid, NULL, 0);
165         close(pipe_fd[0]);
166
167         return props;
168 }
169
170 /**
171 Looks for a property in an array of properties and returns its value.  Returns
172 NULL if the property was not found.
173 */
174 char *get_property_value(Property *props, char *name)
175 {
176         int i;
177         for(i=0; props[i].name; ++i)
178                 if(strcmp(props[i].name, name)==0)
179                         return props[i].value;
180         return NULL;
181 }
182
183 /**
184 Checks if a property has a specific value.  A NULL value is matched if the
185 property does not exist.
186 */
187 int match_property_value(Property *props, char *name, char *value)
188 {
189         char *v = get_property_value(props, name);
190         if(!v)
191                 return value==NULL;
192         return strcmp(v, value)==0;
193 }
194
195 /**
196 Frees an array of properties and all strings contained in it.
197 */
198 void free_properties(Property *props)
199 {
200         int i;
201         if(!props)
202                 return;
203         for(i=0; props[i].name; ++i)
204         {
205                 free(props[i].name);
206                 free(props[i].value);
207         }
208         free(props);
209 }
210
211 /**
212 Reads device names from an fstab/mtab file.
213 */
214 char **get_mount_entries(char *filename, int (*predicate)(struct mntent *))
215 {
216         FILE *file;
217         struct mntent *me;
218         char **devices = NULL;
219         int n_devices = 0;
220
221         file = setmntent(filename, "r");
222         if(!file)
223                 return NULL;
224
225         while((me = getmntent(file)))
226                 if(!predicate || predicate(me))
227                 {
228                         devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
229                         devices[n_devices] = strdup(me->mnt_fsname);
230                         ++n_devices;
231                 }
232
233         endmntent(file);
234         if(devices)
235                 devices[n_devices] = NULL;
236
237         return devices;
238 }
239
240 /**
241 Returns an array of all currently mounted devices.
242 */
243 char **get_mounted_devices(void)
244 {
245         return get_mount_entries("/etc/mtab", NULL);
246 }
247
248 /**
249 Checks if an fstab entry has the user option set.
250 */
251 int is_user_mountable(struct mntent *me)
252 {
253         return hasmntopt(me, "user")!=NULL;
254 }
255
256 /**
257 Returns an array of user-mountable devices listed in fstab.
258 */
259 char **get_fstab_devices(void)
260 {
261         return get_mount_entries("/etc/fstab", &is_user_mountable);
262 }
263
264 /**
265 Checks if an array of strings contains the specified string.
266 */
267 int is_in_array(char **array, char *str)
268 {
269         int i;
270         if(!array || !str)
271                 return 0;
272         for(i=0; array[i]; ++i)
273                 if(!strcmp(str, array[i]))
274                         return 1;
275         return 0;
276 }
277
278 /**
279 Frees an array of strings.
280 */
281 void free_string_array(char **array)
282 {
283         int i;
284         if(!array)
285                 return;
286         for(i=0; array[i]; ++i)
287                 free(array[i]);
288         free(array);
289 }
290
291 /**
292 Checks if a partition identified by a sysfs path is on a removable device.
293 */
294 int is_removable(char *devpath)
295 {
296         char fnbuf[256];
297         int len;
298         char *ptr;
299         int fd;
300
301         len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
302         /* Default to not removable if the path was too long. */
303         if(len+10>=(int)sizeof(fnbuf))
304                 return 0;
305
306         /* We got a partition as a parameter, but the removable property is on the
307         disk.  Replace the last component with "removable". */
308         for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
309         strcpy(ptr, "/removable");
310
311         fd = open(fnbuf, O_RDONLY);
312         if(fd!=-1)
313         {
314                 char c;
315                 read(fd, &c, 1);
316                 close(fd);
317                 if(c=='1')
318                 {
319                         if(verbosity>=2)
320                                 printf("  Removable\n");
321                         return 1;
322                 }
323                 if(verbosity>=2)
324                         printf("  Not removable\n");
325         }
326
327         return 0;
328 }
329
330 /**
331 Checks if a partition's disk or any of its parent devices are connected to any
332 of a set of buses.  The device is identified by a sysfs path.  The bus array
333 must be terminated with a NULL entry.
334 */
335 int check_buses(char *devpath, char **buses)
336 {
337         char fnbuf[256];
338         char *ptr;
339         int len;
340
341         len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
342         /* Default to no match if the path was too long. */
343         if(len+10>=(int)sizeof(fnbuf))
344                 return 0;
345
346         for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
347                 if(*ptr=='/')
348                 {
349                         char linkbuf[256];
350                         /* Replace the last component with "subsystem". */
351                         strcpy(ptr, "/subsystem");
352                         len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
353
354                         if(len!=-1)
355                         {
356                                 linkbuf[len] = 0;
357                                 /* Extract the last component of the subsystem symlink. */
358                                 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
359
360                                 if(verbosity>=2)
361                                 {
362                                         *ptr = 0;
363                                         printf("  Subsystem of %s is %s\n", fnbuf, linkbuf+len);
364                                 }
365
366                                 if(is_in_array(buses, linkbuf+len))
367                                         return 1;
368                         }
369                 }
370
371         return 0;
372 }
373
374 /**
375 Check if an array of properties describes a device that can be mounted.  An
376 array of explicitly allowed devices can be passed in as well.  Both arrays must
377 be terminated by a NULL entry.
378 */
379 int can_mount(Property *props, char **allowed)
380 {
381         static char *removable_buses[] = { "usb", "firewire", 0 };
382         char *devname;
383         char *devpath;
384         char *bus;
385
386         devname = get_property_value(props, "DEVNAME");
387         if(is_in_array(allowed, devname))
388                 return 1;
389
390         /* Special case for CD devices, since they are not partitions.  Only allow
391         mounting if media is inserted. */
392         if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1"))
393                 return 1;
394
395         /* Only allow mounting partitions. */
396         if(!match_property_value(props, "DEVTYPE", "partition"))
397                 return 0;
398
399         devpath = get_property_value(props, "DEVPATH");
400         if(is_removable(devpath))
401                 return 1;
402
403         /* Certain buses are removable by nature, but devices only advertise
404         themselves as removable if they support removable media, e.g. memory card
405         readers. */
406         bus = get_property_value(props, "ID_BUS");
407         if(is_in_array(removable_buses, bus))
408                 return 1;
409
410         return check_buses(devpath, removable_buses);
411 }
412
413 /**
414 Returns an array of all device nodes in a directory.  Symbolic links are
415 dereferenced.
416 */
417 char **get_device_nodes(char *dirname)
418 {
419         DIR *dir;
420         struct dirent *de;
421         char fnbuf[256];
422         char linkbuf[256];
423         struct stat st;
424         char **nodes = NULL;
425         int n_nodes = 0;
426         char **checked = NULL;
427         int n_checked = 0;
428         int i;
429
430         dir = opendir(dirname);
431         if(!dir)
432                 return NULL;
433
434         while((de = readdir(dir)))
435         {
436                 char *node;
437                 int duplicate = 0;
438
439                 /* Ignore . and .. entries. */
440                 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
441                         continue;
442
443                 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
444
445                 node = fnbuf;
446                 lstat(fnbuf, &st);
447                 if(S_ISLNK(st.st_mode))
448                 {
449                         int len;
450                         len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
451                         if(len!=-1)
452                         {
453                                 linkbuf[len] = 0;
454                                 node = linkbuf;
455                         }
456                 }
457
458                 /* There may be multiple symlinks to the same device.  Only include each
459                 device once in the returned array. */
460                 if(checked)
461                 {
462                         for(i=0; (!duplicate && i<n_checked); ++i)
463                                 if(strcmp(node, checked[i])==0)
464                                         duplicate = 1;
465                 }
466                 if(duplicate)
467                 {
468                         if(verbosity>=2)
469                                 printf("Device %s is a duplicate\n", fnbuf);
470                         continue;
471                 }
472
473                 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
474                 checked[n_checked] = strdup(node);
475                 ++n_checked;
476
477                 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
478                 nodes[n_nodes] = strdup(fnbuf);
479                 ++n_nodes;
480         }
481
482         closedir(dir);
483         if(checked)
484         {
485                 for(i=0; i<n_checked; ++i)
486                         free(checked[i]);
487                 free(checked);
488         }
489
490         if(nodes)
491                 nodes[n_nodes] = NULL;
492
493         return nodes;
494 }
495
496 /** Returns an array of all mountable devices. */
497 Device *get_devices(void)
498 {
499         char **nodes = NULL;
500         Device *devices = NULL;
501         int n_devices = 0;
502         char **mounted = NULL;
503         char **fstab = NULL;
504         int i;
505
506         nodes = get_device_nodes("/dev/disk/by-id");
507         mounted = get_mounted_devices();
508         fstab = get_fstab_devices();
509
510         for(i=0; nodes[i]; ++i)
511         {
512                 Property *props;
513
514                 if(verbosity>=1)
515                         printf("Examining device %s\n", nodes[i]);
516
517                 props = get_device_properties(nodes[i]);
518                 if(!props)
519                 {
520                         if(verbosity>=2)
521                                 printf("  No properties\n");
522                         continue;
523                 }
524
525                 if(verbosity>=2)
526                 {
527                         int j;
528                         for(j=0; props[j].name; ++j)
529                                 printf("  %s = %s\n", props[j].name, props[j].value);
530                 }
531
532                 if(can_mount(props, fstab))
533                 {
534                         char *devname;
535                         char *label;
536                         char *vendor;
537                         char *model;
538                         char buf[256];
539                         char pos;
540                         struct stat st;
541
542                         if(verbosity>=1)
543                                 printf("  Using device\n");
544
545                         devname = get_property_value(props, "DEVNAME");
546
547                         /* Get a human-readable label for the device.  Use filesystem label,
548                         filesystem UUID or device node name in order of preference. */
549                         label = get_property_value(props, "ID_FS_LABEL");
550                         if(!label)
551                                 label = get_property_value(props, "ID_FS_UUID");
552                         if(!label)
553                         {
554                                 char *ptr;
555
556                                 label = devname;
557                                 for(ptr=label; *ptr; ++ptr)
558                                         if(*ptr=='/')
559                                                 label = ptr+1;
560                         }
561
562                         vendor = get_property_value(props, "ID_VENDOR");
563                         model = get_property_value(props, "ID_MODEL");
564
565                         pos = snprintf(buf, sizeof(buf), "%s", label);
566                         if(vendor && model)
567                                 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
568
569                         stat(nodes[i], &st);
570
571                         /* Reserve space for a sentinel entry. */
572                         devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
573                         devices[n_devices].node = nodes[i];
574                         devices[n_devices].label = strdup(label);
575                         devices[n_devices].description = strdup(buf);
576                         devices[n_devices].mounted = is_in_array(mounted, devname);
577                         devices[n_devices].time = st.st_mtime;
578                         ++n_devices;
579                 }
580                 else
581                         free(nodes[i]);
582                 free_properties(props);
583         }
584
585         free(nodes);
586         free_string_array(mounted);
587
588         if(devices)
589         {
590                 /* Terminate the array with NULL pointers. */
591                 devices[n_devices].node = NULL;
592                 devices[n_devices].label = NULL;
593                 devices[n_devices].description = NULL;
594         }
595
596         return devices;
597 }
598
599 /**
600 Frees an array of devices and all strings contained in it.
601 */
602 void free_devices(Device *devices)
603 {
604         int i;
605         if(!devices)
606                 return;
607         for(i=0; devices[i].node; ++i)
608         {
609                 free(devices[i].node);
610                 free(devices[i].label);
611                 free(devices[i].description);
612         }
613         free(devices);
614 }
615
616 /**
617 Callback for activating a row in the device list.  Mounts or unmounts the
618 device depending on operating mode.
619 */
620 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
621 {
622         GtkTreeModel *model;
623         GtkTreeIter iter;
624         int umount = *(int *)user_data;
625         char buf[1024];
626         int pos = 0;
627         int status = 0;
628         fd_set fds;
629         struct timeval timeout;
630         Device *device;
631         int pid;
632         int pipe_fd[2];
633
634         model = gtk_tree_view_get_model(list);
635
636         if(!gtk_tree_model_get_iter(model, &iter, path))
637                 return;
638
639         gtk_tree_model_get(model, &iter, 1, &device, -1);
640
641         pipe(pipe_fd);
642
643         pid = fork();
644         if(pid==0)
645         {
646                 /* Child process */
647                 if(verbosity>=1)
648                 {
649                         if(umount)
650                                 printf("Running pumount %s\n", device->node);
651                         else
652                                 printf("Running pmount %s %s\n", device->node, device->label);
653                 }
654
655                 close(pipe_fd[0]);
656                 dup2(pipe_fd[1], 1);
657                 dup2(pipe_fd[1], 2);
658
659                 if(umount)
660                         execl("/usr/bin/pumount", "pumount", device->node, NULL);
661                 else
662                         execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
663                 _exit(1);
664         }
665         else if(pid<0)
666                 return;
667
668         /* Parent process */
669
670         close(pipe_fd[1]);
671         FD_ZERO(&fds);
672         FD_SET(pipe_fd[0], &fds);
673         timeout.tv_sec = 0;
674         timeout.tv_usec = 200000;
675
676         while(1)
677         {
678                 /* The write fd for the pipe may be inherited by a fuse server
679                 process and stay open indefinitely. */
680                 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
681                 {
682                         int len;
683
684                         len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
685                         if(len<=0)
686                                 break;
687                         pos += len;
688                 }
689                 else if(waitpid(pid, &status, 0))
690                 {
691                         pid = 0;
692                         break;
693                 }
694         }
695
696         close(pipe_fd[0]);
697         if(pid)
698                 waitpid(pid, &status, 0);
699
700         buf[pos] = 0;
701
702         if(verbosity>=1)
703         {
704                 if(WIFEXITED(status))
705                 {
706                         if(WEXITSTATUS(status))
707                                 printf("Command exited successfully\n");
708                         else
709                                 printf("Command exited with status %d\n", WEXITSTATUS(status));
710                 }
711                 else if(WIFSIGNALED(status))
712                         printf("Command terminated with signal %d\n", WTERMSIG(status));
713                 else
714                         printf("Command exited with unknown result %04X\n", status);
715         }
716
717         if(!WIFEXITED(status) || WEXITSTATUS(status))
718         {
719                 GtkWidget *dialog;
720
721                 /* Pmount terminated with nonzero status or a signal.  Display an
722                 error to the user. */
723                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
724                 g_signal_connect(dialog, "response", &gtk_main_quit, NULL);
725                 gtk_widget_show_all(dialog);
726                 return;
727         }
728
729         gtk_main_quit();
730
731         if(post_mount_command && !umount)
732         {
733                 char workdir[256];
734                 int len;
735                 len = snprintf(workdir, sizeof(workdir), "/media/%s", device->label);
736                 if(len<(int)sizeof(workdir))
737                 {
738                         if(verbosity>=1)
739                                 printf("Running %s in %s\n", post_mount_command, workdir);
740
741                         pid = fork();
742                         if(pid==0)
743                         {
744                                 chdir(workdir);
745                                 execlp(post_mount_command, post_mount_command, NULL);
746                                 _exit(1);
747                         }
748                 }
749         }
750
751         (void)column;
752 }
753
754 /**
755 Callback for the mount/unmount button.  Causes the selected row in the device
756 list to be activated.
757 */
758 void button_clicked(GtkButton *button, gpointer user_data)
759 {
760         GtkWidget *list = (GtkWidget *)user_data;
761         GtkTreeSelection *selection;
762         GtkTreeIter iter;
763         GtkTreeModel *model;
764         GtkTreePath *path;
765
766         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
767         gtk_tree_selection_get_selected(selection, &model, &iter);
768         path = gtk_tree_model_get_path(model, &iter);
769         gtk_tree_view_row_activated(GTK_TREE_VIEW(list), path, gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0));
770         gtk_tree_path_free(path);
771
772         (void)button;
773 }
774
775 /**
776 Global key press callback for the window.
777 */
778 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
779 {
780         if(event->key.keyval==GDK_KEY_Escape)
781         {
782                 gtk_main_quit();
783                 return TRUE;
784         }
785
786         (void)widget;
787         (void)user_data;
788
789         return FALSE;
790 }
791
792 void show_help(void)
793 {
794         printf("pmount-gui\n"
795                 "Copyright (c) 2011-2015 Mikko Rasa, Mikkosoft Productions\n\n"
796                 "Usage: pmount-gui [-v] [-u] [-r <command>] [-h]\n\n"
797                 "Options:\n"
798                 "  -v  Increase verbosity\n"
799                 "  -u  Unmount a device (default is mount)\n"
800                 "  -r  Run a command after mounting\n"
801                 "  -h  Display this help\n");
802 }
803
804 int main(int argc, char **argv)
805 {
806         GtkWidget *window;
807         GtkWidget *box;
808         GtkWidget *viewport;
809         GtkWidget *list;
810         GtkListStore *store;
811         GtkTreeSelection *selection;
812         GtkWidget *button;
813         GtkTreeIter iter;
814         Device *devices;
815         int i;
816         time_t latest;
817         int opt;
818         int umount = 0;
819         int n_listed;
820
821         gtk_init(&argc, &argv);
822
823         while((opt = getopt(argc, argv, "vur:h"))!=-1) switch(opt)
824         {
825         case 'v':
826                 ++verbosity;
827                 break;
828         case 'u':
829                 umount = 1;
830                 break;
831         case 'r':
832                 post_mount_command = optarg;
833                 break;
834         case 'h':
835                 show_help();
836                 return 0;
837         }
838
839         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
840         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
841         g_signal_connect(window, "destroy", G_CALLBACK(&gtk_main_quit), NULL);
842         g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
843
844         box = gtk_vbox_new(FALSE, 5);
845         gtk_container_add(GTK_CONTAINER(window), box);
846
847         viewport = gtk_viewport_new(NULL, NULL);
848         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
849         gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
850
851         list = gtk_tree_view_new();
852         gtk_container_add(GTK_CONTAINER(viewport), list);
853         g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), &umount);
854
855         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
856         gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
857         gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
858                 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
859
860         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
861
862         button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
863         g_signal_connect(button, "clicked", G_CALLBACK(&button_clicked), list);
864         gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0);
865
866         devices = get_devices();
867         n_listed = 0;
868         if(devices)
869         {
870                 /* Populate the list with devices in appropriate state. */
871                 latest = 0;
872                 for(i=0; devices[i].node; ++i)
873                         if(!devices[i].mounted==!umount)
874                         {
875                                 gtk_list_store_append(store, &iter);
876                                 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
877                                 if(devices[i].time>latest)
878                                 {
879                                         /* Pre-select the device that appeared on the system most recently. */
880                                         latest = devices[i].time;
881                                         gtk_tree_selection_select_iter(selection, &iter);
882                                 }
883
884                                 ++n_listed;
885                         }
886
887         }
888
889         if(n_listed)
890                 gtk_widget_show_all(window);
891         else
892         {
893                 GtkWidget *dialog;
894
895                 /* Don't show the main window if no devices were listed. */
896                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
897                         "No devices to %s", (umount ? "unmount" : "mount"));
898                 g_signal_connect(dialog, "response", G_CALLBACK(&gtk_main_quit), NULL);
899                 gtk_widget_show_all(dialog);
900         }
901
902         gtk_main();
903
904         free_devices(devices);
905
906         return 0;
907 }