]> git.wh0rd.org - patches.git/blob - mdev.patch
more random patches. who knows.
[patches.git] / mdev.patch
1 diff -Naur busybox.orig/util-linux/Config.in busybox/util-linux/Config.in
2 --- busybox.orig/util-linux/Config.in 2008-03-22 09:11:36 +0000
3 +++ busybox/util-linux/Config.in 2008-03-22 15:25:22 +0000
4 @@ -321,6 +321,13 @@
5
6 For more information, please see docs/mdev.txt
7
8 +config FEATURE_MDEV_RENAME_REGEXP
9 + bool "Support regular expressions substitutions when renaming device"
10 + default n
11 + depends on FEATURE_MDEV_RENAME
12 + help
13 + Add support for regular expressions substitutions when renaming device.
14 +
15 config FEATURE_MDEV_EXEC
16 bool "Support command execution at device addition/removal"
17 default n
18 diff -Naur busybox.orig/util-linux/mdev.c busybox/util-linux/mdev.c
19 --- busybox.orig/util-linux/mdev.c 2008-03-17 18:23:37 +0000
20 +++ busybox/util-linux/mdev.c 2008-03-22 19:15:10 +0000
21 @@ -21,6 +21,25 @@
22
23 #define MAX_SYSFS_DEPTH 3 /* prevent infinite loops in /sys symlinks */
24
25 +#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
26 +static char *strsubst(const char *s, const char *str, regmatch_t *vals)
27 +{
28 + char *buf = xzalloc(PATH_MAX), *p = buf;
29 + while (*s) {
30 + if ('%' == *s) {
31 + int i = (*++s-'0');
32 + int n = vals[i].rm_eo-vals[i].rm_so;
33 + strncpy(p, str+vals[i].rm_so, n);
34 + p += n;
35 + } else {
36 + *p++ = *s;
37 + }
38 + s++;
39 + }
40 + return buf;
41 +}
42 +#endif
43 +
44 /* mknod in /dev based on a path like "/sys/block/hda/hda1" */
45 static void make_device(char *path, int delete)
46 {
47 @@ -70,6 +89,9 @@
48
49 while ((vline = line = xmalloc_getline(fp)) != NULL) {
50 int field;
51 +#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
52 + regmatch_t off[10];
53 +#endif
54
55 /* A pristine copy for command execution. */
56 char *orig_line;
57 @@ -98,16 +120,26 @@
58
59 /* Regex to match this device */
60 regex_t match;
61 +#if !ENABLE_FEATURE_MDEV_RENAME_REGEXP
62 regmatch_t off;
63 +#endif
64 int result;
65
66 /* Is this it? */
67 xregcomp(&match, val, REG_EXTENDED);
68 +#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
69 + result = regexec(&match, device_name, 10, off, 0);
70 +#else
71 result = regexec(&match, device_name, 1, &off, 0);
72 +#endif
73 regfree(&match);
74
75 /* If not this device, skip rest of line */
76 +#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
77 + if (result || off[0].rm_so || off[0].rm_eo != strlen(device_name))
78 +#else
79 if (result || off.rm_so || off.rm_eo != strlen(device_name))
80 +#endif
81 goto next_line;
82
83 } else if (field == 1) {
84 @@ -144,8 +176,14 @@
85
86 if (*val != '>')
87 ++field;
88 - else
89 + else {
90 +#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
91 + // substitute %1..9 with off[1..9], if any
92 + alias = strsubst(val + 1, device_name, off);
93 +#else
94 alias = xstrdup(val + 1);
95 +#endif
96 + }
97
98 }
99
100 @@ -209,6 +247,21 @@
101 char *dest;
102
103 temp = strrchr(alias, '/');
104 +#if ENABLE_FEATURE_MDEV_RENAME_REGEXP
105 + // directory given ->
106 + if (temp && temp[1] == '\0') {
107 + // make it
108 + bb_make_directory(alias, 0755, FILEUTILS_RECUR);
109 + // compose device new location
110 + dest = concat_path_file(alias, device_name);
111 + // filename given ->
112 + } else {
113 + // save it
114 + dest = xstrdup(alias);
115 + // create parent directories
116 + bb_make_directory(dirname(alias), 0755, FILEUTILS_RECUR);
117 + }
118 +#else
119 if (temp) {
120 if (temp[1] != '\0')
121 /* given a file name, so rename it */
122 @@ -217,7 +270,7 @@
123 dest = concat_path_file(alias, device_name);
124 } else
125 dest = alias;
126 -
127 +#endif
128 rename(device_name, dest); // TODO: xrename?
129 symlink(dest, device_name);
130