-
Notifications
You must be signed in to change notification settings - Fork 3
/
probe-usb-devices.c
122 lines (105 loc) · 4.33 KB
/
probe-usb-devices.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* Copyright (C) 2017 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <string.h>
#include <udisks/udisks.h>
static UDisksClient *client = NULL;
static GMainLoop *loop = NULL;
int main(int argc, char **argv)
{
GError *error;
GList *objects;
GList *l, *m;
UDisksBlock *block;
UDisksDrive *drive;
UDisksFilesystem *fs;
/* Get a new client */
error = NULL;
client = udisks_client_new_sync(NULL, &error);
if (client == NULL)
{
g_printerr("Error connecting to the udisks daemon: %s\n", error->message);
g_error_free(error);
}
/* Get the list of all objects managed by UDisks */
objects = g_dbus_object_manager_get_objects(udisks_client_get_object_manager(client));
for (l = objects; l != NULL; l = l->next)
{
UDisksObject *object = UDISKS_OBJECT (l->data);
block = udisks_object_peek_block(object);
if (block != NULL)
{
const gchar *device, *drive_str;
const gchar *id, *label;
const gchar * const *symlinks;
device = udisks_block_get_device(block);
if (device == NULL)
continue;
/* Only print block devices backed by a drive */
drive_str = udisks_block_get_drive(block);
if (drive_str == NULL || g_strcmp0(drive_str, "/") == 0)
continue;
/* If udisks says to ignore a block device then do so */
if (udisks_block_get_hint_ignore(block))
continue;
/* Exclude system disks */
if (udisks_block_get_hint_system(block))
continue;
/* Check if there's a USB-backed drive associated with this
* object
*/
for (m = objects; m != NULL; m = m->next)
{
UDisksObject *drive_obj = UDISKS_OBJECT(m->data);
drive = udisks_object_peek_drive(drive_obj);
if (drive != NULL)
{
/* We're only interested in removable USB drives */
const gchar *bus = udisks_drive_get_connection_bus(drive);
gboolean removable = udisks_drive_get_removable(drive);
if (!removable || g_strcmp0(bus, "usb") != 0)
continue;
/* Print the device details */
g_print("device path : %s\n", device);
g_print("device drive: %s\n", drive_str);
/* Print the device ID */
id = udisks_block_get_id(block);
if (id && strlen(id))
g_print("device id : %s\n", id);
/* Print the device label */
label = udisks_block_get_id_label(block);
if (label && strlen(label))
g_print("device label: %s\n", label);
/* Print any mount points associated withthe block device */
fs = udisks_object_peek_filesystem(object);
if (fs != NULL) {
const gchar * const *mount_points;
mount_points = udisks_filesystem_get_mount_points(fs);
if (mount_points[0] != NULL)
g_print("device mount points: \n");
for (guint n = 0; mount_points != NULL & mount_points[n] != NULL; n++)
g_print("%s \n", mount_points[n]);
}
g_print("---\n");
}
}
}
}
g_list_foreach(objects, (GFunc)g_object_unref, NULL);
g_list_free(objects);
return 0;
}