Skip to content

Commit 2b16187

Browse files
committed
umockdev-record: Support attributes in subdirs
The previous commit added support for mocking subdirectory attributes. This provides the corresponding recording. Thanks to evgeni for the initial patch! #41
1 parent 29a3467 commit 2b16187

File tree

4 files changed

+68
-35
lines changed

4 files changed

+68
-35
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
------------------
33
- Fix compilation on sparc with gcc 4.9.
44
- Support attributes with leading directory names, such as "queue/rotational".
5+
(issue #41)
56

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

src/umockdev-record.vala

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,58 @@ dev_contents(string dev)
164164
return result;
165165
}
166166

167+
static void
168+
print_device_attributes(string devpath, string subdir)
169+
{
170+
Dir d;
171+
var attr_dir = Path.build_filename(devpath, subdir);
172+
try {
173+
d = Dir.open(attr_dir);
174+
} catch (Error e) {
175+
if (subdir == "") {
176+
exit_error("Cannot open directory %s: %s", attr_dir, e.message);
177+
} else {
178+
// we ignore this on subdirs, some might be transient or
179+
// inaccessible
180+
debug("Cannot open directory %s: %s", attr_dir, e.message);
181+
}
182+
return;
183+
}
184+
185+
var attributes = new List<string>();
186+
string entry;
187+
// filter out the uninteresting attributes, sort the others
188+
while ((entry = d.read_name()) != null)
189+
if (entry != "subsystem" && entry != "firmware_node" && entry != "uevent")
190+
attributes.append(entry);
191+
else {
192+
// don't look into subdirs which are devices by themselves
193+
if (subdir != "")
194+
return;
195+
}
196+
attributes.sort(strcmp);
197+
198+
foreach (var attr in attributes) {
199+
string attr_path = Path.build_filename(attr_dir, attr);
200+
string attr_name = Path.build_filename(subdir, attr);
201+
if (FileUtils.test(attr_path, FileTest.IS_SYMLINK)) {
202+
try {
203+
stdout.printf("L: %s=%s\n", attr_name, FileUtils.read_link(attr_path));
204+
} catch (Error e) {
205+
exit_error("Cannot read link %s: %s", attr_path, e.message);
206+
}
207+
} else if (FileUtils.test(attr_path, FileTest.IS_REGULAR)) {
208+
uint8[] contents;
209+
try {
210+
FileUtils.get_data(attr_path, out contents);
211+
write_attr(attr_name, contents);
212+
} catch (FileError e) {} // some attributes are EACCES, or "no such device", etc.
213+
} else if (FileUtils.test(attr_path, FileTest.IS_DIR)) {
214+
print_device_attributes(devpath, attr);
215+
}
216+
}
217+
}
218+
167219
static void
168220
record_device(string dev)
169221
{
@@ -208,39 +260,7 @@ record_device(string dev)
208260
}
209261

210262
// now append all attributes
211-
Dir d;
212-
try {
213-
d = Dir.open(dev);
214-
} catch (Error e) {
215-
exit_error("Cannot open directory %s: %s", dev, e.message);
216-
return; /* not reached, just to avoid warnings */
217-
}
218-
var attributes = new List<string>();
219-
string entry;
220-
// filter out the uninteresting attributes, sort the others
221-
while ((entry = d.read_name()) != null)
222-
if (entry != "subsystem" && entry != "firmware_node" && entry != "uevent")
223-
attributes.append(entry);
224-
attributes.sort(strcmp);
225-
226-
foreach (var attr in attributes) {
227-
string attr_path = Path.build_filename(dev, attr);
228-
// only look at files or symlinks
229-
if (FileUtils.test(attr_path, FileTest.IS_SYMLINK)) {
230-
try {
231-
stdout.printf("L: %s=%s\n", attr, FileUtils.read_link(attr_path));
232-
} catch (Error e) {
233-
exit_error("Cannot read link %s: %s", attr, e.message);
234-
}
235-
} else if (FileUtils.test(attr_path, FileTest.IS_REGULAR)) {
236-
uint8[] contents;
237-
try {
238-
FileUtils.get_data(attr_path, out contents);
239-
write_attr(attr, contents);
240-
} catch (FileError e) {} // some attributes are EACCES, or "no such device", etc.
241-
}
242-
}
243-
263+
print_device_attributes(dev, "");
244264
stdout.putc('\n');
245265
}
246266

src/umockdev.vala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ public class Testbed: GLib.Object {
210210
public void set_attribute_link(string devpath, string name, string value)
211211
{
212212
var path = Path.build_filename(this.root_dir, devpath, name);
213+
var dir = Path.get_dirname(path);
214+
if (DirUtils.create_with_parents(dir, 0755) != 0)
215+
error("cannot create attribute dir '%s': %s", dir, strerror(errno));
213216
if (FileUtils.symlink(value, path) < 0) {
214217
error("Cannot create symlink %s: %s", path, strerror(errno));
215218
}

tests/test-umockdev-record.vala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ t_testbed_one ()
8484

8585
var tb = new UMockdev.Testbed ();
8686
var syspath = tb.add_devicev ("pci", "dev1", null,
87-
{"simple_attr", "1", "multiline_attr", "a\\b\nc\\d\nlast"},
87+
{"simple_attr", "1",
88+
"multiline_attr", "a\\b\nc\\d\nlast",
89+
"knobs/red", "off"},
8890
{"SIMPLE_PROP", "1"});
8991
tb.set_attribute_binary (syspath, "binary_attr", {0x41, 0xFF, 0, 5, 0xFF, 0});
9092
tb.set_attribute_link (syspath, "driver", "../../drivers/foo");
93+
tb.set_attribute_link (syspath, "power/fiddle", "../some/where");
9194

9295
spawn (umockdev_record_path + " --all", out sout, out serr, out exit);
9396
assert_cmpstr (serr, Op.EQ, "");
@@ -97,7 +100,9 @@ E: SIMPLE_PROP=1
97100
E: SUBSYSTEM=pci
98101
H: binary_attr=41FF0005FF00
99102
L: driver=../../drivers/foo
103+
A: knobs/red=off
100104
A: multiline_attr=a\\b\nc\\d\nlast
105+
L: power/fiddle=../some/where
101106
A: simple_attr=1
102107
103108
""");
@@ -112,7 +117,9 @@ t_testbed_multiple ()
112117
int exit;
113118

114119
var tb = new UMockdev.Testbed ();
115-
var dev1 = tb.add_devicev ("pci", "dev1", null, {"dev1color", "green"}, {"DEV1COLOR", "GREEN"});
120+
var dev1 = tb.add_devicev ("pci", "dev1", null,
121+
{"dev1color", "green", "knobs/red", "off"},
122+
{"DEV1COLOR", "GREEN"});
116123
var subdev1 = tb.add_devicev ("pci", "subdev1", dev1, {"subdev1color", "yellow"},
117124
{"SUBDEV1COLOR", "YELLOW"});
118125
tb.add_devicev ("pci", "dev2", null, {"dev2color", "brown"}, {"DEV2COLOR", "BROWN"});
@@ -130,6 +137,7 @@ P: /devices/dev1
130137
E: DEV1COLOR=GREEN
131138
E: SUBSYSTEM=pci
132139
A: dev1color=green
140+
A: knobs/red=off
133141
134142
""");
135143

@@ -141,6 +149,7 @@ A: dev1color=green
141149
E: DEV1COLOR=GREEN
142150
E: SUBSYSTEM=pci
143151
A: dev1color=green
152+
A: knobs/red=off
144153
145154
""");
146155

0 commit comments

Comments
 (0)