Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353/* * PROJECT: ReactOS DiskPart * LICENSE: GPL - See COPYING in the top level directory * FILE: base/system/diskpart/delete.c * PURPOSE: Manages all the partitions of the OS in an interactive way. * PROGRAMMERS: Lee Schroeder */
#include "diskpart.h"
#define NDEBUG#include <debug.h>
staticBOOLIsKnownPartition( _In_ PPARTENTRY PartEntry){ if (IsRecognizedPartition(PartEntry->Mbr.PartitionType) || IsContainerPartition(PartEntry->Mbr.PartitionType)) return TRUE;
return FALSE;}
EXIT_CODEDeleteDisk( _In_ INT argc, _In_ PWSTR *argv){ return EXIT_SUCCESS;}
staticVOIDDeleteMbrPartition( _In_ BOOL bOverride){ PPARTENTRY PrevPartEntry; PPARTENTRY NextPartEntry; PPARTENTRY LogicalPartEntry; PLIST_ENTRY Entry; NTSTATUS Status;
ASSERT(CurrentPartition->Mbr.PartitionType != PARTITION_ENTRY_UNUSED);
/* Clear the system partition if it is being deleted */#if 0 if (List->SystemPartition == PartEntry) { ASSERT(List->SystemPartition); List->SystemPartition = NULL; }#endif
if ((bOverride == FALSE) && (IsKnownPartition(CurrentPartition) == FALSE)) { ConResPuts(StdOut, IDS_DELETE_PARTITION_FAIL); return; }
/* Check which type of partition (primary/logical or extended) is being deleted */ if (CurrentDisk->ExtendedPartition == CurrentPartition) { /* An extended partition is being deleted: delete all logical partition entries */ while (!IsListEmpty(&CurrentDisk->LogicalPartListHead)) { Entry = RemoveHeadList(&CurrentDisk->LogicalPartListHead); LogicalPartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
/* Dismount the logical partition */ DismountVolume(LogicalPartEntry);
/* Delete it */ RtlFreeHeap(RtlGetProcessHeap(), 0, LogicalPartEntry); }
CurrentDisk->ExtendedPartition = NULL; } else { /* A primary partition is being deleted: dismount it */ DismountVolume(CurrentPartition); }
/* Adjust the unpartitioned disk space entries */
/* Get pointer to previous and next unpartitioned entries */ PrevPartEntry = GetPrevUnpartitionedEntry(CurrentPartition); NextPartEntry = GetNextUnpartitionedEntry(CurrentPartition);
if (PrevPartEntry != NULL && NextPartEntry != NULL) { /* Merge the previous, current and next unpartitioned entries */
/* Adjust the previous entry length */ PrevPartEntry->SectorCount.QuadPart += (CurrentPartition->SectorCount.QuadPart + NextPartEntry->SectorCount.QuadPart);
/* Remove the current and next entries */ RemoveEntryList(&CurrentPartition->ListEntry); RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition); RemoveEntryList(&NextPartEntry->ListEntry); RtlFreeHeap(RtlGetProcessHeap(), 0, NextPartEntry); } else if (PrevPartEntry != NULL && NextPartEntry == NULL) { /* Merge the current and the previous unpartitioned entries */
/* Adjust the previous entry length */ PrevPartEntry->SectorCount.QuadPart += CurrentPartition->SectorCount.QuadPart;
/* Remove the current entry */ RemoveEntryList(&CurrentPartition->ListEntry); RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition); } else if (PrevPartEntry == NULL && NextPartEntry != NULL) { /* Merge the current and the next unpartitioned entries */
/* Adjust the next entry offset and length */ NextPartEntry->StartSector.QuadPart = CurrentPartition->StartSector.QuadPart; NextPartEntry->SectorCount.QuadPart += CurrentPartition->SectorCount.QuadPart;
/* Remove the current entry */ RemoveEntryList(&CurrentPartition->ListEntry); RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition); } else { /* Nothing to merge but change the current entry */ CurrentPartition->IsPartitioned = FALSE; CurrentPartition->OnDiskPartitionNumber = 0; CurrentPartition->PartitionNumber = 0; // CurrentPartition->PartitionIndex = 0; CurrentPartition->Mbr.BootIndicator = FALSE; CurrentPartition->Mbr.PartitionType = PARTITION_ENTRY_UNUSED; CurrentPartition->FormatState = Unformatted; CurrentPartition->FileSystemName[0] = L'\0'; CurrentPartition->DriveLetter = 0; RtlZeroMemory(CurrentPartition->VolumeLabel, sizeof(CurrentPartition->VolumeLabel)); }
CurrentPartition = NULL;
UpdateMbrDiskLayout(CurrentDisk); Status = WriteMbrPartitions(CurrentDisk); if (!NT_SUCCESS(Status)) { ConResPuts(StdOut, IDS_DELETE_PARTITION_FAIL); return; }
ConResPuts(StdOut, IDS_DELETE_PARTITION_SUCCESS);}
staticVOIDDeleteGptPartition( _In_ BOOL bOverride){ PPARTENTRY PrevPartEntry; PPARTENTRY NextPartEntry; NTSTATUS Status;
/* Clear the system partition if it is being deleted */#if 0 if (List->SystemPartition == PartEntry) { ASSERT(List->SystemPartition); List->SystemPartition = NULL; }#endif
if ((bOverride == FALSE) && ((memcmp(&CurrentPartition->Gpt.PartitionType, &PARTITION_SYSTEM_GUID, sizeof(GUID)) == 0) || (memcmp(&CurrentPartition->Gpt.PartitionType, &PARTITION_MSFT_RESERVED_GUID, sizeof(GUID)) == 0))) { ConResPuts(StdOut, IDS_DELETE_PARTITION_FAIL); return; }
#ifdef DUMP_PARTITION_LIST DumpPartitionList(CurrentDisk);#endif
/* Dismount the partition */ DismountVolume(CurrentPartition);
/* Adjust the unpartitioned disk space entries */
/* Get pointer to previous and next unpartitioned entries */ PrevPartEntry = GetPrevUnpartitionedEntry(CurrentPartition); NextPartEntry = GetNextUnpartitionedEntry(CurrentPartition);
if (PrevPartEntry != NULL && NextPartEntry != NULL) { /* Merge the previous, current and next unpartitioned entries */
/* Adjust the previous entry length */ PrevPartEntry->SectorCount.QuadPart += (CurrentPartition->SectorCount.QuadPart + NextPartEntry->SectorCount.QuadPart);
/* Remove the current and next entries */ RemoveEntryList(&CurrentPartition->ListEntry); RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition); RemoveEntryList(&NextPartEntry->ListEntry); RtlFreeHeap(RtlGetProcessHeap(), 0, NextPartEntry); } else if (PrevPartEntry != NULL && NextPartEntry == NULL) { /* Merge the current and the previous unpartitioned entries */
/* Adjust the previous entry length */ PrevPartEntry->SectorCount.QuadPart += CurrentPartition->SectorCount.QuadPart;
/* Remove the current entry */ RemoveEntryList(&CurrentPartition->ListEntry); RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition); } else if (PrevPartEntry == NULL && NextPartEntry != NULL) { /* Merge the current and the next unpartitioned entries */
/* Adjust the next entry offset and length */ NextPartEntry->StartSector.QuadPart = CurrentPartition->StartSector.QuadPart; NextPartEntry->SectorCount.QuadPart += CurrentPartition->SectorCount.QuadPart;
/* Remove the current entry */ RemoveEntryList(&CurrentPartition->ListEntry); RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition); } else { /* Nothing to merge but change the current entry */ CurrentPartition->IsPartitioned = FALSE; CurrentPartition->OnDiskPartitionNumber = 0; CurrentPartition->PartitionNumber = 0; // CurrentPartition->PartitionIndex = 0;
ZeroMemory(&CurrentPartition->Gpt.PartitionType, sizeof(GUID)); ZeroMemory(&CurrentPartition->Gpt.PartitionId, sizeof(GUID)); CurrentPartition->Gpt.Attributes = 0ULL;
CurrentPartition->FormatState = Unformatted; CurrentPartition->FileSystemName[0] = L'\0'; CurrentPartition->DriveLetter = 0; RtlZeroMemory(CurrentPartition->VolumeLabel, sizeof(CurrentPartition->VolumeLabel)); }
CurrentPartition = NULL;
#ifdef DUMP_PARTITION_LIST DumpPartitionList(CurrentDisk);#endif
UpdateGptDiskLayout(CurrentDisk, TRUE);#ifdef DUMP_PARTITION_TABLE DumpPartitionTable(CurrentDisk);#endif Status = WriteGptPartitions(CurrentDisk); if (!NT_SUCCESS(Status)) { ConResPuts(StdOut, IDS_DELETE_PARTITION_FAIL); return; }
ConResPuts(StdOut, IDS_DELETE_PARTITION_SUCCESS);}
EXIT_CODEDeletePartition( _In_ INT argc, _In_ PWSTR *argv){ INT i; BOOL bOverride = FALSE;
DPRINT("DeletePartition()\n");
if (CurrentDisk == NULL) { ConResPuts(StdOut, IDS_SELECT_NO_DISK); return EXIT_SUCCESS; }
if (CurrentPartition == NULL) { ConResPuts(StdOut, IDS_SELECT_NO_PARTITION); return EXIT_SUCCESS; }
for (i = 2; i < argc; i++) { if (_wcsicmp(argv[i], L"noerr") == 0) { /* noerr */ DPRINT("NOERR\n"); ConPuts(StdOut, L"The NOERR option is not supported yet!\n");#if 0 bNoErr = TRUE;#endif } }
for (i = 2; i < argc; i++) { if (_wcsicmp(argv[i], L"noerr") == 0) { /* noerr - Already handled above */ } else if (_wcsicmp(argv[i], L"override") == 0) { /* override */ DPRINT("OVERRIDE\n"); bOverride = TRUE; } else { ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS); return EXIT_SUCCESS; } }
if (CurrentPartition->IsBoot || CurrentPartition->IsSystem) { ConResPuts(StdOut, IDS_DELETE_PARTITION_SYSTEM); return EXIT_SUCCESS; }
if (CurrentDisk->PartitionStyle == PARTITION_STYLE_MBR) { DeleteMbrPartition(bOverride); } else if (CurrentDisk->PartitionStyle == PARTITION_STYLE_GPT) { DeleteGptPartition(bOverride); }
return EXIT_SUCCESS;}
EXIT_CODEDeleteVolume( _In_ INT argc, _In_ PWSTR *argv){ return EXIT_SUCCESS;}