10 typedef struct sProperty
16 typedef struct sDevice
27 int parse_property(char *str, int size, Property *prop)
32 for(i=0; (equals<0 && i<size); ++i)
39 prop->name = malloc(equals+1);
40 strncpy(prop->name, str, equals);
41 prop->name[equals] = 0;
43 prop->value = malloc(size-equals);
44 strncpy(prop->value, str+equals+1, size-equals-1);
45 prop->value[size-equals-1] = 0;
50 Property *get_device_properties(char *node)
61 printf("Running udevadm info -q property -n \"%s\"\n", node);
66 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
74 Property *props = NULL;
89 len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos);
98 for(i=0; (newline<0 && i<pos); ++i)
105 if(parse_property(buf, newline, &prop)==0)
107 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
108 props[n_props] = prop;
111 memmove(buf, buf+newline+1, pos-newline-1);
120 props[n_props].name = NULL;
121 props[n_props].value = NULL;
124 waitpid(pid, NULL, 0);
138 char *get_property_value(Property *props, char *name)
141 for(i=0; props[i].name; ++i)
142 if(strcmp(props[i].name, name)==0)
143 return props[i].value;
147 int match_property_value(Property *props, char *name, char *value)
149 char *v = get_property_value(props, name);
152 return strcmp(v, value)==0;
155 void free_properties(Property *props)
160 for(i=0; props[i].name; ++i)
163 free(props[i].value);
168 char **get_mounted_devices(void)
172 char **mounted = NULL;
175 mtab = setmntent("/etc/mtab", "r");
179 while((me = getmntent(mtab)))
181 mounted = (char **)realloc(mounted, (n_mounted+2)*sizeof(char *));
182 mounted[n_mounted] = strdup(me->mnt_fsname);
187 mounted[n_mounted] = NULL;
192 int is_mounted(char **mounted, char *devname)
195 for(i=0; mounted[i]; ++i)
196 if(!strcmp(devname, mounted[i]))
201 void free_mounted_devices(char **mounted)
206 for(i=0; mounted[i]; ++i)
211 Device *get_devices(void)
216 Device *devices = NULL;
218 char **mounted = NULL;
220 dir = opendir("/dev/disk/by-id");
224 mounted = get_mounted_devices();
226 while((de = readdir(dir)))
228 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
231 snprintf(fnbuf, sizeof(fnbuf), "/dev/disk/by-id/%s", de->d_name);
233 printf("Examining device %s\n", fnbuf);
235 Property *props = get_device_properties(fnbuf);
239 printf(" No properties\n");
246 for(i=0; props[i].name; ++i)
247 printf(" %s = %s\n", props[i].name, props[i].value);
250 if(match_property_value(props, "ID_BUS", "usb") && match_property_value(props, "DEVTYPE", "partition"))
261 printf(" Using device\n");
263 devname = get_property_value(props, "DEVNAME");
265 label = get_property_value(props, "ID_FS_LABEL");
267 label = get_property_value(props, "ID_FS_UUID");
273 for(ptr=label; *ptr; ++ptr)
278 vendor = get_property_value(props, "ID_VENDOR");
279 model = get_property_value(props, "ID_MODEL");
281 pos = snprintf(buf, sizeof(buf), "%s", label);
283 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
287 devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
288 devices[n_devices].node = strdup(fnbuf);
289 devices[n_devices].label = strdup(label);
290 devices[n_devices].description = strdup(buf);
291 devices[n_devices].mounted = is_mounted(mounted, devname);
292 devices[n_devices].time = st.st_mtime;
295 free_properties(props);
299 free_mounted_devices(mounted);
303 devices[n_devices].node = NULL;
304 devices[n_devices].label = NULL;
305 devices[n_devices].description = NULL;
311 void free_devices(Device *devices)
316 for(i=0; devices[i].node; ++i)
318 free(devices[i].node);
319 free(devices[i].label);
324 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
328 int umount = *(int *)user_data;
330 model = gtk_tree_view_get_model(list);
332 if(gtk_tree_model_get_iter(model, &iter, path))
338 gtk_tree_model_get(model, &iter, 1, &device, -1);
348 printf("Running pumount %s\n", device->node);
350 printf("Running pmount %s %s\n", device->node, device->label);
358 execl("/usr/bin/pumount", "pumount", device->node, NULL);
360 execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
375 len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
383 waitpid(pid, &status, 0);
384 if(!WIFEXITED(status) || WEXITSTATUS(status))
388 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
389 g_signal_connect(dialog, "response", >k_main_quit, NULL);
390 gtk_widget_show_all(dialog);
403 int main(int argc, char **argv)
409 GtkTreeSelection *selection;
418 gtk_init(&argc, &argv);
420 while((opt = getopt(argc, argv, "vu"))!=-1) switch(opt)
430 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
431 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
432 g_signal_connect(window, "destroy", >k_main_quit, NULL);
434 viewport = gtk_viewport_new(NULL, NULL);
435 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
436 gtk_container_add(GTK_CONTAINER(window), viewport);
438 list = gtk_tree_view_new();
439 gtk_container_add(GTK_CONTAINER(viewport), list);
440 g_signal_connect(list, "row-activated", (GCallback)&row_activated, &umount);
442 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
443 gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
444 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
445 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
447 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
449 devices = get_devices();
454 for(i=0; devices[i].node; ++i)
455 if(!devices[i].mounted==!umount)
457 gtk_list_store_append(store, &iter);
458 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
459 if(devices[i].time>latest)
461 latest = devices[i].time;
462 gtk_tree_selection_select_iter(selection, &iter);
471 gtk_widget_show_all(window);
476 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
477 "No devices to %s", (umount ? "unmount" : "mount"));
478 g_signal_connect(dialog, "response", >k_main_quit, NULL);
479 gtk_widget_show_all(dialog);
484 free_devices(devices);