chiark / gitweb /
[PATCH] conditional remove of trailing sysfs whitespace
authorkay.sievers@vrfy.org <kay.sievers@vrfy.org>
Fri, 5 Mar 2004 02:59:13 +0000 (18:59 -0800)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 04:35:08 +0000 (21:35 -0700)
Hey, it may never happen, that one wants to distinguish attributes by
trailing spaces, but we should not lose the control over it, just for
being lazy :)

Here we remove the trailing spaces of the sysfs attribute only if the
configured value to match doesn't have any trailing spaces by itself.
So if you put a attribute in a rule with spaces at the end, the sysfs
attribute _must_ match exactly.

Is that cool for everyone?

As usual, 2 tests are added for it with a artificial sysfs file and
a few words to the man page.

namedev.c
test/sys/devices/pci0000:00/0000:00:1e.0/0000:02:05.0/host0/0:0:0:0/whitespace_test [new file with mode: 0644]
test/udev-test.pl
udev.8

index 9182ed1fdc7b8c4e8cf322df315b919541a75bf0..dfeadee18a7e5100b1e3ff35ac67ec1e41cafc55 100644 (file)
--- a/namedev.c
+++ b/namedev.c
@@ -525,6 +525,8 @@ attr_found:
 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
 {
        struct sysfs_attribute *tmpattr;
+       int i;
+       int len;
 
        if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
                return -ENODEV;
@@ -533,6 +535,18 @@ static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct
        if (tmpattr == NULL)
                return -ENODEV;
 
+       /* strip trailing whitespace of value, if not asked to match for it */
+       if (! isspace(pair->value[strlen(pair->value)-1])) {
+               i = len = strlen(tmpattr->value);
+               while (i > 0 &&  isspace(tmpattr->value[i-1]))
+                       i--;
+               if (i < len) {
+                       tmpattr->value[i] = '\0';
+                       dbg("remove %i trailing whitespace chars from '%s'",
+                           len - i, tmpattr->value);
+               }
+       }
+
        dbg("compare attribute '%s' value '%s' with '%s'",
                  pair->file, tmpattr->value, pair->value);
        if (strcmp_pattern(pair->value, tmpattr->value) != 0)
diff --git a/test/sys/devices/pci0000:00/0000:00:1e.0/0000:02:05.0/host0/0:0:0:0/whitespace_test b/test/sys/devices/pci0000:00/0000:00:1e.0/0000:02:05.0/host0/0:0:0:0/whitespace_test
new file mode 100644 (file)
index 0000000..e3d48f0
--- /dev/null
@@ -0,0 +1 @@
+WHITE  SPACE   
index 60cf16901c13d90a3daf668e1ed013d62241975d..38ea0181f6c42b05795a9ce660ea07f31f089d30 100644 (file)
@@ -465,6 +465,25 @@ EOF
                conf     => <<EOF
 BUS="scsi", ID="*:0:0:0", NAME="scsi-0:0:0:0"
 BUS="scsi", ID="0:0:0:0", NAME="bad"
+EOF
+       },
+       {
+               desc     => "ignore SYSFS attribute whitespace",
+               subsys   => "block",
+               devpath  => "block/sda",
+               expected => "ignored",
+               conf     => <<EOF
+BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE", NAME="ignored"
+EOF
+       },
+       {
+               desc     => "do not ignore SYSFS attribute whitespace",
+               subsys   => "block",
+               devpath  => "block/sda",
+               expected => "matched-with-space",
+               conf     => <<EOF
+BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE ", NAME="wrong-to-ignore"
+BUS="scsi", SYSFS{whitespace_test}="WHITE  SPACE   ", NAME="matched-with-space"
 EOF
        },
 );
diff --git a/udev.8 b/udev.8
index bc7308f48428b06e2dd7130c14029dff399bf4af..76b8913f034bf05039497e0d7b5a0adbb8643731 100644 (file)
--- a/udev.8
+++ b/udev.8
@@ -178,6 +178,9 @@ Match the topological position on bus, like physical port of USB device
 Match sysfs device attribute like label, vendor, USB serial number, SCSI UUID
 or file system label.  Up to 5 different sysfs files can be checked, with
 all of the values being required in order to match the rule.
+.br
+Trailing whitespace characters in the sysfs attribute value are ignored, if
+the key doesn't have any trailing whitespace characters by itself.
 .TP
 .B PROGRAM
 Call external program. This key is valid if the program returns successful.