Skip to content

Commit 29a3467

Browse files
committed
Support attributes with leading directory names
This is useful for mocking block devices, which have attributes like "queue/rotational". #41
1 parent 499af3d commit 29a3467

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0.8.6 (UNRELEASED)
22
------------------
33
- Fix compilation on sparc with gcc 4.9.
4+
- Support attributes with leading directory names, such as "queue/rotational".
45

56
0.8.5 (2014-07-11)
67
------------------

src/umockdev.vala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,24 @@ public class Testbed: GLib.Object {
143143
* umockdev_testbed_set_attribute_binary:
144144
* @self: A #UMockdevTestbed.
145145
* @devpath: The full device path, as returned by #umockdev_testbed_add_device()
146-
* @name: Attribute name
146+
* @name: Attribute name (may contain leading directories like
147+
* "queue/rotational")
147148
* @value: Attribute binary value
148149
* @value_length1: Length of @value in bytes.
149150
*
150151
* Set a binary sysfs attribute for a device.
151152
*/
152153
public void set_attribute_binary(string devpath, string name, uint8[] value)
153154
{
155+
var attr_path = Path.build_filename(this.root_dir, devpath, name);
156+
if ("/" in name) {
157+
string d = Path.get_dirname(attr_path);
158+
if (DirUtils.create_with_parents(d, 0755) != 0)
159+
error("cannot create attribute subdir '%s': %s", d, strerror(errno));
160+
}
161+
154162
try {
155-
FileUtils.set_data(Path.build_filename(this.root_dir, devpath, name), value);
163+
FileUtils.set_data(attr_path, value);
156164
} catch (FileError e) {
157165
error("Cannot write attribute file: %s", e.message);
158166
}
@@ -162,7 +170,8 @@ public class Testbed: GLib.Object {
162170
* umockdev_testbed_set_attribute_int:
163171
* @self: A #UMockdevTestbed.
164172
* @devpath: The full device path, as returned by #umockdev_testbed_add_device()
165-
* @name: Attribute name
173+
* @name: Attribute name (may contain leading directories like
174+
* "queue/rotational")
166175
* @value: Attribute integer value
167176
*
168177
* Set an integer sysfs attribute for a device.

tests/test-umockdev.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ t_testbed_set_attribute(UMockdevTestbedFixture * fixture, gconstpointer data)
426426
/* int attributes */
427427
umockdev_testbed_set_attribute_int(fixture->testbed, syspath, "count", 1000);
428428
umockdev_testbed_set_attribute_hex(fixture->testbed, syspath, "addr", 0x1a01);
429+
/* subdir attribute */
430+
umockdev_testbed_set_attribute(fixture->testbed, syspath, "knobs/red", "off");
429431

430432
device = g_udev_client_query_by_sysfs_path(client, syspath);
431433
g_assert(device);
@@ -434,6 +436,7 @@ t_testbed_set_attribute(UMockdevTestbedFixture * fixture, gconstpointer data)
434436
g_assert_cmpstr(g_udev_device_get_sysfs_attr(device, "color"), ==, "yellow");
435437
g_assert_cmpstr(g_udev_device_get_sysfs_attr(device, "count"), ==, "1000");
436438
g_assert_cmpstr(g_udev_device_get_sysfs_attr(device, "addr"), ==, "1a01");
439+
g_assert_cmpstr(g_udev_device_get_sysfs_attr(device, "knobs/red"), ==, "off");
437440
g_assert_cmpstr(g_udev_device_get_driver(device), ==, "hub");
438441
g_object_unref(device);
439442

@@ -744,6 +747,7 @@ t_testbed_add_from_string(UMockdevTestbedFixture * fixture, gconstpointer data)
744747
"H: binary_attr=41A9FF0005FF00\n"
745748
"A: multiline_attr=a\\\\b\\nc\\\\d\\nlast\n"
746749
"A: simple_attr=1\n"
750+
"A: knobs/red=off\n"
747751
"L: driver=../../foo", &error);
748752
g_assert_no_error(error);
749753
g_assert(success);
@@ -760,6 +764,7 @@ t_testbed_add_from_string(UMockdevTestbedFixture * fixture, gconstpointer data)
760764
g_assert(g_udev_device_get_parent(device) == NULL);
761765
g_assert_cmpstr(g_udev_device_get_sysfs_attr(device, "simple_attr"), ==, "1");
762766
g_assert_cmpstr(g_udev_device_get_sysfs_attr(device, "multiline_attr"), ==, "a\\b\nc\\d\nlast");
767+
g_assert_cmpstr(g_udev_device_get_sysfs_attr(device, "knobs/red"), ==, "off");
763768
g_assert_cmpstr(g_udev_device_get_property(device, "SIMPLE_PROP"), ==, "1");
764769
g_assert_cmpstr(g_udev_device_get_driver(device), ==, "foo");
765770
g_object_unref(device);

0 commit comments

Comments
 (0)