File: toolboxcmd-shrink.c

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1%2Bbuild1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze-lts
  • size: 20,376 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (297 lines) | stat: -rw-r--r-- 7,129 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*********************************************************
 * Copyright (C) 2008 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * toolboxcmd-shrink.c --
 *
 *     The shrink operations for toolbox-cmd
 */


#include <string.h>
#include <stdlib.h>

#ifndef _WIN32
#   include <signal.h>
#endif

#include "toolboxCmdInt.h"
#include "rpcout.h"

#ifndef _WIN32
static void ShrinkWiperDestroy(int signal);
#endif

static Bool ShrinkGetMountPoints(WiperPartition_List *);
static WiperPartition * ShrinkGetPartition(char *mountPoint);
static Wiper_State *wiper = NULL;


/*
 *-----------------------------------------------------------------------------
 *
 * Shrink_List  --
 *
 *      Prints mount points to stdout.
 *
 * Results:
 *      EXIT_SUCCESS.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

int
Shrink_List(void)
{
   WiperPartition_List plist;
   DblLnkLst_Links *curr;

   if (!ShrinkGetMountPoints(&plist)) {
      return EX_TEMPFAIL;
   }

   DblLnkLst_ForEach(curr, &plist.link) {
      WiperPartition *p = DblLnkLst_Container(curr, WiperPartition, link);
      if (p->type != PARTITION_UNSUPPORTED) {
         printf("%s\n", p->mountPoint);
      }
   }

   WiperPartition_Close(&plist);

   return EXIT_SUCCESS;
}


/*
 *-----------------------------------------------------------------------------
 *
 * ShrinkGetPartition  --
 *
 *      Finds the WiperPartion whose mountpoint is given.
 *
 * Results:
 *      The WiperPatition.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static WiperPartition*
ShrinkGetPartition(char *mountPoint)
{
   WiperPartition_List plist;
   WiperPartition *p, *part = NULL;
   DblLnkLst_Links *curr;

   if (!ShrinkGetMountPoints(&plist)) {
      return NULL;
   }

   DblLnkLst_ForEach(curr, &plist.link) {
      p = DblLnkLst_Container(curr, WiperPartition, link);
      if (toolbox_strcmp(p->mountPoint, mountPoint) == 0) {
         part = p;
         /*
          * Detach the element we are interested in so it is not
          * destroyed when we call WiperPartition_Close.
          */
         DblLnkLst_Unlink1(&part->link);
         break;
      }
   }

   WiperPartition_Close(&plist);

   return part;
}


/*
 *-----------------------------------------------------------------------------
 *
 * ShrinkGetMountPoints  --
 *
 *      Gets a list of wiper partitions.
 *
 * Results:
 *      The WiperPartion_List.
 *
 * Side effects:
 *      Prints to stderr on errors.
 *
 *-----------------------------------------------------------------------------
 */

static Bool
ShrinkGetMountPoints(WiperPartition_List *pl) // OUT: Known mount points
{
   if (!GuestApp_IsDiskShrinkCapable()) {
      fprintf(stderr, SHRINK_FEATURE_ERR);
   } else if (!GuestApp_IsDiskShrinkEnabled()) {
      fprintf(stderr, SHRINK_DISABLED_ERR);
   } else if (!WiperPartition_Open(pl)) {
      fprintf(stderr, "Unable to collect partition data.\n");
   } else {
      return TRUE;
   }
   return FALSE;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Shrink_DoShrink  --
 *
 *      Wipe a single partition, returning only when the wiper
 *      operation is done or canceled.
 *
 * Results:
 *      EXIT_SUCCESS on success.
 *      EX_OSFILE if partition is not found.
 *      EX_TEMPFAIL on failure.
 *
 * Side effects:
 *      The wipe operation will fill the partition with dummy files.
 *      Prints to stderr on errors.
 *
 *-----------------------------------------------------------------------------
 */

int
Shrink_DoShrink(char *mountPoint, // IN: mount point
                int quiet_flag)   // IN: verbosity flag
{
   int i;
   int progress = 0;
   unsigned char *err;
   WiperPartition *part;
   int rc;

#ifndef _WIN32
   signal(SIGINT, ShrinkWiperDestroy);
#endif

   part = ShrinkGetPartition(mountPoint);
   if (part == NULL) {
      fprintf(stderr, "Unable to find partition %s\n", mountPoint);
      return EX_OSFILE;
   }

   if (part->type == PARTITION_UNSUPPORTED) {
      fprintf(stderr, "Partition %s is not shrinkable\n", part->mountPoint);
      rc = EX_UNAVAILABLE;
      goto out;
   }

   /*
    * Verify that shrinking is still possible before going through with the
    * wiping. This obviously isn't atomic, but it should take care of
    * the case where the user takes a snapshot with the toolbox open.
    */
   if (!GuestApp_IsDiskShrinkEnabled()) {
      fprintf(stderr, SHRINK_CONFLICT_ERR);
      rc = EX_TEMPFAIL;
      goto out;
   }

   wiper = Wiper_Start(part, MAX_WIPER_FILE_SIZE);

   while (progress < 100 && wiper != NULL) {
      err = Wiper_Next(&wiper, &progress);
      if (strlen(err) > 0) {
         if (strcmp(err, "error.create") == 0) {
            fprintf(stderr, "Error, Unable to create wiper file\n");
         } else {
            fprintf(stderr, "Error, %s", err);
         }

         rc = EX_TEMPFAIL;
      }

      if (!quiet_flag) {
         printf("\rProgress: %d [", progress);
         for (i = 0; i <= progress / 10; i++) {
            putchar('=');
         }
         printf(">%*c", 10 - i + 1, ']');
         fflush(stdout);
      }
   }

   if (progress >= 100) {
      char *result;
      size_t resultLen;

      if (RpcOut_sendOne(&result, &resultLen, "disk.shrink")) {
         if (!quiet_flag) {
            printf("\nDisk shrinking complete\n");
         }
         rc = EXIT_SUCCESS;
         goto out;
      }

      fprintf(stderr, "%s\n", result);
   }

   fprintf(stderr, "Shrinking not completed\n");
   rc = EX_TEMPFAIL;

out:
   WiperSinglePartition_Close(part);
   free(wiper);
   wiper = NULL;
   return rc;
}


#ifndef _WIN32
/*
 *-----------------------------------------------------------------------------
 *
 * ShrinkWiperDestroy  --
 *
 *      Catch SIGINT and cancel wiper operation.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      The wipe operation will be canceled, and "zero" files removed.
 *      We will also exit the vmware-toolbox-cmd program.
 *
 *-----------------------------------------------------------------------------
 */

void
ShrinkWiperDestroy(int signal)	// IN: Signal caught
{
   if (wiper != NULL) {
      Wiper_Cancel(&wiper);
      wiper = NULL;
   }
   printf("Disk shrink canceled\n");
   exit(EXIT_SUCCESS);
}
#endif