All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libblkid: match uppercase UUIDs
@ 2021-06-14 20:40 Alyssa Ross
  2021-06-14 21:04 ` Alyssa Ross
  0 siblings, 1 reply; 4+ messages in thread
From: Alyssa Ross @ 2021-06-14 20:40 UTC (permalink / raw)
  To: util-linux; +Cc: Alyssa Ross

In a script, I wanted to look up the device node for the booted EFI
partition.  systemd-boot exposes this in an EFI variable but it's
uppercase, so when I tried to do this, it didn't work:

    findfs "(< /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440)"

This was very confusing to me, and I lost some time trying to figure
out what was wrong before I realised that case of UUIDs was
significant to findfs.

Here, I've made comparisons of UUID and PARTUUID case-insensitive in
libblkid, which fixes the command above.

Signed-off-by: Alyssa Ross <hi@alyssa.is>
---
 libblkid/src/evaluate.c | 12 ++++++++++++
 libblkid/src/tag.c      | 22 +++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/libblkid/src/evaluate.c b/libblkid/src/evaluate.c
index 710eac956..ceaa63cc7 100644
--- a/libblkid/src/evaluate.c
+++ b/libblkid/src/evaluate.c
@@ -135,6 +135,15 @@ int blkid_send_uevent(const char *devname, const char *action)
 	return rc;
 }
 
+static void ascii_downcase(char *s)
+{
+	while (*s) {
+		if ('A' <= *s && *s <= 'Z')
+			*s += 'a' - 'A';
+		s++;
+	}
+}
+
 static char *evaluate_by_udev(const char *token, const char *value, int uevent)
 {
 	char dev[PATH_MAX];
@@ -163,6 +172,9 @@ static char *evaluate_by_udev(const char *token, const char *value, int uevent)
 	if (blkid_encode_string(value, &dev[len], sizeof(dev) - len) != 0)
 		return NULL;
 
+	if (!strcmp(token, "UUID") || !strcmp(token, "PARTUUID"))
+		ascii_downcase(dev);
+
 	DBG(EVALUATE, ul_debug("expected udev link: %s", dev));
 
 	if (stat(dev, &st))
diff --git a/libblkid/src/tag.c b/libblkid/src/tag.c
index 390a64864..ca81c6921 100644
--- a/libblkid/src/tag.c
+++ b/libblkid/src/tag.c
@@ -10,10 +10,13 @@
  * %End-Header%
  */
 
-#include <unistd.h>
+#include <locale.h>
+#include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
+#include <strings.h>
+#include <unistd.h>
 
 #include "blkidP.h"
 
@@ -101,6 +104,19 @@ static blkid_tag blkid_find_head_cache(blkid_cache cache, const char *type)
 	return head;
 }
 
+static bool tag_value_matches(const char *type, const char *expected,
+			      const char *actual)
+{
+	if (!strcmp(type, "UUID") || !strcmp(type, "PARTUUID")) {
+		locale_t posix = newlocale(0, "POSIX", 0);
+		bool r = !strcasecmp_l(expected, actual, posix);
+		freelocale(posix);
+		return r;
+	}
+
+	return !strcmp(expected, actual);
+}
+
 /*
  * Set a tag on an existing device.
  *
@@ -345,7 +361,7 @@ try_again:
 			blkid_tag tmp = list_entry(p, struct blkid_struct_tag,
 						   bit_names);
 
-			if (!strcmp(tmp->bit_val, value) &&
+			if (tag_value_matches(type, tmp->bit_val, value) &&
 			    (tmp->bit_dev->bid_pri > pri) &&
 			    !access(tmp->bit_dev->bid_name, F_OK)) {
 				dev = tmp->bit_dev;
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] libblkid: match uppercase UUIDs
  2021-06-14 20:40 [PATCH] libblkid: match uppercase UUIDs Alyssa Ross
@ 2021-06-14 21:04 ` Alyssa Ross
  2021-06-15  9:19   ` Anatoly Pugachev
  0 siblings, 1 reply; 4+ messages in thread
From: Alyssa Ross @ 2021-06-14 21:04 UTC (permalink / raw)
  To: util-linux

[-- Attachment #1: Type: text/plain, Size: 861 bytes --]

On Mon, Jun 14, 2021 at 08:40:17PM +0000, Alyssa Ross wrote:
> In a script, I wanted to look up the device node for the booted EFI
> partition.  systemd-boot exposes this in an EFI variable but it's
> uppercase, so when I tried to do this, it didn't work:
>
>     findfs "(< /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440)"

Bogus command here, sorry.  It should have been:

    findfs PARTUUID="$(cat /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f/data)"

But my point stands. :)

> This was very confusing to me, and I lost some time trying to figure
> out what was wrong before I realised that case of UUIDs was
> significant to findfs.
>
> Here, I've made comparisons of UUID and PARTUUID case-insensitive in
> libblkid, which fixes the command above.
>
> Signed-off-by: Alyssa Ross <hi@alyssa.is>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libblkid: match uppercase UUIDs
  2021-06-14 21:04 ` Alyssa Ross
@ 2021-06-15  9:19   ` Anatoly Pugachev
  2021-06-15  9:47     ` Karel Zak
  0 siblings, 1 reply; 4+ messages in thread
From: Anatoly Pugachev @ 2021-06-15  9:19 UTC (permalink / raw)
  To: Alyssa Ross; +Cc: util-linux

On Tue, Jun 15, 2021 at 12:07 AM Alyssa Ross <hi@alyssa.is> wrote:
>
> On Mon, Jun 14, 2021 at 08:40:17PM +0000, Alyssa Ross wrote:
> > In a script, I wanted to look up the device node for the booted EFI
> > partition.  systemd-boot exposes this in an EFI variable but it's
> > uppercase, so when I tried to do this, it didn't work:
> >
> >     findfs "(< /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440)"
>
> Bogus command here, sorry.  It should have been:
>
>     findfs PARTUUID="$(cat /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f/data)"
>
> But my point stands. :)

probably could use a pipe after cat, like:

cat ... | tr [:lower:] [:upper:]

or patch util-linux docs, to implicitly state that comparison is done
with upper case letters (UUID)... Not sure which way is better
(including your patch).

Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] libblkid: match uppercase UUIDs
  2021-06-15  9:19   ` Anatoly Pugachev
@ 2021-06-15  9:47     ` Karel Zak
  0 siblings, 0 replies; 4+ messages in thread
From: Karel Zak @ 2021-06-15  9:47 UTC (permalink / raw)
  To: Anatoly Pugachev; +Cc: Alyssa Ross, util-linux

On Tue, Jun 15, 2021 at 12:19:51PM +0300, Anatoly Pugachev wrote:
> On Tue, Jun 15, 2021 at 12:07 AM Alyssa Ross <hi@alyssa.is> wrote:
> >
> > On Mon, Jun 14, 2021 at 08:40:17PM +0000, Alyssa Ross wrote:
> > > In a script, I wanted to look up the device node for the booted EFI
> > > partition.  systemd-boot exposes this in an EFI variable but it's
> > > uppercase, so when I tried to do this, it didn't work:
> > >
> > >     findfs "(< /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440)"
> >
> > Bogus command here, sorry.  It should have been:
> >
> >     findfs PARTUUID="$(cat /sys/firmware/efi/vars/LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f/data)"
> >
> > But my point stands. :)
> 
> probably could use a pipe after cat, like:
> 
> cat ... | tr [:lower:] [:upper:]
> 
> or patch util-linux docs, to implicitly state that comparison is done
> with upper case letters (UUID)... Not sure which way is better
> (including your patch).

There is no guaranty that the UUID will be the upper or lower case
although in fstab.5 and mount.8 man pages we have "should be based on
lower case characters".                                            
                                                                   
libblkid itself does not modify UUID/PARTUUID when reading it from the
filesystem. It depends on FS-specific prober in the library or in some
cases on the filesystem superblock if UUID is stored as a string there.

And it's also important to support 3rd party UUID from another source as
you can write whatever to your udev rules and use others tools to get
UUIDs.     
                                                                   
So, I think your request to compare UUIDs in a case-insensitive way
makes sense. The library should be smart enough to accept whatever.


I'll think about it and try to improve the current situation.

 Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-15  9:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-14 20:40 [PATCH] libblkid: match uppercase UUIDs Alyssa Ross
2021-06-14 21:04 ` Alyssa Ross
2021-06-15  9:19   ` Anatoly Pugachev
2021-06-15  9:47     ` Karel Zak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.