teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

修正ミス

2020/04/28 16:06

投稿

katsuko
katsuko

スコア3601

answer CHANGED
@@ -1,3 +1,64 @@
1
+ GIOライブラリのGVolumeMonitorで、ある程度の事はできるのではないでしょうか。
2
+
3
+ * [GVolumeMonitor - GIO Reference Manual](https://developer.gnome.org/gio/stable/GVolumeMonitor.html)
4
+
5
+ そもそも「USBメモリ」の判別は難しいと思います。
6
+ (おそらくDVDやUSBハードディスクも引っかかってしまうけど)「エジェクトできるか」で判断するとかで妥協するとか。
7
+
8
+ 「プログラム」としか書かれてなく何の言語化はわかりませんが、GIOライブラリはGObjectIntrospectionに対応しているので、大抵の言語でも使えると思います。
9
+
10
+ * [Bindings based on GObject-Introspection](https://gi.readthedocs.io/en/latest/users.html)
11
+
12
+ 例えば、C言語なら。
13
+
14
+ ```c
15
+ #include <gio/gio.h>
16
+ #include <stdio.h>
17
+
18
+ int main()
19
+ {
20
+ GVolumeMonitor *mon;
21
+ GList *mounts;
22
+ GList *l;
23
+ char *mount_name;
24
+ GFile *root;
25
+ char *root_path;
26
+
27
+ mon = g_volume_monitor_get();
28
+ mounts = g_volume_monitor_get_mounts(mon);
29
+
30
+ for (l = mounts; l != NULL; l = l->next) {
31
+ mount_name = g_mount_get_name(G_MOUNT(l->data));
32
+ root = g_mount_get_root(G_MOUNT(l->data));
33
+ root_path = g_file_get_path(root);
34
+ printf("%s, %s, %s.\n", mount_name, g_mount_can_eject(G_MOUNT(l->data))? "ejectable": "unejectable", root_path);
35
+ g_free(root_path);
36
+ g_object_unref(root);
37
+ g_free(mount_name);
38
+ }
39
+
40
+ g_list_free_full(mounts, (GDestroyNotify)g_object_unref);
41
+ g_object_unref(G_OBJECT(mon));
42
+
43
+ return 0;
44
+ }
45
+
46
+ ```
47
+
48
+ Pythonなら。
49
+
50
+ ```python
51
+ # coding: utf-8
52
+
53
+ from gi.repository import Gio
54
+
55
+ mon = Gio.VolumeMonitor.get()
56
+ for m in mon.get_mounts():
57
+ d = m.get_drive()
58
+ 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())))
59
+
60
+ ```
61
+
1
62
  ---
2
63
  と書いてから、「USBメモリーを差し込む場所はいつも同じ場所で」という点に気づきました。
3
64
  指す場所が一緒ならば、Unixのデバイス名も一緒なのでは?(ちょっと自信ない)

1

デバイス名の取得

2020/04/28 16:06

投稿

katsuko
katsuko

スコア3601

answer CHANGED
@@ -1,64 +1,3 @@
1
- GIOライブラリのGVolumeMonitorで、ある程度の事はできるのではないでしょうか。
2
-
3
- * [GVolumeMonitor - GIO Reference Manual](https://developer.gnome.org/gio/stable/GVolumeMonitor.html)
4
-
5
- そもそも「USBメモリ」の判別は難しいと思います。
6
- (おそらくDVDやUSBハードディスクも引っかかってしまうけど)「エジェクトできるか」で判断するとかで妥協するとか。
7
-
8
- 「プログラム」としか書かれてなく何の言語化はわかりませんが、GIOライブラリはGObjectIntrospectionに対応しているので、大抵の言語でも使えると思います。
9
-
10
- * [Bindings based on GObject-Introspection](https://gi.readthedocs.io/en/latest/users.html)
11
-
12
- 例えば、C言語なら。
13
-
14
- ```c
15
- #include <gio/gio.h>
16
- #include <stdio.h>
17
-
18
- int main()
19
- {
20
- GVolumeMonitor *mon;
21
- GList *mounts;
22
- GList *l;
23
- char *mount_name;
24
- GFile *root;
25
- char *root_path;
26
-
27
- mon = g_volume_monitor_get();
28
- mounts = g_volume_monitor_get_mounts(mon);
29
-
30
- for (l = mounts; l != NULL; l = l->next) {
31
- mount_name = g_mount_get_name(G_MOUNT(l->data));
32
- root = g_mount_get_root(G_MOUNT(l->data));
33
- root_path = g_file_get_path(root);
34
- printf("%s, %s, %s.\n", mount_name, g_mount_can_eject(G_MOUNT(l->data))? "ejectable": "unejectable", root_path);
35
- g_free(root_path);
36
- g_object_unref(root);
37
- g_free(mount_name);
38
- }
39
-
40
- g_list_free_full(mounts, (GDestroyNotify)g_object_unref);
41
- g_object_unref(G_OBJECT(mon));
42
-
43
- return 0;
44
- }
45
-
46
- ```
47
-
48
- Pythonなら。
49
-
50
- ```python
51
- # coding: utf-8
52
-
53
- from gi.repository import Gio
54
-
55
- mon = Gio.VolumeMonitor.get()
56
- for m in mon.get_mounts():
57
- d = m.get_drive()
58
- 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())))
59
-
60
- ```
61
-
62
1
  ---
63
2
  と書いてから、「USBメモリーを差し込む場所はいつも同じ場所で」という点に気づきました。
64
3
  指す場所が一緒ならば、Unixのデバイス名も一緒なのでは?(ちょっと自信ない)