[SETUPLIB][USETUP] Reduce duplicated code

- ScrollDownPartitionList() and ScrollUpPartitionList()
  --> ScrollUpDownPartitionList()

- GetPrimaryPartitionCount() and GetLogicalPartitionCount()
  --> generic GetPartitionCount() and macros.

- GetPrevUnpartitionedEntry() and GetNextUnpartitionedEntry()
  --> GetAdjUnpartitionedEntry() ("Adj" == Adjacent)
This commit is contained in:
Hermès Bélusca-Maïto 2024-07-04 15:28:53 +02:00
parent 97821f00d5
commit 7639cb750a
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
5 changed files with 73 additions and 122 deletions

View file

@ -702,9 +702,6 @@ ResolveArcNameManually(
BOOLEAN UseSignature;
SIZE_T NameLength;
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry = NULL;
if (NtName->MaximumLength < sizeof(UNICODE_NULL))
return STATUS_BUFFER_TOO_SMALL;
@ -757,6 +754,9 @@ ResolveArcNameManually(
else
if (PeripheralType == RDiskPeripheral)
{
PDISKENTRY DiskEntry;
PPARTENTRY PartEntry = NULL;
if (UseSignature)
{
/* The disk signature is stored in AdapterKey */

View file

@ -2432,59 +2432,39 @@ IsSamePrimaryLayoutEntry(
// PartitionInfo->PartitionType == PartEntry->PartitionType
}
/**
* @brief
* Counts the number of partitioned disk regions in a given partition list.
**/
static
ULONG
GetPrimaryPartitionCount(
IN PDISKENTRY DiskEntry)
GetPartitionCount(
_In_ PLIST_ENTRY PartListHead)
{
PLIST_ENTRY Entry;
PPARTENTRY PartEntry;
ULONG Count = 0;
if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return 0;
}
for (Entry = DiskEntry->PrimaryPartListHead.Flink;
Entry != &DiskEntry->PrimaryPartListHead;
for (Entry = PartListHead->Flink;
Entry != PartListHead;
Entry = Entry->Flink)
{
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
if (PartEntry->IsPartitioned)
Count++;
++Count;
}
return Count;
}
static
ULONG
GetLogicalPartitionCount(
IN PDISKENTRY DiskEntry)
{
PLIST_ENTRY ListEntry;
PPARTENTRY PartEntry;
ULONG Count = 0;
#define GetPrimaryPartitionCount(DiskEntry) \
GetPartitionCount(&(DiskEntry)->PrimaryPartListHead)
if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return 0;
}
#define GetLogicalPartitionCount(DiskEntry) \
(((DiskEntry)->DiskStyle == PARTITION_STYLE_MBR) \
? GetPartitionCount(&(DiskEntry)->LogicalPartListHead) : 0)
for (ListEntry = DiskEntry->LogicalPartListHead.Flink;
ListEntry != &DiskEntry->LogicalPartListHead;
ListEntry = ListEntry->Flink)
{
PartEntry = CONTAINING_RECORD(ListEntry, PARTENTRY, ListEntry);
if (PartEntry->IsPartitioned)
Count++;
}
return Count;
}
static
BOOLEAN
@ -2727,73 +2707,53 @@ UpdateDiskLayout(
#endif
}
/**
* @brief
* Retrieves, if any, the unpartitioned disk region that is adjacent
* (next or previous) to the specified partition.
*
* @param[in] PartEntry
* Partition from where to find the adjacent unpartitioned region.
*
* @param[in] Direction
* TRUE or FALSE to search the next or previous region, respectively.
*
* @return The adjacent unpartitioned region, if it exists, or NULL.
**/
static
PPARTENTRY
GetPrevUnpartitionedEntry(
IN PPARTENTRY PartEntry)
GetAdjUnpartitionedEntry(
_In_ PPARTENTRY PartEntry,
_In_ BOOLEAN Direction)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;
PPARTENTRY PrevPartEntry;
PLIST_ENTRY ListHead;
PLIST_ENTRY ListHead, AdjEntry;
if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
/* In case of MBR disks only, check the logical partitions if necessary */
if ((DiskEntry->DiskStyle == PARTITION_STYLE_MBR) &&
PartEntry->LogicalPartition)
{
DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return NULL;
ListHead = &DiskEntry->LogicalPartListHead;
}
else
{
ListHead = &DiskEntry->PrimaryPartListHead;
}
if (PartEntry->LogicalPartition)
ListHead = &DiskEntry->LogicalPartListHead;
if (Direction)
AdjEntry = PartEntry->ListEntry.Flink; // Next region.
else
ListHead = &DiskEntry->PrimaryPartListHead;
AdjEntry = PartEntry->ListEntry.Blink; // Previous region.
if (PartEntry->ListEntry.Blink != ListHead)
if (AdjEntry != ListHead)
{
PrevPartEntry = CONTAINING_RECORD(PartEntry->ListEntry.Blink,
PARTENTRY,
ListEntry);
if (!PrevPartEntry->IsPartitioned)
PartEntry = CONTAINING_RECORD(AdjEntry, PARTENTRY, ListEntry);
if (!PartEntry->IsPartitioned)
{
ASSERT(PrevPartEntry->PartitionType == PARTITION_ENTRY_UNUSED);
return PrevPartEntry;
ASSERT(PartEntry->PartitionType == PARTITION_ENTRY_UNUSED);
return PartEntry;
}
}
return NULL;
}
static
PPARTENTRY
GetNextUnpartitionedEntry(
IN PPARTENTRY PartEntry)
{
PDISKENTRY DiskEntry = PartEntry->DiskEntry;
PPARTENTRY NextPartEntry;
PLIST_ENTRY ListHead;
if (DiskEntry->DiskStyle == PARTITION_STYLE_GPT)
{
DPRINT("GPT-partitioned disk detected, not currently supported by SETUP!\n");
return NULL;
}
if (PartEntry->LogicalPartition)
ListHead = &DiskEntry->LogicalPartListHead;
else
ListHead = &DiskEntry->PrimaryPartListHead;
if (PartEntry->ListEntry.Flink != ListHead)
{
NextPartEntry = CONTAINING_RECORD(PartEntry->ListEntry.Flink,
PARTENTRY,
ListEntry);
if (!NextPartEntry->IsPartitioned)
{
ASSERT(NextPartEntry->PartitionType == PARTITION_ENTRY_UNUSED);
return NextPartEntry;
}
}
return NULL;
}
@ -3094,8 +3054,8 @@ DeletePartition(
/* Adjust the unpartitioned disk space entries */
/* Get pointer to previous and next unpartitioned entries */
PrevPartEntry = GetPrevUnpartitionedEntry(PartEntry);
NextPartEntry = GetNextUnpartitionedEntry(PartEntry);
PrevPartEntry = GetAdjUnpartitionedEntry(PartEntry, FALSE);
NextPartEntry = GetAdjUnpartitionedEntry(PartEntry, TRUE);
if (PrevPartEntry != NULL && NextPartEntry != NULL)
{

View file

@ -833,28 +833,22 @@ DrawPartitionList(
}
}
/**
* @param[in] Direction
* TRUE or FALSE to scroll to the next (down) or previous (up) entry, respectively.
**/
VOID
ScrollDownPartitionList(
IN PPARTLIST_UI ListUi)
ScrollUpDownPartitionList(
_In_ PPARTLIST_UI ListUi,
_In_ BOOLEAN Direction)
{
PPARTENTRY NextPart = GetNextPartition(ListUi->List, ListUi->CurrentPartition);
if (NextPart)
PPARTENTRY PartEntry =
(Direction ? GetNextPartition
: GetPrevPartition)(ListUi->List, ListUi->CurrentPartition);
if (PartEntry)
{
ListUi->CurrentPartition = NextPart;
ListUi->CurrentDisk = NextPart->DiskEntry;
DrawPartitionList(ListUi);
}
}
VOID
ScrollUpPartitionList(
IN PPARTLIST_UI ListUi)
{
PPARTENTRY PrevPart = GetPrevPartition(ListUi->List, ListUi->CurrentPartition);
if (PrevPart)
{
ListUi->CurrentPartition = PrevPart;
ListUi->CurrentDisk = PrevPart->DiskEntry;
ListUi->CurrentPartition = PartEntry;
ListUi->CurrentDisk = PartEntry->DiskEntry;
DrawPartitionList(ListUi);
}
}

View file

@ -85,16 +85,13 @@ InitPartitionListUi(
IN SHORT Right,
IN SHORT Bottom);
VOID
ScrollDownPartitionList(
IN PPARTLIST_UI ListUi);
VOID
ScrollUpPartitionList(
IN PPARTLIST_UI ListUi);
VOID
DrawPartitionList(
IN PPARTLIST_UI ListUi);
VOID
ScrollUpDownPartitionList(
_In_ PPARTLIST_UI ListUi,
_In_ BOOLEAN Direction);
/* EOF */

View file

@ -1689,12 +1689,12 @@ SelectPartitionPage(PINPUT_RECORD Ir)
else if ((Ir->Event.KeyEvent.uChar.AsciiChar == 0x00) &&
(Ir->Event.KeyEvent.wVirtualKeyCode == VK_DOWN)) /* DOWN */
{
ScrollDownPartitionList(&ListUi);
ScrollUpDownPartitionList(&ListUi, TRUE);
}
else if ((Ir->Event.KeyEvent.uChar.AsciiChar == 0x00) &&
(Ir->Event.KeyEvent.wVirtualKeyCode == VK_UP)) /* UP */
{
ScrollUpPartitionList(&ListUi);
ScrollUpDownPartitionList(&ListUi, FALSE);
}
else if (Ir->Event.KeyEvent.wVirtualKeyCode == VK_RETURN) /* ENTER */
{