ReactOS 0.4.16-dev-125-g798ea90
clean.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/clean.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
15BOOL
19{
21 PPARTENTRY PartEntry;
22 PVOLENTRY VolumeEntry;
23 BOOL bAll = FALSE;
24 PUCHAR SectorsBuffer = NULL;
25 ULONG LayoutBufferSize, Size;
26 INT i;
32 LARGE_INTEGER Offset, Count, MaxCount;
34
35 DPRINT("Clean()\n");
36
37 if (CurrentDisk == NULL)
38 {
40 return TRUE;
41 }
42
43 /* Do not allow to clean the boot disk */
44 if ((CurrentDisk->BiosFound == TRUE) &&
46 {
48 return TRUE;
49 }
50
51 for (i = 1; i < argc; i++)
52 {
53 if (_wcsicmp(argv[1], L"all") == 0)
54 {
55 bAll = TRUE;
56 }
57 }
58
59 /* Dismount and remove all logical partitions */
61 {
63 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
64
65 /* Dismount the logical partition */
66 if (PartEntry->PartitionType != 0)
67 {
68 DismountVolume(PartEntry);
69 VolumeEntry = GetVolumeFromPartition(PartEntry);
70 if (VolumeEntry)
71 RemoveVolume(VolumeEntry);
72 }
73
74 /* Delete it */
75 RtlFreeHeap(RtlGetProcessHeap(), 0, PartEntry);
76 }
77
78 /* Dismount and remove all primary partitions */
80 {
82 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
83
84 /* Dismount the primary partition */
85 if ((PartEntry->PartitionType != 0) &&
87 {
88 DismountVolume(PartEntry);
89 VolumeEntry = GetVolumeFromPartition(PartEntry);
90 if (VolumeEntry)
91 RemoveVolume(VolumeEntry);
92 }
93
94 /* Delete it */
95 RtlFreeHeap(RtlGetProcessHeap(), 0, PartEntry);
96 }
97
98 /* Initialize the disk entry */
103
104 /* Wipe the layout buffer */
105 RtlFreeHeap(RtlGetProcessHeap(), 0, CurrentDisk->LayoutBuffer);
106
107 LayoutBufferSize = sizeof(DRIVE_LAYOUT_INFORMATION) +
108 ((4 - ANYSIZE_ARRAY) * sizeof(PARTITION_INFORMATION));
109 CurrentDisk->LayoutBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
111 LayoutBufferSize);
113 {
114 DPRINT1("Failed to allocate the disk layout buffer!\n");
115 return TRUE;
116 }
117
118 /* Allocate a 1MB sectors buffer */
119 SectorsBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
121 1024 * 1024);
122 if (SectorsBuffer == NULL)
123 {
124 DPRINT1("Failed to allocate the sectors buffer!\n");
125 goto done;
126 }
127
128 /* Open the disk for writing */
130 L"\\Device\\Harddisk%d\\Partition0",
132
134
136 &Name,
138 NULL,
139 NULL);
140
145 0,
147 if (!NT_SUCCESS(Status))
148 {
149 DPRINT1("Failed to open the disk! (Status 0x%08lx)\n", Status);
151 goto done;
152 }
153
154 /* Clean sectors */
155 if (bAll)
156 {
157 MaxCount.QuadPart = (CurrentDisk->SectorCount.QuadPart * CurrentDisk->BytesPerSector) / (1024 * 1024);
158 for (Count.QuadPart = 0; Count.QuadPart < MaxCount.QuadPart; Count.QuadPart++)
159 {
160 Offset.QuadPart = Count.QuadPart * (1024 * 1024);
162 NULL,
163 NULL,
164 NULL,
166 SectorsBuffer,
167 1024 * 1024,
168 &Offset,
169 NULL);
170 if (!NT_SUCCESS(Status))
171 {
172 DPRINT1("Failed to write MB! (Status 0x%08lx)\n", Status);
174 goto done;
175 }
176 }
177
179 if (Size != 0)
180 {
181 Offset.QuadPart += (1024 * 1024);
183 NULL,
184 NULL,
185 NULL,
187 SectorsBuffer,
188 Size,
189 &Offset,
190 NULL);
191 if (!NT_SUCCESS(Status))
192 {
193 DPRINT1("Failed to write the last part! (Status 0x%08lx)\n", Status);
195 goto done;
196 }
197 }
198 }
199 else
200 {
201 /* Clean the first MB */
202 Offset.QuadPart = 0;
204 NULL,
205 NULL,
206 NULL,
208 SectorsBuffer,
209 1024 * 1024,
210 &Offset,
211 NULL);
212 if (!NT_SUCCESS(Status))
213 {
214 DPRINT1("Failed to write the first MB! (Status 0x%08lx)\n", Status);
216 goto done;
217 }
218
219 /* Clean the last MB */
220 Offset.QuadPart = (CurrentDisk->SectorCount.QuadPart * CurrentDisk->BytesPerSector) - (1024 * 1024);
222 NULL,
223 NULL,
224 NULL,
226 SectorsBuffer,
227 1024 * 1024,
228 &Offset,
229 NULL);
230 if (!NT_SUCCESS(Status))
231 {
232 DPRINT1("Failed to write the last MB! (Status 0x%08lx)\n", Status);
234 goto done;
235 }
236 }
237
239
240done:
241 if (FileHandle != NULL)
243
244 if (SectorsBuffer != NULL)
245 RtlFreeHeap(RtlGetProcessHeap(), 0, SectorsBuffer);
246
247 return TRUE;
248}
static int argc
Definition: ServiceArgs.c:12
struct NameRec_ * Name
Definition: cdprocs.h:460
#define StdOut
Definition: fc.c:14
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define IDS_CLEAN_FAIL
Definition: resource.h:25
#define IDS_SELECT_NO_DISK
Definition: resource.h:85
#define IDS_CLEAN_SUCCESS
Definition: resource.h:26
#define IDS_CLEAN_SYSTEM
Definition: resource.h:27
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:590
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:608
Definition: bufpool.h:45
BOOL clean_main(_In_ INT argc, _In_ PWSTR *argv)
Definition: clean.c:16
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
VOID RemoveVolume(_In_ PVOLENTRY VolumeEntry)
Definition: partlist.c:2103
PDISKENTRY CurrentDisk
Definition: partlist.c:74
PVOLENTRY GetVolumeFromPartition(_In_ PPARTENTRY PartEntry)
Definition: partlist.c:2066
#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
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#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
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define FILE_SYNCHRONOUS_IO_NONALERT
Definition: from_kernel.h:31
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
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define argv
Definition: mplay32.c:18
#define _In_
Definition: ms_sal.h:308
int Count
Definition: noreturn.cpp:7
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3952
#define SYNCHRONIZE
Definition: nt_native.h:61
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define GENERIC_WRITE
Definition: nt_native.h:90
#define IsContainerPartition(PartitionType)
Definition: ntdddisk.h:321
struct _PARTITION_INFORMATION PARTITION_INFORMATION
struct _DRIVE_LAYOUT_INFORMATION DRIVE_LAYOUT_INFORMATION
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
#define L(x)
Definition: ntvdm.h:50
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define DPRINT
Definition: sndvol32.h:73
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
ULONG BiosDiskNumber
Definition: diskpart.h:162
ULARGE_INTEGER SectorCount
Definition: partlist.h:115
PPARTENTRY ExtendedPartition
Definition: partlist.h:153
LIST_ENTRY LogicalPartListHead
Definition: partlist.h:150
BOOLEAN NewDisk
Definition: partlist.h:138
ULONG DiskNumber
Definition: partlist.h:129
ULONG BytesPerSector
Definition: partlist.h:113
BOOLEAN BiosFound
Definition: partlist.h:120
BOOLEAN Dirty
Definition: partlist.h:136
BOOLEAN NoMbr
Definition: diskpart.h:176
LIST_ENTRY PrimaryPartListHead
Definition: partlist.h:149
PDRIVE_LAYOUT_INFORMATION LayoutBuffer
Definition: partlist.h:143
Definition: typedefs.h:120
UCHAR PartitionType
Definition: partlist.h:73
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
uint16_t * PWSTR
Definition: typedefs.h:56
#define ANYSIZE_ARRAY
Definition: typedefs.h:46
int32_t INT
Definition: typedefs.h:58
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
LONGLONG QuadPart
Definition: typedefs.h:114
NTSTATUS DismountVolume(_Inout_ PVOLINFO Volume, _In_ BOOLEAN Force)
Attempts to dismount the designated volume.
Definition: volutil.c:152
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
__wchar_t WCHAR
Definition: xmlstorage.h:180