/
vfs-volume-nohal.c
4731 lines (4311 loc) · 162 KB
/
vfs-volume-nohal.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SpaceFM vfs-volume-nohal.c
*
* Copyright (C) 2014 IgnorantGuru <ignorantguru@gmx.com>
* Copyright (C) 2006 Hong Jen Yee (PCMan) <pcman.tw (AT) gmail.com>
*
* License: See COPYING file
*
* udev & mount monitor code by IgnorantGuru
* device info code uses code excerpts from freedesktop's udisks v1.0.4
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "vfs-volume.h"
#include <glib.h>
#include <glib/gstdio.h>
#include "glib-mem.h"
#include <glib/gi18n.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
// udev
#include <libudev.h>
#include <fcntl.h>
#include <errno.h>
// waitpid
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h> // kill
#include <linux/kdev_t.h> // MAJOR MINOR
#include <sys/sysmacros.h> // needed for dev_t
#ifdef HAVE_STATVFS
#include <sys/statvfs.h>
#endif
#include <vfs-file-info.h>
#include "ptk-file-task.h"
#include "main-window.h"
#include "ptk-handler.h"
#include "ptk-location-view.h"
#define MOUNTINFO "/proc/self/mountinfo"
#define MTAB "/proc/mounts"
#define HIDDEN_NON_BLOCK_FS "devpts proc fusectl pstore sysfs tmpfs devtmpfs ramfs aufs overlayfs cgroup binfmt_misc rpc_pipefs fuse.gvfsd-fuse"
void vfs_volume_monitor_start();
VFSVolume* vfs_volume_read_by_device( struct udev_device *udevice );
VFSVolume* vfs_volume_read_by_mount( dev_t devnum, const char* mount_points );
static void vfs_volume_device_added ( VFSVolume* volume, gboolean automount );
static void vfs_volume_device_removed ( struct udev_device* udevice );
static gboolean vfs_volume_nonblock_removed( dev_t devnum );
static void call_callbacks( VFSVolume* vol, VFSVolumeState state );
void unmount_if_mounted( VFSVolume* vol );
typedef struct _VFSVolumeCallbackData
{
VFSVolumeCallback cb;
gpointer user_data;
}VFSVolumeCallbackData;
static GList* volumes = NULL;
static GArray* callbacks = NULL;
GPid monpid = 0;
gboolean global_inhibit_auto = FALSE;
typedef struct devmount_t {
guint major;
guint minor;
char* mount_points;
char* fstype;
GList* mounts;
} devmount_t;
GList* devmounts = NULL;
struct udev *udev = NULL;
struct udev_monitor *umonitor = NULL;
GIOChannel* uchannel = NULL;
GIOChannel* mchannel = NULL;
/* *************************************************************************
* device info
************************************************************************** */
typedef struct device_t {
struct udev_device *udevice;
dev_t devnum;
char *devnode;
char *native_path;
char *mount_points;
gboolean device_is_system_internal;
gboolean device_is_partition;
gboolean device_is_partition_table;
gboolean device_is_removable;
gboolean device_is_media_available;
gboolean device_is_read_only;
gboolean device_is_drive;
gboolean device_is_optical_disc;
gboolean device_is_mounted;
char *device_presentation_hide;
char *device_presentation_nopolicy;
char *device_presentation_name;
char *device_presentation_icon_name;
char *device_automount_hint;
char *device_by_id;
guint64 device_size;
guint64 device_block_size;
char *id_usage;
char *id_type;
char *id_version;
char *id_uuid;
char *id_label;
char *drive_vendor;
char *drive_model;
char *drive_revision;
char *drive_serial;
char *drive_wwn;
char *drive_connection_interface;
guint64 drive_connection_speed;
char *drive_media_compatibility;
char *drive_media;
gboolean drive_is_media_ejectable;
gboolean drive_can_detach;
char *partition_scheme;
char *partition_number;
char *partition_type;
char *partition_label;
char *partition_uuid;
char *partition_flags;
char *partition_offset;
char *partition_size;
char *partition_alignment_offset;
char *partition_table_scheme;
char *partition_table_count;
gboolean optical_disc_is_blank;
gboolean optical_disc_is_appendable;
gboolean optical_disc_is_closed;
char *optical_disc_num_tracks;
char *optical_disc_num_audio_tracks;
char *optical_disc_num_sessions;
} device_t;
static char *
_dupv8 (const char *s)
{
const char *end_valid;
if (!g_utf8_validate (s, -1, &end_valid))
{
g_print ("**** NOTE: The string '%s' is not valid UTF-8. Invalid characters begins at '%s'\n", s, end_valid);
return g_strndup (s, end_valid - s);
}
else
{
return g_strdup (s);
}
}
/* unescapes things like \x20 to " " and ensures the returned string is valid UTF-8.
*
* see volume_id_encode_string() in extras/volume_id/lib/volume_id.c in the
* udev tree for the encoder
*/
static gchar *
decode_udev_encoded_string (const gchar *str)
{
GString *s;
gchar *ret;
const gchar *end_valid;
guint n;
s = g_string_new (NULL);
for (n = 0; str[n] != '\0'; n++)
{
if (str[n] == '\\')
{
gint val;
if (str[n + 1] != 'x' || str[n + 2] == '\0' || str[n + 3] == '\0')
{
g_print ("**** NOTE: malformed encoded string '%s'\n", str);
break;
}
val = (g_ascii_xdigit_value (str[n + 2]) << 4) | g_ascii_xdigit_value (str[n + 3]);
g_string_append_c (s, val);
n += 3;
}
else
{
g_string_append_c (s, str[n]);
}
}
if (!g_utf8_validate (s->str, -1, &end_valid))
{
g_print ("**** NOTE: The string '%s' is not valid UTF-8. Invalid characters begins at '%s'\n", s->str, end_valid);
ret = g_strndup (s->str, end_valid - s->str);
g_string_free (s, TRUE);
}
else
{
ret = g_string_free (s, FALSE);
}
return ret;
}
static gint
ptr_str_array_compare (const gchar **a,
const gchar **b)
{
return g_strcmp0 (*a, *b);
}
static double
sysfs_get_double (const char *dir,
const char *attribute)
{
double result;
char *contents;
char *filename;
result = 0.0;
filename = g_build_filename (dir, attribute, NULL);
if (g_file_get_contents (filename, &contents, NULL, NULL))
{
result = atof (contents);
g_free (contents);
}
g_free (filename);
return result;
}
static char *
sysfs_get_string (const char *dir,
const char *attribute)
{
char *result;
char *filename;
result = NULL;
filename = g_build_filename (dir, attribute, NULL);
if (!g_file_get_contents (filename, &result, NULL, NULL))
{
result = g_strdup ("");
}
g_free (filename);
return result;
}
static int
sysfs_get_int (const char *dir,
const char *attribute)
{
int result;
char *contents;
char *filename;
result = 0;
filename = g_build_filename (dir, attribute, NULL);
if (g_file_get_contents (filename, &contents, NULL, NULL))
{
result = strtol (contents, NULL, 0);
g_free (contents);
}
g_free (filename);
return result;
}
static guint64
sysfs_get_uint64 (const char *dir,
const char *attribute)
{
guint64 result;
char *contents;
char *filename;
result = 0;
filename = g_build_filename (dir, attribute, NULL);
if (g_file_get_contents (filename, &contents, NULL, NULL))
{
result = strtoll (contents, NULL, 0);
g_free (contents);
}
g_free (filename);
return result;
}
static gboolean
sysfs_file_exists (const char *dir,
const char *attribute)
{
gboolean result;
char *filename;
result = FALSE;
filename = g_build_filename (dir, attribute, NULL);
if (g_file_test (filename, G_FILE_TEST_EXISTS))
{
result = TRUE;
}
g_free (filename);
return result;
}
static char *
sysfs_resolve_link (const char *sysfs_path,
const char *name)
{
char *full_path;
char link_path[PATH_MAX];
char resolved_path[PATH_MAX];
ssize_t num;
gboolean found_it;
found_it = FALSE;
full_path = g_build_filename (sysfs_path, name, NULL);
//g_debug ("name='%s'", name);
//g_debug ("full_path='%s'", full_path);
num = readlink (full_path, link_path, sizeof(link_path) - 1);
if (num != -1)
{
char *absolute_path;
link_path[num] = '\0';
//g_debug ("link_path='%s'", link_path);
absolute_path = g_build_filename (sysfs_path, link_path, NULL);
//g_debug ("absolute_path='%s'", absolute_path);
if (realpath (absolute_path, resolved_path) != NULL)
{
//g_debug ("resolved_path='%s'", resolved_path);
found_it = TRUE;
}
g_free (absolute_path);
}
g_free (full_path);
if (found_it)
return g_strdup (resolved_path);
else
return NULL;
}
gboolean info_is_system_internal( device_t *device )
{
const char *value;
if ( value = udev_device_get_property_value( device->udevice, "UDISKS_SYSTEM_INTERNAL" ) )
return atoi( value ) != 0;
/* A Linux MD device is system internal if, and only if
*
* - a single component is system internal
* - there are no components
* SKIP THIS TEST
*/
/* a partition is system internal only if the drive it belongs to is system internal */
//TODO
/* a LUKS cleartext device is system internal only if the underlying crypto-text
* device is system internal
* SKIP THIS TEST
*/
// devices with removable media are never system internal
if ( device->device_is_removable )
return FALSE;
/* devices on certain buses are never system internal */
if ( device->drive_connection_interface != NULL )
{
if (strcmp (device->drive_connection_interface, "ata_serial_esata") == 0
|| strcmp (device->drive_connection_interface, "sdio") == 0
|| strcmp (device->drive_connection_interface, "usb") == 0
|| strcmp (device->drive_connection_interface, "firewire") == 0)
return FALSE;
}
return TRUE;
}
void info_drive_connection( device_t *device )
{
char *s;
char *p;
char *q;
char *model;
char *vendor;
char *subsystem;
char *serial;
char *revision;
const char *connection_interface;
guint64 connection_speed;
connection_interface = NULL;
connection_speed = 0;
/* walk up the device tree to figure out the subsystem */
s = g_strdup (device->native_path);
do
{
p = sysfs_resolve_link (s, "subsystem");
if ( !device->device_is_removable && sysfs_get_int( s, "removable") != 0 )
device->device_is_removable = TRUE;
if (p != NULL)
{
subsystem = g_path_get_basename (p);
g_free (p);
if (strcmp (subsystem, "scsi") == 0)
{
connection_interface = "scsi";
connection_speed = 0;
/* continue walking up the chain; we just use scsi as a fallback */
/* grab the names from SCSI since the names from udev currently
* - replaces whitespace with _
* - is missing for e.g. Firewire
*/
vendor = sysfs_get_string (s, "vendor");
if (vendor != NULL)
{
g_strstrip (vendor);
/* Don't overwrite what we set earlier from ID_VENDOR */
if (device->drive_vendor == NULL)
{
device->drive_vendor = _dupv8 (vendor);
}
g_free (vendor);
}
model = sysfs_get_string (s, "model");
if (model != NULL)
{
g_strstrip (model);
/* Don't overwrite what we set earlier from ID_MODEL */
if (device->drive_model == NULL)
{
device->drive_model = _dupv8 (model);
}
g_free (model);
}
/* TODO: need to improve this code; we probably need the kernel to export more
* information before we can properly get the type and speed.
*/
if (device->drive_vendor != NULL && strcmp (device->drive_vendor, "ATA") == 0)
{
connection_interface = "ata";
break;
}
}
else if (strcmp (subsystem, "usb") == 0)
{
double usb_speed;
/* both the interface and the device will be 'usb'. However only
* the device will have the 'speed' property.
*/
usb_speed = sysfs_get_double (s, "speed");
if (usb_speed > 0)
{
connection_interface = "usb";
connection_speed = usb_speed * (1000 * 1000);
break;
}
}
else if (strcmp (subsystem, "firewire") == 0 || strcmp (subsystem, "ieee1394") == 0)
{
/* TODO: krh has promised a speed file in sysfs; theoretically, the speed can
* be anything from 100, 200, 400, 800 and 3200. Till then we just hardcode
* a resonable default of 400 Mbit/s.
*/
connection_interface = "firewire";
connection_speed = 400 * (1000 * 1000);
break;
}
else if (strcmp (subsystem, "mmc") == 0)
{
/* TODO: what about non-SD, e.g. MMC? Is that another bus? */
connection_interface = "sdio";
/* Set vendor name. According to this MMC document
*
* http://www.mmca.org/membership/IAA_Agreement_10_12_06.pdf
*
* - manfid: the manufacturer id
* - oemid: the customer of the manufacturer
*
* Apparently these numbers are kept secret. It would be nice
* to map these into names for setting the manufacturer of the drive,
* e.g. Panasonic, Sandisk etc.
*/
model = sysfs_get_string (s, "name");
if (model != NULL)
{
g_strstrip (model);
/* Don't overwrite what we set earlier from ID_MODEL */
if (device->drive_model == NULL)
{
device->drive_model = _dupv8 (model);
}
g_free (model);
}
serial = sysfs_get_string (s, "serial");
if (serial != NULL)
{
g_strstrip (serial);
/* Don't overwrite what we set earlier from ID_SERIAL */
if (device->drive_serial == NULL)
{
/* this is formatted as a hexnumber; drop the leading 0x */
device->drive_serial = _dupv8 (serial + 2);
}
g_free (serial);
}
/* TODO: use hwrev and fwrev files? */
revision = sysfs_get_string (s, "date");
if (revision != NULL)
{
g_strstrip (revision);
/* Don't overwrite what we set earlier from ID_REVISION */
if (device->drive_revision == NULL)
{
device->drive_revision = _dupv8 (revision);
}
g_free (revision);
}
/* TODO: interface speed; the kernel driver knows; would be nice
* if it could export it */
}
else if (strcmp (subsystem, "platform") == 0)
{
const gchar *sysfs_name;
sysfs_name = g_strrstr (s, "/");
if (g_str_has_prefix (sysfs_name + 1, "floppy.")
&& device->drive_vendor == NULL )
{
device->drive_vendor = g_strdup( "Floppy Drive" );
connection_interface = "platform";
}
}
g_free (subsystem);
}
/* advance up the chain */
p = g_strrstr (s, "/");
if (p == NULL)
break;
*p = '\0';
/* but stop at the root */
if (strcmp (s, "/sys/devices") == 0)
break;
}
while (TRUE);
if (connection_interface != NULL)
{
device->drive_connection_interface = g_strdup( connection_interface );
device->drive_connection_speed = connection_speed;
}
g_free (s);
}
static const struct
{
const char *udev_property;
const char *media_name;
} drive_media_mapping[] =
{
{ "ID_DRIVE_FLASH", "flash" },
{ "ID_DRIVE_FLASH_CF", "flash_cf" },
{ "ID_DRIVE_FLASH_MS", "flash_ms" },
{ "ID_DRIVE_FLASH_SM", "flash_sm" },
{ "ID_DRIVE_FLASH_SD", "flash_sd" },
{ "ID_DRIVE_FLASH_SDHC", "flash_sdhc" },
{ "ID_DRIVE_FLASH_MMC", "flash_mmc" },
{ "ID_DRIVE_FLOPPY", "floppy" },
{ "ID_DRIVE_FLOPPY_ZIP", "floppy_zip" },
{ "ID_DRIVE_FLOPPY_JAZ", "floppy_jaz" },
{ "ID_CDROM", "optical_cd" },
{ "ID_CDROM_CD_R", "optical_cd_r" },
{ "ID_CDROM_CD_RW", "optical_cd_rw" },
{ "ID_CDROM_DVD", "optical_dvd" },
{ "ID_CDROM_DVD_R", "optical_dvd_r" },
{ "ID_CDROM_DVD_RW", "optical_dvd_rw" },
{ "ID_CDROM_DVD_RAM", "optical_dvd_ram" },
{ "ID_CDROM_DVD_PLUS_R", "optical_dvd_plus_r" },
{ "ID_CDROM_DVD_PLUS_RW", "optical_dvd_plus_rw" },
{ "ID_CDROM_DVD_PLUS_R_DL", "optical_dvd_plus_r_dl" },
{ "ID_CDROM_DVD_PLUS_RW_DL", "optical_dvd_plus_rw_dl" },
{ "ID_CDROM_BD", "optical_bd" },
{ "ID_CDROM_BD_R", "optical_bd_r" },
{ "ID_CDROM_BD_RE", "optical_bd_re" },
{ "ID_CDROM_HDDVD", "optical_hddvd" },
{ "ID_CDROM_HDDVD_R", "optical_hddvd_r" },
{ "ID_CDROM_HDDVD_RW", "optical_hddvd_rw" },
{ "ID_CDROM_MO", "optical_mo" },
{ "ID_CDROM_MRW", "optical_mrw" },
{ "ID_CDROM_MRW_W", "optical_mrw_w" },
{ NULL, NULL }, };
static const struct
{
const char *udev_property;
const char *media_name;
} media_mapping[] =
{
{ "ID_DRIVE_MEDIA_FLASH", "flash" },
{ "ID_DRIVE_MEDIA_FLASH_CF", "flash_cf" },
{ "ID_DRIVE_MEDIA_FLASH_MS", "flash_ms" },
{ "ID_DRIVE_MEDIA_FLASH_SM", "flash_sm" },
{ "ID_DRIVE_MEDIA_FLASH_SD", "flash_sd" },
{ "ID_DRIVE_MEDIA_FLASH_SDHC", "flash_sdhc" },
{ "ID_DRIVE_MEDIA_FLASH_MMC", "flash_mmc" },
{ "ID_DRIVE_MEDIA_FLOPPY", "floppy" },
{ "ID_DRIVE_MEDIA_FLOPPY_ZIP", "floppy_zip" },
{ "ID_DRIVE_MEDIA_FLOPPY_JAZ", "floppy_jaz" },
{ "ID_CDROM_MEDIA_CD", "optical_cd" },
{ "ID_CDROM_MEDIA_CD_R", "optical_cd_r" },
{ "ID_CDROM_MEDIA_CD_RW", "optical_cd_rw" },
{ "ID_CDROM_MEDIA_DVD", "optical_dvd" },
{ "ID_CDROM_MEDIA_DVD_R", "optical_dvd_r" },
{ "ID_CDROM_MEDIA_DVD_RW", "optical_dvd_rw" },
{ "ID_CDROM_MEDIA_DVD_RAM", "optical_dvd_ram" },
{ "ID_CDROM_MEDIA_DVD_PLUS_R", "optical_dvd_plus_r" },
{ "ID_CDROM_MEDIA_DVD_PLUS_RW", "optical_dvd_plus_rw" },
{ "ID_CDROM_MEDIA_DVD_PLUS_R_DL", "optical_dvd_plus_r_dl" },
{ "ID_CDROM_MEDIA_DVD_PLUS_RW_DL", "optical_dvd_plus_rw_dl" },
{ "ID_CDROM_MEDIA_BD", "optical_bd" },
{ "ID_CDROM_MEDIA_BD_R", "optical_bd_r" },
{ "ID_CDROM_MEDIA_BD_RE", "optical_bd_re" },
{ "ID_CDROM_MEDIA_HDDVD", "optical_hddvd" },
{ "ID_CDROM_MEDIA_HDDVD_R", "optical_hddvd_r" },
{ "ID_CDROM_MEDIA_HDDVD_RW", "optical_hddvd_rw" },
{ "ID_CDROM_MEDIA_MO", "optical_mo" },
{ "ID_CDROM_MEDIA_MRW", "optical_mrw" },
{ "ID_CDROM_MEDIA_MRW_W", "optical_mrw_w" },
{ NULL, NULL }, };
void info_drive_properties ( device_t *device )
{
GPtrArray *media_compat_array;
const char *media_in_drive;
gboolean drive_is_ejectable;
gboolean drive_can_detach;
char *decoded_string;
guint n;
const char *value;
// drive identification
device->device_is_drive = sysfs_file_exists( device->native_path, "range" );
// vendor
if ( value = udev_device_get_property_value( device->udevice, "ID_VENDOR_ENC" ) )
{
decoded_string = decode_udev_encoded_string ( value );
g_strstrip (decoded_string);
device->drive_vendor = decoded_string;
}
else if ( value = udev_device_get_property_value( device->udevice, "ID_VENDOR" ) )
{
device->drive_vendor = g_strdup( value );
}
// model
if ( value = udev_device_get_property_value( device->udevice, "ID_MODEL_ENC" ) )
{
decoded_string = decode_udev_encoded_string ( value );
g_strstrip (decoded_string);
device->drive_model = decoded_string;
}
else if ( value = udev_device_get_property_value( device->udevice, "ID_MODEL" ) )
{
device->drive_model = g_strdup( value );
}
// revision
device->drive_revision = g_strdup( udev_device_get_property_value(
device->udevice, "ID_REVISION" ) );
// serial
if ( value = udev_device_get_property_value( device->udevice, "ID_SCSI_SERIAL" ) )
{
/* scsi_id sometimes use the WWN as the serial - annoying - see
* http://git.kernel.org/?p=linux/hotplug/udev.git;a=commit;h=4e9fdfccbdd16f0cfdb5c8fa8484a8ba0f2e69d3
* for details
*/
device->drive_serial = g_strdup( value );
}
else if ( value = udev_device_get_property_value( device->udevice, "ID_SERIAL_SHORT" ) )
{
device->drive_serial = g_strdup( value );
}
// wwn
if ( value = udev_device_get_property_value( device->udevice, "ID_WWN_WITH_EXTENSION" ) )
{
device->drive_wwn = g_strdup( value + 2 );
}
else if ( value = udev_device_get_property_value( device->udevice, "ID_WWN" ) )
{
device->drive_wwn = g_strdup( value + 2 );
}
/* pick up some things (vendor, model, connection_interface, connection_speed)
* not (yet) exported by udev helpers
*/
//update_drive_properties_from_sysfs (device);
info_drive_connection( device );
// is_ejectable
if ( value = udev_device_get_property_value( device->udevice, "ID_DRIVE_EJECTABLE" ) )
{
drive_is_ejectable = atoi( value ) != 0;
}
else
{
drive_is_ejectable = FALSE;
drive_is_ejectable |= ( udev_device_get_property_value(
device->udevice, "ID_CDROM" ) != NULL );
drive_is_ejectable |= ( udev_device_get_property_value(
device->udevice, "ID_DRIVE_FLOPPY_ZIP" ) != NULL );
drive_is_ejectable |= ( udev_device_get_property_value(
device->udevice, "ID_DRIVE_FLOPPY_JAZ" ) != NULL );
}
device->drive_is_media_ejectable = drive_is_ejectable;
// drive_media_compatibility
media_compat_array = g_ptr_array_new ();
for (n = 0; drive_media_mapping[n].udev_property != NULL; n++)
{
if ( udev_device_get_property_value( device->udevice,
drive_media_mapping[n].udev_property ) == NULL )
continue;
g_ptr_array_add (media_compat_array, (gpointer) drive_media_mapping[n].media_name);
}
/* special handling for SDIO since we don't yet have a sdio_id helper in udev to set properties */
if (g_strcmp0 (device->drive_connection_interface, "sdio") == 0)
{
gchar *type;
type = sysfs_get_string (device->native_path, "../../type");
g_strstrip (type);
if (g_strcmp0 (type, "MMC") == 0)
{
g_ptr_array_add (media_compat_array, "flash_mmc");
}
else if (g_strcmp0 (type, "SD") == 0)
{
g_ptr_array_add (media_compat_array, "flash_sd");
}
else if (g_strcmp0 (type, "SDHC") == 0)
{
g_ptr_array_add (media_compat_array, "flash_sdhc");
}
g_free (type);
}
g_ptr_array_sort (media_compat_array, (GCompareFunc) ptr_str_array_compare);
g_ptr_array_add (media_compat_array, NULL);
device->drive_media_compatibility = g_strjoinv( " ", (gchar**)media_compat_array->pdata );
// drive_media
media_in_drive = NULL;
if (device->device_is_media_available)
{
for (n = 0; media_mapping[n].udev_property != NULL; n++)
{
if ( udev_device_get_property_value( device->udevice,
media_mapping[n].udev_property ) == NULL )
continue;
// should this be media_mapping[n] ? doesn't matter, same?
media_in_drive = drive_media_mapping[n].media_name;
break;
}
/* If the media isn't set (from e.g. udev rules), just pick the first one in media_compat - note
* that this may be NULL (if we don't know what media is compatible with the drive) which is OK.
*/
if (media_in_drive == NULL)
media_in_drive = ((const gchar **) media_compat_array->pdata)[0];
}
device->drive_media = g_strdup( media_in_drive );
g_ptr_array_free (media_compat_array, TRUE);
// drive_can_detach
// right now, we only offer to detach USB devices
drive_can_detach = FALSE;
if (g_strcmp0 (device->drive_connection_interface, "usb") == 0)
{
drive_can_detach = TRUE;
}
if ( value = udev_device_get_property_value( device->udevice, "ID_DRIVE_DETACHABLE" ) )
{
drive_can_detach = atoi( value ) != 0;
}
device->drive_can_detach = drive_can_detach;
}
void info_device_properties( device_t *device )
{
const char* value;
device->native_path = g_strdup( udev_device_get_syspath( device->udevice ) );
device->devnode = g_strdup( udev_device_get_devnode( device->udevice ) );
device->devnum = udev_device_get_devnum( device->udevice );
//device->major = g_strdup( udev_device_get_property_value( device->udevice, "MAJOR") );
//device->minor = g_strdup( udev_device_get_property_value( device->udevice, "MINOR") );
if ( !device->native_path || !device->devnode || device->devnum == 0 )
{
if ( device->native_path )
g_free( device->native_path );
device->native_path = NULL;
device->devnum = 0;
return;
}
//by id - would need to read symlinks in /dev/disk/by-id
// is_removable may also be set in info_drive_connection walking up sys tree
device->device_is_removable = sysfs_get_int( device->native_path, "removable");
device->device_presentation_hide = g_strdup( udev_device_get_property_value(
device->udevice, "UDISKS_PRESENTATION_HIDE") );
device->device_presentation_nopolicy = g_strdup( udev_device_get_property_value(
device->udevice, "UDISKS_PRESENTATION_NOPOLICY") );
device->device_presentation_name = g_strdup( udev_device_get_property_value(
device->udevice, "UDISKS_PRESENTATION_NAME") );
device->device_presentation_icon_name = g_strdup( udev_device_get_property_value(
device->udevice, "UDISKS_PRESENTATION_ICON_NAME") );
device->device_automount_hint = g_strdup( udev_device_get_property_value(
device->udevice, "UDISKS_AUTOMOUNT_HINT") );
// filesystem properties
gchar *decoded_string;
const gchar *partition_scheme;
gint partition_type = 0;
partition_scheme = udev_device_get_property_value( device->udevice, "UDISKS_PARTITION_SCHEME");
if ( value = udev_device_get_property_value( device->udevice, "UDISKS_PARTITION_TYPE") )
partition_type = atoi( value );
if (g_strcmp0 (partition_scheme, "mbr") == 0 && (partition_type == 0x05 ||
partition_type == 0x0f ||
partition_type == 0x85))
{
}
else
{
device->id_usage = g_strdup( udev_device_get_property_value( device->udevice,
"ID_FS_USAGE" ) );
device->id_type = g_strdup( udev_device_get_property_value( device->udevice,
"ID_FS_TYPE" ) );
device->id_version = g_strdup( udev_device_get_property_value( device->udevice,
"ID_FS_VERSION" ) );
device->id_uuid = g_strdup( udev_device_get_property_value( device->udevice,
"ID_FS_UUID" ) );
if ( value = udev_device_get_property_value( device->udevice, "ID_FS_LABEL_ENC" ) )
{
decoded_string = decode_udev_encoded_string ( value );
g_strstrip (decoded_string);
device->id_label = decoded_string;
}
else if ( value = udev_device_get_property_value( device->udevice, "ID_FS_LABEL" ) )
{
device->id_label = g_strdup( value );
}
}
// device_is_media_available
gboolean media_available = FALSE;
if ( ( device->id_usage && device->id_usage[0] != '\0' ) ||
( device->id_type && device->id_type[0] != '\0' ) ||
( device->id_uuid && device->id_uuid[0] != '\0' ) ||
( device->id_label && device->id_label[0] != '\0' ) )
{
media_available = TRUE;
}
else if ( g_str_has_prefix( device->devnode, "/dev/loop" ) )
media_available = FALSE;
else if ( device->device_is_removable )
{
gboolean is_cd, is_floppy;
if ( value = udev_device_get_property_value( device->udevice, "ID_CDROM" ) )
is_cd = atoi( value ) != 0;
else
is_cd = FALSE;
if ( value = udev_device_get_property_value( device->udevice, "ID_DRIVE_FLOPPY" ) )
is_floppy = atoi( value ) != 0;
else
is_floppy = FALSE;
if ( !is_cd && !is_floppy )
{
// this test is limited for non-root - user may not have read
// access to device file even if media is present
int fd;
fd = open( device->devnode, O_RDONLY );
if ( fd >= 0 )
{
media_available = TRUE;
close( fd );
}
}
else if ( value = udev_device_get_property_value( device->udevice, "ID_CDROM_MEDIA" ) )
media_available = ( atoi( value ) == 1 );
}
else if ( value = udev_device_get_property_value( device->udevice, "ID_CDROM_MEDIA" ) )
media_available = ( atoi( value ) == 1 );
else
media_available = TRUE;
device->device_is_media_available = media_available;
/* device_size, device_block_size and device_is_read_only properties */
if (device->device_is_media_available)
{
guint64 block_size;
device->device_size = sysfs_get_uint64( device->native_path, "size")
* ((guint64) 512);
device->device_is_read_only = (sysfs_get_int (device->native_path,
"ro") != 0);
/* This is not available on all devices so fall back to 512 if unavailable.
*
* Another way to get this information is the BLKSSZGET ioctl but we don't want
* to open the device. Ideally vol_id would export it.
*/
block_size = sysfs_get_uint64 (device->native_path, "queue/hw_sector_size");
if (block_size == 0)
block_size = 512;
device->device_block_size = block_size;
}
else
{
device->device_size = device->device_block_size = 0;
device->device_is_read_only = FALSE;
}
// links
struct udev_list_entry *entry = udev_device_get_devlinks_list_entry(
device->udevice );
while ( entry )
{
const char *entry_name = udev_list_entry_get_name( entry );
if ( entry_name && ( g_str_has_prefix( entry_name, "/dev/disk/by-id/" )
|| g_str_has_prefix( entry_name, "/dev/disk/by-uuid/" ) ) )
{
device->device_by_id = g_strdup( entry_name );
break;
}
entry = udev_list_entry_get_next( entry );
}
}