File: toolboxShrink.c

package info (click to toggle)
open-vm-tools 1%3A8.4.2-261024-1
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 20,368 kB
  • ctags: 30,043
  • sloc: ansic: 164,785; sh: 10,713; cpp: 6,525; makefile: 3,386
file content (404 lines) | stat: -rw-r--r-- 13,836 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*********************************************************
 * Copyright (C) 2004 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.
 *
 *********************************************************/

/*
 * toolboxOptions.c --
 *
 *     The options tab for the linux gtk toolbox
 */

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

#include "toolboxGtkInt.h"
#include "debug.h"
#include "guestApp.h"
#include "wiper.h"

/*
 * Globals
 */
static GtkWidget *shrinkList;
static GtkWidget *shrinkWipeDlg;
static GtkWidget *shrinkWipeProgress;
static Wiper_State *wiper = NULL;

void Shrink_OnShrinkClicked(GtkButton *btn, gpointer user_data);
Bool Shrink_DoWipe(WiperPartition *part, GtkWidget* mainWnd);
void  Shrink_OnWipeDestroy(GtkWidget *widget, gpointer user_data);


/*
 *-----------------------------------------------------------------------------
 *
 * Shrink_Create  --
 *
 *      Create, layout, and init the Shrink tab UI and all its widgets.
 *
 * Results:
 *      The Shrink tab widget (it's a vbox).
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

GtkWidget*
Shrink_Create(GtkWidget *mainWnd)
{
   GtkWidget *shrinktab;
   GtkWidget *label;
   GtkWidget *scrollwin;
   GtkWidget *hbox;
   GtkWidget *button;
   GtkWidget *viewport;
   GtkWidget *ebox;

   WiperPartition_List plist;
   gchar *items;
   int newrow;

   shrinktab = gtk_vbox_new(FALSE, 10);
   gtk_widget_show(shrinktab);
   gtk_container_set_border_width(GTK_CONTAINER(shrinktab), 10);

   /* Only root can do shrink. */
   if (geteuid() != 0) {
      Debug("User not allowed to do shrink");
      label =
         gtk_label_new("This option is enabled only if you run VMware Tools as root.");
      gtk_widget_show(label);
      gtk_box_pack_start(GTK_BOX(shrinktab), label, FALSE, FALSE, 0);
   } else {
      label = gtk_label_new("Select the partitions you wish to shrink.");
      gtk_widget_show(label);
      gtk_box_pack_start(GTK_BOX(shrinktab), label, FALSE, FALSE, 0);
      gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
      gtk_misc_set_alignment(GTK_MISC(label), 0, 0);

      scrollwin = gtk_scrolled_window_new(NULL, NULL);
      gtk_widget_show(scrollwin);
      gtk_box_pack_start(GTK_BOX(shrinktab), scrollwin, TRUE, TRUE, 0);
      gtk_container_set_border_width(GTK_CONTAINER(scrollwin), 0);
      gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
                                     GTK_POLICY_AUTOMATIC,
                                     GTK_POLICY_AUTOMATIC);
      viewport =
         gtk_viewport_new(
            gtk_scrolled_window_get_hadjustment(
               GTK_SCROLLED_WINDOW(scrollwin)),
            gtk_scrolled_window_get_vadjustment(
               GTK_SCROLLED_WINDOW(scrollwin)));
      gtk_widget_show(viewport);
      gtk_container_add(GTK_CONTAINER(scrollwin), viewport);
      gtk_signal_connect(GTK_OBJECT(viewport), "size_request",
                         GTK_SIGNAL_FUNC(OnViewportSizeRequest), 0);
      gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
      gtk_container_set_border_width(GTK_CONTAINER(viewport), 0);

      ebox = gtk_event_box_new();
      gtk_widget_show(ebox);
      gtk_container_add(GTK_CONTAINER(viewport), ebox);
      gtk_container_set_border_width(GTK_CONTAINER(ebox), 0);

      {
         GtkStyle *style;
         GdkColor color;
         gdk_color_parse("#FFFFFF", &color);
         style = gtk_style_new();
         style->bg[GTK_STATE_NORMAL] = color;
         gtk_widget_set_style(ebox, style);
         gtk_style_unref(style);
      }

      hbox = gtk_hbox_new(FALSE, 0);
      gtk_widget_show(hbox);
      gtk_box_pack_end(GTK_BOX(shrinktab), hbox, FALSE, FALSE, 0);

#ifdef GTK2
      button = gtk_button_new_with_mnemonic("_Shrink");
#else
      button = gtk_button_new_with_label("Shrink");
#endif
      gtk_widget_show(button);
      gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
      gtk_widget_set_sensitive(button, FALSE);
      gtk_signal_connect(GTK_OBJECT(button), "clicked",
                         GTK_SIGNAL_FUNC(Shrink_OnShrinkClicked), mainWnd);

      if (GuestApp_IsDiskShrinkCapable()) {
         if (GuestApp_IsDiskShrinkEnabled()) {
            gtk_widget_set_sensitive(button, TRUE);
            shrinkList = gtk_clist_new(1);
            gtk_widget_show(shrinkList);
            gtk_container_add(GTK_CONTAINER(ebox), shrinkList);
            gtk_container_set_border_width(GTK_CONTAINER(shrinkList), 0);
            gtk_clist_set_selection_mode(GTK_CLIST(shrinkList), GTK_SELECTION_MULTIPLE);

            Wiper_Init(NULL);
            if (WiperPartition_Open(&plist)) {
               DblLnkLst_Links *curr, *next;

               DblLnkLst_ForEachSafe(curr, next, &plist.link) {
                  WiperPartition *part = DblLnkLst_Container(curr, WiperPartition, link);

                  if (part->type != PARTITION_UNSUPPORTED) {
                     /*
                      * Detach the element we are interested in so it is not
                      * destroyed when we call WiperPartition_Close.
                      */
                     DblLnkLst_Unlink1(&part->link);
                     items  = part->mountPoint;
                     newrow = gtk_clist_append(GTK_CLIST(shrinkList), &items);
                     gtk_clist_set_row_data_full(GTK_CLIST(shrinkList), newrow,
                                 part, (GDestroyNotify)WiperSinglePartition_Close);
                  }
               }
               WiperPartition_Close(&plist);
            }
         } else {
            label = gtk_label_new(SHRINK_DISABLED_ERR);
            gtk_widget_show(label);
            gtk_container_add(GTK_CONTAINER(ebox), label);
            gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
            gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
            gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
         }
      } else {
         label = gtk_label_new(SHRINK_FEATURE_ERR);
         gtk_widget_show(label);
         gtk_container_add(GTK_CONTAINER(ebox), label);
         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
      }
   }

   return shrinktab;
}


/*
 *-----------------------------------------------------------------------------
 *
 * Shrink_OnShrinkClicked  --
 *
 *      Callback for the gtk signal "clicked" on the Shrink tab's shrink button
 *      Cycle thru all the selected partitions wipe them (Shrink_DoWipe).
 *      After wiping all selected partitions, tell the vmx to shrink disks.
 *      If the user clicks cancel (Shrink_DoWipe returns FALSE), cancel the
 *      entire operation and don't send a mesage to the vmx.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      The selected partitons will be wiped and shrunk.
 *
 *-----------------------------------------------------------------------------
 */

void
Shrink_OnShrinkClicked(GtkButton *btn,     // IN: unused
                       gpointer user_data) // IN: unused
{
   int rnum;
   GList *slist;
   WiperPartition *part;
   int disks_to_shrink = 0;
   GtkWidget *mainWnd = GTK_WIDGET(user_data);
   slist = GTK_CLIST(shrinkList)->selection;
   if (slist) {
      if (!ToolsMain_YesNoBox("Shrink Disk",
                                "Do you want to prepare the disk(s) for shrinking?\n")) {
         return;
      }
      do {
         rnum =  GPOINTER_TO_UINT(slist->data);
         part = gtk_clist_get_row_data(GTK_CLIST(shrinkList), rnum);
         if (Shrink_DoWipe(part, mainWnd)) {
            disks_to_shrink++;
            slist = slist->next;
         } else {
            disks_to_shrink = 0;
            break;
         }
      } while (slist);

      if (disks_to_shrink > 0) {
         if (ToolsMain_YesNoBox("Shrink Disk",
                                "Do you want to shrink the disk(s)?\n")) {
            if (GuestApp_DiskShrink()) {
               ToolsMain_MsgBox("Information", "The shrink process has finished.");
            }
            gtk_clist_unselect_all(GTK_CLIST(shrinkList));
         }
      }
   } else {
      ToolsMain_MsgBox("Information", "Please select a partition\n");
   }
}


/*
 *-----------------------------------------------------------------------------
 *
 * Shrink_DoWipe  --
 *
 *      Wipe a single partition, displaying a modal dialog with a progress
 *      bar. This function works similar to Win32's DoModal in that it blocks
 *      the caller and pumps its own messages, returning only when the wiper
 *      operation is done or canceled.
 *
 * Results:
 *      TRUE if the wipe operation completes successfully, FALSE on error or
 *      user cancel.
 *
 * Side effects:
 *      The wipe operation will fill the partition with dummy files.
 *
 *-----------------------------------------------------------------------------
 */

Bool
Shrink_DoWipe(WiperPartition *part, GtkWidget* mainWnd) // IN: partition to be wiped
{
   Bool performShrink = FALSE;
   GtkWidget *btn;
   int progress = 0;
   unsigned char *err;

   /*
    * 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()) {
         performShrink = TRUE;
   }

   if (!performShrink) {
      ToolsMain_MsgBox("Error", SHRINK_CONFLICT_ERR);
      return FALSE;
   }

   shrinkWipeDlg = gtk_dialog_new();
   gtk_window_set_title(GTK_WINDOW(shrinkWipeDlg), "Please Wait...");
   gtk_window_set_transient_for(GTK_WINDOW(shrinkWipeDlg), GTK_WINDOW(mainWnd));
   gtk_window_set_position(GTK_WINDOW(shrinkWipeDlg), GTK_WIN_POS_CENTER);
   gtk_widget_show(shrinkWipeDlg);
   gtk_window_set_modal(GTK_WINDOW(shrinkWipeDlg), TRUE);
   gtk_container_set_border_width(GTK_CONTAINER(GTK_DIALOG(shrinkWipeDlg)->vbox),
                                  10);
   gdk_window_set_icon_list(shrinkWipeDlg->window, gIconList);
   gtk_signal_connect(GTK_OBJECT(shrinkWipeDlg), "destroy",
                      GTK_SIGNAL_FUNC(Shrink_OnWipeDestroy), shrinkWipeDlg);

   shrinkWipeProgress = gtk_progress_bar_new();
   gtk_widget_show(shrinkWipeProgress);
   gtk_progress_set_show_text(GTK_PROGRESS(shrinkWipeProgress), TRUE);
   gtk_progress_set_format_string(GTK_PROGRESS(shrinkWipeProgress),
                                  "Preparing to shrink... (%p%%)");
   gtk_progress_set_text_alignment(GTK_PROGRESS(shrinkWipeProgress), 0, 0.5);
   gtk_progress_set_activity_mode(GTK_PROGRESS(shrinkWipeProgress), FALSE);
   gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(shrinkWipeProgress),
                                  GTK_PROGRESS_CONTINUOUS);
   gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(shrinkWipeProgress),
                                    GTK_PROGRESS_LEFT_TO_RIGHT);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(shrinkWipeDlg)->vbox),
                      shrinkWipeProgress, FALSE, FALSE, 0);

#ifdef GTK2
   btn = gtk_button_new_with_mnemonic("_Cancel");
#else
   btn = gtk_button_new_with_label("Cancel");
#endif
   gtk_widget_show(btn);
   gtk_box_pack_end(GTK_BOX(GTK_DIALOG(shrinkWipeDlg)->action_area), btn,
                    FALSE, FALSE, 0);
   gtk_widget_set_usize(btn, 70, 25);
   gtk_signal_connect_object(GTK_OBJECT(btn), "clicked",
                             GTK_SIGNAL_FUNC(gtk_widget_destroy),
                             GTK_OBJECT(shrinkWipeDlg));

   gtk_widget_show_all(shrinkWipeDlg);


   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) {
            ToolsMain_MsgBox("Error", "Unable to create wiper file\n");
         }
         else {
            ToolsMain_MsgBox("Error", err);
         }
         wiper = NULL;
         gtk_widget_destroy(shrinkWipeDlg);
      } else {
         gtk_progress_set_percentage(GTK_PROGRESS(shrinkWipeProgress),
                                     progress/100.0);
      }

      while (gtk_events_pending()) {
         gtk_main_iteration();
      }

   }

   if (progress >= 100) {
      wiper = NULL;
      gtk_widget_destroy(shrinkWipeDlg);
      return TRUE;
   } else {
      return FALSE;
   }
}


/*
 *-----------------------------------------------------------------------------
 *
 * Shrink_OnWipeDestroy  --
 *
 *      Callback for the gtk signal "destroy" on the wipe progress dialog.
 *      Cancel the wipe operation, setting global variables so the loop in
 *      Shrink_DoWipe will exit.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      The wipe operation will be canceled, and "zero" files removed.
 *
 *-----------------------------------------------------------------------------
 */

void
Shrink_OnWipeDestroy(GtkWidget *widget,     // IN: the cancel button
                     gpointer user_data) // IN: unused
{
   if (wiper != NULL) {
      Wiper_Cancel(&wiper);
      wiper = NULL;
   }
}