public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fix partx --update ranges and out of order tables
@ 2014-01-13 20:32 Scott Moser
  2014-01-14  8:54 ` Karel Zak
  0 siblings, 1 reply; 7+ messages in thread
From: Scott Moser @ 2014-01-13 20:32 UTC (permalink / raw)
  To: util-linux; +Cc: Philip Susi

partx --update DEVICE NUMBER
was broken in 2 cases:
 * if NUMBER != 1
 * if the partition table was "out of order".
   Ie, where sda2 came after sda3.

fixes http://pad.lv/1244662.
See bug for example recreates.

diff --git a/disk-utils/partx.c b/disk-utils/partx.c
index 880d779..df03e59 100644
--- a/disk-utils/partx.c
+++ b/disk-utils/partx.c
@@ -412,10 +412,41 @@ static void upd_parts_warnx(const char *device, int first, int last)
 				device, first, last);
 }

+/**
+ * get_partition_by_partno:
+ * @ls: partitions list
+ * @n: the partition number (e.g. 'N' from sda'N')
+ *
+ * This does not assume any order of the input blkid_partlist.
+ * And correctly handles "out of order" partition tables.
+ * partition N is located after partition N+1 on the disk.
+ *
+ * Returns: partition object or NULL in case or error.
+ */
+blkid_partition get_partition_by_partno(blkid_partlist ls, int n)
+{
+	int i, nparts;
+	blkid_partition par;
+	if (!ls)
+		return NULL;
+
+	nparts = blkid_partlist_numof_partitions(ls);
+	if (nparts < 0)
+		return NULL;
+
+	for (i = 0; i < nparts; i++) {
+		par = blkid_partlist_get_partition(ls, i);
+		if (n == blkid_partition_get_partno(par)) {
+			return par;
+		}
+	}
+	return NULL;
+}
+
 static int upd_parts(int fd, const char *device, dev_t devno,
 		     blkid_partlist ls, int lower, int upper)
 {
-	int i, n, an, nparts, rc = 0, errfirst = 0, errlast = 0, err;
+	int n, nparts, rc = 0, errfirst = 0, errlast = 0, err;
 	blkid_partition par;
 	uintmax_t start, size;

@@ -441,18 +472,16 @@ static int upd_parts(int fd, const char *device, dev_t devno,
 		return -1;
 	}

-	for (i = 0, n = lower; n <= upper; n++) {
-		par = blkid_partlist_get_partition(ls, i);
-		an = blkid_partition_get_partno(par);
-
-		if (lower && n < lower)
-			continue;
-		if (upper && n > upper)
+	for (n = lower; n <= upper; n++) {
+		par = get_partition_by_partno(ls, n);
+		if (!par) {
+			if (verbose)
+				warn(_("%s: no partition #%d"), device, n);
 			continue;
+		}

 		start = blkid_partition_get_start(par);
 		size =  blkid_partition_get_size(par);
-
 		if (blkid_partition_is_extended(par))
 			/*
 			 * Let's follow the Linux kernel and reduce
@@ -463,25 +492,21 @@ static int upd_parts(int fd, const char *device, dev_t devno,
 		err = partx_del_partition(fd, n);
 		if (err == -1 && errno == ENXIO)
 			err = 0; /* good, it already doesn't exist */
-		if (an == n)
+		if (err == -1 && errno == EBUSY)
 		{
-			if (i < nparts)
-				i++;
-			if (err == -1 && errno == EBUSY)
-			{
-				/* try to resize */
-				err = partx_resize_partition(fd, n, start, size);
-				if (verbose)
-					printf(_("%s: partition #%d resized\n"), device, n);
-				if (err == 0)
-					continue;
-			}
-			if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
-				if (verbose)
-					printf(_("%s: partition #%d added\n"), device, n);
+			/* try to resize */
+			err = partx_resize_partition(fd, n, start, size);
+			if (verbose)
+				printf(_("%s: partition #%d resized\n"), device, n);
+			if (err == 0)
 				continue;
-			}
 		}
+		if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
+			if (verbose)
+				printf(_("%s: partition #%d added\n"), device, n);
+			continue;
+		}
+
 		if (err == 0)
 			continue;
 		rc = -1;

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

* Re: [PATCH] fix partx --update ranges and out of order tables
  2014-01-13 20:32 [PATCH] fix partx --update ranges and out of order tables Scott Moser
@ 2014-01-14  8:54 ` Karel Zak
  2014-01-14 14:41   ` Scott Moser
  2014-01-14 14:43   ` Phillip Susi
  0 siblings, 2 replies; 7+ messages in thread
From: Karel Zak @ 2014-01-14  8:54 UTC (permalink / raw)
  To: Scott Moser; +Cc: util-linux, Philip Susi

On Mon, Jan 13, 2014 at 03:32:49PM -0500, Scott Moser wrote:
> partx --update DEVICE NUMBER
> was broken in 2 cases:
>  * if NUMBER != 1
>  * if the partition table was "out of order".
>    Ie, where sda2 came after sda3.
> 
> fixes http://pad.lv/1244662.
> See bug for example recreates.

 Good catch.

> -	for (i = 0, n = lower; n <= upper; n++) {
> -		par = blkid_partlist_get_partition(ls, i);
> -		an = blkid_partition_get_partno(par);
> -
> -		if (lower && n < lower)
> -			continue;
> -		if (upper && n > upper)


 It was really stupid code :-(

 Applied, thanks! 


 You forgot "Signed-off-by: ", I have fixed this mistake before commit
 as I guess you agree with this change. OK?

    Karel

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

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

* Re: [PATCH] fix partx --update ranges and out of order tables
  2014-01-14  8:54 ` Karel Zak
@ 2014-01-14 14:41   ` Scott Moser
  2014-01-14 14:43   ` Phillip Susi
  1 sibling, 0 replies; 7+ messages in thread
From: Scott Moser @ 2014-01-14 14:41 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Philip Susi

On Tue, 14 Jan 2014, Karel Zak wrote:

> On Mon, Jan 13, 2014 at 03:32:49PM -0500, Scott Moser wrote:
> > partx --update DEVICE NUMBER
> > was broken in 2 cases:
> >  * if NUMBER != 1
> >  * if the partition table was "out of order".
> >    Ie, where sda2 came after sda3.
> >
> > fixes http://pad.lv/1244662.
> > See bug for example recreates.
>
>  Good catch.
>
> > -	for (i = 0, n = lower; n <= upper; n++) {
> > -		par = blkid_partlist_get_partition(ls, i);
> > -		an = blkid_partition_get_partno(par);
> > -
> > -		if (lower && n < lower)
> > -			continue;
> > -		if (upper && n > upper)
>
>
>  It was really stupid code :-(
>
>  Applied, thanks!
>
>
>  You forgot "Signed-off-by: ", I have fixed this mistake before commit
>  as I guess you agree with this change. OK?

I agree.  Thanks.

Signed-off-by: Scott Moser <smoser@ubuntu.com>

Scott

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

* Re: [PATCH] fix partx --update ranges and out of order tables
  2014-01-14  8:54 ` Karel Zak
  2014-01-14 14:41   ` Scott Moser
@ 2014-01-14 14:43   ` Phillip Susi
  2014-01-14 14:53     ` Scott Moser
  2014-01-15  9:55     ` Karel Zak
  1 sibling, 2 replies; 7+ messages in thread
From: Phillip Susi @ 2014-01-14 14:43 UTC (permalink / raw)
  To: Karel Zak, Scott Moser; +Cc: util-linux

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> +blkid_partition get_partition_by_partno(blkid_partlist ls, int n)

I was wondering why libblkid didn't already have a function like this,
should this be moved there instead of just being in partx.c?


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJS1U0JAAoJEI5FoCIzSKrwKeYH/1NRDc5G+ehMb14+ieyGjRqq
sUXYaVNqJfjHek+Od38gkX0kYsSzszj4gI/whPs4hJt0dgn/DwjPxsD5heyD6/LX
80hElWRyZ8bA0ZxXRwx0Z8Lrgk11R2RQgOTOXBKGTH3VrrViTJ1xugOOzO8NCpUW
uRdt0SHmymCR/yqt/bk3SY4qFsf7voFXa0UqeX/bGZT+jKFrs/Gzvop4ZbLJDW9o
X8Ww+Bq7vlE+l6RUndbmbvbQzGHbQ/4qOD2bneQfkshIoWJjw3jbNqsmP2kYwZ1Q
FISFaOs+H8RdZbaDGfwvz28OoAL7gjiSCssvsVyOJQapVt7xou6a+chr0/6pWK4=
=yXFk
-----END PGP SIGNATURE-----

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

* Re: [PATCH] fix partx --update ranges and out of order tables
  2014-01-14 14:43   ` Phillip Susi
@ 2014-01-14 14:53     ` Scott Moser
  2014-01-14 15:21       ` Phillip Susi
  2014-01-15  9:55     ` Karel Zak
  1 sibling, 1 reply; 7+ messages in thread
From: Scott Moser @ 2014-01-14 14:53 UTC (permalink / raw)
  To: Phillip Susi; +Cc: Karel Zak, util-linux

On Tue, 14 Jan 2014, Phillip Susi wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> > +blkid_partition get_partition_by_partno(blkid_partlist ls, int n)
>
> I was wondering why libblkid didn't already have a function like this,
> should this be moved there instead of just being in partx.c?

I figured I'd leave that up to upstream.
I had an implementation of that, but didn't know of the policy for adding
stuff to libblkid.  This was less invasive.  I'm fine with libblkid also.

Scott

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

* Re: [PATCH] fix partx --update ranges and out of order tables
  2014-01-14 14:53     ` Scott Moser
@ 2014-01-14 15:21       ` Phillip Susi
  0 siblings, 0 replies; 7+ messages in thread
From: Phillip Susi @ 2014-01-14 15:21 UTC (permalink / raw)
  To: Scott Moser; +Cc: Karel Zak, util-linux

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 1/14/2014 9:53 AM, Scott Moser wrote:
> I figured I'd leave that up to upstream. I had an implementation of
> that, but didn't know of the policy for adding stuff to libblkid.
> This was less invasive.  I'm fine with libblkid also.

Right, that was directed at Karel ;)


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJS1VX3AAoJEI5FoCIzSKrw2poIAJlwO24I6M6I+8RGMx3DGzNv
8/dX2CyIQQ59AXoCEIs/t6nY/LO+CuSn1h/xZx+zdDrp4/apSEHl058kBAFhfb3U
4YTtlFgvdJOuWLrXYAMTBYNLS6SsMNEiIipsdomFSRMhufgE12jx/HXcp7sZw75Q
7D0k9kGZfw376RE8R4OveOsU9cpIbhgP509rCXNF5En+lil0npOV18YshaI2FapE
d1hUaKrio16YKKLlxMNEX+aUWBLgkBLapohRZBv1ce4NeMPoSRBshq4fEMA5K5ou
Lw30+oVCynwKCqFqAqKH5Lx6vKgIZIr78j6+5rD+U0LoZkq/fklUEfn2BGnRh2k=
=vgEx
-----END PGP SIGNATURE-----

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

* Re: [PATCH] fix partx --update ranges and out of order tables
  2014-01-14 14:43   ` Phillip Susi
  2014-01-14 14:53     ` Scott Moser
@ 2014-01-15  9:55     ` Karel Zak
  1 sibling, 0 replies; 7+ messages in thread
From: Karel Zak @ 2014-01-15  9:55 UTC (permalink / raw)
  To: Phillip Susi; +Cc: Scott Moser, util-linux

On Tue, Jan 14, 2014 at 09:43:21AM -0500, Phillip Susi wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> > +blkid_partition get_partition_by_partno(blkid_partlist ls, int n)
> 
> I was wondering why libblkid didn't already have a function like this,
> should this be moved there instead of just being in partx.c?

 Yes, good point.
 
 I have moved the function to libblkid in the master branch (in the
 stable/v2.24 is the original Scott's version -- I don't want to
 change the API in x.y.1 releases). Thanks.

    Karel

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

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

end of thread, other threads:[~2014-01-15  9:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-13 20:32 [PATCH] fix partx --update ranges and out of order tables Scott Moser
2014-01-14  8:54 ` Karel Zak
2014-01-14 14:41   ` Scott Moser
2014-01-14 14:43   ` Phillip Susi
2014-01-14 14:53     ` Scott Moser
2014-01-14 15:21       ` Phillip Susi
2014-01-15  9:55     ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox