ReactOS 0.4.16-dev-2104-gb84fa49
delete.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/delete.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
14static
15BOOL
17 _In_ PPARTENTRY PartEntry)
18{
19 if (IsRecognizedPartition(PartEntry->Mbr.PartitionType) ||
20 IsContainerPartition(PartEntry->Mbr.PartitionType))
21 return TRUE;
22
23 return FALSE;
24}
25
26
31{
32 return EXIT_SUCCESS;
33}
34
35
36static
37VOID
39 _In_ BOOL bOverride)
40{
41 PPARTENTRY PrevPartEntry;
42 PPARTENTRY NextPartEntry;
43 PPARTENTRY LogicalPartEntry;
46
48
49 /* Clear the system partition if it is being deleted */
50#if 0
51 if (List->SystemPartition == PartEntry)
52 {
53 ASSERT(List->SystemPartition);
54 List->SystemPartition = NULL;
55 }
56#endif
57
58 if ((bOverride == FALSE) && (IsKnownPartition(CurrentPartition) == FALSE))
59 {
61 return;
62 }
63
64 /* Check which type of partition (primary/logical or extended) is being deleted */
66 {
67 /* An extended partition is being deleted: delete all logical partition entries */
69 {
71 LogicalPartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
72
73 /* Dismount the logical partition */
74 DismountVolume(LogicalPartEntry);
75
76 /* Delete it */
77 RtlFreeHeap(RtlGetProcessHeap(), 0, LogicalPartEntry);
78 }
79
81 }
82 else
83 {
84 /* A primary partition is being deleted: dismount it */
86 }
87
88 /* Adjust the unpartitioned disk space entries */
89
90 /* Get pointer to previous and next unpartitioned entries */
93
94 if (PrevPartEntry != NULL && NextPartEntry != NULL)
95 {
96 /* Merge the previous, current and next unpartitioned entries */
97
98 /* Adjust the previous entry length */
99 PrevPartEntry->SectorCount.QuadPart += (CurrentPartition->SectorCount.QuadPart + NextPartEntry->SectorCount.QuadPart);
100
101 /* Remove the current and next entries */
103 RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition);
104 RemoveEntryList(&NextPartEntry->ListEntry);
105 RtlFreeHeap(RtlGetProcessHeap(), 0, NextPartEntry);
106 }
107 else if (PrevPartEntry != NULL && NextPartEntry == NULL)
108 {
109 /* Merge the current and the previous unpartitioned entries */
110
111 /* Adjust the previous entry length */
113
114 /* Remove the current entry */
116 RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition);
117 }
118 else if (PrevPartEntry == NULL && NextPartEntry != NULL)
119 {
120 /* Merge the current and the next unpartitioned entries */
121
122 /* Adjust the next entry offset and length */
125
126 /* Remove the current entry */
128 RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition);
129 }
130 else
131 {
132 /* Nothing to merge but change the current entry */
136 // CurrentPartition->PartitionIndex = 0;
143 }
144
146
149 if (!NT_SUCCESS(Status))
150 {
152 return;
153 }
154
156}
157
158
159static
160VOID
162 _In_ BOOL bOverride)
163{
164 PPARTENTRY PrevPartEntry;
165 PPARTENTRY NextPartEntry;
167
168 /* Clear the system partition if it is being deleted */
169#if 0
170 if (List->SystemPartition == PartEntry)
171 {
172 ASSERT(List->SystemPartition);
173 List->SystemPartition = NULL;
174 }
175#endif
176
177 if ((bOverride == FALSE) &&
178 ((memcmp(&CurrentPartition->Gpt.PartitionType, &PARTITION_SYSTEM_GUID, sizeof(GUID)) == 0) ||
179 (memcmp(&CurrentPartition->Gpt.PartitionType, &PARTITION_MSFT_RESERVED_GUID, sizeof(GUID)) == 0)))
180 {
182 return;
183 }
184
185#ifdef DUMP_PARTITION_LIST
186 DumpPartitionList(CurrentDisk);
187#endif
188
189 /* Dismount the partition */
191
192 /* Adjust the unpartitioned disk space entries */
193
194 /* Get pointer to previous and next unpartitioned entries */
197
198 if (PrevPartEntry != NULL && NextPartEntry != NULL)
199 {
200 /* Merge the previous, current and next unpartitioned entries */
201
202 /* Adjust the previous entry length */
203 PrevPartEntry->SectorCount.QuadPart += (CurrentPartition->SectorCount.QuadPart + NextPartEntry->SectorCount.QuadPart);
204
205 /* Remove the current and next entries */
207 RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition);
208 RemoveEntryList(&NextPartEntry->ListEntry);
209 RtlFreeHeap(RtlGetProcessHeap(), 0, NextPartEntry);
210 }
211 else if (PrevPartEntry != NULL && NextPartEntry == NULL)
212 {
213 /* Merge the current and the previous unpartitioned entries */
214
215 /* Adjust the previous entry length */
217
218 /* Remove the current entry */
220 RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition);
221 }
222 else if (PrevPartEntry == NULL && NextPartEntry != NULL)
223 {
224 /* Merge the current and the next unpartitioned entries */
225
226 /* Adjust the next entry offset and length */
229
230 /* Remove the current entry */
232 RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentPartition);
233 }
234 else
235 {
236 /* Nothing to merge but change the current entry */
240 // CurrentPartition->PartitionIndex = 0;
241
245
250 }
251
253
254#ifdef DUMP_PARTITION_LIST
255 DumpPartitionList(CurrentDisk);
256#endif
257
259#ifdef DUMP_PARTITION_TABLE
260 DumpPartitionTable(CurrentDisk);
261#endif
263 if (!NT_SUCCESS(Status))
264 {
266 return;
267 }
268
270}
271
272
275 _In_ INT argc,
276 _In_ PWSTR *argv)
277{
278 INT i;
279 BOOL bOverride = FALSE;
280
281 DPRINT("DeletePartition()\n");
282
283 if (CurrentDisk == NULL)
284 {
286 return EXIT_SUCCESS;
287 }
288
289 if (CurrentPartition == NULL)
290 {
292 return EXIT_SUCCESS;
293 }
294
295 for (i = 2; i < argc; i++)
296 {
297 if (_wcsicmp(argv[i], L"noerr") == 0)
298 {
299 /* noerr */
300 DPRINT("NOERR\n");
301 ConPuts(StdOut, L"The NOERR option is not supported yet!\n");
302#if 0
303 bNoErr = TRUE;
304#endif
305 }
306 }
307
308 for (i = 2; i < argc; i++)
309 {
310 if (_wcsicmp(argv[i], L"noerr") == 0)
311 {
312 /* noerr - Already handled above */
313 }
314 else if (_wcsicmp(argv[i], L"override") == 0)
315 {
316 /* override */
317 DPRINT("OVERRIDE\n");
318 bOverride = TRUE;
319 }
320 else
321 {
323 return EXIT_SUCCESS;
324 }
325 }
326
328 {
329 DeleteMbrPartition(bOverride);
330 }
332 {
333 DeleteGptPartition(bOverride);
334 }
335
336 return EXIT_SUCCESS;
337}
338
339
342 _In_ INT argc,
343 _In_ PWSTR *argv)
344{
345 return EXIT_SUCCESS;
346}
static int argc
Definition: ServiceArgs.c:12
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
#define StdOut
Definition: fc.c:14
#define StdErr
Definition: fc.c:15
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
LONG NTSTATUS
Definition: precomp.h:26
static BOOL IsKnownPartition(_In_ PPARTENTRY PartEntry)
Definition: delete.c:16
static VOID DeleteGptPartition(_In_ BOOL bOverride)
Definition: delete.c:161
EXIT_CODE DeletePartition(_In_ INT argc, _In_ PWSTR *argv)
Definition: delete.c:274
static VOID DeleteMbrPartition(_In_ BOOL bOverride)
Definition: delete.c:38
EXIT_CODE DeleteVolume(_In_ INT argc, _In_ PWSTR *argv)
Definition: delete.c:341
EXIT_CODE DeleteDisk(_In_ INT argc, _In_ PWSTR *argv)
Definition: delete.c:28
#define IDS_SELECT_NO_PARTITION
Definition: resource.h:126
#define IDS_ERROR_INVALID_ARGS
Definition: resource.h:229
#define IDS_DELETE_PARTITION_SUCCESS
Definition: resource.h:52
#define IDS_DELETE_PARTITION_FAIL
Definition: resource.h:51
#define IDS_SELECT_NO_DISK
Definition: resource.h:121
#define PARTITION_ENTRY_UNUSED
Definition: disk.h:71
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:634
VOID UpdateMbrDiskLayout(_In_ PDISKENTRY DiskEntry)
Definition: partlist.c:2362
enum _EXIT_CODE EXIT_CODE
PPARTENTRY GetPrevUnpartitionedEntry(_In_ PPARTENTRY PartEntry)
Definition: partlist.c:2629
VOID UpdateGptDiskLayout(_In_ PDISKENTRY DiskEntry, _In_ BOOL DeleteEntry)
Definition: partlist.c:2539
PDISKENTRY CurrentDisk
Definition: partlist.c:75
NTSTATUS WriteMbrPartitions(_In_ PDISKENTRY DiskEntry)
Definition: partlist.c:2034
NTSTATUS WriteGptPartitions(_In_ PDISKENTRY DiskEntry)
Definition: partlist.c:2158
PPARTENTRY GetNextUnpartitionedEntry(_In_ PPARTENTRY PartEntry)
Definition: partlist.c:2658
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
_ACRTIMP int __cdecl _wcsicmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:159
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2802
#define L(x)
Definition: resources.c:13
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
#define RemoveHeadList(ListHead)
Definition: env_spec_w32.h:964
unsigned int BOOL
Definition: ntddk_ex.h:94
Status
Definition: gdiplustypes.h:25
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
@ Unformatted
Definition: partlist.h:34
#define ZeroMemory
Definition: minwinbase.h:31
#define ASSERT(a)
Definition: mode.c:44
@ PARTITION_STYLE_GPT
Definition: imports.h:202
@ PARTITION_STYLE_MBR
Definition: imports.h:201
#define ULL(a, b)
Definition: format_msg.c:27
#define argv
Definition: mplay32.c:18
#define _In_
Definition: no_sal2.h:158
#define IsContainerPartition(PartitionType)
Definition: ntdddisk.h:321
#define IsRecognizedPartition(PartitionType)
Definition: ntdddisk.h:342
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
#define DPRINT
Definition: sndvol32.h:73
base of all file and directory entries
Definition: entries.h:83
PPARTENTRY ExtendedPartition
Definition: partlist.h:153
LIST_ENTRY LogicalPartListHead
Definition: partlist.h:150
DWORD PartitionStyle
Definition: diskpart.h:219
DWORD64 Attributes
Definition: diskpart.h:127
Definition: typedefs.h:120
BOOLEAN BootIndicator
Definition: diskpart.h:119
BOOLEAN IsPartitioned
Definition: partlist.h:82
ULARGE_INTEGER SectorCount
Definition: partlist.h:70
CHAR VolumeLabel[17]
Definition: diskpart.h:150
CHAR DriveLetter
Definition: diskpart.h:149
MBR_PARTITION_DATA Mbr
Definition: diskpart.h:141
FORMATSTATE FormatState
Definition: diskpart.h:152
GPT_PARTITION_DATA Gpt
Definition: diskpart.h:142
ULONG OnDiskPartitionNumber
Definition: partlist.h:74
LIST_ENTRY ListEntry
Definition: partlist.h:63
ULONG PartitionNumber
Definition: partlist.h:75
ULARGE_INTEGER StartSector
Definition: partlist.h:69
CHAR FileSystemName[9]
Definition: diskpart.h:151
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
uint16_t * PWSTR
Definition: typedefs.h:56
int32_t INT
Definition: typedefs.h:58
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
static PPARTENTRY CurrentPartition
Definition: usetup.c:78
NTSTATUS DismountVolume(_Inout_ PVOLINFO Volume, _In_ BOOLEAN Force)
Attempts to dismount the designated volume.
Definition: volutil.c:152
_Must_inspect_result_ _In_ WDFCMRESLIST List
Definition: wdfresource.h:550