ReactOS 0.4.16-dev-2293-g4d8327b
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 pszOutBuffer,
21 _In_ ULONG ulOutBufferSize)
22{
23 WCHAR szUnitBuffer[8];
24 INT nUnitId;
25
26 if (ullSize >= SIZE_10TB) /* 10 TB */
27 {
28 ullSize = RoundingDivide(ullSize, SIZE_1TB);
29 nUnitId = IDS_UNIT_TB;
30 }
31 else if (ullSize >= SIZE_10GB) /* 10 GB */
32 {
33 ullSize = RoundingDivide(ullSize, SIZE_1GB);
34 nUnitId = IDS_UNIT_GB;
35 }
36 else if (ullSize >= SIZE_10MB) /* 10 MB */
37 {
38 ullSize = RoundingDivide(ullSize, SIZE_1MB);
39 nUnitId = IDS_UNIT_MB;
40 }
41 else if (ullSize >= SIZE_10KB) /* 10 KB */
42 {
43 ullSize = RoundingDivide(ullSize, SIZE_1KB);
44 nUnitId = IDS_UNIT_KB;
45 }
46 else
47 {
48 nUnitId = IDS_UNIT_B;
49 }
50
52 nUnitId,
53 szUnitBuffer, ARRAYSIZE(szUnitBuffer));
54
55 swprintf(pszOutBuffer, L"%4I64u %-2s", ullSize, szUnitBuffer);
56 StringCchPrintfW(pszOutBuffer,
57 ulOutBufferSize,
58 L"%4I64u %-2s", ullSize, szUnitBuffer);
59}
60
61
62static
65 _In_ PDISKENTRY DiskEntry)
66{
69 PPARTENTRY PartEntry;
70
71 if (DiskEntry->PartitionStyle == PARTITION_STYLE_MBR)
72 {
73 SectorCount = DiskEntry->EndSector.QuadPart - DiskEntry->StartSector.QuadPart + 1;
74
75 Entry = DiskEntry->PrimaryPartListHead.Flink;
76 while (Entry != &DiskEntry->PrimaryPartListHead)
77 {
78 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
79
80 if ((PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED) &&
82 {
83 SectorCount -= PartEntry->SectorCount.QuadPart;
84 }
85
86 Entry = Entry->Flink;
87 }
88
89 Entry = DiskEntry->LogicalPartListHead.Flink;
90 while (Entry != &DiskEntry->LogicalPartListHead)
91 {
92 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
93
95 {
96 SectorCount -= PartEntry->SectorCount.QuadPart;
97 }
98
99 Entry = Entry->Flink;
100 }
101 }
102 else if (DiskEntry->PartitionStyle == PARTITION_STYLE_GPT)
103 {
104 SectorCount = DiskEntry->EndSector.QuadPart - DiskEntry->StartSector.QuadPart + 1;
105
106 Entry = DiskEntry->PrimaryPartListHead.Flink;
107 while (Entry != &DiskEntry->PrimaryPartListHead)
108 {
109 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
110
111 if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
112 {
113 SectorCount -= PartEntry->SectorCount.QuadPart;
114 }
115
116 Entry = Entry->Flink;
117 }
118 }
119 else
120 {
121 SectorCount = DiskEntry->SectorCount.QuadPart;
122 }
123
124 return SectorCount * DiskEntry->BytesPerSector;
125}
126
127
128VOID
130 _In_ PDISKENTRY DiskEntry)
131{
132 WCHAR szDiskSizeBuffer[8];
133 WCHAR szFreeSizeBuffer[8];
134 WCHAR szBuffer[40];
135 ULONGLONG DiskSize;
136 ULONGLONG FreeSize;
137
138 DiskSize = DiskEntry->SectorCount.QuadPart *
139 (ULONGLONG)DiskEntry->BytesPerSector;
140 PrintSize(DiskSize, szDiskSizeBuffer, ARRAYSIZE(szDiskSizeBuffer));
141
142 FreeSize = GetFreeDiskSize(DiskEntry);
143 PrintSize(FreeSize, szFreeSizeBuffer, ARRAYSIZE(szFreeSizeBuffer));
144
147 szBuffer, ARRAYSIZE(szBuffer));
148
150 (CurrentDisk == DiskEntry) ? L'*' : L' ',
151 DiskEntry->DiskNumber,
152 szBuffer,
153 szDiskSizeBuffer,
154 szFreeSizeBuffer,
155 L" ",
156 (DiskEntry->PartitionStyle == PARTITION_STYLE_GPT) ? L"*" : L" ");
157}
158
159
162 _In_ INT argc,
163 _In_ PWSTR *argv)
164{
166 PDISKENTRY DiskEntry;
167
168 /* Header labels */
169 ConPuts(StdOut, L"\n");
172
174 while (Entry != &DiskListHead)
175 {
176 DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
177
178 PrintDisk(DiskEntry);
179
180 Entry = Entry->Flink;
181 }
182
183 ConPuts(StdOut, L"\n\n");
184
185 return EXIT_SUCCESS;
186}
187
188
191 _In_ INT argc,
192 _In_ PWSTR *argv)
193{
195 PPARTENTRY PartEntry;
196 ULONGLONG PartSize;
197 ULONGLONG PartOffset;
198 ULONG PartNumber = 1;
199 BOOL bPartitionFound = FALSE;
200 WCHAR szPartitionTypeBuffer[40];
201 WCHAR szSizeBuffer[8];
202 WCHAR szOffsetBuffer[8];
203 INT nPartitionType;
204
205 if (CurrentDisk == NULL)
206 {
208 return EXIT_SUCCESS;
209 }
210
212 {
215 {
216 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
217 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
218 bPartitionFound = TRUE;
219
220 Entry = Entry->Flink;
221 }
222 }
224 {
227 {
228 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
229 if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
230 bPartitionFound = TRUE;
231
232 Entry = Entry->Flink;
233 }
234 }
235
236 if (bPartitionFound == FALSE)
237 {
238 ConPuts(StdOut, L"\n");
240 ConPuts(StdOut, L"\n");
241 return EXIT_SUCCESS;
242 }
243
244 /* Header labels */
245 ConPuts(StdOut, L"\n");
248
250 {
253 {
254 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
255
256 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
257 {
258 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
259 PrintSize(PartSize, szSizeBuffer, ARRAYSIZE(szSizeBuffer));
260
261 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
262 PrintSize(PartOffset, szOffsetBuffer, ARRAYSIZE(szOffsetBuffer));
263
266 szPartitionTypeBuffer, ARRAYSIZE(szPartitionTypeBuffer));
267
269 (CurrentPartition == PartEntry) ? L'*' : L' ',
270 PartNumber++,
271 szPartitionTypeBuffer,
272 szSizeBuffer,
273 szOffsetBuffer);
274 }
275
276 Entry = Entry->Flink;
277 }
278
281 {
282 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
283
284 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
285 {
286 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
287 PrintSize(PartSize, szSizeBuffer, ARRAYSIZE(szSizeBuffer));
288
289 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
290 PrintSize(PartOffset, szOffsetBuffer, ARRAYSIZE(szOffsetBuffer));
291
294 szPartitionTypeBuffer, ARRAYSIZE(szPartitionTypeBuffer));
296 (CurrentPartition == PartEntry) ? L'*' : L' ',
297 PartNumber++,
298 szPartitionTypeBuffer,
299 szSizeBuffer,
300 szOffsetBuffer);
301 }
302
303 Entry = Entry->Flink;
304 }
305 }
307 {
310 {
311 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
312
313 if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
314 {
315 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
316 PrintSize(PartSize, szSizeBuffer, ARRAYSIZE(szSizeBuffer));
317
318 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
319 PrintSize(PartOffset, szOffsetBuffer, ARRAYSIZE(szOffsetBuffer));
320
321 if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
322 {
323 nPartitionType = IDS_PARTITION_TYPE_UNUSED;
324 }
325 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_BASIC_DATA_GUID))
326 {
327 nPartitionType = IDS_PARTITION_TYPE_PRIMARY;
328 }
329 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_SYSTEM_GUID))
330 {
331 nPartitionType = IDS_PARTITION_TYPE_SYSTEM;
332 }
333 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_MSFT_RESERVED_GUID))
334 {
335 nPartitionType = IDS_PARTITION_TYPE_RESERVED;
336 }
337 else
338 {
339 nPartitionType = IDS_PARTITION_TYPE_UNKNOWN;
340 }
341
343 nPartitionType,
344 szPartitionTypeBuffer, ARRAYSIZE(szPartitionTypeBuffer));
345
347 (CurrentPartition == PartEntry) ? L'*' : L' ',
348 PartNumber++,
349 szPartitionTypeBuffer,
350 szSizeBuffer,
351 szOffsetBuffer);
352 }
353
354 Entry = Entry->Flink;
355 }
356 }
357
358 ConPuts(StdOut, L"\n");
359
360 return EXIT_SUCCESS;
361}
362
363
364VOID
366 _In_ PVOLENTRY VolumeEntry)
367{
368 WCHAR szVolumeTypeBuffer[30];
369 WCHAR szInfoBuffer[16];
370 WCHAR szSizeBuffer[8];
371 INT nVolumeType;
372
373 switch (VolumeEntry->VolumeType)
374 {
376 nVolumeType = IDS_VOLUME_TYPE_DVD;
377 break;
378
380 nVolumeType = IDS_VOLUME_TYPE_PARTITION;
381 break;
382
384 nVolumeType = IDS_VOLUME_TYPE_REMOVABLE;
385 break;
386
388 default:
389 nVolumeType = IDS_VOLUME_TYPE_UNKNOWN;
390 break;
391 }
392
393 LoadStringW(GetModuleHandle(NULL), nVolumeType, szVolumeTypeBuffer, ARRAYSIZE(szVolumeTypeBuffer));
394
395 PrintSize(VolumeEntry->Size.QuadPart, szSizeBuffer, ARRAYSIZE(szSizeBuffer));
396
397 szInfoBuffer[0] = UNICODE_NULL;
398 if (VolumeEntry->IsSystem)
399 LoadStringW(GetModuleHandle(NULL), IDS_INFO_SYSTEM, szInfoBuffer, ARRAYSIZE(szInfoBuffer));
400 else if (VolumeEntry->IsBoot)
401 LoadStringW(GetModuleHandle(NULL), IDS_INFO_BOOT, szInfoBuffer, ARRAYSIZE(szInfoBuffer));
402
404 (CurrentVolume == VolumeEntry) ? L'*' : L' ',
405 VolumeEntry->VolumeNumber,
406 VolumeEntry->DriveLetter,
407 (VolumeEntry->pszLabel) ? VolumeEntry->pszLabel : L"",
408 (VolumeEntry->pszFilesystem) ? VolumeEntry->pszFilesystem : L"",
409 szVolumeTypeBuffer,
410 szSizeBuffer,
411 L"",
412 szInfoBuffer);
413}
414
415
418 _In_ INT argc,
419 _In_ PWSTR *argv)
420{
422 PVOLENTRY VolumeEntry;
423
424 ConPuts(StdOut, L"\n");
427
429 while (Entry != &VolumeListHead)
430 {
431 VolumeEntry = CONTAINING_RECORD(Entry, VOLENTRY, ListEntry);
432
433 PrintVolume(VolumeEntry);
434
435 Entry = Entry->Flink;
436 }
437
438 ConPuts(StdOut, L"\n");
439
440 return EXIT_SUCCESS;
441}
442
443
446 _In_ INT argc,
447 _In_ PWSTR *argv)
448{
449 ConPuts(StdOut, L"The LIST VDISK command is not implemented yet!\n");
450 return EXIT_SUCCESS;
451}
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:190
EXIT_CODE ListVolume(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:417
EXIT_CODE ListVirtualDisk(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:445
EXIT_CODE ListDisk(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:161
static ULONGLONG GetFreeDiskSize(_In_ PDISKENTRY DiskEntry)
Definition: list.c:64
VOID PrintDisk(_In_ PDISKENTRY DiskEntry)
Definition: list.c:129
static VOID PrintSize(_In_ ULONGLONG ullSize, _Out_ PWSTR pszOutBuffer, _In_ ULONG ulOutBufferSize)
Definition: list.c:18
VOID PrintVolume(_In_ PVOLENTRY VolumeEntry)
Definition: list.c:365
#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
STRSAFEAPI StringCchPrintfW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:530
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