@@ -43,68 +43,71 @@ public class UdevTest extends TestCase {
4343 public void testEnumerateDevices () {
4444 // Start with the context object
4545 UdevContext udev = UDEV .udev_new ();
46- assertNotNull ("Failed to create udev context" , udev );
47- // Create an enumerator
48- UdevEnumerate enumerate = UDEV .udev_enumerate_new (udev );
49- assertNotNull ("Failed to create udev enumerator" , enumerate );
50- // It's a good assumption the machine we're on has a disk and partition
51- // Add a filter for block devices and scan them
52- UDEV .udev_enumerate_add_match_subsystem (enumerate , "block" );
53- UDEV .udev_enumerate_scan_devices (enumerate );
54- // Enumerator now points to block devices.
46+ try {
47+ assertNotNull ("Failed to create udev context" , udev );
48+ // Create an enumerator
49+ UdevEnumerate enumerate = udev .newEnumerate ();
50+ try {
51+ assertNotNull ("Failed to create udev enumerator" , enumerate );
52+ // It's a good assumption the machine we're on has a disk and partition
53+ // Add a filter for block devices and scan them
54+ enumerate .addMatchSubsystem ("block" );
55+ enumerate .scanDevices ();
56+ // Enumerator now points to block devices.
5557
56- // Iterate the list
57- UdevListEntry entry = UDEV .udev_enumerate_get_list_entry (enumerate );
58- assertNotNull ("Failed to get an enumerator entry" , entry );
59- String entryName = UDEV .udev_list_entry_get_name (entry );
60- assertEquals ("Udev entry name should start with /sys" , "/sys" , entryName .substring (0 , 4 ));
61- UdevDevice device = UDEV .udev_device_new_from_syspath (udev , entryName );
62- while (device != null ) {
63- // devnode is what we use as name, like /dev/sda
64- String devnode = UDEV .udev_device_get_devnode (device );
65- // Ignore loopback and ram disks
66- if (devnode != null && !devnode .startsWith ("/dev/loop" ) && !devnode .startsWith ("/dev/ram" )) {
67- String devType = UDEV .udev_device_get_devtype (device );
68- // For partition, grab values of parent disk
69- String parentSize = null ;
70- String parentMajor = null ;
71- if ("partition" .equals (devType )) {
72- UdevDevice parent = UDEV .udev_device_get_parent_with_subsystem_devtype (device , "block" , "disk" );
73- String parentName = UDEV .udev_device_get_devnode (parent );
74- // These should match same parent without restricting to block and disk
75- UdevDevice parent2 = UDEV .udev_device_get_parent (device );
76- String parentName2 = UDEV .udev_device_get_devnode (parent2 );
77- assertEquals ("Partition parent should match with and without filter" , parentName , parentName2 );
78- // Save the size and major
79- parentSize = UDEV .udev_device_get_sysattr_value (parent , "size" );
80- parentMajor = UDEV .udev_device_get_property_value (parent , "MAJOR" );
58+ // Iterate the list
59+ for (UdevListEntry entry = enumerate .getListEntry (); entry != null ; entry = entry .getNext ()) {
60+ assertNotNull ("Failed to get an enumerator entry" , entry );
61+ String entryName = entry .getName ();
62+ assertEquals ("Udev entry name should start with /sys" , "/sys" , entryName .substring (0 , 4 ));
63+ UdevDevice device = udev .newDeviceFromSyspath (entryName );
64+ if (device != null ) {
65+ try {
66+ // devnode is what we use as name, like /dev/sda
67+ String devnode = device .getDevnode ();
68+ // Ignore loopback and ram disks
69+ if (devnode != null && !devnode .startsWith ("/dev/loop" ) && !devnode .startsWith ("/dev/ram" )) {
70+ String devType = device .getDevtype ();
71+ // For partition, grab values of parent disk
72+ String parentSize = null ;
73+ String parentMajor = null ;
74+ if ("partition" .equals (devType )) {
75+ UdevDevice parent = device .getParentWithSubsystemDevtype ("block" , "disk" );
76+ String parentName = parent .getDevnode ();
77+ // These should match same parent without restricting to block and disk
78+ UdevDevice parent2 = device .getParent ();
79+ String parentName2 = parent2 .getDevnode ();
80+ assertEquals ("Partition parent should match with and without filter" , parentName , parentName2 );
81+ // Save the size and major
82+ parentSize = parent .getSysattrValue ("size" );
83+ parentMajor = parent .getPropertyValue ("MAJOR" );
84+ }
85+ String size = device .getSysattrValue ("size" );
86+ assertTrue ("Size must be nonnegative" , 0 <= Long .parseLong (size ));
87+ if (parentSize != null ) {
88+ assertTrue ("Partition can't be bigger than its disk" ,
89+ Long .parseLong (size ) <= Long .parseLong (parentSize ));
90+ }
91+ String major = device .getPropertyValue ("MAJOR" );
92+ assertTrue ("Major value must be nonnegative" , 0 <= Long .parseLong (major ));
93+ if (parentMajor != null ) {
94+ assertEquals ("Partition and its parent disk should have same major number" , major , parentMajor );
95+ }
96+ assertEquals ("DevType mismatch" , devType , device .getDevtype ());
97+ assertEquals ("Subsystem mismatch" , "block" , device .getSubsystem ());
98+ assertEquals ("Syspath mismatch" , entryName , device .getSyspath ());
99+ assertTrue ("Syspath should end with name" , entryName .endsWith (device .getSysname ()));
100+ }
101+ } finally {
102+ device .unref ();
103+ }
104+ }
81105 }
82- String size = UDEV .udev_device_get_sysattr_value (device , "size" );
83- assertTrue ("Size must be nonnegative" , 0 <= Long .parseLong (size ));
84- if (parentSize != null ) {
85- assertTrue ("Partition can't be bigger than its disk" ,
86- Long .parseLong (size ) <= Long .parseLong (parentSize ));
87- }
88- String major = UDEV .udev_device_get_property_value (device , "MAJOR" );
89- assertTrue ("Major value must be nonnegative" , 0 <= Long .parseLong (major ));
90- if (parentMajor != null ) {
91- assertEquals ("Partition and its parent disk should have same major number" , major , parentMajor );
92- }
93- assertEquals ("DevType mismatch" , devType , UDEV .udev_device_get_devtype (device ));
94- assertEquals ("Subsystem mismatch" , "block" , UDEV .udev_device_get_subsystem (device ));
95- assertEquals ("Syspath mismatch" , entryName , UDEV .udev_device_get_syspath (device ));
96- assertTrue ("Syspath should end with name" , entryName .endsWith (UDEV .udev_device_get_sysname (device )));
97- }
98- UDEV .udev_device_unref (device );
99- // Release the reference and iterate to the next device
100- entry = UDEV .udev_list_entry_get_next (entry );
101- entryName = UDEV .udev_list_entry_get_name (entry );
102- if (entry != null ) {
103- assertEquals ("Udev entry name should start with /sys" , "/sys" , entryName .substring (0 , 4 ));
106+ } finally {
107+ enumerate .unref ();
104108 }
105- device = UDEV .udev_device_new_from_syspath (udev , entryName );
109+ } finally {
110+ udev .unref ();
106111 }
107- UDEV .udev_enumerate_unref (enumerate );
108- UDEV .udev_unref (udev );
109112 }
110113}
0 commit comments