Blame SOURCES/0085-libblkid-store-only-canonical-devnames-to-the-cache.patch

05ad79
From 75b6c0e045abb7e07773b924237c562ab9920c60 Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Thu, 26 May 2016 12:02:12 +0200
05ad79
Subject: [PATCH 85/86] libblkid: store only canonical devnames to the cache
05ad79
05ad79
Let's try to use symlink:
05ad79
05ad79
 # ls -la /dev/block/8\:1
05ad79
 # lrwxrwxrwx 1 root root 7 May 25 16:42 /dev/block/8:1 -> ../sda1
05ad79
05ad79
 # blkid /dev/block/8:1
05ad79
 /dev/block/8:3: LABEL="HOME" UUID="196972ad-3b13-4bba-ac54-4cb3f7b409a4" TYPE="ext4" PARTUUID="6073277f-87bc-43ff-bcfd-724c4484a63a"
05ad79
05ad79
unfortunately the symlink is stored to the cache:
05ad79
05ad79
 <device DEVNO="0x0803" TIME="1464253300.715279" LABEL="HOME" UUID="196972ad-3b13-4bba-ac54-4cb3f7b409a4" TYPE="ext4" PARTUUID="6073277f-87bc-43ff-bcfd-724c4484a63a">/dev/block/8:3</device>
05ad79
05ad79
next time if you ask for LABEL=HOME the answer will be /dev/block/8:3
05ad79
rather than /dev/sda3.
05ad79
05ad79
It seems better to canonicalize the paths we store to the cache.
05ad79
05ad79
Unfortunately if you ask for /dev/block/8:3 then you probably expect
05ad79
that blkid_dev_devname() returns the same path. This patch introduces
05ad79
dev->bid_xname, this is the path used by application (and never stored
05ad79
in the cache).
05ad79
05ad79
Upstream: http://github.com/karelzak/util-linux/commit/924c93d9df118338fd54cd73b4a45ccddc4ac103
05ad79
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1007734
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 libblkid/src/blkidP.h  |  4 +++-
05ad79
 libblkid/src/dev.c     | 11 +++++++++--
05ad79
 libblkid/src/devname.c | 43 +++++++++++++++++++++++++++++++++++++------
05ad79
 3 files changed, 49 insertions(+), 9 deletions(-)
05ad79
05ad79
diff --git a/libblkid/src/blkidP.h b/libblkid/src/blkidP.h
05ad79
index 7de84b4..8183c12 100644
05ad79
--- a/libblkid/src/blkidP.h
05ad79
+++ b/libblkid/src/blkidP.h
05ad79
@@ -44,7 +44,7 @@ struct blkid_struct_dev
05ad79
 	struct list_head	bid_devs;	/* All devices in the cache */
05ad79
 	struct list_head	bid_tags;	/* All tags for this device */
05ad79
 	blkid_cache		bid_cache;	/* Dev belongs to this cache */
05ad79
-	char			*bid_name;	/* Device inode pathname */
05ad79
+	char			*bid_name;	/* Device real pathn (as used in cache) */
05ad79
 	char			*bid_type;	/* Preferred device TYPE */
05ad79
 	int			bid_pri;	/* Device priority */
05ad79
 	dev_t			bid_devno;	/* Device major/minor number */
05ad79
@@ -53,6 +53,8 @@ struct blkid_struct_dev
05ad79
 	unsigned int		bid_flags;	/* Device status bitflags */
05ad79
 	char			*bid_label;	/* Shortcut to device LABEL */
05ad79
 	char			*bid_uuid;	/* Shortcut to binary UUID */
05ad79
+
05ad79
+	char			*bid_xname;	/* Device path as used by application (maybe symlink..) */
05ad79
 };
05ad79
 
05ad79
 #define BLKID_BID_FL_VERIFIED	0x0001	/* Device data validated from disk */
05ad79
diff --git a/libblkid/src/dev.c b/libblkid/src/dev.c
05ad79
index a4b2aea..d2fd3f4 100644
05ad79
--- a/libblkid/src/dev.c
05ad79
+++ b/libblkid/src/dev.c
05ad79
@@ -60,16 +60,23 @@ void blkid_free_dev(blkid_dev dev)
05ad79
 					   bit_tags);
05ad79
 		blkid_free_tag(tag);
05ad79
 	}
05ad79
+	free(dev->bid_xname);
05ad79
 	free(dev->bid_name);
05ad79
 	free(dev);
05ad79
 }
05ad79
 
05ad79
 /*
05ad79
- * Given a blkid device, return its name
05ad79
+ * Given a blkid device, return its name. The function returns the name
05ad79
+ * previously used for blkid_get_dev(). This name does not have to be canonical
05ad79
+ * (real path) name, but for example symlink.
05ad79
  */
05ad79
 const char *blkid_dev_devname(blkid_dev dev)
05ad79
 {
05ad79
-	return dev ? dev->bid_name : NULL;
05ad79
+	if (!dev)
05ad79
+		return NULL;
05ad79
+	if (dev->bid_xname)
05ad79
+		return dev->bid_xname;
05ad79
+	return dev->bid_name;
05ad79
 }
05ad79
 
05ad79
 #ifdef CONFIG_BLKID_DEBUG
05ad79
diff --git a/libblkid/src/devname.c b/libblkid/src/devname.c
05ad79
index 497deaf..55b9594 100644
05ad79
--- a/libblkid/src/devname.c
05ad79
+++ b/libblkid/src/devname.c
05ad79
@@ -51,28 +51,55 @@ blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
05ad79
 {
05ad79
 	blkid_dev dev = NULL, tmp;
05ad79
 	struct list_head *p, *pnext;
05ad79
+	char *cn = NULL;
05ad79
 
05ad79
 	if (!cache || !devname)
05ad79
 		return NULL;
05ad79
 
05ad79
+	/* search by name */
05ad79
 	list_for_each(p, &cache->bic_devs) {
05ad79
 		tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
05ad79
 		if (strcmp(tmp->bid_name, devname))
05ad79
 			continue;
05ad79
-
05ad79
-		DBG(DEVNAME, blkid_debug("found devname %s in cache", tmp->bid_name));
05ad79
 		dev = tmp;
05ad79
 		break;
05ad79
 	}
05ad79
 
05ad79
+	/* try canonicalize the name */
05ad79
+	if (!dev && (cn = canonicalize_path(devname))) {
05ad79
+		if (strcmp(cn, devname) != 0) {
05ad79
+			DBG(DEVNAME, blkid_debug("search cannonical %s", cn));
05ad79
+			list_for_each(p, &cache->bic_devs) {
05ad79
+				tmp = list_entry(p, struct blkid_struct_dev, bid_devs);
05ad79
+				if (strcmp(tmp->bid_name, cn))
05ad79
+					continue;
05ad79
+				dev = tmp;
05ad79
+
05ad79
+				/* update name returned by blkid_dev_devname() */
05ad79
+				free(dev->bid_xname);
05ad79
+				dev->bid_xname = strdup(devname);
05ad79
+				break;
05ad79
+			}
05ad79
+		} else {
05ad79
+			free(cn);
05ad79
+			cn = NULL;
05ad79
+		}
05ad79
+	}
05ad79
+
05ad79
 	if (!dev && (flags & BLKID_DEV_CREATE)) {
05ad79
 		if (access(devname, F_OK) < 0)
05ad79
-			return NULL;
05ad79
+			goto done;
05ad79
 		dev = blkid_new_dev();
05ad79
 		if (!dev)
05ad79
-			return NULL;
05ad79
+			goto done;
05ad79
 		dev->bid_time = INT_MIN;
05ad79
-		dev->bid_name = strdup(devname);
05ad79
+		if (cn) {
05ad79
+			dev->bid_name = cn;
05ad79
+			dev->bid_xname = strdup(devname);
05ad79
+			cn = NULL;	/* see free() below */
05ad79
+		} else
05ad79
+			dev->bid_name = strdup(devname);
05ad79
+
05ad79
 		dev->bid_cache = cache;
05ad79
 		list_add_tail(&dev->bid_devs, &cache->bic_devs);
05ad79
 		cache->bic_flags |= BLKID_BIC_FL_CHANGED;
05ad79
@@ -81,7 +108,7 @@ blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
05ad79
 	if (flags & BLKID_DEV_VERIFY) {
05ad79
 		dev = blkid_verify(cache, dev);
05ad79
 		if (!dev || !(dev->bid_flags & BLKID_BID_FL_VERIFIED))
05ad79
-			return dev;
05ad79
+			goto done;
05ad79
 		/*
05ad79
 		 * If the device is verified, then search the blkid
05ad79
 		 * cache for any entries that match on the type, uuid,
05ad79
@@ -112,6 +139,10 @@ blkid_dev blkid_get_dev(blkid_cache cache, const char *devname, int flags)
05ad79
 				blkid_free_dev(dev2);
05ad79
 		}
05ad79
 	}
05ad79
+done:
05ad79
+	if (dev)
05ad79
+		DBG(DEVNAME, blkid_debug("%s requested, found %s in cache", devname, dev->bid_name));
05ad79
+	free(cn);
05ad79
 	return dev;
05ad79
 }
05ad79
 
05ad79
-- 
05ad79
2.7.4
05ad79