Skip to content

Commit 7c0671c

Browse files
dlechmartinpitt
authored andcommitted
Add support for devnode symlinks
This implements creating symlinks for S: lines in umockdev device files. Closes #90
1 parent 6e92627 commit 7c0671c

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/umockdev.vala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ public class Testbed: GLib.Object {
956956
string cur_data = data;
957957
string? devnode_path = null;
958958
uint8[] devnode_contents = {};
959+
string[] devnode_links = {};
959960

960961
cur_data = this.record_parse_line(cur_data, out type, out key, out devpath);
961962
if (cur_data == null || type != 'P')
@@ -1018,7 +1019,10 @@ public class Testbed: GLib.Object {
10181019
break;
10191020

10201021
case 'S':
1021-
/* TODO: ignored for now */
1022+
/* collect symlinks */
1023+
if (val == null)
1024+
throw new UMockdev.Error.PARSE("invalid S: line in description of device %s", devpath);
1025+
devnode_links += Path.build_filename(this.root_dir, "dev", val);
10221026
break;
10231027

10241028
default:
@@ -1043,9 +1047,16 @@ public class Testbed: GLib.Object {
10431047
this.set_attribute_link (syspath, linkattrs[i], linkattrs[i+1]);
10441048

10451049
/* create fake device node */
1046-
if (devnode_path != null)
1050+
if (devnode_path != null) {
10471051
this.create_node_for_device(subsystem, devnode_path, devnode_contents, majmin);
10481052

1053+
/* create symlinks */
1054+
for (int i = 0; i < devnode_links.length; i++) {
1055+
assert (DirUtils.create_with_parents(Path.get_dirname(devnode_links[i]), 0755) == 0);
1056+
assert (FileUtils.symlink(devnode_path, devnode_links[i]) == 0);
1057+
}
1058+
}
1059+
10491060
/* skip over multiple blank lines */
10501061
while (cur_data[0] != '\0' && cur_data[0] == '\n')
10511062
cur_data = cur_data.next_char();

tests/test-umockdev.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ static void
12541254
t_testbed_add_from_string_dev_char(UMockdevTestbedFixture * fixture, gconstpointer data)
12551255
{
12561256
GError *error = NULL;
1257-
gchar *contents;
1257+
gchar *contents, *target;
12581258
gsize length;
12591259
GStatBuf st;
12601260

@@ -1270,6 +1270,20 @@ t_testbed_add_from_string_dev_char(UMockdevTestbedFixture * fixture, gconstpoint
12701270
g_assert(S_ISCHR(st.st_mode));
12711271
g_assert_cmpint(st.st_rdev, ==, makedev(1, 3));
12721272

1273+
/* N: with S: should create a symlink to DEVNAME */
1274+
g_assert(umockdev_testbed_add_from_string(fixture->testbed,
1275+
"P: /devices/target\nN: target\nS: link\n"
1276+
"E: SUBSYSTEM=foo\nE: DEVNAME=/dev/target\n"
1277+
"A: dev=1:4\n", &error));
1278+
g_assert_no_error(error);
1279+
1280+
contents = g_file_read_link("/dev/link", &error);
1281+
g_assert_no_error(error);
1282+
target = g_build_filename(umockdev_testbed_get_root_dir(fixture->testbed), "dev/target", NULL);
1283+
g_assert_cmpstr(contents, ==, target);
1284+
g_free(target);
1285+
g_free(contents);
1286+
12731287
/* N: another N without value whose name looks like hex */
12741288
umockdev_testbed_add_from_string(fixture->testbed,
12751289
"P: /devices/001\nN: bus/usb/001\n"

0 commit comments

Comments
 (0)