[parted-devel] [PATCH 1/2] parted: mkpart: subtract one sector fron end if IEC unit is used
Petr Uzel
petr.uzel at suse.cz
Tue Nov 1 10:40:20 UTC 2011
On Fri, Oct 28, 2011 at 07:21:59PM +0200, Jim Meyering wrote:
> Petr Uzel wrote:
> > Before, if the user specified start and end in mkpart command using
> > IEC units, parted created a partition that starts and ends exactly on
> > these positions. With such behavior, it is impossible to create
> > partitions as follows: 1MiB-2MiB, 2MiB-3MiB - parted would complain
> > that it cannot create the second partition, because the first one
> > occupied sectors 2048-4096 and the second one sectors 4096-3072,
> > so they would overlap at sector 4096.
> >
> > With this patch, if the user uses IEC units to specify end of the
> > partition, parted creates the partition which ends one sector before
> > the specified position.
>
> Thanks for this improvement.
Thanks for the review and improvements.
> I've fixed a few minor problems and added a test to highlight
> one more that I spotted in review. That test is currently failing.
Good catch - I did not realize this possibility at all.
> Would you please suggest a patch on top of these 4 (which I plan
> to merge into yours) that fixes the new failing test?
What about the one below?
From 6972fd36c6f60b7af6d3f644a4f9504508e90aba Mon Sep 17 00:00:00 2001
From: Petr Uzel <petr.uzel at suse.cz>
Date: Tue, 1 Nov 2011 11:31:51 +0100
Subject: [PATCH] parted: fix test failing because of incomplete mkpart & IEC units feature
Prior to this fix, if the user specified default unit as one of the IEC
ones, but yet used explicit ending sector, parted did adjust the ending
sector, which is unwanted. Fix by checking the default unit only if no
unit is specified explicitly.
* parted/parted.c (_string_has_unit_suffix): New function.
(_adjust_end_if_iec): Use _string_has_unit_suffix.
* tests/t0208-mkpart-end-in-IEC.sh: Add missing colon to expected
output.
---
parted/parted.c | 22 ++++++++++++++++++++--
tests/t0208-mkpart-end-in-IEC.sh | 2 +-
2 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/parted/parted.c b/parted/parted.c
index f43ca49..b6f89aa 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -547,6 +547,23 @@ _string_ends_with_iec_unit(const char *str)
return strchr ("kMGTPEZY", *p) && c_strcasecmp (p+1, "iB") == 0;
}
+/* Return true if str ends with explicit unit identifier.
+ * Do not parse the unit, just check if the unit is specified.
+ * This function expects trailing spaces to be already stripped.
+ */
+static bool
+_string_has_unit_suffix(const char *str)
+{
+ /* At least 1 digit and 1 character to meet the condition */
+ if (!str || strlen(str) < 2)
+ return false;
+
+ if (!isdigit(str[strlen(str) - 1]))
+ return true;
+
+ return false;
+}
+
/* If the selected unit is one of kiB, MiB, GiB or TiB and the partition is not
* only 1 sector long, then adjust the end so that it is one sector before the
* given position. Also adjust range_end accordingly. Thus next partition can
@@ -571,8 +588,9 @@ _adjust_end_if_iec (PedSector* start, PedSector* end,
_strip_trailing_spaces(end_input);
PedUnit unit = ped_unit_get_default();
if (_string_ends_with_iec_unit(end_input) ||
- (unit == PED_UNIT_KIBIBYTE) || (unit == PED_UNIT_MEBIBYTE) ||
- (unit == PED_UNIT_GIBIBYTE) || (unit == PED_UNIT_TEBIBYTE)) {
+ (!_string_has_unit_suffix(end_input) &&
+ ((unit == PED_UNIT_KIBIBYTE) || (unit == PED_UNIT_MEBIBYTE) ||
+ (unit == PED_UNIT_GIBIBYTE) || (unit == PED_UNIT_TEBIBYTE)))) {
*end -= 1;
range_end->start -= 1;
range_end->end -= 1;
diff --git a/tests/t0208-mkpart-end-in-IEC.sh b/tests/t0208-mkpart-end-in-IEC.sh
index 23abd27..475190b 100644
--- a/tests/t0208-mkpart-end-in-IEC.sh
+++ b/tests/t0208-mkpart-end-in-IEC.sh
@@ -51,7 +51,7 @@ cat <<EOF > exp || framework_failure
1:2048s:4095s:2048s::p1:;
2:4096s:6143s:2048s::p2:;
3:6144s:8191s:2048s::p3:;
-4:8192s:10239s:2048s:p4:;
+4:8192s:10239s:2048s::p4:;
EOF
# compare expected and actual outputs
--
1.7.3.4
Petr
--
Petr Uzel
IRC: ptr_uzl @ freenode
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.alioth.debian.org/pipermail/parted-devel/attachments/20111101/b5c8e9c6/attachment.pgp>
More information about the parted-devel
mailing list