ReactOS 0.4.16-dev-2104-gb84fa49
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
19 _In_ PDISKENTRY DiskEntry)
20{
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) &&
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
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
82VOID
84 _In_ PDISKENTRY DiskEntry)
85{
86 ULONGLONG DiskSize;
87 ULONGLONG FreeSize;
88 LPWSTR lpSizeUnit;
89 LPWSTR lpFreeUnit;
90
91 DiskSize = DiskEntry->SectorCount.QuadPart *
92 (ULONGLONG)DiskEntry->BytesPerSector;
93
94 if (DiskSize >= SIZE_10TB) /* 10 TB */
95 {
96 DiskSize = RoundingDivide(DiskSize, SIZE_1TB);
97 lpSizeUnit = L"TB";
98 }
99 else if (DiskSize >= SIZE_10GB) /* 10 GB */
100 {
101 DiskSize = RoundingDivide(DiskSize, SIZE_1GB);
102 lpSizeUnit = L"GB";
103 }
104 else
105 {
106 DiskSize = RoundingDivide(DiskSize, SIZE_1MB);
107 if (DiskSize == 0)
108 DiskSize = 1;
109 lpSizeUnit = L"MB";
110 }
111
112 FreeSize = GetFreeDiskSize(DiskEntry);
113 if (FreeSize >= SIZE_10TB) /* 10 TB */
114 {
115 FreeSize = RoundingDivide(FreeSize, SIZE_1TB);
116 lpFreeUnit = L"TB";
117 }
118 else if (FreeSize >= SIZE_10GB) /* 10 GB */
119 {
120 FreeSize = RoundingDivide(FreeSize, SIZE_1GB);
121 lpFreeUnit = L"GB";
122 }
123 else if (FreeSize >= SIZE_10MB) /* 10 MB */
124 {
125 FreeSize = RoundingDivide(FreeSize, SIZE_1MB);
126 lpFreeUnit = L"MB";
127 }
128 else if (FreeSize >= SIZE_10KB) /* 10 KB */
129 {
130 FreeSize = RoundingDivide(FreeSize, SIZE_1KB);
131 lpFreeUnit = L"KB";
132 }
133 else
134 {
135 lpFreeUnit = L"B";
136 }
137
139 (CurrentDisk == DiskEntry) ? L'*' : L' ',
140 DiskEntry->DiskNumber,
141 L"Online",
142 DiskSize,
143 lpSizeUnit,
144 FreeSize,
145 lpFreeUnit,
146 L" ",
147 (DiskEntry->PartitionStyle == PARTITION_STYLE_GPT) ? L"*" : L" ");
148}
149
150
153 _In_ INT argc,
154 _In_ PWSTR *argv)
155{
157 PDISKENTRY DiskEntry;
158
159 /* Header labels */
160 ConPuts(StdOut, L"\n");
163
165 while (Entry != &DiskListHead)
166 {
167 DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
168
169 PrintDisk(DiskEntry);
170
171 Entry = Entry->Flink;
172 }
173
174 ConPuts(StdOut, L"\n\n");
175
176 return EXIT_SUCCESS;
177}
178
179
182 _In_ INT argc,
183 _In_ PWSTR *argv)
184{
186 PPARTENTRY PartEntry;
187 ULONGLONG PartSize;
188 ULONGLONG PartOffset;
189 LPWSTR lpSizeUnit;
190 LPWSTR lpOffsetUnit;
191 ULONG PartNumber = 1;
192 BOOL bPartitionFound = FALSE;
193
194 if (CurrentDisk == NULL)
195 {
197 return EXIT_SUCCESS;
198 }
199
201 {
204 {
205 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
206 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
207 bPartitionFound = TRUE;
208
209 Entry = Entry->Flink;
210 }
211 }
213 {
216 {
217 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
218 if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
219 bPartitionFound = TRUE;
220
221 Entry = Entry->Flink;
222 }
223 }
224
225 if (bPartitionFound == FALSE)
226 {
227 ConPuts(StdOut, L"\n");
229 ConPuts(StdOut, L"\n");
230 return EXIT_SUCCESS;
231 }
232
233 /* Header labels */
234 ConPuts(StdOut, L"\n");
237
239 {
242 {
243 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
244
245 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
246 {
247 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
248
249 if (PartSize >= SIZE_10TB) /* 10 TB */
250 {
251 PartSize = RoundingDivide(PartSize, SIZE_1TB);
252 lpSizeUnit = L"TB";
253 }
254 else if (PartSize >= SIZE_10GB) /* 10 GB */
255 {
256 PartSize = RoundingDivide(PartSize, SIZE_1GB);
257 lpSizeUnit = L"GB";
258 }
259 else if (PartSize >= SIZE_10MB) /* 10 MB */
260 {
261 PartSize = RoundingDivide(PartSize, SIZE_1MB);
262 lpSizeUnit = L"MB";
263 }
264 else
265 {
266 PartSize = RoundingDivide(PartSize, SIZE_1KB);
267 lpSizeUnit = L"KB";
268 }
269
270 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
271
272 if (PartOffset >= SIZE_10TB) /* 10 TB */
273 {
274 PartOffset = RoundingDivide(PartOffset, SIZE_1TB);
275 lpOffsetUnit = L"TB";
276 }
277 else if (PartOffset >= SIZE_10GB) /* 10 GB */
278 {
279 PartOffset = RoundingDivide(PartOffset, SIZE_1GB);
280 lpOffsetUnit = L"GB";
281 }
282 else if (PartOffset >= SIZE_10MB) /* 10 MB */
283 {
284 PartOffset = RoundingDivide(PartOffset, SIZE_1MB);
285 lpOffsetUnit = L"MB";
286 }
287 else
288 {
289 PartOffset = RoundingDivide(PartOffset, SIZE_1KB);
290 lpOffsetUnit = L"KB";
291 }
292
294 (CurrentPartition == PartEntry) ? L'*' : L' ',
295 PartNumber++,
296 IsContainerPartition(PartEntry->Mbr.PartitionType) ? L"Extended" : L"Primary",
297 PartSize,
298 lpSizeUnit,
299 PartOffset,
300 lpOffsetUnit);
301 }
302
303 Entry = Entry->Flink;
304 }
305
308 {
309 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
310
311 if (PartEntry->Mbr.PartitionType != PARTITION_ENTRY_UNUSED)
312 {
313 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
314
315 if (PartSize >= SIZE_10TB) /* 10 TB */
316 {
317 PartSize = RoundingDivide(PartSize, SIZE_1TB);
318 lpSizeUnit = L"TB";
319 }
320 else if (PartSize >= SIZE_10GB) /* 10 GB */
321 {
322 PartSize = RoundingDivide(PartSize, SIZE_1GB);
323 lpSizeUnit = L"GB";
324 }
325 else if (PartSize >= SIZE_10MB) /* 10 MB */
326 {
327 PartSize = RoundingDivide(PartSize, SIZE_1MB);
328 lpSizeUnit = L"MB";
329 }
330 else
331 {
332 PartSize = RoundingDivide(PartSize, SIZE_1KB);
333 lpSizeUnit = L"KB";
334 }
335
336 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
337
338 if (PartOffset >= SIZE_10TB) /* 10 TB */
339 {
340 PartOffset = RoundingDivide(PartOffset, SIZE_1TB);
341 lpOffsetUnit = L"TB";
342 }
343 else if (PartOffset >= SIZE_10GB) /* 10 GB */
344 {
345 PartOffset = RoundingDivide(PartOffset, SIZE_1GB);
346 lpOffsetUnit = L"GB";
347 }
348 else if (PartOffset >= SIZE_10MB) /* 10 MB */
349 {
350 PartOffset = RoundingDivide(PartOffset, SIZE_1MB);
351 lpOffsetUnit = L"MB";
352 }
353 else
354 {
355 PartOffset = RoundingDivide(PartOffset, SIZE_1KB);
356 lpOffsetUnit = L"KB";
357 }
358
360 (CurrentPartition == PartEntry) ? L'*' : L' ',
361 PartNumber++,
362 L"Logical",
363 PartSize,
364 lpSizeUnit,
365 PartOffset,
366 lpOffsetUnit);
367 }
368
369 Entry = Entry->Flink;
370 }
371 }
373 {
374 LPWSTR lpPartitionType;
375
378 {
379 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
380
381 if (!IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
382 {
383 PartSize = PartEntry->SectorCount.QuadPart * CurrentDisk->BytesPerSector;
384
385 if (PartSize >= SIZE_10TB) /* 10 TB */
386 {
387 PartSize = RoundingDivide(PartSize, SIZE_1TB);
388 lpSizeUnit = L"TB";
389 }
390 else if (PartSize >= SIZE_10GB) /* 10 GB */
391 {
392 PartSize = RoundingDivide(PartSize, SIZE_1GB);
393 lpSizeUnit = L"GB";
394 }
395 else if (PartSize >= SIZE_10MB) /* 10 MB */
396 {
397 PartSize = RoundingDivide(PartSize, SIZE_1MB);
398 lpSizeUnit = L"MB";
399 }
400 else
401 {
402 PartSize = RoundingDivide(PartSize, SIZE_1KB);
403 lpSizeUnit = L"KB";
404 }
405
406 PartOffset = PartEntry->StartSector.QuadPart * CurrentDisk->BytesPerSector;
407
408 if (PartOffset >= SIZE_10TB) /* 10 TB */
409 {
410 PartOffset = RoundingDivide(PartOffset, SIZE_1TB);
411 lpOffsetUnit = L"TB";
412 }
413 else if (PartOffset >= SIZE_10GB) /* 10 GB */
414 {
415 PartOffset = RoundingDivide(PartOffset, SIZE_1GB);
416 lpOffsetUnit = L"GB";
417 }
418 else if (PartOffset >= SIZE_10MB) /* 10 MB */
419 {
420 PartOffset = RoundingDivide(PartOffset, SIZE_1MB);
421 lpOffsetUnit = L"MB";
422 }
423 else
424 {
425 PartOffset = RoundingDivide(PartOffset, SIZE_1KB);
426 lpOffsetUnit = L"KB";
427 }
428
429 if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_ENTRY_UNUSED_GUID))
430 {
431 lpPartitionType = L"Unused";
432 }
433 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_BASIC_DATA_GUID))
434 {
435 lpPartitionType = L"Primary";
436 }
437 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_SYSTEM_GUID))
438 {
439 lpPartitionType = L"System";
440 }
441 else if (IsEqualGUID(&PartEntry->Gpt.PartitionType, &PARTITION_MSFT_RESERVED_GUID))
442 {
443 lpPartitionType = L"Reserved";
444 }
445 else
446 {
447 lpPartitionType = L"Other"; /* ??? */
448 }
449
451 (CurrentPartition == PartEntry) ? L'*' : L' ',
452 PartNumber++,
453 lpPartitionType,
454 PartSize,
455 lpSizeUnit,
456 PartOffset,
457 lpOffsetUnit);
458 }
459
460 Entry = Entry->Flink;
461 }
462 }
463
464 ConPuts(StdOut, L"\n");
465
466 return EXIT_SUCCESS;
467}
468
469
470VOID
472 _In_ PVOLENTRY VolumeEntry)
473{
474 ULONGLONG VolumeSize;
475 PWSTR pszSizeUnit;
476 PWSTR pszVolumeType;
477
478 VolumeSize = VolumeEntry->Size.QuadPart;
479 if (VolumeSize >= SIZE_10TB) /* 10 TB */
480 {
481 VolumeSize = RoundingDivide(VolumeSize, SIZE_1TB);
482 pszSizeUnit = L"TB";
483 }
484 else if (VolumeSize >= SIZE_10GB) /* 10 GB */
485 {
486 VolumeSize = RoundingDivide(VolumeSize, SIZE_1GB);
487 pszSizeUnit = L"GB";
488 }
489 else if (VolumeSize >= SIZE_10MB) /* 10 MB */
490 {
491 VolumeSize = RoundingDivide(VolumeSize, SIZE_1MB);
492 pszSizeUnit = L"MB";
493 }
494 else
495 {
496 VolumeSize = RoundingDivide(VolumeSize, SIZE_1KB);
497 pszSizeUnit = L"KB";
498 }
499
500 switch (VolumeEntry->VolumeType)
501 {
503 pszVolumeType = L"DVD";
504 break;
506 pszVolumeType = L"Partition";
507 break;
509 pszVolumeType = L"Removable";
510 break;
512 default:
513 pszVolumeType = L"Unknown";
514 break;
515 }
516
518 (CurrentVolume == VolumeEntry) ? L'*' : L' ',
519 VolumeEntry->VolumeNumber,
520 VolumeEntry->DriveLetter,
521 (VolumeEntry->pszLabel) ? VolumeEntry->pszLabel : L"",
522 (VolumeEntry->pszFilesystem) ? VolumeEntry->pszFilesystem : L"",
523 pszVolumeType,
524 VolumeSize, pszSizeUnit,
525 L"", L"");
526}
527
528
531 _In_ INT argc,
532 _In_ PWSTR *argv)
533{
535 PVOLENTRY VolumeEntry;
536
537 ConPuts(StdOut, L"\n");
540
542 while (Entry != &VolumeListHead)
543 {
544 VolumeEntry = CONTAINING_RECORD(Entry, VOLENTRY, ListEntry);
545
546 PrintVolume(VolumeEntry);
547
548 Entry = Entry->Flink;
549 }
550
551 ConPuts(StdOut, L"\n");
552
553 return EXIT_SUCCESS;
554}
555
556
559 _In_ INT argc,
560 _In_ PWSTR *argv)
561{
562 ConPuts(StdOut, L"ListVirtualDisk()!\n");
563 return EXIT_SUCCESS;
564}
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:181
EXIT_CODE ListVolume(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:530
EXIT_CODE ListVirtualDisk(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:558
EXIT_CODE ListDisk(_In_ INT argc, _In_ PWSTR *argv)
Definition: list.c:152
static ULONGLONG GetFreeDiskSize(_In_ PDISKENTRY DiskEntry)
Definition: list.c:18
VOID PrintDisk(_In_ PDISKENTRY DiskEntry)
Definition: list.c:83
VOID PrintVolume(_In_ PVOLENTRY VolumeEntry)
Definition: list.c:471
#define IDS_LIST_DISK_LINE
Definition: resource.h:102
#define IDS_LIST_PARTITION_NO_DISK
Definition: resource.h:107
#define IDS_LIST_DISK_HEAD
Definition: resource.h:101
#define IDS_LIST_VOLUME_LINE
Definition: resource.h:110
#define IDS_LIST_VOLUME_HEAD
Definition: resource.h:109
#define IDS_LIST_DISK_FORMAT
Definition: resource.h:103
#define IDS_LIST_PARTITION_HEAD
Definition: resource.h:104
#define IDS_LIST_PARTITION_LINE
Definition: resource.h:105
#define IDS_LIST_VOLUME_FORMAT
Definition: resource.h:111
#define IDS_LIST_PARTITION_NONE
Definition: resource.h:108
#define IDS_LIST_PARTITION_FORMAT
Definition: resource.h:106
#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:264
enum _EXIT_CODE EXIT_CODE
#define SIZE_1TB
Definition: diskpart.h:263
PDISKENTRY CurrentDisk
Definition: partlist.c:75
LIST_ENTRY DiskListHead
Definition: partlist.c:71
#define SIZE_10GB
Definition: diskpart.h:262
#define SIZE_1KB
Definition: diskpart.h:257
#define SIZE_1MB
Definition: diskpart.h:259
#define SIZE_10MB
Definition: diskpart.h:260
PVOLENTRY CurrentVolume
Definition: partlist.c:77
#define SIZE_10KB
Definition: diskpart.h:258
#define SIZE_1GB
Definition: diskpart.h:261
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
if(dx< 0)
Definition: linetemp.h:194
@ PARTITION_STYLE_GPT
Definition: imports.h:202
@ PARTITION_STYLE_MBR
Definition: imports.h:201
#define argv
Definition: mplay32.c:18
#define _In_
Definition: no_sal2.h:158
#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:219
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
WCHAR * LPWSTR
Definition: xmlstorage.h:184