cdown / rpms / util-linux

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