~n-muench/ubuntu/oneiric/open-vm-tools/open-vm-tools.fix-836277

« back to all changes in this revision

Viewing changes to toolbox/toolboxcmd-shrink.c

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2008-08-15 21:21:40 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080815212140-05fhxj8wroosysmj
Tags: 2008.08.08-109361-1ubuntu1
* Merge from Debian unstable (LP: #258393), remaining Ubuntu change:
  - add ubuntu_toolchain_FTBFS.dpatch patch, fix FTBFS
* Update ubuntu_toolchain_FTBFS.dpatch patch for the new version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2008 VMware, Inc. All rights reserved.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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.
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
/*
 
20
 * toolboxcmd-shrink.c --
 
21
 *
 
22
 *     The shrink operations for toolbox-cmd
 
23
 */
 
24
 
 
25
 
 
26
#include <unistd.h>
 
27
#include <string.h>
 
28
#include <stdlib.h>
 
29
 
 
30
#include <signal.h>
 
31
#include "toolboxCmdInt.h"
 
32
 
 
33
 
 
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;
 
38
 
 
39
 
 
40
/*
 
41
 *-----------------------------------------------------------------------------
 
42
 *
 
43
 * Shrink_List  --
 
44
 *
 
45
 *      Prints mount points to stdout.
 
46
 *
 
47
 * Results:
 
48
 *      EXIT_SUCCESS.
 
49
 *
 
50
 * Side effects:
 
51
 *      None.
 
52
 *
 
53
 *-----------------------------------------------------------------------------
 
54
 */
 
55
 
 
56
int
 
57
Shrink_List(void) // IN: Verbosity flag.
 
58
{
 
59
   int i;
 
60
   WiperPartition_List *plist = ShrinkGetMountPoints();
 
61
   if (plist == NULL) {
 
62
      return EX_TEMPFAIL;
 
63
   }
 
64
   for (i = 0; i < plist->size; i++) {
 
65
      if (strlen(plist->partitions[i].comment) == 0) {
 
66
         printf("%s\n", plist->partitions[i].mountPoint);
 
67
      }
 
68
   }
 
69
   WiperPartition_Close(plist);
 
70
   return EXIT_SUCCESS;
 
71
}
 
72
 
 
73
 
 
74
/*
 
75
 *-----------------------------------------------------------------------------
 
76
 *
 
77
 * ShrinkGetPartition  --
 
78
 *
 
79
 *      Finds the WiperPartion whose mountpoint is given.
 
80
 *
 
81
 * Results:
 
82
 *      The WiperPatition.
 
83
 *
 
84
 * Side effects:
 
85
 *      None
 
86
 *
 
87
 *-----------------------------------------------------------------------------
 
88
 */
 
89
 
 
90
static WiperPartition*
 
91
ShrinkGetPartition(char *mountPoint, // IN: mount point
 
92
                   int quiet_flag)   // IN: Verbosity flag
 
93
{
 
94
   int i;
 
95
   WiperPartition_List *plist = ShrinkGetMountPoints();
 
96
   if (!plist) {
 
97
      return NULL;
 
98
   }
 
99
   WiperPartition *part;
 
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);
 
105
         return part;
 
106
      }
 
107
   }
 
108
   WiperPartition_Close(plist);
 
109
   return NULL;
 
110
}
 
111
 
 
112
 
 
113
/*
 
114
 *-----------------------------------------------------------------------------
 
115
 *
 
116
 * ShrinkGetMountPoints  --
 
117
 *
 
118
 *      Gets a list of wiper partitions.
 
119
 *
 
120
 * Results:
 
121
 *      The WiperPartion_List.
 
122
 *
 
123
 * Side effects:
 
124
 *      Prints to stderr on errors.
 
125
 *
 
126
 *-----------------------------------------------------------------------------
 
127
 */
 
128
 
 
129
static WiperPartition_List*
 
130
ShrinkGetMountPoints(void) // IN: Verbosity flag
 
131
{
 
132
   if (GuestApp_IsDiskShrinkCapable()) {
 
133
      if (GuestApp_IsDiskShrinkEnabled()) {
 
134
         Wiper_Init(NULL);
 
135
         return WiperPartition_Open();
 
136
      } else {
 
137
         fprintf(stderr,SHRINK_DISABLED_ERR);
 
138
      }
 
139
   } else {
 
140
      fprintf(stderr, SHRINK_FEATURE_ERR);
 
141
   }
 
142
   return NULL;
 
143
}
 
144
 
 
145
 
 
146
/*
 
147
 *-----------------------------------------------------------------------------
 
148
 *
 
149
 * Shrink_DoShrink  --
 
150
 *
 
151
 *      Wipe a single partition, returning only when the wiper
 
152
 *      operation is done or canceled.
 
153
 *
 
154
 * Results:
 
155
 *      EXIT_SUCCESS on success.
 
156
 *      EX_OSFILE if partition is not found.
 
157
 *      EX_TEMPFAIL on failure.
 
158
 *
 
159
 * Side effects:
 
160
 *      The wipe operation will fill the partition with dummy files.
 
161
 *      Prints to stderr on errors.
 
162
 *
 
163
 *-----------------------------------------------------------------------------
 
164
 */
 
165
 
 
166
int
 
167
Shrink_DoShrink(char *mountPoint, // IN: mount point
 
168
                int quiet_flag)   // IN: verbosity flag
 
169
{
 
170
   int i;
 
171
   int progress = 0;
 
172
   unsigned char *err;
 
173
   WiperPartition *part;
 
174
   signal(SIGINT, ShrinkWiperDestroy);
 
175
   part = ShrinkGetPartition(mountPoint, quiet_flag);
 
176
   if (part == NULL) {
 
177
      fprintf(stderr, "Unable to find partition\n");
 
178
      return EX_OSFILE;
 
179
   }
 
180
   /*
 
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.
 
184
    */
 
185
   if (!GuestApp_IsDiskShrinkEnabled()) {
 
186
      fprintf(stderr, SHRINK_CONFLICT_ERR);
 
187
      free(part);
 
188
      return EX_TEMPFAIL;
 
189
   }
 
190
 
 
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");
 
197
            free(part);
 
198
            return EX_TEMPFAIL;
 
199
         }
 
200
         else {
 
201
            fprintf(stderr, "Error, %s", err);
 
202
            free(part);
 
203
            return EX_TEMPFAIL;
 
204
         }
 
205
         free(wiper);
 
206
         wiper = NULL;
 
207
      }
 
208
      if (!quiet_flag) {
 
209
         printf("\rProgress: %d [", progress);
 
210
         for (i = 0; i <= progress / 10; i++) {
 
211
            printf("=");
 
212
         }
 
213
         printf(">");
 
214
         for (; i <= 100 / 10; i++) {
 
215
            printf(" ");
 
216
         }
 
217
         printf("]");
 
218
      }
 
219
   }
 
220
   if (progress >= 100) {
 
221
      if (!quiet_flag) {
 
222
         printf("\nDisk shrinking complete\n");
 
223
      }
 
224
      wiper = NULL;
 
225
      free(part);
 
226
      return EXIT_SUCCESS;
 
227
   } else {
 
228
      fprintf(stderr, "Shrinking not completed\n");
 
229
      return EX_TEMPFAIL;
 
230
   }
 
231
}
 
232
 
 
233
 
 
234
/*
 
235
 *-----------------------------------------------------------------------------
 
236
 *
 
237
 * ShrinkWiperDestroy  --
 
238
 *
 
239
 *      Catch SIGINT and cancel wiper operation.
 
240
 *
 
241
 * Results:
 
242
 *      None.
 
243
 *
 
244
 * Side effects:
 
245
 *      The wipe operation will be canceled, and "zero" files removed.
 
246
 *      We will also exit the vmware-toolbox-cmd program.
 
247
 *
 
248
 *-----------------------------------------------------------------------------
 
249
 */
 
250
 
 
251
void
 
252
ShrinkWiperDestroy(int signal)  // IN: Signal caught
 
253
{
 
254
   if (wiper != NULL) {
 
255
      Wiper_Cancel(&wiper);
 
256
      wiper = NULL;
 
257
   }
 
258
   printf("Disk shrink canceled\n");
 
259
   exit(EXIT_SUCCESS);
 
260
}