From 60f862b21c1a2f712ce467627f985fc5fdb3d77a Mon Sep 17 00:00:00 2001 From: Alexander Egorenkov Date: Wed, 1 Jun 2022 11:02:05 +0200 Subject: [PATCH] libutil/util_part: return partition containing given block range MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Return the partition number which contains the given block range, before it was tested for exact match between a partition block range and the one provided by user. The old behavior with exact match should still work, this change just relaxes the partition matching algorithm and allows one to find a partition which contains the given block range. Signed-off-by: Alexander Egorenkov Reviewed-by: Mikhail Zaslonko Signed-off-by: Jan Höppner --- libutil/util_part.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libutil/util_part.c b/libutil/util_part.c index c260a4f0..04bafc02 100644 --- a/libutil/util_part.c +++ b/libutil/util_part.c @@ -128,7 +128,7 @@ static int mbr_table_ext_search(int fh, size_t blk_start_mbr, start = blk_start_mbr + le32toh(mbr.part_entry_vec[0].blk_start); cnt = le32toh(mbr.part_entry_vec[0].blk_cnt); - if ((start == blk_start) && (cnt == blk_cnt)) + if ((start <= blk_start) && (cnt >= (blk_start - start) + blk_cnt)) return part_num; /* Second entry contains relative offset for next logical volume */ @@ -162,11 +162,11 @@ static int mbr_table_search(int fh, struct mbr *mbr, size_t blk_start, * The kernel sets count for extended partitions explicitly. * Therefore we do not check count here. */ - if (mbr_part_is_ext(type) && (start == blk_start)) { + if (mbr_part_is_ext(type) && (start <= blk_start)) { *part_ext = 1; return part_num; } - if ((start == blk_start) && (cnt == blk_cnt)) + if ((start <= blk_start) && (cnt >= (blk_start - start) + blk_cnt)) return part_num; if (!mbr_part_is_ext(type)) continue; @@ -207,7 +207,7 @@ static int gpt_table_search(int fh, struct gpt *gpt, size_t blk_start, end = le64toh(part_entry->blk_end); if (start == 0) /* Empty slot */ continue; - if ((start == blk_start) && (end == blk_end)) + if ((start <= blk_start) && (end >= blk_end)) return part_num; } return 0;