Skip to content

Commit 48fbf27

Browse files
committed
[DISKPART] Calculate the free disk space for the 'list disk' command
1 parent fa5cf28 commit 48fbf27

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

base/system/diskpart/list.c

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,72 @@
1313

1414
/* FUNCTIONS ******************************************************************/
1515

16+
static
17+
ULONGLONG
18+
GetFreeDiskSize(
19+
_In_ PDISKENTRY DiskEntry)
20+
{
21+
ULONGLONG SectorCount;
22+
PLIST_ENTRY Entry;
23+
PPARTENTRY PartEntry;
24+
25+
if (DiskEntry->PartitionStyle == PARTITION_STYLE_MBR)
26+
{
27+
SectorCount = DiskEntry->EndSector.QuadPart - DiskEntry->StartSector.QuadPart + 1;
28+
29+
Entry = DiskEntry->PrimaryPartListHead.Flink;
30+
while (Entry != &DiskEntry->PrimaryPartListHead)
31+
{
32+
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
33+
34+
if ((PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED) &&
35+
!IsContainerPartition(PartEntry->Mbr.PartitionType))
36+
{
37+
SectorCount -= PartEntry->SectorCount.QuadPart;
38+
}
39+
40+
Entry = Entry->Flink;
41+
}
42+
43+
Entry = DiskEntry->LogicalPartListHead.Flink;
44+
while (Entry != &DiskEntry->LogicalPartListHead)
45+
{
46+
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
47+
48+
if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
49+
{
50+
SectorCount -= PartEntry->SectorCount.QuadPart;
51+
}
52+
53+
Entry = Entry->Flink;
54+
}
55+
}
56+
else if (DiskEntry->PartitionStyle == PARTITION_STYLE_GPT)
57+
{
58+
SectorCount = DiskEntry->EndSector.QuadPart - DiskEntry->StartSector.QuadPart + 1;
59+
60+
Entry = DiskEntry->PrimaryPartListHead.Flink;
61+
while (Entry != &DiskEntry->PrimaryPartListHead)
62+
{
63+
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
64+
65+
if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
66+
{
67+
SectorCount -= PartEntry->SectorCount.QuadPart;
68+
}
69+
70+
Entry = Entry->Flink;
71+
}
72+
}
73+
else
74+
{
75+
SectorCount = DiskEntry->SectorCount.QuadPart;
76+
}
77+
78+
return SectorCount * DiskEntry->BytesPerSector;
79+
}
80+
81+
1682
VOID
1783
PrintDisk(
1884
_In_ PDISKENTRY DiskEntry)
@@ -38,9 +104,26 @@ PrintDisk(
38104
lpSizeUnit = L"MB";
39105
}
40106

41-
/* FIXME */
42-
FreeSize = 0;
43-
lpFreeUnit = L"B";
107+
FreeSize = GetFreeDiskSize(DiskEntry);
108+
if (FreeSize >= 10737418240) /* 10 GB */
109+
{
110+
FreeSize = RoundingDivide(FreeSize, 1073741824);
111+
lpFreeUnit = L"GB";
112+
}
113+
else if (FreeSize >= 10485760) /* 10 MB */
114+
{
115+
FreeSize = RoundingDivide(FreeSize, 1048576);
116+
lpFreeUnit = L"MB";
117+
}
118+
else if (FreeSize >= 10240) /* 10 KB */
119+
{
120+
FreeSize = RoundingDivide(FreeSize, 1024);
121+
lpFreeUnit = L"KB";
122+
}
123+
else
124+
{
125+
lpFreeUnit = L"B";
126+
}
44127

45128
ConResPrintf(StdOut, IDS_LIST_DISK_FORMAT,
46129
(CurrentDisk == DiskEntry) ? L'*' : L' ',
@@ -115,7 +198,7 @@ ListPartition(
115198
{
116199
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
117200

118-
if (PartEntry->Mbr.PartitionType != 0)
201+
if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
119202
{
120203
PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
121204

@@ -171,7 +254,7 @@ ListPartition(
171254
{
172255
PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
173256

174-
if (PartEntry->Mbr.PartitionType != 0)
257+
if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
175258
{
176259
PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
177260

0 commit comments

Comments
 (0)