bug-parted
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] make parted "do what I say" with IEC start and end values like 5


From: Jim Meyering
Subject: [PATCH] make parted "do what I say" with IEC start and end values like 5GiB
Date: Fri, 18 Mar 2011 15:12:51 +0100

Following up on a suggestion I made a month or so ago,
here's a proposed patch that's nearly complete (still no doc
or NEWS update, but the log should be close to what I'll put in .texi):

>From b562fa89508e85b7204c81bf31a9bb7478b7caaf Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Tue, 22 Feb 2011 16:12:28 +0100
Subject: [PATCH] make parted "do what I say" with IEC start and end values
 like 5GiB

In a command like this,
  parted -s -- $dev mklabel gpt mkpart P-NAME 4MB -34s
parted interprets the "4MB" as merely a suggestion for the starting
sector number.  It uses half of the MB-units value as a "radius"
about the sector containing byte 4,000,000, and it may choose some
other more appropriate sector, based on partition table or alignment
constraints within the range 3,500,000B..4,500,000B.
Before, parted handled IEC units, i.e., KiB, MiB, GiB, etc.,
with identically "helpful" sloppiness, of course honoring the
power-of-two semantics.
Now, however if you use IEC units, i.e., KiB, MiB, GiB, etc.,
the "radius" is taken to be zero, so parted uses precisely
whatever multiple of a power of two you've specified.
Hence, adjusting the example above to use MiB, rather than "MB",
  parted -s -- $dev mklabel gpt mkpart P 4MiB -34s
that will works just like this:
  parted -s -- $dev mklabel gpt mkpart P-NAME 4194304MiB -34s
I.e., it uses the sector containing precisely that byte, and
does not perform any "extra" adjustment.
* libparted/unit.c (is_power_of_2): New function.
(ped_unit_parse_custom): Use it to avoid interpreting a large
input string as "sloppy" (i.e. large radius) when it uses IEC
binary notation like 34KiB, 3GiB and 65TiB.
* tests/t0207-IEC-binary-notation.sh: New test.
* tests/Makefile.am (TESTS): Add it.
---
 libparted/unit.c                   |   13 +++++++++++
 tests/Makefile.am                  |    1 +
 tests/t0207-IEC-binary-notation.sh |   40 ++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 0 deletions(-)
 create mode 100644 tests/t0207-IEC-binary-notation.sh

diff --git a/libparted/unit.c b/libparted/unit.c
index 5885d83..dc4205b 100644
--- a/libparted/unit.c
+++ b/libparted/unit.c
@@ -480,6 +480,12 @@ parse_unit_suffix (const char* suffix, PedUnit 
suggested_unit)
        return suggested_unit;
 }

+static bool
+is_power_of_2 (long long n)
+{
+  return (n & (n - 1)) == 0;
+}
+
 /**
  * If \p str contains a valid description of a location on \p dev, then
  * \p *sector is modified to describe the location and a geometry is created
@@ -530,6 +536,13 @@ ped_unit_parse_custom (const char* str, const PedDevice* 
dev, PedUnit unit,
        radius = ped_div_round_up (unit_size, dev->sector_size) - 1;
        if (radius < 0)
                radius = 0;
+       /* If the user specifies units in a power of 2, e.g., 4MiB, as in
+              parted -s -- $dev mklabel gpt mkpart P-NAME 4MiB -34s
+          do not use 4MiB as the range.  Rather, presume that they
+          are specifying precisely the starting or ending number,
+          and treat "4MiB" just as we would treat "4194304B".  */
+       if (is_power_of_2 (unit_size))
+               radius = 0;

        *sector = num * unit_size / dev->sector_size;
        /* negative numbers count from the end */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4f95429..93e5995 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -13,6 +13,7 @@ TESTS = \
   t0202-gpt-pmbr.sh \
   t0205-gpt-list-clobbers-pmbr.sh \
   t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh \
+  t0207-IEC-binary-notation.sh \
   t0220-gpt-msftres.sh \
   t0250-gpt.sh \
   t0280-gpt-corrupt.sh \
diff --git a/tests/t0207-IEC-binary-notation.sh 
b/tests/t0207-IEC-binary-notation.sh
new file mode 100644
index 0000000..2228ce1
--- /dev/null
+++ b/tests/t0207-IEC-binary-notation.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+# Show how parted treats a starting or ending sector number w/IEC units.
+
+# Copyright (C) 2011 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted
+
+ss=$sector_size_
+n_sectors=3000
+dev=dev-file
+
+dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
+parted --align=none -s $dev mklabel gpt mkpart p1 $((64*1024))B 
$((1024*1024))B \
+    > err 2>&1 || fail=1
+compare err /dev/null || fail=1
+parted -m -s $dev u s p > exp || fail=1
+
+rm $dev
+dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
+parted --align=none -s $dev mklabel gpt mkpart p1 64KiB 1MiB \
+    > err 2>&1 || fail=1
+compare err /dev/null || fail=1
+parted -m -s $dev u s p > out || fail=1
+
+compare out exp || fail=1
+
+Exit $fail
--
1.7.4.1.494.g5ddab



reply via email to

[Prev in Thread] Current Thread [Next in Thread]