回答編集履歴

2

修正ミス

2020/04/28 16:06

投稿

katsuko
katsuko

スコア3471

test CHANGED
@@ -1,3 +1,125 @@
1
+ GIOライブラリのGVolumeMonitorで、ある程度の事はできるのではないでしょうか。
2
+
3
+
4
+
5
+ * [GVolumeMonitor - GIO Reference Manual](https://developer.gnome.org/gio/stable/GVolumeMonitor.html)
6
+
7
+
8
+
9
+ そもそも「USBメモリ」の判別は難しいと思います。
10
+
11
+ (おそらくDVDやUSBハードディスクも引っかかってしまうけど)「エジェクトできるか」で判断するとかで妥協するとか。
12
+
13
+
14
+
15
+ 「プログラム」としか書かれてなく何の言語化はわかりませんが、GIOライブラリはGObjectIntrospectionに対応しているので、大抵の言語でも使えると思います。
16
+
17
+
18
+
19
+ * [Bindings based on GObject-Introspection](https://gi.readthedocs.io/en/latest/users.html)
20
+
21
+
22
+
23
+ 例えば、C言語なら。
24
+
25
+
26
+
27
+ ```c
28
+
29
+ #include <gio/gio.h>
30
+
31
+ #include <stdio.h>
32
+
33
+
34
+
35
+ int main()
36
+
37
+ {
38
+
39
+ GVolumeMonitor *mon;
40
+
41
+ GList *mounts;
42
+
43
+ GList *l;
44
+
45
+ char *mount_name;
46
+
47
+ GFile *root;
48
+
49
+ char *root_path;
50
+
51
+
52
+
53
+ mon = g_volume_monitor_get();
54
+
55
+ mounts = g_volume_monitor_get_mounts(mon);
56
+
57
+
58
+
59
+ for (l = mounts; l != NULL; l = l->next) {
60
+
61
+ mount_name = g_mount_get_name(G_MOUNT(l->data));
62
+
63
+ root = g_mount_get_root(G_MOUNT(l->data));
64
+
65
+ root_path = g_file_get_path(root);
66
+
67
+ printf("%s, %s, %s.\n", mount_name, g_mount_can_eject(G_MOUNT(l->data))? "ejectable": "unejectable", root_path);
68
+
69
+ g_free(root_path);
70
+
71
+ g_object_unref(root);
72
+
73
+ g_free(mount_name);
74
+
75
+ }
76
+
77
+
78
+
79
+ g_list_free_full(mounts, (GDestroyNotify)g_object_unref);
80
+
81
+ g_object_unref(G_OBJECT(mon));
82
+
83
+
84
+
85
+ return 0;
86
+
87
+ }
88
+
89
+
90
+
91
+ ```
92
+
93
+
94
+
95
+ Pythonなら。
96
+
97
+
98
+
99
+ ```python
100
+
101
+ # coding: utf-8
102
+
103
+
104
+
105
+ from gi.repository import Gio
106
+
107
+
108
+
109
+ mon = Gio.VolumeMonitor.get()
110
+
111
+ for m in mon.get_mounts():
112
+
113
+ d = m.get_drive()
114
+
115
+ print('%s, %s, %s(%s).' % (repr(m.get_name()), repr(m.get_root().get_path()), repr(d.get_name() if d is not None else '(none)'), repr(m.can_eject())))
116
+
117
+
118
+
119
+ ```
120
+
121
+
122
+
1
123
  ---
2
124
 
3
125
  と書いてから、「USBメモリーを差し込む場所はいつも同じ場所で」という点に気づきました。

1

デバイス名の取得

2020/04/28 16:06

投稿

katsuko
katsuko

スコア3471

test CHANGED
@@ -1,125 +1,3 @@
1
- GIOライブラリのGVolumeMonitorで、ある程度の事はできるのではないでしょうか。
2
-
3
-
4
-
5
- * [GVolumeMonitor - GIO Reference Manual](https://developer.gnome.org/gio/stable/GVolumeMonitor.html)
6
-
7
-
8
-
9
- そもそも「USBメモリ」の判別は難しいと思います。
10
-
11
- (おそらくDVDやUSBハードディスクも引っかかってしまうけど)「エジェクトできるか」で判断するとかで妥協するとか。
12
-
13
-
14
-
15
- 「プログラム」としか書かれてなく何の言語化はわかりませんが、GIOライブラリはGObjectIntrospectionに対応しているので、大抵の言語でも使えると思います。
16
-
17
-
18
-
19
- * [Bindings based on GObject-Introspection](https://gi.readthedocs.io/en/latest/users.html)
20
-
21
-
22
-
23
- 例えば、C言語なら。
24
-
25
-
26
-
27
- ```c
28
-
29
- #include <gio/gio.h>
30
-
31
- #include <stdio.h>
32
-
33
-
34
-
35
- int main()
36
-
37
- {
38
-
39
- GVolumeMonitor *mon;
40
-
41
- GList *mounts;
42
-
43
- GList *l;
44
-
45
- char *mount_name;
46
-
47
- GFile *root;
48
-
49
- char *root_path;
50
-
51
-
52
-
53
- mon = g_volume_monitor_get();
54
-
55
- mounts = g_volume_monitor_get_mounts(mon);
56
-
57
-
58
-
59
- for (l = mounts; l != NULL; l = l->next) {
60
-
61
- mount_name = g_mount_get_name(G_MOUNT(l->data));
62
-
63
- root = g_mount_get_root(G_MOUNT(l->data));
64
-
65
- root_path = g_file_get_path(root);
66
-
67
- printf("%s, %s, %s.\n", mount_name, g_mount_can_eject(G_MOUNT(l->data))? "ejectable": "unejectable", root_path);
68
-
69
- g_free(root_path);
70
-
71
- g_object_unref(root);
72
-
73
- g_free(mount_name);
74
-
75
- }
76
-
77
-
78
-
79
- g_list_free_full(mounts, (GDestroyNotify)g_object_unref);
80
-
81
- g_object_unref(G_OBJECT(mon));
82
-
83
-
84
-
85
- return 0;
86
-
87
- }
88
-
89
-
90
-
91
- ```
92
-
93
-
94
-
95
- Pythonなら。
96
-
97
-
98
-
99
- ```python
100
-
101
- # coding: utf-8
102
-
103
-
104
-
105
- from gi.repository import Gio
106
-
107
-
108
-
109
- mon = Gio.VolumeMonitor.get()
110
-
111
- for m in mon.get_mounts():
112
-
113
- d = m.get_drive()
114
-
115
- print('%s, %s, %s(%s).' % (repr(m.get_name()), repr(m.get_root().get_path()), repr(d.get_name() if d is not None else '(none)'), repr(m.can_eject())))
116
-
117
-
118
-
119
- ```
120
-
121
-
122
-
123
1
  ---
124
2
 
125
3
  と書いてから、「USBメモリーを差し込む場所はいつも同じ場所で」という点に気づきました。