ReactOS 0.4.16-dev-2208-g6350669
list.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS DiskPart
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/diskpart/list.c
5 * PURPOSE: Manages all the partitions of the OS in an interactive way.
6 * PROGRAMMERS: Lee Schroeder
7 */
8
9#include "diskpart.h"
10
11#define NDEBUG
12#include <debug.h>
13
14/* FUNCTIONS ******************************************************************/
15
16static
17VOID
19 _In_ ULONGLONG ullSize,
20 _Out_ PWSTR pszSizeBuffer)
21{
22 WCHAR szUnitBuffer[8];
23 INT nUnitId;
24
25 if (ullSize >= SIZE_10TB) /* 10 TB */
26 {
27 ullSize = RoundingDivide(ullSize, SIZE_1TB);
28 nUnitId = IDS_UNIT_TB;
29 }
30 else if (ullSize >= SIZE_10GB) /* 10 GB */
31 {
32 ullSize = RoundingDivide(ullSize, SIZE_1GB);
33 nUnitId = IDS_UNIT_GB;
34 }
35 else if (ullSize >= SIZE_10MB) /* 10 MB */
36 {
37 ullSize = RoundingDivide(ullSize, SIZE_1MB);
38 nUnitId = IDS_UNIT_MB;
39 }
40 else if (ullSize >= SIZE_10KB) /* 10 KB */
41 {
42 ullSize = RoundingDivide(ullSize, SIZE_1KB);
43 nUnitId = IDS_UNIT_KB;
44 }
45 else
46 {
47 nUnitId = IDS_UNIT_B;
48 }
49
51 nUnitId,
52 szUnitBuffer, ARRAYSIZE(szUnitBuffer));
53
54 swprintf(pszSizeBuffer, L"%4I64u %-2s", ullSize, szUnitBuffer);
55}
56
57
58static
61 _In_ PDISKENTRY DiskEntry)
62{
65 PPARTENTRY PartEntry;
66
67 if (DiskEntry->PartitionStyle == PARTITION_STYLE_MBR)
68 {
69 SectorCount = DiskEntry->EndSector.QuadPart - DiskEntry->StartSector.QuadPart + 1;
70
71 Entry = DiskEntry->PrimaryPartListHead.Flink;
72 while (Entry != &DiskEntry->PrimaryPartListHead)
73 {
74 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
75
76 if ((PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED) &&
78 {
79 SectorCount -= PartEntry->SectorCount.QuadPart;
80 }
81
82 Entry = Entry->Flink;
83 }
84
85 Entry = DiskEntry->LogicalPartListHead.Flink;
86 while (Entry != &DiskEntry->LogicalPartListHead)
87 {
88 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
89
91 {
92 SectorCount -= PartEntry->SectorCount.QuadPart;
93 }
94
95 Entry = Entry->Flink;
96 }
97 }
98 else if (DiskEntry->PartitionStyle == PARTITION_STYLE_GPT)
99 {
100 SectorCount = DiskEntry->EndSector.QuadPart - DiskEntry->StartSector.QuadPart + 1;
101
102 Entry = DiskEntry->PrimaryPartListHead.Flink;
103 while (Entry != &DiskEntry->PrimaryPartListHead)
104 {
105 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
106
107 if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
108 {
109 SectorCount -= PartEntry->SectorCount.QuadPart;
110 }
111
112 Entry = Entry->Flink;
113 }
114 }
115 else
116 {
117 SectorCount = DiskEntry->SectorCount.QuadPart;
118 }
119
120 return SectorCount * DiskEntry->BytesPerSector;
121}
122
123
124VOID
126 _In_ PDISKENTRY DiskEntry)
127{
128 WCHAR szDiskSizeBuffer[8];
129 WCHAR szFreeSizeBuffer[8];
130 WCHAR szBuffer[40];
131 ULONGLONG DiskSize;
132 ULONGLONG FreeSize;
133
134 DiskSize = DiskEntry->SectorCount.QuadPart *
135 (ULONGLONG)DiskEntry->BytesPerSector;
136 PrintSize(DiskSize, szDiskSizeBuffer);
137
138 FreeSize = GetFreeDiskSize(DiskEntry);
139 PrintSize(FreeSize, szFreeSizeBuffer);
140
143 szBuffer, ARRAYSIZE(szBuffer));
144
146 (CurrentDisk == DiskEntry) ? L'*' : L' ',
147 DiskEntry->DiskNumber,
148 szBuffer,
149 szDiskSizeBuffer,
150 szFreeSizeBuffer,
151 L" ",
152 (DiskEntry->PartitionStyle == PARTITION_STYLE_GPT) ? L"*" : L" ");
153}
154
155
158 _In_ INT argc,
159 _In_ PWSTR *argv)
160{
162 PDISKENTRY DiskEntry;
163
164 /* Header labels */
165 ConPuts(StdOut, L"\n");
168
170 while (Entry != &DiskListHead)
171 {
172 DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
173
174 PrintDisk(DiskEntry);
175
176 Entry = Entry->Flink;
177 }
178
179 ConPuts(StdOut, L"\n\n");
180
181 return EXIT_SUCCESS;
182}
183
184
187 _In_ INT argc,
188 _In_ PWSTR *argv)
189{
191 PPARTENTRY PartEntry;
192 ULONGLONG PartSize;
193 ULONGLONG PartOffset;
194 ULONG PartNumber = 1;
195 BOOL bPartitionFound = FALSE;
196 WCHAR szPartitionTypeBuffer[40];
197 WCHAR szSizeBuffer[8];
198 WCHAR szOffsetBuffer[8];
199 INT nPartitionType;
200
201 if (CurrentDisk == NULL)
202 {
204 return EXIT_SUCCESS;
205 }
206
208 {
211 {
212 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
213 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
214 bPartitionFound = TRUE;
215
216 Entry = Entry->Flink;
217 }
218 }
220 {
223 {
224 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
225 if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
226 bPartitionFound = TRUE;
227
228 Entry = Entry->Flink;
229 }
230 }
231
232 if (bPartitionFound == FALSE)
233 {
234 ConPuts(StdOut, L"\n");
236 ConPuts(StdOut, L"\n");
237 return EXIT_SUCCESS;
238 }
239
240 /* Header labels */
241 ConPuts(StdOut, L"\n");
244
246 {
249 {
250 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
251
252 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
253 {
254 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
255 PrintSize(PartSize, szSizeBuffer);
256
257 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
258 PrintSize(PartOffset, szOffsetBuffer);
259
262 szPartitionTypeBuffer, ARRAYSIZE(szPartitionTypeBuffer));
263
265 (CurrentPartition == PartEntry) ? L'*' : L' ',
266 PartNumber++,
267 szPartitionTypeBuffer,
268 szSizeBuffer,
269 szOffsetBuffer);
270 }
271
272 Entry = Entry->Flink;
273 }
274
277 {
278 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
279
280 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
281 {
282 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
283 PrintSize(PartSize, szSizeBuffer);
284
285 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
286 PrintSize(PartOffset, szOffsetBuffer);
287
290 szPartitionTypeBuffer, ARRAYSIZE(szPartitionTypeBuffer));
292 (CurrentPartition == PartEntry) ? L'*' : L' ',
293 PartNumber++,
294 szPartitionTypeBuffer,
295 szSizeBuffer,
296 szOffsetBuffer);
297 }
298
299 Entry = Entry->Flink;
300 }
301 }
303 {
306 {
307 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
308
309 if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
310 {
311 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
312 PrintSize(PartSize, szSizeBuffer);
313
314 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
315 PrintSize(PartOffset, szOffsetBuffer);
316
317 if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
318 {
319 nPartitionType = IDS_PARTITION_TYPE_UNUSED;
320 }
321 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_BASIC_DATA_GUID))
322 {
323 nPartitionType = IDS_PARTITION_TYPE_PRIMARY;
324 }
325 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_SYSTEM_GUID))
326 {
327 nPartitionType = IDS_PARTITION_TYPE_SYSTEM;
328 }
329 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_MSFT_RESERVED_GUID))
330 {
331 nPartitionType = IDS_PARTITION_TYPE_RESERVED;
332 }
333 else
334 {
335 nPartitionType = IDS_PARTITION_TYPE_UNKNOWN;
336 }
337
339 nPartitionType,
340 szPartitionTypeBuffer, ARRAYSIZE(szPartitionTypeBuffer));
341
343 (CurrentPartition == PartEntry) ? L'*' : L' ',
344 PartNumber++,
345 szPartitionTypeBuffer,
346 szSizeBuffer,
347 szOffsetBuffer);
348 }
349
350 Entry = Entry->Flink;
351 }
352 }
353
354 ConPuts(StdOut, L"\n");
355
356 return EXIT_SUCCESS;
357}
358
359
360VOID
362 _In_ PVOLENTRY VolumeEntry)
363{
364 WCHAR szVolumeTypeBuffer[30];
365 WCHAR szInfoBuffer[16];
366 WCHAR szSizeBuffer[8];
367 INT nVolumeType;
368
369 switch (VolumeEntry->VolumeType)
370 {
372 nVolumeType = IDS_VOLUME_TYPE_DVD;
373 break;
374
376 nVolumeType = IDS_VOLUME_TYPE_PARTITION;
377 break;
378
380 nVolumeType = IDS_VOLUME_TYPE_REMOVABLE;
381 break;
382
384 default:
385 nVolumeType = IDS_VOLUME_TYPE_UNKNOWN;
386 break;
387 }
388
389 LoadStringW(GetModuleHandle(NULL), nVolumeType, szVolumeTypeBuffer, ARRAYSIZE(szVolumeTypeBuffer));
390
391 PrintSize(VolumeEntry->Size.QuadPart, szSizeBuffer);
392
393 szInfoBuffer[0] = UNICODE_NULL;
394 if (VolumeEntry->IsSystem)
395 LoadStringW(GetModuleHandle(NULL), IDS_INFO_SYSTEM, szInfoBuffer, ARRAYSIZE(szInfoBuffer));
396 else if (VolumeEntry->IsBoot)
397 LoadStringW(GetModuleHandle(NULL), IDS_INFO_BOOT, szInfoBuffer, ARRAYSIZE(szInfoBuffer));
398
400 (CurrentVolume == VolumeEntry) ? L'*' : L' ',
401 VolumeEntry->VolumeNumber,
402 VolumeEntry->DriveLetter,
403 (VolumeEntry->pszLabel) ? VolumeEntry->pszLabel : L"",
404 (VolumeEntry->pszFilesystem) ? VolumeEntry->pszFilesystem : L"",
405 szVolumeTypeBuffer,
406 szSizeBuffer,
407 L"",
408 szInfoBuffer);
409}
410
411
414 _In_ INT argc,
415 _In_ PWSTR *argv)
416{
418 PVOLENTRY VolumeEntry;
419
420 ConPuts(StdOut, L"\n");
423
425 while (Entry != &VolumeListHead)
426 {
427 VolumeEntry = CONTAINING_RECORD(Entry, VOLENTRY, ListEntry);
428
429 PrintVolume(VolumeEntry);
430
431 Entry = Entry->Flink;
432 }
433
434 ConPuts(StdOut, L"\n");
435
436 return EXIT_SUCCESS;
437}
438
439
442 _In_ INT argc,
443 _In_ PWSTR *argv)
444{
445 ConPuts(StdOut, L"The LIST VDISK command is not implemented yet!\n");
446 return EXIT_SUCCESS;
447}
static int argc
Definition: ServiceArgs.c:12
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
#define StdOut
Definition: fc.c:14
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
EXIT_CODE ListPartition(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:186
EXIT_CODE ListVolume(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:413
EXIT_CODE ListVirtualDisk(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:441
EXIT_CODE ListDisk(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:157
static ULONGLONG GetFreeDiskSize(_In_ PDISKENTRY DiskEntry)
Definition: list.c:60
static VOID PrintSize(_In_ ULONGLONG ullSize, _Out_ PWSTR pszSizeBuffer)
Definition: list.c:18
VOID PrintDisk(_In_ PDISKENTRY DiskEntry)
Definition: list.c:125
VOID PrintVolume(_In_ PVOLENTRY VolumeEntry)
Definition: list.c:361
#define IDS_PARTITION_TYPE_EXTENDED
Definition: resource.h:259
#define IDS_VOLUME_TYPE_DVD
Definition: resource.h:267
#define IDS_PARTITION_TYPE_UNKNOWN
Definition: resource.h:264
#define IDS_LIST_DISK_LINE
Definition: resource.h:104
#define IDS_LIST_PARTITION_NO_DISK
Definition: resource.h:109
#define IDS_VOLUME_TYPE_REMOVABLE
Definition: resource.h:269
#define IDS_LIST_DISK_HEAD
Definition: resource.h:103
#define IDS_PARTITION_TYPE_LOGICAL
Definition: resource.h:260
#define IDS_LIST_VOLUME_LINE
Definition: resource.h:112
#define IDS_LIST_VOLUME_HEAD
Definition: resource.h:111
#define IDS_STATUS_ONLINE
Definition: resource.h:148
#define IDS_LIST_DISK_FORMAT
Definition: resource.h:105
#define IDS_PARTITION_TYPE_SYSTEM
Definition: resource.h:263
#define IDS_LIST_PARTITION_HEAD
Definition: resource.h:106
#define IDS_INFO_SYSTEM
Definition: resource.h:152
#define IDS_VOLUME_TYPE_PARTITION
Definition: resource.h:268
#define IDS_UNIT_GB
Definition: resource.h:273
#define IDS_UNIT_B
Definition: resource.h:276
#define IDS_PARTITION_TYPE_PRIMARY
Definition: resource.h:261
#define IDS_UNIT_MB
Definition: resource.h:274
#define IDS_PARTITION_TYPE_UNUSED
Definition: resource.h:265
#define IDS_PARTITION_TYPE_RESERVED
Definition: resource.h:262
#define IDS_LIST_PARTITION_LINE
Definition: resource.h:107
#define IDS_LIST_VOLUME_FORMAT
Definition: resource.h:113
#define IDS_VOLUME_TYPE_UNKNOWN
Definition: resource.h:270
#define IDS_LIST_PARTITION_NONE
Definition: resource.h:110
#define IDS_UNIT_KB
Definition: resource.h:275
#define IDS_INFO_BOOT
Definition: resource.h:151
#define IDS_UNIT_TB
Definition: resource.h:272
#define IDS_LIST_PARTITION_FORMAT
Definition: resource.h:108
#define PARTITION_ENTRY_UNUSED
Definition: disk.h:71
LIST_ENTRY VolumeListHead
Definition: partlist.c:73
@ VOLUME_TYPE_UNKNOWN
Definition: diskpart.h:114
@ VOLUME_TYPE_REMOVABLE
Definition: diskpart.h:113
@ VOLUME_TYPE_CDROM
Definition: diskpart.h:111
@ VOLUME_TYPE_PARTITION
Definition: diskpart.h:112
#define SIZE_10TB
Definition: diskpart.h:273
enum _EXIT_CODE EXIT_CODE
#define SIZE_1TB
Definition: diskpart.h:272
PDISKENTRY CurrentDisk
Definition: partlist.c:75
LIST_ENTRY DiskListHead
Definition: partlist.c:71
#define SIZE_10GB
Definition: diskpart.h:271
#define SIZE_1KB
Definition: diskpart.h:266
#define SIZE_1MB
Definition: diskpart.h:268
#define SIZE_10MB
Definition: diskpart.h:269
PVOLENTRY CurrentVolume
Definition: partlist.c:77
#define SIZE_10KB
Definition: diskpart.h:267
#define SIZE_1GB
Definition: diskpart.h:270
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
INT WINAPI DECLSPEC_HOTPATCH LoadStringW(HINSTANCE instance, UINT resource_id, LPWSTR buffer, INT buflen)
Definition: string.c:1220
#define swprintf
Definition: precomp.h:40
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
@ PARTITION_STYLE_GPT
Definition: imports.h:202
@ PARTITION_STYLE_MBR
Definition: imports.h:201
#define argv
Definition: mplay32.c:18
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define UNICODE_NULL
#define IsContainerPartition(PartitionType)
Definition: ntdddisk.h:321
ULONG SectorCount
Definition: part_xbox.c:31
#define IsEqualGUID(rguid1, rguid2)
Definition: guiddef.h:147
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
ULONGLONG RoundingDivide(IN ULONGLONG Dividend, IN ULONGLONG Divisor)
Definition: partlist.c:95
base of all file and directory entries
Definition: entries.h:83
LIST_ENTRY LogicalPartListHead
Definition: partlist.h:150
ULONG BytesPerSector
Definition: partlist.h:113
DWORD PartitionStyle
Definition: diskpart.h:223
LIST_ENTRY PrimaryPartListHead
Definition: partlist.h:149
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
ULARGE_INTEGER SectorCount
Definition: partlist.h:70
MBR_PARTITION_DATA Mbr
Definition: diskpart.h:141
GPT_PARTITION_DATA Gpt
Definition: diskpart.h:142
ULARGE_INTEGER StartSector
Definition: partlist.h:69
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
uint16_t * PWSTR
Definition: typedefs.h:56
int32_t INT
Definition: typedefs.h:58
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
static PPARTENTRY CurrentPartition
Definition: usetup.c:78
#define GetModuleHandle
Definition: winbase.h:3576
__wchar_t WCHAR
Definition: xmlstorage.h:180