]> git.tdb.fi Git - pmount-gui.git/blob - main.c
Use te device mounted flag to decide what to do
[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;
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         umount = !!device->mounted;
641
642         pipe(pipe_fd);
643
644         pid = fork();
645         if(pid==0)
646         {
647                 /* Child process */
648                 if(verbosity>=1)
649                 {
650                         if(umount)
651                                 printf("Running pumount %s\n", device->node);
652                         else
653                                 printf("Running pmount %s %s\n", device->node, device->label);
654                 }
655
656                 close(pipe_fd[0]);
657                 dup2(pipe_fd[1], 1);
658                 dup2(pipe_fd[1], 2);
659
660                 if(umount)
661                         execl("/usr/bin/pumount", "pumount", device->node, NULL);
662                 else
663                         execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
664                 _exit(1);
665         }
666         else if(pid<0)
667                 return;
668
669         /* Parent process */
670
671         close(pipe_fd[1]);
672         FD_ZERO(&fds);
673         FD_SET(pipe_fd[0], &fds);
674         timeout.tv_sec = 0;
675         timeout.tv_usec = 200000;
676
677         while(1)
678         {
679                 /* The write fd for the pipe may be inherited by a fuse server
680                 process and stay open indefinitely. */
681                 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
682                 {
683                         int len;
684
685                         len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
686                         if(len<=0)
687                                 break;
688                         pos += len;
689                 }
690                 else if(waitpid(pid, &status, 0))
691                 {
692                         pid = 0;
693                         break;
694                 }
695         }
696
697         close(pipe_fd[0]);
698         if(pid)
699                 waitpid(pid, &status, 0);
700
701         buf[pos] = 0;
702
703         if(verbosity>=1)
704         {
705                 if(WIFEXITED(status))
706                 {
707                         if(WEXITSTATUS(status))
708                                 printf("Command exited successfully\n");
709                         else
710                                 printf("Command exited with status %d\n", WEXITSTATUS(status));
711                 }
712                 else if(WIFSIGNALED(status))
713                         printf("Command terminated with signal %d\n", WTERMSIG(status));
714                 else
715                         printf("Command exited with unknown result %04X\n", status);
716         }
717
718         if(!WIFEXITED(status) || WEXITSTATUS(status))
719         {
720                 GtkWidget *dialog;
721
722                 /* Pmount terminated with nonzero status or a signal.  Display an
723                 error to the user. */
724                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
725                 g_signal_connect(dialog, "response", &gtk_main_quit, NULL);
726                 gtk_widget_show_all(dialog);
727                 return;
728         }
729
730         gtk_main_quit();
731
732         if(post_mount_command && !umount)
733         {
734                 char workdir[256];
735                 int len;
736                 len = snprintf(workdir, sizeof(workdir), "/media/%s", device->label);
737                 if(len<(int)sizeof(workdir))
738                 {
739                         if(verbosity>=1)
740                                 printf("Running %s in %s\n", post_mount_command, workdir);
741
742                         pid = fork();
743                         if(pid==0)
744                         {
745                                 chdir(workdir);
746                                 execlp(post_mount_command, post_mount_command, NULL);
747                                 _exit(1);
748                         }
749                 }
750         }
751
752         (void)column;
753         (void)user_data;
754 }
755
756 /**
757 Callback for the mount/unmount button.  Causes the selected row in the device
758 list to be activated.
759 */
760 void button_clicked(GtkButton *button, gpointer user_data)
761 {
762         GtkWidget *list = (GtkWidget *)user_data;
763         GtkTreeSelection *selection;
764         GtkTreeIter iter;
765         GtkTreeModel *model;
766         GtkTreePath *path;
767
768         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
769         gtk_tree_selection_get_selected(selection, &model, &iter);
770         path = gtk_tree_model_get_path(model, &iter);
771         gtk_tree_view_row_activated(GTK_TREE_VIEW(list), path, gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0));
772         gtk_tree_path_free(path);
773
774         (void)button;
775 }
776
777 /**
778 Global key press callback for the window.
779 */
780 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
781 {
782         if(event->key.keyval==GDK_KEY_Escape)
783         {
784                 gtk_main_quit();
785                 return TRUE;
786         }
787
788         (void)widget;
789         (void)user_data;
790
791         return FALSE;
792 }
793
794 void show_help(void)
795 {
796         printf("pmount-gui\n"
797                 "Copyright (c) 2011-2015 Mikko Rasa, Mikkosoft Productions\n\n"
798                 "Usage: pmount-gui [-v] [-u] [-r <command>] [-h]\n\n"
799                 "Options:\n"
800                 "  -v  Increase verbosity\n"
801                 "  -u  Unmount a device (default is mount)\n"
802                 "  -r  Run a command after mounting\n"
803                 "  -h  Display this help\n");
804 }
805
806 int main(int argc, char **argv)
807 {
808         GtkWidget *window;
809         GtkWidget *box;
810         GtkWidget *viewport;
811         GtkWidget *list;
812         GtkListStore *store;
813         GtkTreeSelection *selection;
814         GtkWidget *button;
815         GtkTreeIter iter;
816         Device *devices;
817         int i;
818         time_t latest;
819         int opt;
820         int umount = 0;
821         int n_listed;
822
823         gtk_init(&argc, &argv);
824
825         while((opt = getopt(argc, argv, "vur:h"))!=-1) switch(opt)
826         {
827         case 'v':
828                 ++verbosity;
829                 break;
830         case 'u':
831                 umount = 1;
832                 break;
833         case 'r':
834                 post_mount_command = optarg;
835                 break;
836         case 'h':
837                 show_help();
838                 return 0;
839         }
840
841         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
842         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
843         g_signal_connect(window, "destroy", G_CALLBACK(&gtk_main_quit), NULL);
844         g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
845
846         box = gtk_vbox_new(FALSE, 5);
847         gtk_container_add(GTK_CONTAINER(window), box);
848
849         viewport = gtk_viewport_new(NULL, NULL);
850         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
851         gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
852
853         list = gtk_tree_view_new();
854         gtk_container_add(GTK_CONTAINER(viewport), list);
855         g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), NULL);
856
857         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
858         gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
859         gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
860                 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
861
862         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
863
864         button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
865         g_signal_connect(button, "clicked", G_CALLBACK(&button_clicked), list);
866         gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0);
867
868         devices = get_devices();
869         n_listed = 0;
870         if(devices)
871         {
872                 /* Populate the list with devices in appropriate state. */
873                 latest = 0;
874                 for(i=0; devices[i].node; ++i)
875                         if(!devices[i].mounted==!umount)
876                         {
877                                 gtk_list_store_append(store, &iter);
878                                 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
879                                 if(devices[i].time>latest)
880                                 {
881                                         /* Pre-select the device that appeared on the system most recently. */
882                                         latest = devices[i].time;
883                                         gtk_tree_selection_select_iter(selection, &iter);
884                                 }
885
886                                 ++n_listed;
887                         }
888
889         }
890
891         if(n_listed)
892                 gtk_widget_show_all(window);
893         else
894         {
895                 GtkWidget *dialog;
896
897                 /* Don't show the main window if no devices were listed. */
898                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
899                         "No devices to %s", (umount ? "unmount" : "mount"));
900                 g_signal_connect(dialog, "response", G_CALLBACK(&gtk_main_quit), NULL);
901                 gtk_widget_show_all(dialog);
902         }
903
904         gtk_main();
905
906         free_devices(devices);
907
908         return 0;
909 }