1
/*********************************************************
2
* Copyright (C) 2008 VMware, Inc. All rights reserved.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License as published
6
* by the Free Software Foundation version 2.1 and no later version.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
11
* License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program; if not, write to the Free Software Foundation, Inc.,
15
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
*********************************************************/
20
* toolboxcmd-shrink.c --
22
* The shrink operations for toolbox-cmd
31
#include "toolboxCmdInt.h"
34
static void ShrinkWiperDestroy(int signal);
35
static WiperPartition_List * ShrinkGetMountPoints(void);
36
static WiperPartition * ShrinkGetPartition(char *mountPoint, int quiet_flag);
37
static Wiper_State *wiper = NULL;
41
*-----------------------------------------------------------------------------
45
* Prints mount points to stdout.
53
*-----------------------------------------------------------------------------
57
Shrink_List(void) // IN: Verbosity flag.
60
WiperPartition_List *plist = ShrinkGetMountPoints();
64
for (i = 0; i < plist->size; i++) {
65
if (strlen(plist->partitions[i].comment) == 0) {
66
printf("%s\n", plist->partitions[i].mountPoint);
69
WiperPartition_Close(plist);
75
*-----------------------------------------------------------------------------
77
* ShrinkGetPartition --
79
* Finds the WiperPartion whose mountpoint is given.
87
*-----------------------------------------------------------------------------
90
static WiperPartition*
91
ShrinkGetPartition(char *mountPoint, // IN: mount point
92
int quiet_flag) // IN: Verbosity flag
95
WiperPartition_List *plist = ShrinkGetMountPoints();
100
part = (WiperPartition *) malloc(sizeof *part);
101
for (i = 0; i < plist->size; i++) {
102
if (strcmp(plist->partitions[i].mountPoint, mountPoint) == 0) {
103
memcpy(part, &plist->partitions[i], sizeof *part);
104
WiperPartition_Close(plist);
108
WiperPartition_Close(plist);
114
*-----------------------------------------------------------------------------
116
* ShrinkGetMountPoints --
118
* Gets a list of wiper partitions.
121
* The WiperPartion_List.
124
* Prints to stderr on errors.
126
*-----------------------------------------------------------------------------
129
static WiperPartition_List*
130
ShrinkGetMountPoints(void) // IN: Verbosity flag
132
if (GuestApp_IsDiskShrinkCapable()) {
133
if (GuestApp_IsDiskShrinkEnabled()) {
135
return WiperPartition_Open();
137
fprintf(stderr,SHRINK_DISABLED_ERR);
140
fprintf(stderr, SHRINK_FEATURE_ERR);
147
*-----------------------------------------------------------------------------
151
* Wipe a single partition, returning only when the wiper
152
* operation is done or canceled.
155
* EXIT_SUCCESS on success.
156
* EX_OSFILE if partition is not found.
157
* EX_TEMPFAIL on failure.
160
* The wipe operation will fill the partition with dummy files.
161
* Prints to stderr on errors.
163
*-----------------------------------------------------------------------------
167
Shrink_DoShrink(char *mountPoint, // IN: mount point
168
int quiet_flag) // IN: verbosity flag
173
WiperPartition *part;
174
signal(SIGINT, ShrinkWiperDestroy);
175
part = ShrinkGetPartition(mountPoint, quiet_flag);
177
fprintf(stderr, "Unable to find partition\n");
181
* Verify that shrinking is still possible before going through with the
182
* wiping. This obviously isn't atomic, but it should take care of
183
* the case where the user takes a snapshot with the toolbox open.
185
if (!GuestApp_IsDiskShrinkEnabled()) {
186
fprintf(stderr, SHRINK_CONFLICT_ERR);
191
wiper = Wiper_Start (part, MAX_WIPER_FILE_SIZE);
192
while (progress < 100 && wiper != NULL) {
193
err = Wiper_Next(&wiper, &progress);
194
if (strlen(err) > 0) {
195
if (strcmp(err, "error.create") == 0) {
196
fprintf(stderr, "Error, Unable to create wiper file\n");
201
fprintf(stderr, "Error, %s", err);
209
printf("\rProgress: %d [", progress);
210
for (i = 0; i <= progress / 10; i++) {
214
for (; i <= 100 / 10; i++) {
220
if (progress >= 100) {
222
printf("\nDisk shrinking complete\n");
228
fprintf(stderr, "Shrinking not completed\n");
235
*-----------------------------------------------------------------------------
237
* ShrinkWiperDestroy --
239
* Catch SIGINT and cancel wiper operation.
245
* The wipe operation will be canceled, and "zero" files removed.
246
* We will also exit the vmware-toolbox-cmd program.
248
*-----------------------------------------------------------------------------
252
ShrinkWiperDestroy(int signal) // IN: Signal caught
255
Wiper_Cancel(&wiper);
258
printf("Disk shrink canceled\n");