gaps: add within()
To simplify tracking of gap usage, allow looking up a gap based on a preceeding, usually larger, gap.
This commit is contained in:
parent
5d5f9ca49e
commit
7a9e920de1
|
@ -230,3 +230,14 @@ def at_offset(device, offset):
|
|||
if pg.offset == offset:
|
||||
return pg
|
||||
return None
|
||||
|
||||
|
||||
def within(device, gap):
|
||||
"""Find the first gap that is contained wholly inside the supplied gap."""
|
||||
gap_end = gap.offset + gap.size
|
||||
for pg in parts_and_gaps(device):
|
||||
if isinstance(pg, Gap):
|
||||
pg_end = pg.offset + pg.size
|
||||
if pg.offset >= gap.offset and pg_end <= gap_end:
|
||||
return pg
|
||||
return None
|
||||
|
|
|
@ -82,6 +82,59 @@ class TestAtOffset(unittest.TestCase):
|
|||
self.assertEqual(g2, gaps.at_offset(d, 60 << 20))
|
||||
|
||||
|
||||
class TestWithin(unittest.TestCase):
|
||||
def test_identity(self):
|
||||
d = make_disk()
|
||||
[gap] = gaps.parts_and_gaps(d)
|
||||
self.assertEqual(gap, gaps.within(d, gap))
|
||||
|
||||
def test_front_used(self):
|
||||
m, d = make_model_and_disk(size=200 << 20)
|
||||
m.storage_version = 2
|
||||
make_partition(m, d, offset=100 << 20, size=1 << 20)
|
||||
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
|
||||
make_partition(m, d, offset=0, size=20 << 20)
|
||||
[p1, g1, p2, g2] = gaps.parts_and_gaps(d)
|
||||
self.assertEqual(g1, gaps.within(d, orig_g1))
|
||||
|
||||
def test_back_used(self):
|
||||
m, d = make_model_and_disk(size=200 << 20)
|
||||
m.storage_version = 2
|
||||
make_partition(m, d, offset=100 << 20, size=1 << 20)
|
||||
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
|
||||
make_partition(m, d, offset=80 << 20, size=20 << 20)
|
||||
[g1, p1, p2, g2] = gaps.parts_and_gaps(d)
|
||||
self.assertEqual(g1, gaps.within(d, orig_g1))
|
||||
|
||||
def test_front_and_back_used(self):
|
||||
m, d = make_model_and_disk(size=200 << 20)
|
||||
m.storage_version = 2
|
||||
make_partition(m, d, offset=100 << 20, size=1 << 20)
|
||||
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
|
||||
make_partition(m, d, offset=0, size=20 << 20)
|
||||
make_partition(m, d, offset=80 << 20, size=20 << 20)
|
||||
[p1, g1, p2, p3, g2] = gaps.parts_and_gaps(d)
|
||||
self.assertEqual(g1, gaps.within(d, orig_g1))
|
||||
|
||||
def test_multi_gap(self):
|
||||
m, d = make_model_and_disk(size=200 << 20)
|
||||
m.storage_version = 2
|
||||
make_partition(m, d, offset=100 << 20, size=1 << 20)
|
||||
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
|
||||
make_partition(m, d, offset=20 << 20, size=20 << 20)
|
||||
[g1, p1, g2, p2, g3] = gaps.parts_and_gaps(d)
|
||||
self.assertEqual(g1, gaps.within(d, orig_g1))
|
||||
|
||||
def test_later_part_of_disk(self):
|
||||
m, d = make_model_and_disk(size=200 << 20)
|
||||
m.storage_version = 2
|
||||
make_partition(m, d, offset=100 << 20, size=1 << 20)
|
||||
[orig_g1, p1, orig_g2] = gaps.parts_and_gaps(d)
|
||||
make_partition(m, d, offset=120 << 20, size=20 << 20)
|
||||
[g1, p1, g2, p2, g3] = gaps.parts_and_gaps(d)
|
||||
self.assertEqual(g2, gaps.within(d, orig_g2))
|
||||
|
||||
|
||||
class TestDiskGaps(unittest.TestCase):
|
||||
|
||||
def test_no_partition_gpt(self):
|
||||
|
|
Loading…
Reference in New Issue