source: MondoRescue/trunk/mondo/mondo/mondorestore/mondo-rstr-newt.c@ 274

Last change on this file since 274 was 274, checked in by bcornec, 18 years ago

merge -r272:273 $SVN_M/branches/2.06

  • Property svn:keywords set to Id
File size: 86.9 KB
Line 
1/***************************************************************************
2 * $Id: mondo-rstr-newt.c 274 2006-01-03 15:15:54Z bcornec $
3*/
4
5
6/**
7 * @file
8 * Functions for handling GUI interfaces in the restore.
9 */
10
11#ifdef __FreeBSD__
12#define OSSWAP(linux,fbsd) fbsd
13//#include <libgen.h>
14#else
15#define OSSWAP(linux,fbsd) linux
16#endif
17
18#include "mondo-rstr-newt.h"
19
20//static char cvsid[] = "$Id: mondo-rstr-newt.c 274 2006-01-03 15:15:54Z bcornec $";
21
22extern char err_log_lines[NOOF_ERR_LINES][MAX_STR_LEN];
23
24
25/**
26 * @defgroup restoreGuiDisklist Disklist GUI
27 * Functions for manipulating the disklist GUI.
28 * @ingroup restoreGuiGroup
29 */
30/**
31 * @defgroup restoreGuiMountlist Mountlist GUI
32 * Functions for manipulating the mountlist/raidlist GUI.
33 * @ingroup restoreGuiGroup
34 */
35/**
36 * @defgroup restoreGuiVarslist RAID Variables GUI
37 * Functions for manipulating the RAID variables GUI.
38 * @ingroup restoreGuiGroup
39 */
40
41/**
42 * @addtogroup restoreGuiGroup
43 * @{
44 */
45/**
46 * Add an entry in @p disklist from the list in @p unallocated_raid_partitions.
47 * @param disklist The disklist to add an entry to.
48 * @param raid_device Unused; make sure it's non-NULL non-"".
49 * @param unallocated_raid_partitions The list of unallocated RAID partitions
50 * that the user may choose from.
51 * @bug raid_device is unused.
52 * @ingroup restoreGuiDisklist
53 */
54void
55add_disklist_entry(struct list_of_disks *disklist, char *raid_device,
56 struct mountlist_itself *unallocated_raid_partitions)
57{
58 /** buffers ***********************************************************/
59 char *tmp;
60
61 /** newt **************************************************************/
62 newtComponent myForm;
63 newtComponent bOK;
64 newtComponent bCancel;
65 newtComponent b_res;
66 newtComponent partitionsListbox;
67 newtComponent headerMsg;
68
69 /** prototypes *********************************************************/
70 void *keylist[ARBITRARY_MAXIMUM];
71 void *curr_choice;
72
73 /** int ****************************************************************/
74 int i = 0;
75 int index = 0;
76 int currline = 0;
77 int items = 0;
78
79 assert(disklist != NULL);
80 assert_string_is_neither_NULL_nor_zerolength(raid_device);
81 assert(unallocated_raid_partitions != NULL);
82
83 newtPushHelpLine
84 (" Add one of the following unallocated RAID partitions to this RAID device.");
85 asprintf(&tmp, "%-26s %s", "Device", "Size");
86 headerMsg = newtLabel(1, 1, tmp);
87 paranoid_free(tmp);
88
89 partitionsListbox =
90 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
91 redraw_unallocpartnslist(unallocated_raid_partitions, keylist,
92 partitionsListbox);
93 i = 7;
94 bOK = newtCompactButton(i, 9, " OK ");
95 bCancel = newtCompactButton(i += 9, 9, "Cancel");
96 newtOpenWindow(22, 6, 36, 10, "Unallocated RAID partitions");
97 myForm = newtForm(NULL, NULL, 0);
98 newtFormAddComponents(myForm, headerMsg, partitionsListbox, bOK,
99 bCancel, NULL);
100 b_res = newtRunForm(myForm);
101 if (b_res != bCancel) {
102 curr_choice = newtListboxGetCurrent(partitionsListbox);
103 for (currline = 0;
104 currline < unallocated_raid_partitions->entries
105 && keylist[currline] != curr_choice; currline++);
106 if (currline == unallocated_raid_partitions->entries
107 && unallocated_raid_partitions->entries > 0) {
108 log_it("I don't know what this button does");
109 } else {
110 index = find_next_free_index_in_disklist(disklist);
111
112 items = disklist->entries;
113 strcpy(disklist->el[items].device,
114 unallocated_raid_partitions->el[currline].device);
115 disklist->el[items].index = index;
116 disklist->entries = ++items;
117
118 }
119 }
120 newtFormDestroy(myForm);
121 newtPopWindow();
122 newtPopHelpLine();
123}
124
125
126/**
127 * Add an entry to @p mountlist.
128 * @param mountlist The mountlist to add an entry to.
129 * @param raidlist The raidlist that accompanies @p mountlist.
130 * @param listbox The listbox component in the mountlist editor.
131 * @param currline The line selected in @p listbox.
132 * @param keylist The list of keys for @p listbox.
133 * @ingroup restoreGuiMountlist
134 */
135void
136add_mountlist_entry(struct mountlist_itself *mountlist,
137 struct raidlist_itself *raidlist,
138 newtComponent listbox, int currline, void *keylist[])
139{
140
141 /** int **************************************************************/
142 int i = 0;
143 int num_to_add = 0;
144
145 /** newt *************************************************************/
146 newtComponent myForm;
147 newtComponent bOK;
148 newtComponent bCancel;
149 newtComponent b_res;
150 newtComponent mountpointComp;
151 newtComponent label0;
152 newtComponent label1;
153 newtComponent label2;
154 newtComponent label3;
155 newtComponent sizeComp;
156 newtComponent deviceComp;
157 newtComponent formatComp;
158
159 /** buffers **********************************************************/
160 char *drive_to_add;
161 char *mountpoint_str;
162 char *size_str = NULL;
163 char *device_str;
164 char *format_str;
165
166 /** pointers *********************************************************/
167 char *mountpoint_here;
168 char *size_here;
169 char *device_here;
170 char *format_here;
171
172 assert(mountlist != NULL);
173 assert(raidlist != NULL);
174 assert(listbox != NULL);
175 assert(keylist != NULL);
176
177 asprintf(&device_str, "/dev/");
178 asprintf(&mountpoint_str, "/");
179#ifdef __FreeBSD__
180 asprintf(&format_str, "ufs");
181#else
182 asprintf(&format_str, "ext2");
183#endif
184 newtOpenWindow(20, 5, 48, 10, "Add entry");
185 label0 = newtLabel(2, 1, "Device: ");
186 label1 = newtLabel(2, 2, "Mountpoint:");
187 label2 = newtLabel(2, 3, "Size (MB): ");
188 label3 = newtLabel(2, 4, "Format: ");
189 deviceComp =
190 newtEntry(14, 1, device_str, 30, (void *) &device_here, 0);
191 mountpointComp =
192 newtEntry(14, 2, mountpoint_str, 30, (void *) &mountpoint_here, 0);
193
194 formatComp =
195 newtEntry(14, 4, format_str, 15, (void *) &format_here, 0);
196 sizeComp = newtEntry(14, 3, size_str, 10, (void *) &size_here, 0);
197 bOK = newtButton(5, 6, " OK ");
198 bCancel = newtButton(17, 6, "Cancel");
199 newtPushHelpLine
200 ("To add an entry to the mountlist, please fill in these fields and then hit 'OK'");
201 myForm = newtForm(NULL, NULL, 0);
202 newtFormAddComponents(myForm, deviceComp, mountpointComp, sizeComp,
203 formatComp, label0, label1, label2, label3, bOK,
204 bCancel, NULL);
205 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
206 b_res = newtRunForm(myForm);
207
208 paranoid_free(device_str);
209 asprintf(&device_str, device_here);
210 strip_spaces(device_str);
211
212 paranoid_free(format_str);
213 asprintf(&format_str, format_here);
214 strip_spaces(format_str);
215
216 paranoid_free(mountpoint_str);
217 asprintf(&mountpoint_str, mountpoint_here);
218 strip_spaces(mountpoint_str);
219
220 paranoid_free(size_str);
221 asprintf(&size_str, size_here);
222 strip_spaces(size_str);
223
224 if (b_res == bOK) {
225 if (device_str[strlen(device_str) - 1] == '/') {
226 popup_and_OK("You left the device nearly blank!");
227 b_res = NULL;
228 }
229 if (size_of_specific_device_in_mountlist(mountlist, device_str)
230 >= 0) {
231 popup_and_OK("Can't add this - you've got one already!");
232 b_res = NULL;
233 }
234 }
235 }
236 newtFormDestroy(myForm);
237 newtPopHelpLine();
238 newtPopWindow();
239 if (b_res == bCancel) {
240 return;
241 }
242 asprintf(&drive_to_add, device_str);
243 for (i = strlen(drive_to_add); isdigit(drive_to_add[i - 1]); i--);
244 num_to_add = atoi(drive_to_add + i);
245 drive_to_add[i] = '\0';
246 paranoid_free(drive_to_add);
247
248 currline = mountlist->entries;
249 strcpy(mountlist->el[currline].device, device_str);
250 strcpy(mountlist->el[currline].mountpoint, mountpoint_str);
251 paranoid_free(mountpoint_str);
252
253 strcpy(mountlist->el[currline].format, format_str);
254 paranoid_free(format_str);
255
256 mountlist->el[currline].size = atol(size_str) * 1024;
257 paranoid_free(size_str);
258
259 mountlist->entries++;
260 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)) {
261 initiate_new_raidlist_entry(raidlist, mountlist, currline,
262 device_str);
263 }
264 paranoid_free(device_str);
265 redraw_mountlist(mountlist, keylist, listbox);
266}
267
268
269#ifndef __FreeBSD__
270/**
271 * Add an entry to the additional RAID variables section of @p raidrec.
272 * @param raidrec The RAID device record containing the RAID variables list to add to.
273 * @ingroup restoreGuiVarslist
274 */
275void add_varslist_entry(struct raid_device_record *raidrec)
276{
277
278 /** buffers ***********************************************************/
279 char *sz_out = NULL;
280
281 /** int ****************************************************************/
282 int items = 0;
283 int i = 0;
284
285 assert(raidrec != NULL);
286
287 if (popup_and_get_string
288 ("Add variable", "Enter the name of the variable to add", sz_out,
289 MAX_STR_LEN)) {
290 strip_spaces(sz_out);
291 items = raidrec->additional_vars.entries;
292 for (i = 0;
293 i < items
294 && strcmp(raidrec->additional_vars.el[i].label, sz_out); i++);
295 if (i < items) {
296 popup_and_OK
297 ("No need to add that variable. It is already listed here.");
298 } else {
299 strcpy(raidrec->additional_vars.el[items].label, sz_out);
300 edit_varslist_entry(raidrec, items);
301 raidrec->additional_vars.entries = ++items;
302 }
303 }
304}
305#endif
306
307/**
308 * Calculate the size of @p raid_device.
309 * @param mountlist The mountlist containing information about the user's partitions.
310 * @param raidlist The raidlist that goes with @p mountlist.
311 * @param raid_device The device to calculate the size of.
312 * @return The size of the RAID device in Kilobytes.
313 * @ingroup restoreUtilityGroup
314 */
315long
316calculate_raid_device_size(struct mountlist_itself *mountlist,
317 struct raidlist_itself *raidlist,
318 char *raid_device)
319{
320#ifdef __FreeBSD__
321 /** FreeBSD-specific version of calculate_raid_device_size() **/
322
323 /** structures ********************************************************/
324 struct vinum_volume *raidrec;
325
326 /** int ***************************************************************/
327 int i = 0, j = 0;
328 int noof_partitions = 0;
329
330 /** long **************************************************************/
331 long total_size = 0;
332 long plex_size = 0;
333 long smallest_partition = 999999999;
334 long smallest_plex = 999999999;
335 long sp = 0;
336
337 /** buffers ***********************************************************/
338 char *tmp;
339 char *devname;
340
341
342
343
344 for (i = 0;
345 i < raidlist->entries
346 && strcmp(raidlist->el[i].volname, basename(raid_device)); i++);
347 if (i == raidlist->entries) {
348 asprintf(&tmp,
349 "Cannot calc size of raid device %s - cannot find it in raidlist",
350 raid_device);
351 log_it(tmp);
352 paranoid_free(tmp);
353 return (0); // Isn't this more sensible than 999999999? If the raid dev !exists,
354 // then it has no size, right?
355 }
356 raidrec = &raidlist->el[i];
357 total_size = 0;
358 if (raidrec->plexes == 0)
359 return 0;
360 for (j = 0; j < raidrec->plexes; j++) {
361 plex_size = 0;
362 int k = 0, l = 0;
363 for (k = 0; k < raidrec->plex[j].subdisks; ++k) {
364 asprintf(&devname, raidrec->plex[j].sd[k].which_device);
365 for (l = 0; l < raidlist->disks.entries; ++l) {
366 if (!strcmp(devname, raidlist->disks.el[l].name)) {
367 switch (raidrec->plex[j].raidlevel) {
368 case -1:
369 plex_size +=
370 size_of_specific_device_in_mountlist(mountlist,
371 raidlist->
372 disks.
373 el[l].
374 device);
375 break;
376 case 0:
377 case 5:
378 if (size_of_specific_device_in_mountlist(mountlist,
379 raidlist->
380 disks.
381 el[l].
382 device) <
383 smallest_partition) {
384 smallest_partition =
385 size_of_specific_device_in_mountlist
386 (mountlist, raidlist->disks.el[l].device);
387 }
388 break;
389 }
390 }
391 }
392 paranoid_free(devname);
393 }
394
395 if (!is_this_raid_personality_registered
396 (raidrec->plex[j].raidlevel)) {
397 log_it
398 ("%s has a really weird RAID level - couldn't calc size :(",
399 raid_device);
400 return (999999999);
401 }
402 if (raidrec->plex[j].raidlevel != -1) {
403 plex_size = smallest_partition * (raidrec->plex[j].subdisks -
404 (raidrec->plex[j].
405 raidlevel == 5 ? 1 : 0));
406 }
407 if (plex_size < smallest_plex)
408 smallest_plex = plex_size;
409
410 smallest_partition = 999999999;
411 }
412
413 asprintf(&tmp, "I have calculated %s's real size to be %ld", raid_device,
414 (long) smallest_plex);
415 log_it(tmp);
416 paranoid_free(tmp);
417 return (smallest_plex);
418#else
419 /** Linux-specific version of calculate_raid_device_size() **/
420
421 /** structures ********************************************************/
422 struct raid_device_record *raidrec;
423
424 /** int ***************************************************************/
425 int i = 0;
426 int noof_partitions = 0;
427
428 /** long **************************************************************/
429 long total_size = 0;
430 long smallest_partition = 999999999;
431 long sp = 0;
432
433 /** buffers ***********************************************************/
434 char *tmp;
435
436 assert(mountlist != NULL);
437 assert(raidlist != NULL);
438 assert_string_is_neither_NULL_nor_zerolength(raid_device);
439
440 for (i = 0;
441 i < raidlist->entries
442 && strcmp(raidlist->el[i].raid_device, raid_device); i++);
443 if (i == raidlist->entries) {
444 asprintf(&tmp,
445 "Cannot calc size of raid device %s - cannot find it in raidlist",
446 raid_device);
447 log_it(tmp);
448 paranoid_free(tmp);
449 return (999999999);
450 }
451 raidrec = &raidlist->el[i];
452 noof_partitions = raidrec->data_disks.entries;
453 if (raidrec->raid_level == -1 || raidrec->raid_level == 0) {
454 for (total_size = 0, i = 0; i < noof_partitions; i++) {
455 total_size +=
456 size_of_specific_device_in_mountlist(mountlist,
457 raidrec->data_disks.
458 el[i].device);
459 }
460 } else {
461 for (i = 0; i < noof_partitions; i++) {
462 sp = size_of_specific_device_in_mountlist(mountlist,
463 raidrec->data_disks.
464 el[i].device);
465 if (smallest_partition > sp) {
466 smallest_partition = sp;
467 }
468 }
469 total_size = smallest_partition * (noof_partitions - 1);
470 }
471 asprintf(&tmp, "I have calculated %s's real size to be %ld", raid_device,
472 (long) total_size);
473 log_it(tmp);
474 paranoid_free(tmp);
475 return (total_size);
476#endif
477}
478
479
480
481/**
482 * Choose the RAID level for the RAID device record in @p raidrec.
483 * @param raidrec The RAID device record to set the RAID level of.
484 * @ingroup restoreGuiMountlist
485 */
486void
487choose_raid_level(struct OSSWAP (raid_device_record, vinum_plex) * raidrec)
488{
489
490#ifdef __FreeBSD__
491
492 /** int ***************************************************************/
493 int res = 0;
494 int out = 0;
495
496 /** buffers ***********************************************************/
497 char *tmp;
498 char *prompt;
499 char *sz;
500
501 asprintf(&prompt,
502 "Please enter the RAID level you want. (concat, striped, raid5)");
503 if (raidrec->raidlevel == -1) {
504 asprintf(&tmp, "concat");
505 } else if (raidrec->raidlevel == 0) {
506 asprintf(&tmp, "striped");
507 } else {
508 asprintf(&tmp, "raid%i", raidrec->raidlevel);
509 }
510 for (out = 999; out == 999;) {
511 res = popup_and_get_string("Specify RAID level", prompt, tmp, 10);
512 if (!res) {
513 return;
514 }
515 strip_spaces(tmp);
516 if (tmp[0] == '[' && tmp[strlen(tmp) - 1] == ']') {
517 asprintf(&sz, tmp);
518 strncpy(tmp, sz + 1, strlen(sz) - 2);
519 tmp[strlen(sz) - 2] = '\0';
520 paranoid_free(sz);
521 }
522 if (!strcmp(tmp, "concat")) {
523 out = -1;
524 } else if (!strcmp(tmp, "striped")) {
525 out = 0;
526 } else if (!strcmp(tmp, "raid5")) {
527 out = 5;
528 }
529 log_it(tmp);
530 paranoid_free(tmp);
531 if (is_this_raid_personality_registered(out)) {
532 log_it
533 ("Groovy. You've picked a RAID personality which is registered.");
534 } else {
535 if (ask_me_yes_or_no
536 ("You have chosen a RAID personality which is not registered with the kernel. Make another selection?"))
537 {
538 out = 999;
539 }
540 }
541 }
542 paranoid_free(prompt);
543 raidrec->raidlevel = out;
544#else
545 /** buffers ***********************************************************/
546 char *tmp;
547 char personalities[MAX_STR_LEN];
548 char *prompt;
549 char *sz;
550 int out = 0, res = 0;
551
552
553 assert(raidrec != NULL);
554 system
555 ("grep Pers /proc/mdstat > /tmp/raid-personalities.txt 2> /dev/null");
556 strcpy(personalities,
557 last_line_of_file("/tmp/raid-personalities.txt"));
558 asprintf(&prompt, "Please enter the RAID level you want. %s",
559 personalities);
560 if (raidrec->raid_level == -1) {
561 asprintf(&tmp, "linear");
562 } else {
563 asprintf(&tmp, "%d", raidrec->raid_level);
564 }
565 for (out = 999;
566 out != -1 && out != 0 && out != 1 && out != 4 && out != 5
567 && out != 10;) {
568 res = popup_and_get_string("Specify RAID level", prompt, tmp, 10);
569 if (!res) {
570 return;
571 }
572 strip_spaces(tmp);
573 if (tmp[0] == '[' && tmp[strlen(tmp) - 1] == ']') {
574 asprintf(&sz, tmp);
575 strncpy(tmp, sz + 1, strlen(sz) - 2);
576 tmp[strlen(sz) - 2] = '\0';
577 paranoid_free(sz);
578 }
579 if (!strcmp(tmp, "linear")) {
580 out = -1;
581 } else if (!strncmp(tmp, "raid", 4)) {
582 out = atoi(tmp + 4);
583 } else {
584 out = atoi(tmp);
585 }
586 log_it(tmp);
587 paranoid_free(tmp);
588 if (is_this_raid_personality_registered(out)) {
589 log_it
590 ("Groovy. You've picked a RAID personality which is registered.");
591 } else {
592 if (ask_me_yes_or_no
593 ("You have chosen a RAID personality which is not registered with the kernel. Make another selection?"))
594 {
595 out = 999;
596 }
597 }
598 }
599 paranoid_free(prompt);
600 raidrec->raid_level = out;
601#endif
602}
603
604
605/**
606 * Delete the partitions in @p disklist from @p mountlist because they
607 * were part of a deleted RAID device.
608 * @param mountlist The mountlist containing information about the user's partitions.
609 * @param raidlist The raidlist that goes with @p mounntlist.
610 * @param disklist The list of disks to remove from @p mountlist.
611 * @ingroup restoreGuiDisklist
612 */
613void
614del_partns_listed_in_disklist(struct mountlist_itself *mountlist,
615 struct raidlist_itself *raidlist,
616 struct list_of_disks *disklist)
617{
618
619 /** int ***************************************************************/
620 int i = 0;
621 int pos = 0;
622
623 /** buffers ***********************************************************/
624 char *tmp;
625
626 assert(mountlist != NULL);
627 assert(raidlist != NULL);
628 assert(disklist != NULL);
629
630 for (i = 0; i < disklist->entries; i++) {
631 for (pos = 0;
632 pos < mountlist->entries
633 && strcmp(mountlist->el[pos].device, disklist->el[i].device);
634 pos++);
635 if (pos < mountlist->entries) {
636 asprintf(&tmp,
637 "Deleting partition %s cos it was part of a now-defunct RAID",
638 mountlist->el[pos].device);
639 log_it(tmp);
640 paranoid_free(tmp);
641 memcpy((void *) &mountlist->el[pos],
642 (void *) &mountlist->el[mountlist->entries - 1],
643 sizeof(struct mountlist_line));
644 mountlist->entries--;
645 }
646 }
647}
648
649
650/**
651 * Delete entry number @p currline from @p disklist.
652 * @param disklist The disklist to remove the entry from.
653 * @param raid_device The RAID device containing the partition we're removing.
654 * Used only in the popup "are you sure?" box.
655 * @param currline The line number (starting from 0) of the item to delete.
656 * @ingroup restoreGuiDisklist
657 */
658void
659delete_disklist_entry(struct list_of_disks *disklist, char *raid_device,
660 int currline)
661{
662
663 /** int ***************************************************************/
664 int pos = 0;
665
666 /** buffers ***********************************************************/
667 char *tmp;
668
669 assert(disklist != NULL);
670 assert_string_is_neither_NULL_nor_zerolength(raid_device);
671
672 asprintf(&tmp, "Delete %s from RAID device %s - are you sure?",
673 disklist->el[currline].device, raid_device);
674 if (!ask_me_yes_or_no(tmp)) {
675 paranoid_free(tmp);
676 return;
677 }
678 paranoid_free(tmp);
679 for (pos = currline; pos < disklist->entries - 1; pos++) {
680 /* memcpy((void*)&disklist->el[pos], (void*)&disklist->el[pos+1], sizeof(struct s_disk)); */
681 strcpy(disklist->el[pos].device, disklist->el[pos + 1].device);
682 }
683 disklist->entries--;
684}
685
686
687/**
688 * Delete entry number @p currline from @p mountlist.
689 * @param mountlist The mountlist to delete the entry from.
690 * @param raidlist The raidlist that goes with @p mountlist.
691 * @param listbox The Newt listbox component in the mountlist editor.
692 * @param currline The line number (starting from 0) of the item to delete.
693 * @param keylist The list of keys for @p listbox.
694 * @ingroup restoreGuiMountlist
695 */
696void
697delete_mountlist_entry(struct mountlist_itself *mountlist,
698 struct raidlist_itself *raidlist,
699 newtComponent listbox, int currline,
700 void *keylist[])
701{
702
703 /** int ***************************************************************/
704 int pos = 0;
705
706 /** buffers ***********************************************************/
707 char *tmp;
708 char *device;
709
710 assert(mountlist != NULL);
711 assert(raidlist != NULL);
712 assert(listbox != NULL);
713 assert(keylist != NULL);
714
715 pos =
716 which_raid_device_is_using_this_partition(raidlist,
717 mountlist->el[currline].
718 device);
719 if (pos >= 0) {
720 asprintf(&tmp, "Cannot delete %s: it is in use by RAID device %s",
721 mountlist->el[currline].device,
722 raidlist->el[pos].OSSWAP(raid_device, volname));
723 popup_and_OK(tmp);
724 paranoid_free(tmp);
725 return;
726 }
727 asprintf(&tmp, "Delete %s - are you sure?",
728 mountlist->el[currline].device);
729 if (!ask_me_yes_or_no(tmp)) {
730 paranoid_free(tmp);
731 return;
732 }
733 paranoid_free(tmp);
734
735 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)) {
736 asprintf(&device, mountlist->el[currline].device);
737 delete_raidlist_entry(mountlist, raidlist, device);
738 for (currline = 0;
739 currline < mountlist->entries
740 && strcmp(mountlist->el[currline].device, device);
741 currline++);
742 if (currline == mountlist->entries) {
743 log_it("Dev is gone. I can't delete it. Ho-hum");
744 paranoid_free(device);
745 return;
746 }
747 paranoid_free(device);
748 }
749 memcpy((void *) &mountlist->el[currline],
750 (void *) &mountlist->el[mountlist->entries - 1],
751 sizeof(struct mountlist_line));
752 mountlist->entries--;
753 redraw_mountlist(mountlist, keylist, listbox);
754}
755
756
757/**
758 * Delete @p device from @p raidlist.
759 * @param mountlist The mountlist containing information about the user's partitions.
760 * @param raidlist The raidlist containing the RAID device to delete.
761 * @param device The device (e.g. /dev/md0) to delete.
762 * @ingroup restoreGuiMountlist
763 */
764void
765delete_raidlist_entry(struct mountlist_itself *mountlist,
766 struct raidlist_itself *raidlist, char *device)
767{
768
769 /** int ***************************************************************/
770 int i = 0;
771 int items = 0;
772
773 /** bool **************************************************************/
774 bool delete_partitions_too;
775
776 /** buffers ***********************************************************/
777 char *tmp;
778
779 assert(mountlist != NULL);
780 assert(raidlist != NULL);
781 assert_string_is_neither_NULL_nor_zerolength(device);
782
783 i = find_raid_device_in_raidlist(raidlist, device);
784 if (i < 0) {
785 return;
786 }
787 asprintf(&tmp, "Do you want me to delete %s's partitions, too?", device);
788 delete_partitions_too = ask_me_yes_or_no(tmp);
789 if (delete_partitions_too) {
790#ifdef __FreeBSD__
791 // static so it's zeroed
792 static struct list_of_disks d;
793 int x, y, z;
794
795 for (x = 0; x < raidlist->el[i].plexes; ++x) {
796 for (y = 0; y < raidlist->el[i].plex[x].subdisks; ++y) {
797 for (z = 0; z < raidlist->disks.entries; ++z) {
798 if (!strcmp(raidlist->el[i].plex[x].sd[y].which_device,
799 raidlist->disks.el[z].name)) {
800 strcpy(d.el[d.entries].name,
801 raidlist->disks.el[z].name);
802 strcpy(d.el[d.entries++].device,
803 raidlist->disks.el[z].device);
804 }
805 }
806 }
807 }
808
809 del_partns_listed_in_disklist(mountlist, raidlist, &d);
810#else
811 del_partns_listed_in_disklist(mountlist, raidlist,
812 &raidlist->el[i].data_disks);
813 del_partns_listed_in_disklist(mountlist, raidlist,
814 &raidlist->el[i].spare_disks);
815 del_partns_listed_in_disklist(mountlist, raidlist,
816 &raidlist->el[i].parity_disks);
817 del_partns_listed_in_disklist(mountlist, raidlist,
818 &raidlist->el[i].failed_disks);
819#endif
820 }
821 items = raidlist->entries;
822 if (items == 1) {
823 items = 0;
824 } else {
825 log_it(tmp);
826 memcpy((void *) &raidlist->el[i],
827 (void *) &raidlist->el[items - 1],
828 sizeof(struct OSSWAP (raid_device_record, vinum_volume)));
829 items--;
830 }
831 paranoid_free(tmp);
832
833 raidlist->entries = items;
834}
835
836
837#ifndef __FreeBSD__
838/**
839 * Delete entry number @p lino in the additional RAID variables section of @p raidrec.
840 * @param raidrec The RAID device record containing the RAID variable to delete.
841 * @param lino The line number (starting from 0) of the variable to delete.
842 * @ingroup restoreGuiVarslist
843 */
844void delete_varslist_entry(struct raid_device_record *raidrec, int lino)
845{
846
847 /** buffers ************************************************************/
848 char *tmp;
849
850 /** structures *********************************************************/
851 struct additional_raid_variables *av;
852
853 assert(raidrec != NULL);
854
855 av = &raidrec->additional_vars;
856 asprintf(&tmp, "Delete %s - are you sure?", av->el[lino].label);
857 if (ask_me_yes_or_no(tmp)) {
858 if (!strcmp(av->el[lino].label, "persistent-superblock")
859 || !strcmp(av->el[lino].label, "chunk-size")) {
860 paranoid_free(tmp);
861 asprintf(&tmp, "%s must not be deleted. It would be bad.",
862 av->el[lino].label);
863 popup_and_OK(tmp);
864 } else {
865 memcpy((void *) &av->el[lino], (void *) &av->el[av->entries--],
866 sizeof(struct raid_var_line));
867 }
868 }
869 paranoid_free(tmp);
870}
871#endif
872
873
874/**
875 * Redraw the filelist display.
876 * @param filelist The filelist structure to edit.
877 * @param keylist The list of keys for @p listbox.
878 * @param listbox The Newt listbox component containing some of the filelist entries.
879 * @return The number of lines currently being displayed.
880 * @ingroup restoreGuiGroup
881 */
882int
883redraw_filelist(struct s_node *filelist, void *keylist[ARBITRARY_MAXIMUM],
884 newtComponent listbox)
885{
886
887 /** int ***************************************************************/
888 static int lines_in_flist_window = 0;
889 static int depth = 0;
890 int i = 0;
891
892 /** structures *******************************************************/
893 struct s_node *node;
894
895 /** buffers **********************************************************/
896 static char *current_filename;
897 char *tmp;
898
899 /** bool *************************************************************/
900 /* void*dummyptr; */
901 bool dummybool;
902 static bool warned_already;
903
904 assert(filelist != NULL);
905 assert(keylist != NULL);
906 assert(listbox != NULL);
907
908
909 if (depth == 0) {
910 lines_in_flist_window = 0;
911 warned_already = FALSE;
912 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
913 g_strings_of_flist_window[i][0] = '\0';
914 g_is_path_selected[i] = FALSE;
915 }
916 }
917 for (node = filelist; node != NULL; node = node->right) {
918 current_filename[depth] = node->ch;
919 if (node->down) {
920 depth++;
921 i = redraw_filelist(node->down, keylist, listbox);
922 depth--;
923 }
924 if (node->ch == '\0' && node->expanded) {
925 if (lines_in_flist_window == ARBITRARY_MAXIMUM) {
926 if (!warned_already) {
927 warned_already = TRUE;
928 asprintf(&tmp,
929 "Too many lines. Displaying first %d entries only. Close a directory to see more.",
930 ARBITRARY_MAXIMUM);
931 popup_and_OK(tmp);
932 paranoid_free(tmp);
933 }
934 } else {
935 strcpy(g_strings_of_flist_window[lines_in_flist_window],
936 current_filename);
937 g_is_path_selected[lines_in_flist_window] = node->selected;
938 lines_in_flist_window++;
939 }
940 }
941 }
942 if (depth == 0) {
943 if (lines_in_flist_window > ARBITRARY_MAXIMUM) {
944 lines_in_flist_window = ARBITRARY_MAXIMUM;
945 }
946/* do an elementary sort */
947 for (i = 1; i < lines_in_flist_window; i++) {
948 if (strcmp
949 (g_strings_of_flist_window[i],
950 g_strings_of_flist_window[i - 1]) < 0) {
951 asprintf(&tmp, g_strings_of_flist_window[i]);
952 strcpy(g_strings_of_flist_window[i],
953 g_strings_of_flist_window[i - 1]);
954 strcpy(g_strings_of_flist_window[i - 1], tmp);
955 paranoid_free(tmp);
956
957 dummybool = g_is_path_selected[i];
958 g_is_path_selected[i] = g_is_path_selected[i - 1];
959 g_is_path_selected[i - 1] = dummybool;
960 i = 0;
961 }
962 }
963/* write list to screen */
964 newtListboxClear(listbox);
965 for (i = 0; i < lines_in_flist_window; i++) {
966 asprintf(&tmp, "%c%c %-80s", (g_is_path_selected[i] ? '*' : ' '),
967 (g_is_path_expanded[i] ? '+' : '-'),
968 strip_path(g_strings_of_flist_window[i]));
969 // BERLIOS: this is dangerous now
970 if (strlen(tmp) > 71) {
971 tmp[70] = '\0';
972 }
973 keylist[i] = (void *) i;
974 newtListboxAppendEntry(listbox, tmp, keylist[i]);
975 paranoid_free(tmp);
976 }
977 return (lines_in_flist_window);
978 } else {
979 return (0);
980 }
981}
982
983
984/**
985 * Strip a path to the bare minimum (^ pointing to the directory above, plus filename).
986 * @param tmp The path to strip.
987 * @return The stripped path.
988 * @author Conor Daly
989 * @ingroup restoreUtilityGroup
990 */
991char *strip_path(char *tmp)
992{
993
994 int i = 0, j = 0, slashcount = 0;
995 int slashloc = 0, lastslashloc = 0;
996
997 while (tmp[i] != '\0') { /* Count the slashes in tmp
998 1 slash per dir */
999 if (tmp[i] == '/') {
1000 slashcount++;
1001 lastslashloc = slashloc;
1002 slashloc = i;
1003 if (tmp[i + 1] == '\0') { /* if this slash is last char, back off */
1004 slashcount--;
1005 slashloc = lastslashloc;
1006 }
1007 }
1008 i++;
1009 }
1010 if (slashcount > 0)
1011 slashcount--; /* Keep one slash 'cos Hugh does... */
1012
1013 for (i = 0; i < slashcount; i++) { /* Replace each dir with a space char */
1014 tmpnopath[i] = ' ';
1015 }
1016
1017 i = slashloc;
1018 j = slashcount;
1019 while (tmp[i] != '\0') { /* Now add what's left of tmp */
1020 if ((tmpprevpath[j] == ' ' || tmpprevpath[j] == '^')
1021 && tmp[i] == '/' && tmpnopath[j - 1] != '^' && j != 0) { /* Add a pointer upwards if this is not in the same dir as line above */
1022 tmpnopath[j - 1] = '^';
1023 } else {
1024 tmpnopath[j++] = tmp[i++];
1025 }
1026 }
1027 tmpnopath[j] = '\0';
1028
1029 strcpy(tmpprevpath, tmpnopath); /* Make a copy for next iteration */
1030
1031 return (tmpnopath);
1032}
1033
1034
1035/**
1036 * Allow the user to edit the filelist and choose which files to restore.
1037 * @param filelist The node structure containing the filelist.
1038 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
1039 */
1040int edit_filelist(struct s_node *filelist)
1041{
1042
1043 /** newt **************************************************************/
1044 newtComponent myForm;
1045 newtComponent bLess = NULL;
1046 newtComponent bMore = NULL;
1047 newtComponent bToggle = NULL;
1048 newtComponent bOK = NULL;
1049 newtComponent bCancel = NULL;
1050 newtComponent b_res = NULL;
1051 newtComponent filelistListbox = NULL;
1052 newtComponent bRegex = NULL;
1053
1054 /** int ***************************************************************/
1055 int finished = FALSE;
1056 int lines_in_flist_window = 0;
1057 int indexno = 0;
1058 int j = 0;
1059
1060 /** ???? **************************************************************/
1061 void *curr_choice;
1062 void *keylist[ARBITRARY_MAXIMUM];
1063
1064 /** buffers ***********************************************************/
1065 char tmp[MAX_STR_LEN];
1066
1067 /** bool **************************************************************/
1068 bool dummybool;
1069
1070/* struct s_node *node; */
1071
1072 assert(filelist != NULL);
1073
1074 log_to_screen("Editing filelist");
1075 newtPushHelpLine
1076 (" Please edit the filelist to your satisfaction, then click OK or Cancel.");
1077 j = 4;
1078 bLess = newtCompactButton(j, 17, " Less ");
1079 bMore = newtCompactButton(j += 12, 17, " More ");
1080 bToggle = newtCompactButton(j += 12, 17, "Toggle");
1081 bRegex = newtCompactButton(j += 12, 17, "RegEx");
1082 bCancel = newtCompactButton(j += 12, 17, "Cancel");
1083 bOK = newtCompactButton(j += 12, 17, " OK ");
1084 filelistListbox =
1085 newtListbox(2, 1, 15, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1086 toggle_all_root_dirs_on(filelist);
1087 lines_in_flist_window =
1088 redraw_filelist(filelist, keylist, filelistListbox);
1089 newtOpenWindow(1, 3, 77, 18, "Editing filelist");
1090 myForm = newtForm(NULL, NULL, 0);
1091 newtFormAddComponents(myForm, filelistListbox, bLess, bMore, bToggle,
1092 bRegex, bCancel, bOK, NULL);
1093 while (!finished) {
1094 b_res = newtRunForm(myForm);
1095 if (b_res == bOK) {
1096 finished =
1097 ask_me_yes_or_no
1098 ("Are you happy with your file selection?");
1099 } else if (b_res == bCancel) {
1100 finished = TRUE;
1101 } else if (b_res == bRegex) {
1102 popup_and_OK("I haven't implemented this yet...");
1103 } else {
1104 curr_choice = newtListboxGetCurrent(filelistListbox);
1105 for (indexno = 0;
1106 indexno < lines_in_flist_window
1107 && keylist[indexno] != curr_choice; indexno++);
1108 if (indexno == lines_in_flist_window) {
1109 log_it
1110 ("I don't know what this button does; assuming I am to toggle 1st entry");
1111 indexno = 0;
1112 }
1113 sprintf(tmp, "You selected '%s'",
1114 g_strings_of_flist_window[indexno]);
1115 log_it(tmp);
1116 if (b_res == bMore) {
1117 g_is_path_expanded[indexno] = TRUE;
1118 toggle_path_expandability(filelist,
1119 g_strings_of_flist_window
1120 [indexno], TRUE);
1121 lines_in_flist_window =
1122 redraw_filelist(filelist, keylist, filelistListbox);
1123 newtListboxSetCurrentByKey(filelistListbox, curr_choice);
1124 } else if (b_res == bLess) {
1125 g_is_path_expanded[indexno] = FALSE;
1126 toggle_path_expandability(filelist,
1127 g_strings_of_flist_window
1128 [indexno], FALSE);
1129 lines_in_flist_window =
1130 redraw_filelist(filelist, keylist, filelistListbox);
1131 newtListboxSetCurrentByKey(filelistListbox, curr_choice);
1132 } else {
1133 if (!strcmp(g_strings_of_flist_window[indexno], "/")) {
1134 dummybool = !g_is_path_selected[indexno];
1135 for (j = 1; j < lines_in_flist_window; j++) {
1136 toggle_path_selection(filelist,
1137 g_strings_of_flist_window[j],
1138 dummybool);
1139 }
1140 } else {
1141 toggle_path_selection(filelist,
1142 g_strings_of_flist_window
1143 [indexno],
1144 !g_is_path_selected[indexno]);
1145 lines_in_flist_window =
1146 redraw_filelist(filelist, keylist,
1147 filelistListbox);
1148 }
1149 newtListboxSetCurrentByKey(filelistListbox, curr_choice);
1150 }
1151 for (indexno = 0;
1152 indexno < lines_in_flist_window
1153 && keylist[indexno] != curr_choice; indexno++);
1154 if (indexno == lines_in_flist_window) {
1155 log_it
1156 ("Layout of table has changed. Y pointer is reverting to zero.");
1157 indexno = 0;
1158 }
1159 }
1160 }
1161 newtFormDestroy(myForm);
1162 newtPopWindow();
1163 newtPopHelpLine();
1164 if (b_res == bOK) {
1165 return (0);
1166 } else {
1167/* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
1168 return (1);
1169 }
1170}
1171
1172
1173/**
1174 * Edit an entry in @p mountlist.
1175 * @param mountlist The mountlist containing information about the user's partitions.
1176 * @param raidlist The raidlist to accompany @p mountlist.
1177 * @param listbox The Newt listbox component in the mountlist editor.
1178 * @param currline The selected line (starting from 0) in @p listbox.
1179 * @param keylist The list of keys for @p listbox.
1180 * @ingroup restoreGuiMountlist
1181 */
1182void
1183edit_mountlist_entry(struct mountlist_itself *mountlist,
1184 struct raidlist_itself *raidlist,
1185 newtComponent listbox, int currline, void *keylist[])
1186{
1187
1188 /** structures ********************************************************/
1189 static struct raidlist_itself bkp_raidlist;
1190
1191 /** newt **************************************************************/
1192 newtComponent myForm;
1193 newtComponent bOK;
1194 newtComponent bCancel;
1195 newtComponent b_res;
1196 newtComponent mountpointComp;
1197 newtComponent label0;
1198 newtComponent label1;
1199 newtComponent label2;
1200 newtComponent label3;
1201 newtComponent sizeComp;
1202 newtComponent deviceComp;
1203 newtComponent formatComp;
1204 newtComponent b_raid = NULL;
1205
1206 /** buffers ***********************************************************/
1207 char device_str[MAX_STR_LEN];
1208 char mountpoint_str[MAX_STR_LEN];
1209 char size_str[MAX_STR_LEN];
1210 char format_str[MAX_STR_LEN];
1211 char tmp[MAX_STR_LEN];
1212 char device_used_to_be[MAX_STR_LEN];
1213 char mountpt_used_to_be[MAX_STR_LEN];
1214
1215 /** pointers **********************************************************/
1216 char *device_here;
1217 char *mountpoint_here;
1218 char *size_here;
1219 char *format_here;
1220
1221 /** int ***************************************************************/
1222 int j = 0;
1223
1224 assert(mountlist != NULL);
1225 assert(raidlist != NULL);
1226 assert(listbox != NULL);
1227 assert(keylist != NULL);
1228
1229 memcpy((void *) &bkp_raidlist, (void *) raidlist,
1230 sizeof(struct raidlist_itself));
1231 strcpy(device_str, mountlist->el[currline].device);
1232 strcpy(device_used_to_be, mountlist->el[currline].device);
1233 strcpy(mountpoint_str, mountlist->el[currline].mountpoint);
1234 strcpy(mountpt_used_to_be, mountlist->el[currline].mountpoint);
1235 strcpy(format_str, mountlist->el[currline].format);
1236 sprintf(size_str, "%lld", mountlist->el[currline].size / 1024);
1237 newtOpenWindow(20, 5, 48, 10, "Edit entry");
1238 label0 = newtLabel(2, 1, "Device:");
1239 label1 = newtLabel(2, 2, "Mountpoint:");
1240 label2 = newtLabel(2, 3, "Size (MB): ");
1241 label3 = newtLabel(2, 4, "Format: ");
1242 deviceComp =
1243 newtEntry(14, 1, device_str, 30, (void *) &device_here, 0);
1244 mountpointComp =
1245 newtEntry(14, 2, mountpoint_str, 30, (void *) &mountpoint_here, 0);
1246 formatComp =
1247 newtEntry(14, 4, format_str, 15, (void *) &format_here, 0);
1248 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1249 || !strcmp(mountlist->el[currline].mountpoint, "image")) {
1250 sizeComp = newtLabel(14, 3, size_str);
1251 } else {
1252 sizeComp = newtEntry(14, 3, size_str, 10, (void *) &size_here, 0);
1253 }
1254 bOK = newtButton(2, 6, " OK ");
1255 bCancel = newtButton(14, 6, "Cancel");
1256 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)) {
1257 b_raid = newtButton(26, 6, "RAID..");
1258 }
1259 newtPushHelpLine
1260 (" Edit this partition's mountpoint, size and format; then click 'OK'.");
1261 myForm = newtForm(NULL, NULL, 0);
1262 newtFormAddComponents(myForm, deviceComp, mountpointComp, sizeComp,
1263 formatComp, label0, label1, label2, label3, bOK,
1264 bCancel, b_raid, NULL);
1265 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
1266 b_res = newtRunForm(myForm);
1267 strcpy(device_str, device_here);
1268 strip_spaces(device_str);
1269 strcpy(mountpoint_str, mountpoint_here);
1270 strip_spaces(mountpoint_str);
1271 strcpy(format_str, format_here);
1272 strip_spaces(format_str);
1273 if (b_res == bOK && strstr(device_str, RAID_DEVICE_STUB)
1274 && strstr(device_used_to_be, RAID_DEVICE_STUB)
1275 && strcmp(device_str, device_used_to_be)) {
1276 popup_and_OK("You can't change /dev/mdX to /dev/mdY.");
1277 b_res = NULL;
1278 continue;
1279 } else if (b_res == bOK && !strcmp(mountpoint_str, "image")
1280 && strcmp(mountpt_used_to_be, "image")) {
1281 popup_and_OK("You can't change a regular device to an image.");
1282 b_res = NULL;
1283 continue;
1284 }
1285 if (!strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1286 && strcmp(mountlist->el[currline].mountpoint, "image")) {
1287 strcpy(size_str, size_here);
1288 strip_spaces(size_str);
1289 } else {
1290 sprintf(size_str, "%ld",
1291 calculate_raid_device_size(mountlist, raidlist,
1292 mountlist->el[currline].
1293 device) / 1024);
1294 newtLabelSetText(sizeComp, size_str);
1295 }
1296 /* do not let user click RAID button if user has changed device_str */
1297 if (b_res == b_raid) {
1298 if (strcmp(device_str, mountlist->el[currline].device)) {
1299 /*
1300 can't change mountlist's entry from /dex/mdX to /dev/mdY: it would ugly
1301 when you try to map the changes over to the raidtab list, trust me
1302 */
1303 popup_and_OK
1304 ("You cannot edit the RAID settings until you have OK'd your change to the device node.");
1305 } else {
1306 j = find_raid_device_in_raidlist(raidlist,
1307 mountlist->el[currline].
1308 device);
1309 if (j < 0) {
1310 sprintf(tmp,
1311 "/etc/raidtab does not have an entry for %s; please delete it and add it again",
1312 mountlist->el[currline].device);
1313 popup_and_OK(tmp);
1314 } else {
1315 log_it("edit_raidlist_entry - calling");
1316 edit_raidlist_entry(mountlist, raidlist,
1317 &raidlist->el[j], currline);
1318 }
1319 }
1320 }
1321 }
1322 newtFormDestroy(myForm);
1323 newtPopHelpLine();
1324 newtPopWindow();
1325 if (b_res == bCancel) {
1326 memcpy((void *) raidlist, (void *) &bkp_raidlist,
1327 sizeof(struct raidlist_itself));
1328 return;
1329 }
1330 strcpy(mountlist->el[currline].device, device_str);
1331 strcpy(mountlist->el[currline].mountpoint, mountpoint_str);
1332 strcpy(mountlist->el[currline].format, format_str);
1333 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1334 || !strcmp(mountlist->el[currline].mountpoint, "image")) {
1335 mountlist->el[currline].size =
1336 calculate_raid_device_size(mountlist, raidlist,
1337 mountlist->el[currline].device);
1338 } else {
1339 mountlist->el[currline].size = atol(size_str) * 1024;
1340 }
1341 newtListboxSetEntry(listbox, (int) keylist[currline],
1342 mountlist_entry_to_string(mountlist, currline));
1343 /* if new /dev/md RAID device then do funky stuff */
1344 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1345 && !strstr(device_used_to_be, RAID_DEVICE_STUB)) {
1346 initiate_new_raidlist_entry(raidlist, mountlist, currline,
1347 device_str);
1348 }
1349 /* if moving from RAID to non-RAID then do funky stuff */
1350 else if (strstr(device_used_to_be, RAID_DEVICE_STUB)
1351 && !strstr(device_str, RAID_DEVICE_STUB)) {
1352 delete_raidlist_entry(mountlist, raidlist, device_str);
1353 }
1354 /* if moving a non-RAID to another non-RAID then re-jig any RAID disks, if necessary */
1355 else if (!strstr(device_used_to_be, RAID_DEVICE_STUB)
1356 && !strstr(device_str, RAID_DEVICE_STUB)) {
1357 rejig_partition_name_in_raidlist_if_necessary(raidlist,
1358 device_used_to_be,
1359 device_str);
1360 }
1361/* else, moving a RAID to another RAID; bad idea, or so I thought */
1362#ifndef __FreeBSD__ /* It works fine under FBSD. */
1363 else if (strcmp(device_used_to_be, device_str)) {
1364 popup_and_OK
1365 ("You are renaming a RAID device as another RAID device. I don't like it but I'll allow it.");
1366 }
1367#endif
1368 redraw_mountlist(mountlist, keylist, listbox);
1369}
1370
1371
1372#if __FreeBSD__
1373/**
1374 * Add a subdisk to @p raidrec.
1375 * @param raidlist The raidlist containing information about RAID partitions.
1376 * @param raidrec The RAID device record to add the subdisk to.
1377 * @param temp The device name of the RAID disk to add it to.
1378 * @author Joshua Oreman
1379 * @ingroup restoreGuiMountlist
1380 */
1381void
1382add_raid_subdisk(struct raidlist_itself *raidlist,
1383 struct vinum_plex *raidrec, char *temp)
1384{
1385 int i;
1386 bool found = FALSE;
1387
1388 for (i = 0; i < raidlist->disks.entries; ++i) {
1389 if (!strcmp(raidlist->disks.el[i].device, temp)) {
1390 strcpy(raidrec->sd[raidrec->subdisks].which_device,
1391 raidlist->disks.el[i].name);
1392 found = TRUE;
1393 }
1394 }
1395 if (!found) {
1396 sprintf(raidlist->disks.el[raidlist->disks.entries].name,
1397 "drive%i", raidlist->disks.entries);
1398 sprintf(raidrec->sd[raidrec->subdisks].which_device, "drive%i",
1399 raidlist->disks.entries);
1400 strcpy(raidlist->disks.el[raidlist->disks.entries++].device, temp);
1401 }
1402 raidrec->subdisks++;
1403}
1404
1405
1406/**
1407 * Determine the /dev entry for @p vinum_name.
1408 * @param raidlist The raidlist containing information about RAID devices.
1409 * @param vinum_name The name of the Vinum drive to map to a /dev entry.
1410 * @return The /dev entry, or NULL if none was found.
1411 * @note The returned string points to static storage that will be overwritten with each call.
1412 * @author Joshua Oreman
1413 * @ingroup restoreUtilityGroup
1414 */
1415char *find_dev_entry_for_raid_device_name(struct raidlist_itself *raidlist,
1416 char *vinum_name)
1417{
1418 int i;
1419 for (i = 0; i < raidlist->disks.entries; ++i) {
1420 if (!strcmp(raidlist->disks.el[i].name, vinum_name)) {
1421 return raidlist->disks.el[i].device;
1422 }
1423 }
1424 return NULL;
1425}
1426
1427void
1428edit_raidlist_plex(struct mountlist_itself *mountlist,
1429 struct raidlist_itself *raidlist,
1430 struct vinum_plex *raidrec, int currline,
1431 int currline2);
1432
1433#endif
1434
1435
1436/**
1437 * Edit the entry for @p raidrec in @p raidlist.
1438 * @param mountlist The mountlist to get some information from.
1439 * @param raidlist The raidlist containing information about RAID devices.
1440 * @param raidrec The RAID device record for this partition.
1441 * @param currline The line number (starting from 0) in the mountlist of the RAID device.
1442 * @ingroup restoreGuiMountlist
1443 */
1444void
1445edit_raidlist_entry(struct mountlist_itself *mountlist,
1446 struct raidlist_itself *raidlist,
1447 struct OSSWAP (raid_device_record,
1448 vinum_volume) * raidrec, int currline)
1449{
1450
1451#ifdef __FreeBSD__
1452 /** structures ********************************************************/
1453 struct vinum_volume bkp_raidrec;
1454
1455
1456 /** buffers ***********************************************************/
1457 char title_of_editraidForm_window[MAX_STR_LEN];
1458
1459 /** newt **************************************************************/
1460 newtComponent editraidForm;
1461 newtComponent bOK;
1462 newtComponent bCancel;
1463 newtComponent bEdit;
1464 newtComponent bAdd;
1465 newtComponent bDelete;
1466 newtComponent b_res;
1467 newtComponent plexesListbox;
1468 newtComponent plexesHeader;
1469
1470 void *keylist[10];
1471 void *curr_choice;
1472
1473 int currline2 = 0;
1474
1475 log_it("Started edit_raidlist_entry");
1476 memcpy((void *) &bkp_raidrec, (void *) raidrec,
1477 sizeof(struct vinum_volume));
1478 sprintf(title_of_editraidForm_window, "Plexes on %s",
1479 raidrec->volname);
1480 newtPushHelpLine(" Please select a plex to edit");
1481 newtOpenWindow(13, 5, 54, 15, title_of_editraidForm_window);
1482 for (;;) {
1483 int i;
1484 char headerstr[MAX_STR_LEN];
1485 snprintf(headerstr, MAX_STR_LEN, "%-14s %-8s %11s %8s",
1486 "Plex", "Level", "Stripe Size", "Subdisks");
1487
1488 bOK = newtCompactButton(2, 13, " OK ");
1489 bCancel = newtCompactButton(12, 13, "Cancel");
1490 bAdd = newtCompactButton(22, 13, " Add ");
1491 bEdit = newtCompactButton(32, 13, " Edit ");
1492 bDelete = newtCompactButton(42, 13, "Delete");
1493
1494 plexesListbox =
1495 newtListbox(2, 3, 9, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1496 plexesHeader = newtLabel(2, 2, headerstr);
1497 editraidForm = newtForm(NULL, NULL, 0);
1498
1499 newtListboxClear(plexesListbox);
1500 for (i = 0; i < 10; ++i) {
1501 keylist[i] = (void *) i;
1502 if (i < raidrec->plexes) {
1503 char pname[64], entry[MAX_STR_LEN], raidlevel[64],
1504 chunksize[64];
1505 switch (raidrec->plex[i].raidlevel) {
1506 case -1:
1507 strcpy(raidlevel, "concat");
1508 break;
1509 case 0:
1510 strcpy(raidlevel, "striped");
1511 break;
1512 case 5:
1513 strcpy(raidlevel, "raid5");
1514 break;
1515 default:
1516 sprintf(raidlevel, "raid%i",
1517 raidrec->plex[i].raidlevel);
1518 break;
1519 }
1520
1521 if (raidrec->plex[i].raidlevel == -1) {
1522 strcpy(chunksize, "N/A");
1523 } else {
1524 sprintf(chunksize, "%dk", raidrec->plex[i].stripesize);
1525 }
1526 snprintf(pname, 64, "%s.p%i", raidrec->volname, i);
1527 snprintf(entry, MAX_STR_LEN, "%-14s %-8s %11s %8d",
1528 pname, raidlevel, chunksize,
1529 raidrec->plex[i].subdisks);
1530 newtListboxAppendEntry(plexesListbox, entry, keylist[i]);
1531 }
1532 }
1533
1534 newtFormAddComponents(editraidForm, bOK, bCancel, bAdd, bEdit,
1535 bDelete, plexesListbox, plexesHeader, NULL);
1536
1537 b_res = newtRunForm(editraidForm);
1538 if (b_res == bOK || b_res == bCancel) {
1539 break;
1540 }
1541
1542 curr_choice = newtListboxGetCurrent(plexesListbox);
1543 for (currline2 = 0; currline2 < raidrec->plexes; ++currline2) {
1544 if (currline2 > 9)
1545 break;
1546 if (keylist[currline2] == curr_choice)
1547 break;
1548 }
1549
1550 if (b_res == bDelete) {
1551 char msg[MAX_STR_LEN];
1552 sprintf(msg, "Are you sure you want to delete %s.p%i?",
1553 raidrec->volname, currline2);
1554 if (ask_me_yes_or_no(msg)) {
1555 log_it("Deleting RAID plex");
1556 memcpy((void *) &raidrec->plex[currline2],
1557 (void *) &raidrec->plex[raidrec->plexes - 1],
1558 sizeof(struct vinum_plex));
1559 raidrec->plexes--;
1560 }
1561 continue;
1562 }
1563 if (b_res == bAdd) {
1564 raidrec->plex[raidrec->plexes].raidlevel = 0;
1565 raidrec->plex[raidrec->plexes].stripesize = 279;
1566 raidrec->plex[raidrec->plexes].subdisks = 0;
1567 currline2 = raidrec->plexes++;
1568 }
1569 edit_raidlist_plex(mountlist, raidlist, &raidrec->plex[currline2],
1570 currline, currline2);
1571 newtFormDestroy(editraidForm);
1572 }
1573 if (b_res == bCancel) {
1574 memcpy((void *) raidrec, (void *) &bkp_raidrec,
1575 sizeof(struct vinum_volume));
1576 }
1577 newtPopHelpLine();
1578 newtPopWindow();
1579 mountlist->el[currline].size =
1580 calculate_raid_device_size(mountlist, raidlist, raidrec->volname);
1581#else
1582 /** structures ********************************************************/
1583 struct raid_device_record *bkp_raidrec;
1584
1585
1586 /** buffers ***********************************************************/
1587 char *title_of_editraidForm_window;
1588 char *sz_raid_level;
1589 char *sz_data_disks;
1590 char *sz_spare_disks;
1591 char *sz_parity_disks;
1592 char *sz_failed_disks;
1593
1594 /** newt **************************************************************/
1595 newtComponent editraidForm;
1596 newtComponent bOK;
1597 newtComponent bCancel;
1598 newtComponent bAdditional;
1599 newtComponent bChangeRaid;
1600 newtComponent bSelectData;
1601 newtComponent bSelectSpare;
1602 newtComponent bSelectParity;
1603 newtComponent bSelectFailed;
1604 newtComponent b_res;
1605
1606 assert(mountlist != NULL);
1607 assert(raidlist != NULL);
1608 assert(raidrec != NULL);
1609
1610 malloc_string(title_of_editraidForm_window);
1611 malloc_string(sz_raid_level);
1612 malloc_string(sz_data_disks);
1613 malloc_string(sz_spare_disks);
1614 malloc_string(sz_parity_disks);
1615 malloc_string(sz_failed_disks);
1616 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
1617 fatal_error("Cannot malloc space for raidrec");
1618 }
1619
1620 log_it("Started edit_raidlist_entry");
1621
1622 memcpy((void *) bkp_raidrec, (void *) raidrec,
1623 sizeof(struct raid_device_record));
1624 sprintf(title_of_editraidForm_window, "%s", raidrec->raid_device);
1625 log_msg(2, "Opening newt window");
1626 newtOpenWindow(20, 5, 40, 14, title_of_editraidForm_window);
1627 for (;;) {
1628 log_msg(2, "Main loop");
1629 sprintf(title_of_editraidForm_window, "Edit %s",
1630 raidrec->raid_device);
1631 strcpy(sz_raid_level,
1632 turn_raid_level_number_to_string(raidrec->raid_level));
1633 strcpy(sz_data_disks,
1634 number_of_disks_as_string(raidrec->data_disks.entries,
1635 "data"));
1636 strcpy(sz_spare_disks,
1637 number_of_disks_as_string(raidrec->spare_disks.entries,
1638 "spare"));
1639 strcpy(sz_parity_disks,
1640 number_of_disks_as_string(raidrec->parity_disks.entries,
1641 "parity"));
1642 strcpy(sz_failed_disks,
1643 number_of_disks_as_string(raidrec->failed_disks.entries,
1644 "failed"));
1645 bSelectData = newtButton(1, 1, sz_data_disks);
1646 bSelectSpare = newtButton(20, 1, sz_spare_disks);
1647 bSelectParity = newtButton(1, 5, sz_parity_disks);
1648 bSelectFailed = newtButton(20, 5, sz_failed_disks);
1649 bChangeRaid = newtButton(1, 9, sz_raid_level);
1650 bOK = newtButton(16 + (raidrec->raid_level == -1), 9, " OK ");
1651 bCancel = newtButton(28, 9, "Cancel");
1652 bAdditional =
1653 newtCompactButton(1, 13,
1654 "Additional settings and information");
1655 newtPushHelpLine
1656 (" Edit the RAID device's settings to your heart's content, then hit OK/Cancel.");
1657 editraidForm = newtForm(NULL, NULL, 0);
1658 newtFormAddComponents(editraidForm, bSelectData, bSelectParity,
1659 bChangeRaid, bSelectSpare, bSelectFailed,
1660 bOK, bCancel, bAdditional);
1661 b_res = newtRunForm(editraidForm);
1662 if (b_res == bChangeRaid) {
1663 choose_raid_level(raidrec);
1664 } else if (b_res == bSelectData) {
1665 select_raid_disks(mountlist, raidlist, raidrec, "data",
1666 &raidrec->data_disks);
1667 } else if (b_res == bSelectSpare) {
1668 select_raid_disks(mountlist, raidlist, raidrec, "spare",
1669 &raidrec->spare_disks);
1670 } else if (b_res == bSelectParity) {
1671 select_raid_disks(mountlist, raidlist, raidrec, "parity",
1672 &raidrec->parity_disks);
1673 } else if (b_res == bSelectFailed) {
1674 select_raid_disks(mountlist, raidlist, raidrec, "failed",
1675 &raidrec->failed_disks);
1676 } else if (b_res == bAdditional) {
1677 edit_raidrec_additional_vars(raidrec);
1678 }
1679 newtFormDestroy(editraidForm);
1680 if (b_res == bOK || b_res == bCancel) {
1681 break;
1682 }
1683 }
1684 if (b_res == bCancel) {
1685 memcpy((void *) raidrec, (void *) bkp_raidrec,
1686 sizeof(struct raid_device_record));
1687 }
1688 newtPopHelpLine();
1689 newtPopWindow();
1690 mountlist->el[currline].size =
1691 calculate_raid_device_size(mountlist, raidlist,
1692 raidrec->raid_device);
1693 paranoid_free(title_of_editraidForm_window);
1694 paranoid_free(sz_raid_level);
1695 paranoid_free(sz_data_disks);
1696 paranoid_free(sz_spare_disks);
1697 paranoid_free(sz_parity_disks);
1698 paranoid_free(sz_failed_disks);
1699 paranoid_free(bkp_raidrec);
1700#endif
1701}
1702
1703#ifdef __FreeBSD__
1704
1705/**
1706 * Edit the plex @p raidrec in @p raidlist.
1707 * @param mountlist The mountlist to get some of the information from.
1708 * @param raidlist The raidlist containing information about RAID devices.
1709 * @param raidrec The plex to edit.
1710 * @param currline The line number (starting from 0) of the RAID device in @p mountlist.
1711 * @param currline2 The line number (starting from 0) of the plex within the RAID device.
1712 * @author Joshua Oreman
1713 * @ingroup restoreGuiMountlist
1714 */
1715void
1716edit_raidlist_plex(struct mountlist_itself *mountlist,
1717 struct raidlist_itself *raidlist,
1718 struct vinum_plex *raidrec, int currline, int currline2)
1719{
1720
1721 /** structures ********************************************************/
1722 struct vinum_plex bkp_raidrec;
1723
1724
1725 /** buffers ***********************************************************/
1726 char title_of_editraidForm_window[MAX_STR_LEN];
1727
1728 /** newt **************************************************************/
1729 newtComponent editraidForm;
1730 newtComponent bOK;
1731 newtComponent bCancel;
1732 newtComponent bEdit;
1733 newtComponent bAdd;
1734 newtComponent bDelete;
1735 newtComponent b_res;
1736 newtComponent unallocListbox, allocListbox;
1737 newtComponent bLevel, sLevel;
1738 newtComponent bStripeSize, sStripeSize;
1739 newtComponent bAlloc, bUnalloc;
1740
1741 void *keylist[ARBITRARY_MAXIMUM];
1742 void *curr_choice_a, *curr_choice_u;
1743 int currline_a, currline_u;
1744
1745 struct mountlist_itself *unallocparts;
1746
1747 unallocparts = malloc(sizeof(struct mountlist_itself));
1748
1749 log_it("Started edit_raidlist_entry");
1750 memcpy((void *) &bkp_raidrec, (void *) raidrec,
1751 sizeof(struct vinum_plex));
1752 sprintf(title_of_editraidForm_window, "%s.p%i",
1753 raidlist->el[currline].volname, currline2);
1754 newtPushHelpLine
1755 (" Please select a subdisk to edit, or edit this plex's parameters");
1756 newtOpenWindow(13, 3, 54, 18, title_of_editraidForm_window);
1757 for (;;) {
1758 int i;
1759 char headerstr[MAX_STR_LEN];
1760 char tmp[64];
1761 snprintf(headerstr, MAX_STR_LEN, "%-24s %s", "Subdisk", "Device");
1762
1763
1764 switch (raidrec->raidlevel) {
1765 case -1:
1766 strcpy(tmp, "concat");
1767 break;
1768 case 0:
1769 strcpy(tmp, "striped");
1770 break;
1771 case 5:
1772 strcpy(tmp, "raid5");
1773 break;
1774 default:
1775 sprintf(tmp, "unknown (%i)", raidrec->raidlevel);
1776 break;
1777 }
1778 bLevel = newtCompactButton(2, 2, " RAID level ");
1779 sLevel = newtLabel(19, 2, tmp);
1780
1781 if (raidrec->raidlevel >= 0) {
1782 sprintf(tmp, "%ik", raidrec->stripesize);
1783 bStripeSize = newtCompactButton(2, 4, " Stripe size ");
1784 } else {
1785 strcpy(tmp, "N/A");
1786 bStripeSize = newtLabel(2, 4, "Stripe size:");
1787 }
1788 sStripeSize = newtLabel(19, 4, tmp);
1789
1790 bOK = newtCompactButton(2, 16, " OK ");
1791 bCancel = newtCompactButton(12, 16, "Cancel");
1792 bAdd = newtCompactButton(22, 16, " Add ");
1793 bEdit = newtCompactButton(32, 16, " Edit ");
1794 bDelete = newtCompactButton(42, 16, "Delete");
1795
1796
1797 // plexesListbox = newtListbox (2, 7, 9, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1798 // plexesHeader = newtLabel (2, 6, headerstr);
1799 unallocListbox =
1800 newtListbox(2, 7, 7, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1801 allocListbox =
1802 newtListbox(33, 7, 7, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1803 bAlloc = newtButton(23, 7, " -> ");
1804 bUnalloc = newtButton(23, 11, " <- ");
1805
1806 editraidForm = newtForm(NULL, NULL, 0);
1807
1808 newtListboxClear(allocListbox);
1809 newtListboxClear(unallocListbox);
1810 bzero(unallocparts, sizeof(struct mountlist_itself));
1811 make_list_of_unallocated_raid_partitions(unallocparts, mountlist,
1812 raidlist);
1813 for (i = 0; i < ARBITRARY_MAXIMUM; ++i) {
1814 char entry[MAX_STR_LEN];
1815 keylist[i] = (void *) i;
1816 if (i < raidrec->subdisks) {
1817 snprintf(entry, MAX_STR_LEN, "%-17s",
1818 find_dev_entry_for_raid_device_name(raidlist,
1819 raidrec->
1820 sd[i].
1821 which_device));
1822 newtListboxAppendEntry(allocListbox, entry, keylist[i]);
1823 }
1824 if (i < unallocparts->entries) {
1825 snprintf(entry, MAX_STR_LEN, "%-17s",
1826 unallocparts->el[i].device);
1827 newtListboxAppendEntry(unallocListbox, entry, keylist[i]);
1828 }
1829 }
1830
1831#define COMP(x) newtFormAddComponent (editraidForm, x)
1832#define UCOMP(x) if (unallocparts->entries > 0) COMP(x)
1833#define ACOMP(x) if (raidrec->subdisks > 0) COMP(x)
1834 editraidForm = newtForm(NULL, NULL, 0);
1835 UCOMP(unallocListbox);
1836 UCOMP(bAlloc);
1837 ACOMP(allocListbox);
1838 ACOMP(bUnalloc);
1839 COMP(bOK);
1840 COMP(bCancel);
1841 COMP(bLevel);
1842 COMP(sLevel);
1843 if (raidrec->raidlevel != -1) {
1844 COMP(bStripeSize);
1845 COMP(sStripeSize);
1846 }
1847#undef COMP
1848#undef UCOMP
1849#undef ACOMP
1850
1851 newtRefresh();
1852 b_res = newtRunForm(editraidForm);
1853 if (b_res == bOK || b_res == bCancel) {
1854 break;
1855 }
1856
1857 curr_choice_a = (raidrec->subdisks > 0) ?
1858 newtListboxGetCurrent(allocListbox) : (void *) 1234;
1859 curr_choice_u = (unallocparts->entries > 0) ?
1860 newtListboxGetCurrent(unallocListbox) : (void *) 1234;
1861 for (currline_a = 0; currline_a < raidrec->subdisks; ++currline_a) {
1862 if (currline_a > ARBITRARY_MAXIMUM)
1863 break;
1864 if (keylist[currline_a] == curr_choice_a)
1865 break;
1866 }
1867 for (currline_u = 0; currline_u < unallocparts->entries;
1868 ++currline_u) {
1869 if (currline_u > ARBITRARY_MAXIMUM)
1870 break;
1871 if (keylist[currline_u] == curr_choice_u)
1872 break;
1873 }
1874 if (b_res == bLevel) {
1875 choose_raid_level(raidrec);
1876 } else if (b_res == bStripeSize) {
1877 char tmp[64];
1878 sprintf(tmp, "%i", raidrec->stripesize);
1879 if (popup_and_get_string
1880 ("Stripe size",
1881 "Please enter the stripe size in kilobytes.", tmp, 20)) {
1882 raidrec->stripesize = atoi(tmp);
1883 }
1884 } else if ((b_res == bAlloc) || (b_res == unallocListbox)) {
1885 if (currline_u <= unallocparts->entries)
1886 add_raid_subdisk(raidlist, raidrec,
1887 unallocparts->el[currline_u].device);
1888 } else if ((b_res == bUnalloc) || (b_res == allocListbox)) {
1889 if (currline_a <= raidrec->subdisks) {
1890 memcpy((void *) &raidrec->sd[currline_a],
1891 (void *) &raidrec->sd[raidrec->subdisks - 1],
1892 sizeof(struct vinum_subdisk));
1893 raidrec->subdisks--;
1894 }
1895 }
1896#if 0
1897 } else {
1898 edit_raid_subdisk(raidlist, raidrec, &raidrec->sd[currline3],
1899 currline3);
1900 }
1901#endif
1902 newtFormDestroy(editraidForm);
1903 newtRefresh();
1904}
1905
1906if (b_res == bCancel) {
1907 memcpy((void *) raidrec, (void *) &bkp_raidrec,
1908 sizeof(struct vinum_plex));
1909}
1910newtPopWindow();
1911newtPopHelpLine();
1912}
1913#else
1914/**
1915 * Edit additional RAID variable number @p lino.
1916 * @param raidrec The RAID device record to edit the variable in.
1917 * @param lino The line number (starting from 0) of the variable to edit.
1918 * @ingroup restoreGuiVarslist
1919 */
1920void edit_varslist_entry(struct raid_device_record *raidrec, int lino)
1921{
1922
1923 /** buffers ***********************************************************/
1924 char header[MAX_STR_LEN];
1925 char comment[MAX_STR_LEN];
1926 char sz_out[MAX_STR_LEN];
1927
1928 assert(raidrec != 0);
1929 assert(lino >= 0);
1930
1931 strcpy(sz_out, raidrec->additional_vars.el[lino].value);
1932 sprintf(header, "Edit %s", raidrec->additional_vars.el[lino].label);
1933 sprintf(comment, "Please set %s's value (currently '%s')",
1934 raidrec->additional_vars.el[lino].label, sz_out);
1935 if (popup_and_get_string(header, comment, sz_out, MAX_STR_LEN)) {
1936 strip_spaces(sz_out);
1937 strcpy(raidrec->additional_vars.el[lino].value, sz_out);
1938 }
1939}
1940
1941
1942/* I'm not racist against white people. I just don't like people who think Liberia is near Spain. - Hugo, 09/01/2001 */
1943
1944#endif
1945
1946/**
1947 * Edit the mountlist using Newt.
1948 * @param mountlist The mountlist to edit.
1949 * @param raidlist The raidlist that goes with @p mountlist.
1950 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
1951 */
1952int
1953edit_mountlist_in_newt(char *mountlist_fname,
1954 struct mountlist_itself *mountlist,
1955 struct raidlist_itself *raidlist)
1956{
1957
1958 /** newt **************************************************************/
1959 newtComponent myForm;
1960 newtComponent bAdd;
1961 newtComponent bEdit;
1962 newtComponent bDelete;
1963 newtComponent bOK;
1964 newtComponent bCancel;
1965 newtComponent b_res = NULL;
1966 newtComponent partitionsListbox;
1967 newtComponent headerMsg;
1968 newtComponent flawsLabelA;
1969 newtComponent flawsLabelB;
1970 newtComponent flawsLabelC;
1971 newtComponent bReload;
1972
1973 /** ???? *************************************************************/
1974 void *curr_choice;
1975 void *keylist[ARBITRARY_MAXIMUM];
1976
1977 /** int **************************************************************/
1978 int i = 0;
1979 int currline = 0;
1980 int finished = FALSE;
1981
1982 /** buffers **********************************************************/
1983 char tmp[MAX_STR_LEN];
1984 char flaws_str_A[MAX_STR_LEN];
1985 char flaws_str_B[MAX_STR_LEN];
1986 char flaws_str_C[MAX_STR_LEN];
1987
1988 assert(mountlist != NULL);
1989 assert(raidlist != NULL);
1990
1991 strcpy(flaws_str_A, "xxxxxxxxx");
1992 strcpy(flaws_str_B, "xxxxxxxxx");
1993 strcpy(flaws_str_C, "xxxxxxxxx");
1994 if (mountlist->entries > ARBITRARY_MAXIMUM) {
1995 log_to_screen("Arbitrary limits suck, man!");
1996 finish(1);
1997 }
1998 newtPushHelpLine
1999 (" Please edit the mountlist to your satisfaction, then click OK or Cancel.");
2000 i = 4;
2001 bAdd = newtCompactButton(i, 17, " Add ");
2002 bEdit = newtCompactButton(i += 11, 17, " Edit ");
2003 bDelete = newtCompactButton(i += 12, 17, "Delete");
2004 bReload = newtCompactButton(i += 12, 17, "Reload");
2005 bCancel = newtCompactButton(i += 12, 17, "Cancel");
2006 bOK = newtCompactButton(i += 12, 17, " OK ");
2007 sprintf(tmp, "%-24s %-24s %-8s %s", "Device", "Mountpoint", "Format",
2008 "Size (MB)");
2009 headerMsg = newtLabel(2, 1, tmp);
2010 flawsLabelA = newtLabel(2, 13, flaws_str_A);
2011 flawsLabelB = newtLabel(2, 14, flaws_str_B);
2012 flawsLabelC = newtLabel(2, 15, flaws_str_C);
2013 partitionsListbox =
2014 newtListbox(2, 2, 10, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2015 redraw_mountlist(mountlist, keylist, partitionsListbox);
2016 newtOpenWindow(1, 3, 77, 18, "Editing mountlist");
2017 myForm = newtForm(NULL, NULL, 0);
2018 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2019 flawsLabelA, flawsLabelB, flawsLabelC, bAdd,
2020 bEdit, bDelete, bReload, bCancel, bOK, NULL);
2021 while (!finished) {
2022 evaluate_mountlist(mountlist, flaws_str_A, flaws_str_B,
2023 flaws_str_C);
2024 newtLabelSetText(flawsLabelA, flaws_str_A);
2025 newtLabelSetText(flawsLabelB, flaws_str_B);
2026 newtLabelSetText(flawsLabelC, flaws_str_C);
2027 b_res = newtRunForm(myForm);
2028 if (b_res == bOK) {
2029 if (!evaluate_mountlist
2030 (mountlist, flaws_str_A, flaws_str_B, flaws_str_C)) {
2031 finished =
2032 ask_me_yes_or_no
2033 ("Your mountlist might not work. Continue anyway?");
2034 } else {
2035 finished =
2036 ask_me_yes_or_no
2037 ("Are you sure you want to save your mountlist and continue? (No changes will be made to your partition table at this time.)");
2038 }
2039 } else if (b_res == bCancel) {
2040 finished = TRUE;
2041 } else if (b_res == bReload) {
2042 if (ask_me_yes_or_no("Reload original mountlist?")) {
2043/*
2044This would be really dumb. RAIDTAB_FNAME is #define'd. --- Hugo, 2003/04/24
2045 if (!RAIDTAB_FNAME[0])
2046 {
2047 strcpy(RAIDTAB_FNAME, "/etc/raidtab");
2048 log_it("Warning - raidtab_fname is blank. Assuming %s", g_raidtab_fname);
2049 }
2050*/
2051 load_mountlist(mountlist, mountlist_fname);
2052 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2053 redraw_mountlist(mountlist, keylist, partitionsListbox);
2054 }
2055 } else {
2056 curr_choice = newtListboxGetCurrent(partitionsListbox);
2057 for (i = 0;
2058 i < mountlist->entries && keylist[i] != curr_choice; i++);
2059 if (i == mountlist->entries && mountlist->entries > 0) {
2060 log_to_screen("I don't know what that button does!");
2061 } else {
2062 currline = i;
2063 if (b_res == bAdd) {
2064 add_mountlist_entry(mountlist, raidlist,
2065 partitionsListbox, currline,
2066 keylist);
2067 } else if (b_res == bDelete) {
2068 delete_mountlist_entry(mountlist, raidlist,
2069 partitionsListbox, currline,
2070 keylist);
2071 } else {
2072 if (mountlist->entries > 0) {
2073 edit_mountlist_entry(mountlist, raidlist,
2074 partitionsListbox, currline,
2075 keylist);
2076 } else {
2077 popup_and_OK
2078 ("Please add an entry. Then press ENTER to edit it.");
2079 }
2080 }
2081 }
2082 }
2083 }
2084 newtFormDestroy(myForm);
2085 newtPopWindow();
2086 newtPopHelpLine();
2087 if (b_res == bOK) {
2088 log_it("You pushed 'OK'. I shall now continue.");
2089 return (0);
2090 } else {
2091 /* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
2092 return (1);
2093 }
2094}
2095
2096
2097
2098/**
2099 * Edit the mountlist.
2100 * @param mountlist The mountlist to edit.
2101 * @param raidlist The raidlist that goes with @p mountlist.
2102 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
2103 */
2104int
2105edit_mountlist(char *mountlist_fname, struct mountlist_itself *mountlist,
2106 struct raidlist_itself *raidlist)
2107{
2108 int res = 0;
2109// char tmp[MAX_STR_LEN];
2110
2111 iamhere("entering eml");
2112
2113 if (g_text_mode) {
2114 fatal_error("Don't call edit_mountlist() in text mode");
2115 } else {
2116 log_it
2117 ("I'm in GUI mode, so I shall edit mountlist using edit_mountlist()");
2118 res = edit_mountlist_in_newt(mountlist_fname, mountlist, raidlist);
2119 }
2120 iamhere("leaving eml");
2121 return (res);
2122}
2123
2124
2125
2126
2127#ifndef __FreeBSD__
2128/**
2129 * Edit the additional RAID variables in @p raidrec.
2130 * @param raidrec The RAID device record to edit the RAID variables in.
2131 * @ingroup restoreGuiVarslist
2132 */
2133void edit_raidrec_additional_vars(struct raid_device_record *raidrec)
2134{
2135
2136 /** structure *********************************************************/
2137 struct raid_device_record bkp_raidrec;
2138
2139 /** newt **************************************************************/
2140 newtComponent myForm;
2141 newtComponent bAdd;
2142 newtComponent bEdit;
2143 newtComponent bDelete;
2144 newtComponent bOK;
2145 newtComponent bCancel;
2146 newtComponent b_res;
2147 newtComponent varsListbox;
2148 newtComponent headerMsg;
2149
2150 /** ?? ***************************************************************/
2151 void *keylist[ARBITRARY_MAXIMUM], *curr_choice;
2152
2153 /** buffers **********************************************************/
2154 char title_of_window[MAX_STR_LEN];
2155
2156 /** int **************************************************************/
2157 int i = 0;
2158 int currline = 0;
2159
2160
2161 assert(raidrec != NULL);
2162
2163 memcpy((void *) &bkp_raidrec, (void *) raidrec,
2164 sizeof(struct raid_device_record));
2165 sprintf(title_of_window, "Additional variables");
2166 newtPushHelpLine
2167 (" Edit the additional fields to your heart's content, then click OK or Cancel.");
2168 headerMsg = newtLabel(1, 1, "Label Value");
2169 varsListbox =
2170 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2171 i = 1;
2172 bAdd = newtCompactButton(i, 9, " Add ");
2173 bEdit = newtCompactButton(i += 8, 9, " Edit ");
2174 bDelete = newtCompactButton(i += 9, 9, "Delete");
2175 bOK = newtCompactButton(i += 9, 9, " OK ");
2176 bCancel = newtCompactButton(i += 9, 9, "Cancel");
2177 newtOpenWindow(17, 7, 46, 10, title_of_window);
2178 myForm = newtForm(NULL, NULL, 0);
2179 newtFormAddComponents(myForm, headerMsg, varsListbox, bAdd, bEdit,
2180 bDelete, bOK, bCancel, NULL);
2181 insert_essential_additionalvars(raidrec);
2182 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2183 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
2184 b_res = newtRunForm(myForm);
2185 curr_choice = newtListboxGetCurrent(varsListbox);
2186 for (currline = 0;
2187 currline < raidrec->additional_vars.entries
2188 && keylist[currline] != curr_choice; currline++);
2189 if (currline == raidrec->additional_vars.entries
2190 && raidrec->additional_vars.entries > 0) {
2191 log_it("Warning - I don't know what this button does");
2192 }
2193 if (b_res == bOK) { /* do nothing */
2194 } else if (b_res == bCancel) { /* do nothing */
2195 } else if (b_res == bAdd) {
2196 add_varslist_entry(raidrec);
2197 } else if (b_res == bDelete) {
2198 delete_varslist_entry(raidrec, currline);
2199 } else {
2200 edit_varslist_entry(raidrec, currline);
2201 }
2202 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2203 }
2204 remove_essential_additionalvars(raidrec);
2205 newtFormDestroy(myForm);
2206 newtPopWindow();
2207 newtPopHelpLine();
2208 if (b_res == bCancel) {
2209 memcpy((void *) raidrec, (void *) &bkp_raidrec,
2210 sizeof(struct raid_device_record));
2211 }
2212 return;
2213}
2214#endif
2215
2216
2217/**
2218 * Find the next free location to place a disk in @p disklist.
2219 * @param disklist The disklist to operate on.
2220 * @return The next free location (starting from 0).
2221 * @ingroup restoreGuiDisklist
2222 */
2223int find_next_free_index_in_disklist(struct list_of_disks *disklist)
2224{
2225
2226 /** int ***************************************************************/
2227 int index = -1;
2228 int pos = 0;
2229
2230 /** bool **************************************************************/
2231 bool done;
2232
2233 assert(disklist != NULL);
2234
2235 for (done = FALSE; !done;) {
2236 for (pos = 0;
2237 pos < disklist->entries && disklist->el[pos].index <= index;
2238 pos++);
2239 if (pos >= disklist->entries) {
2240 done = TRUE;
2241 } else {
2242 index = disklist->el[pos].index;
2243 }
2244 }
2245 return (index + 1);
2246}
2247
2248
2249
2250/**
2251 * Locate @p device in @p raidlist.
2252 * @param raidlist The raidlist ot search in.
2253 * @param device The RAID device to search for.
2254 * @return The index of the device, or -1 if it could not be found.
2255 * @ingroup restoreGuiMountlist
2256 */
2257int
2258find_raid_device_in_raidlist(struct raidlist_itself *raidlist,
2259 char *device)
2260{
2261
2262 /** int ***************************************************************/
2263 int i = 0;
2264#ifdef __FreeBSD__
2265 char vdev[64];
2266#else
2267// Linux
2268#endif
2269
2270 assert(raidlist != NULL);
2271 assert_string_is_neither_NULL_nor_zerolength(device);
2272
2273#ifdef __FreeBSD__
2274 for (i = 0; i < raidlist->entries; i++) {
2275 sprintf(vdev, "/dev/vinum/%s", raidlist->el[i].volname);
2276 if (!strcmp(device, vdev))
2277 break;
2278 }
2279#else
2280
2281 for (i = 0; strcmp(raidlist->el[i].raid_device, device)
2282 && i < raidlist->entries; i++);
2283#endif
2284 if (i == raidlist->entries) {
2285 return (-1);
2286 } else {
2287 return (i);
2288 }
2289}
2290
2291
2292/**
2293 * Get information about the location of ISO images from the user.
2294 * @param isodir_device Where to put the device (e.g. /dev/hda4) the user enters.
2295 * @param isodir_format Where to put the format (e.g. ext2) the user enters.
2296 * @param isodir_path Where to put the path (e.g. /root/images/mondo) the user enters.
2297 * @param nuke_me_please Whether we're planning on nuking or not.
2298 * @return TRUE if OK was pressed, FALSE otherwise.
2299 */
2300bool
2301get_isodir_info(char *isodir_device, char *isodir_format,
2302 char *isodir_path, bool nuke_me_please)
2303{
2304
2305 /** initialize ********************************************************/
2306
2307 assert(isodir_device != NULL);
2308 assert(isodir_format != NULL);
2309 assert(isodir_path != NULL);
2310
2311 log_it("%d - AAA - isodir_path = %s", isodir_path);
2312 isodir_format[0] = '\0';
2313 if (isodir_device[0] == '\0') {
2314 strcpy(isodir_device, "/dev/");
2315 }
2316 if (isodir_path[0] == '\0') {
2317 strcpy(isodir_path, "/");
2318 }
2319 if (does_file_exist("/tmp/NFS-SERVER-PATH")) {
2320 strcpy(isodir_device, last_line_of_file("/tmp/NFS-SERVER-MOUNT"));
2321 strcpy(isodir_format, "nfs");
2322 strcpy(isodir_path, last_line_of_file("/tmp/NFS-SERVER-PATH"));
2323 }
2324 if (nuke_me_please) {
2325 return (TRUE);
2326 }
2327
2328 if (popup_and_get_string
2329 ("ISO Mode - device", "On what device do the ISO files live?",
2330 isodir_device, MAX_STR_LEN / 4)) {
2331 if (popup_and_get_string
2332 ("ISO Mode - format",
2333 "What is the disk format of the device? (Hit ENTER if you don't know.)",
2334 isodir_format, 16)) {
2335 if (popup_and_get_string
2336 ("ISO Mode - path",
2337 "At what path on this device can the ISO files be found?",
2338 isodir_path, MAX_STR_LEN / 4)) {
2339 strip_spaces(isodir_device);
2340 strip_spaces(isodir_format);
2341 strip_spaces(isodir_path);
2342 log_it("%d - BBB - isodir_path = %s", isodir_path);
2343 return (TRUE);
2344 }
2345 }
2346 }
2347 return (FALSE);
2348}
2349
2350
2351/**
2352 * Create a new raidtab entry for @p device in @p raidlist.
2353 * @param raidlist The raidlist to add the device to.
2354 * @param mountlist The mountlist containing information about the user's partitions.
2355 * @param currline The selected line in the mountlist.
2356 * @param device The RAID device (e.g. /dev/md0) to use.
2357 * @ingroup restoreGuiMountlist
2358 */
2359void
2360initiate_new_raidlist_entry(struct raidlist_itself *raidlist,
2361 struct mountlist_itself *mountlist,
2362 int currline, char *device)
2363{
2364
2365 /** structure *********************************************************/
2366 struct OSSWAP (raid_device_record, vinum_volume) * raidrec;
2367
2368 /** int ***************************************************************/
2369 int pos_in_raidlist = 0;
2370
2371 assert(raidlist != NULL);
2372 assert(mountlist != NULL);
2373 assert_string_is_neither_NULL_nor_zerolength(device);
2374
2375 pos_in_raidlist =
2376 find_raid_device_in_raidlist(raidlist,
2377 mountlist->el[currline].device);
2378 if (pos_in_raidlist >= 0) {
2379 fatal_error("Sorry, that RAID device already exists. Weird.");
2380 }
2381 pos_in_raidlist = raidlist->entries++;
2382 raidrec = &raidlist->el[pos_in_raidlist];
2383 initialize_raidrec(raidrec);
2384 strcpy(raidrec->OSSWAP(raid_device, volname),
2385 OSSWAP(device, basename(device)));
2386#ifndef __FreeBSD__
2387 choose_raid_level(raidrec);
2388 select_raid_disks(mountlist, raidlist, raidrec, "data",
2389 &raidrec->data_disks);
2390#endif
2391 edit_raidlist_entry(mountlist, raidlist, raidrec, currline);
2392}
2393
2394
2395#ifndef __FreeBSD__
2396/**
2397 * Insert the RAID variables not stored in the "additional RAID variables" list there too.
2398 * @param raidrec The RAID device record to operate on.
2399 * @ingroup restoreGuiVarslist
2400 */
2401void insert_essential_additionalvars(struct raid_device_record *raidrec)
2402{
2403
2404 /** int **************************************************************/
2405 int items = 0;
2406
2407 assert(raidrec != NULL);
2408
2409 items = raidrec->additional_vars.entries;
2410 write_variableINT_to_raid_var_line(raidrec, items++,
2411 "persistent-superblock",
2412 raidrec->persistent_superblock);
2413 write_variableINT_to_raid_var_line(raidrec, items++, "chunk-size",
2414 raidrec->chunk_size);
2415 raidrec->additional_vars.entries = items;
2416}
2417
2418#endif
2419
2420/**
2421 * Dummy function that proves that we can get to the point where Mondo is run.
2422 */
2423void nuke_mode_dummy()
2424{
2425
2426 /** newt *************************************************************/
2427 newtComponent myForm;
2428 newtComponent b1;
2429 newtComponent b2;
2430 newtComponent b3;
2431 newtComponent b_res;
2432
2433
2434 newtPushHelpLine
2435 ("This is where I nuke your hard drives. Mhahahahaha. No-one can stop Mojo Jojo!");
2436 newtOpenWindow(24, 3, 32, 13, "Nuking");
2437 b1 = newtButton(7, 1, "Slowly");
2438 b2 = newtButton(7, 5, "Medium");
2439 b3 = newtButton(7, 9, "Quickly");
2440 myForm = newtForm(NULL, NULL, 0);
2441 newtFormAddComponents(myForm, b1, b2, b3, NULL);
2442 b_res = newtRunForm(myForm);
2443 newtFormDestroy(myForm);
2444 newtPopWindow();
2445 newtPopHelpLine();
2446}
2447
2448
2449
2450/**
2451 * Redraw the disklist.
2452 * @param disklist The disklist to read from.
2453 * @param keylist The list of keys for @p listbox.
2454 * @param listbox The Newt listbox component to redraw.
2455 * @ingroup restoreGuiDisklist
2456 */
2457void
2458redraw_disklist(struct list_of_disks *disklist,
2459 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2460{
2461
2462 /** int *************************************************************/
2463 int i = 0;
2464
2465 assert(disklist != NULL);
2466 assert(keylist != NULL);
2467 assert(listbox != NULL);
2468
2469 newtListboxClear(listbox);
2470
2471 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2472 keylist[i] = (void *) i;
2473 }
2474 for (i = 0; i < disklist->entries; i++) {
2475 newtListboxAppendEntry(listbox,
2476 disklist_entry_to_string(disklist, i),
2477 keylist[i]);
2478 }
2479}
2480
2481
2482/**
2483 * Redraw the mountlist.
2484 * @param mountlist The mountlist to read from.
2485 * @param keylist The list of keys for @p listbox.
2486 * @param listbox The Newt listbox component to redraw.
2487 * @ingroup restoreGuiMountlist
2488 */
2489void
2490redraw_mountlist(struct mountlist_itself *mountlist,
2491 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2492{
2493
2494 /** int **************************************************************/
2495 int i = 0;
2496
2497 assert(mountlist != NULL);
2498 assert(keylist != NULL);
2499 assert(listbox != NULL);
2500
2501 newtListboxClear(listbox);
2502// sort_mountlist_by_device (mountlist);
2503 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2504 keylist[i] = (void *) i;
2505 }
2506 for (i = 0; i < mountlist->entries; i++) {
2507 newtListboxAppendEntry(listbox,
2508 mountlist_entry_to_string(mountlist, i),
2509 keylist[i]);
2510 }
2511}
2512
2513
2514
2515
2516/**
2517 * Redraw the list of unallocated RAID partitions.
2518 * @param unallocated_raid_partitions The mountlist containing unallocated RAID partitions.
2519 * @param keylist The list of keys for @p listbox.
2520 * @param listbox The Newt listbox component to redraw.
2521 * @ingroup restoreGuiDisklist
2522 */
2523void redraw_unallocpartnslist(struct mountlist_itself
2524 *unallocated_raid_partitions,
2525 void *keylist[ARBITRARY_MAXIMUM],
2526 newtComponent listbox)
2527{
2528
2529 /** int **************************************************************/
2530 int i = 0;
2531
2532 /** buffers **********************************************************/
2533 char tmp[MAX_STR_LEN];
2534
2535 assert(unallocated_raid_partitions != NULL);
2536 assert(keylist != NULL);
2537 assert(listbox != NULL);
2538
2539 newtListboxClear(listbox);
2540 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2541 keylist[i] = (void *) i;
2542 }
2543 for (i = 0; i < unallocated_raid_partitions->entries; i++) {
2544 sprintf(tmp, "%-22s %8lld",
2545 unallocated_raid_partitions->el[i].device,
2546 unallocated_raid_partitions->el[i].size / 1024);
2547 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2548 }
2549}
2550
2551#ifndef __FreeBSD__
2552/**
2553 * Redraw the list of additional RAID variables.
2554 * @param additional_vars The list of additional RAID varibals.
2555 * @param keylist The list of keys for @p listbox.
2556 * @param listbox The Newt listbox component to redraw.
2557 * @ingroup restoreGuiVarslist
2558 */
2559void
2560redraw_varslist(struct additional_raid_variables *additional_vars,
2561 void *keylist[], newtComponent listbox)
2562{
2563 /** int *************************************************************/
2564 int i = 0;
2565
2566 /** buffers *********************************************************/
2567 char tmp[MAX_STR_LEN];
2568
2569 assert(additional_vars != NULL);
2570 assert(keylist != NULL);
2571 assert(listbox != NULL);
2572
2573 newtListboxClear(listbox);
2574
2575 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2576 keylist[i] = (void *) i;
2577 }
2578 for (i = 0; i < additional_vars->entries; i++) {
2579 sprintf(tmp, "%-32s %-8s", additional_vars->el[i].label,
2580 additional_vars->el[i].value);
2581 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2582 }
2583}
2584
2585
2586/**
2587 * Remove variable @p label from the RAID variables list in @p raidrec.
2588 * @param raidrec The RAID device record to remove the variable from.
2589 * @param label The variable name to remove.
2590 * @return The value of the variable removed.
2591 * @ingroup restoreUtilityGroup
2592 */
2593int read_variableINT_and_remove_from_raidvars(struct
2594 OSSWAP (raid_device_record,
2595 vinum_volume) *
2596 raidrec, char *label)
2597{
2598 /** int ***************************************************************/
2599 int i = 0;
2600 int res = 0;
2601
2602
2603 assert(raidrec != NULL);
2604 assert(label != NULL);
2605
2606 for (i = 0;
2607 i < raidrec->additional_vars.entries
2608 && strcmp(raidrec->additional_vars.el[i].label, label); i++);
2609 if (i == raidrec->additional_vars.entries) {
2610 res = -1;
2611 } else {
2612 res = atoi(raidrec->additional_vars.el[i].value);
2613 for (i++; i < raidrec->additional_vars.entries; i++) {
2614 memcpy((void *) &raidrec->additional_vars.el[i - 1],
2615 (void *) &raidrec->additional_vars.el[i],
2616 sizeof(struct raid_var_line));
2617 }
2618 raidrec->additional_vars.entries--;
2619 }
2620 return (res);
2621}
2622#endif
2623
2624/**
2625 * Change all RAID devices to use @p new_dev instead of @p old_dev.
2626 * @param raidlist The raidlist to make the changes in.
2627 * @param old_dev The old name of the device (what it used to be).
2628 * @param new_dev The new name of the device (what it is now).
2629 * @ingroup restoreGuiMountlist
2630 */
2631void rejig_partition_name_in_raidlist_if_necessary(struct raidlist_itself
2632 *raidlist,
2633 char *old_dev,
2634 char *new_dev)
2635{
2636 /** buffers ********************************************************/
2637 char tmp[MAX_STR_LEN];
2638
2639 /** int ************************************************************/
2640 int pos = 0;
2641 int j = 0;
2642
2643 assert(raidlist != NULL);
2644 assert_string_is_neither_NULL_nor_zerolength(old_dev);
2645 assert_string_is_neither_NULL_nor_zerolength(new_dev);
2646
2647 pos = which_raid_device_is_using_this_partition(raidlist, old_dev);
2648 if (pos < 0) {
2649 sprintf(tmp, "No need to rejig %s in raidlist: it's not listed.",
2650 old_dev);
2651 log_it(tmp);
2652 } else {
2653 if ((j =
2654 where_in_drivelist_is_drive(&raidlist->
2655 OSSWAP(el[pos].data_disks, disks),
2656 old_dev)) >= 0) {
2657 strcpy(raidlist->OSSWAP(el[pos].data_disks, disks).el[j].
2658 device, new_dev);
2659 } else
2660 if ((j =
2661 where_in_drivelist_is_drive(&raidlist->
2662 OSSWAP(el[pos].spare_disks,
2663 spares),
2664 old_dev)) >= 0) {
2665 strcpy(raidlist->OSSWAP(el[pos].spare_disks, spares).el[j].
2666 device, new_dev);
2667 }
2668#ifndef __FreeBSD__
2669 else if ((j =
2670 where_in_drivelist_is_drive(&raidlist->el[pos].
2671 parity_disks,
2672 old_dev)) >= 0) {
2673 strcpy(raidlist->el[pos].parity_disks.el[j].device, new_dev);
2674 } else
2675 if ((j =
2676 where_in_drivelist_is_drive(&raidlist->el[pos].
2677 failed_disks,
2678 old_dev)) >= 0) {
2679 strcpy(raidlist->el[pos].failed_disks.el[j].device, new_dev);
2680 }
2681#endif
2682 else {
2683 sprintf(tmp,
2684 "%s is supposed to be listed in this raid dev but it's not...",
2685 old_dev);
2686 log_it(tmp);
2687 }
2688 }
2689}
2690
2691
2692#ifndef __FreeBSD__
2693/**
2694 * Remove the essential RAID variables from the "additional variables" section.
2695 * If they have been changed, set them in their normal locations too.
2696 * @param raidrec The RAID device record to operate on.
2697 * @ingroup restoreUtilityVarslist
2698 */
2699void remove_essential_additionalvars(struct raid_device_record *raidrec)
2700{
2701
2702 /** int **************************************************************/
2703 int res = 0;
2704
2705 assert(raidrec != NULL);
2706
2707 res =
2708 read_variableINT_and_remove_from_raidvars(raidrec,
2709 "persistent-superblock");
2710 if (res > 0) {
2711 raidrec->persistent_superblock = res;
2712 }
2713 res = read_variableINT_and_remove_from_raidvars(raidrec, "chunk-size");
2714 if (res > 0) {
2715 raidrec->chunk_size = res;
2716 }
2717 res = read_variableINT_and_remove_from_raidvars(raidrec, "block-size");
2718}
2719
2720/**
2721 * Select the RAID disks to use in @p raidrec.
2722 * @param mountlist_dontedit The mountlist (will not be edited).
2723 * @param raidlist The raidlist to modify.
2724 * @param raidrec The RAID device record in @p raidlist to work on.
2725 * @param description_of_list The type of disks we're selecting (e.g. "data").
2726 * @param disklist The disklist to put the user-selected disks in.
2727 * @ingroup restoreGuiMountlist
2728 */
2729void
2730select_raid_disks(struct mountlist_itself *mountlist_dontedit,
2731 struct raidlist_itself *raidlist,
2732 struct raid_device_record *raidrec,
2733 char *description_of_list,
2734 struct list_of_disks *disklist)
2735{
2736 void *curr_choice;
2737
2738 /** ??? ***************************************************************/
2739
2740 /** structures ********************************************************/
2741 struct raidlist_itself *bkp_raidlist;
2742 struct raid_device_record *bkp_raidrec;
2743 struct list_of_disks *bkp_disklist;
2744 struct mountlist_itself *unallocated_raid_partitions;
2745
2746 /** newt **************************************************************/
2747 newtComponent myForm = NULL;
2748 newtComponent bAdd = NULL;
2749 newtComponent bDelete = NULL;
2750 newtComponent bOK = NULL;
2751 newtComponent bCancel = NULL;
2752 newtComponent b_res = NULL;
2753 newtComponent partitionsListbox = NULL;
2754 newtComponent headerMsg = NULL;
2755
2756 /** buffers **********************************************************/
2757 void *keylist[ARBITRARY_MAXIMUM];
2758 char *tmp;
2759 char *help_text;
2760 char *title_of_window;
2761 char *sz_res;
2762 char *header_text;
2763
2764 /** int **************************************************************/
2765 int i = 0;
2766 int currline = 0;
2767
2768 assert(mountlist_dontedit != NULL);
2769 assert(raidlist != NULL);
2770 assert(raidrec != NULL);
2771 assert(description_of_list != NULL);
2772 assert(disklist != NULL);
2773
2774 iamhere("malloc'ing");
2775 malloc_string(tmp);
2776 malloc_string(help_text);
2777 malloc_string(title_of_window);
2778 malloc_string(sz_res);
2779 malloc_string(header_text);
2780 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
2781 fatal_error("Cannot malloc space for raidrec");
2782 }
2783 if (!(bkp_disklist = malloc(sizeof(struct list_of_disks)))) {
2784 fatal_error("Cannot malloc space for disklist");
2785 }
2786 if (!(bkp_raidlist = malloc(sizeof(struct raidlist_itself)))) {
2787 fatal_error("Cannot malloc space for raidlist");
2788 }
2789 if (!
2790 (unallocated_raid_partitions =
2791 malloc(sizeof(struct mountlist_itself)))) {
2792 fatal_error("Cannot malloc space for unallocated_raid_partitions");
2793 }
2794
2795 memcpy((void *) bkp_raidlist, (void *) raidlist,
2796 sizeof(struct raidlist_itself));
2797 memcpy((void *) bkp_raidrec, (void *) raidrec,
2798 sizeof(struct raid_device_record));
2799 memcpy((void *) bkp_disklist, (void *) disklist,
2800 sizeof(struct list_of_disks));
2801
2802 iamhere("Post-malloc");
2803 strcpy(help_text,
2804 " Edit this RAID device's list of partitions. Choose OK or Cancel when done.");
2805 sprintf(header_text, "%-24s %s", "Device", "Index");
2806 sprintf(title_of_window, "%s contains...", raidrec->raid_device);
2807 newtPushHelpLine(help_text);
2808 for (b_res = (newtComponent) 12345; b_res != bOK && b_res != bCancel;) {
2809 headerMsg = newtLabel(1, 1, header_text);
2810 partitionsListbox =
2811 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2812 redraw_disklist(disklist, keylist, partitionsListbox);
2813 i = 1;
2814 bAdd = newtCompactButton(i, 9, " Add ");
2815 bDelete = newtCompactButton(i += 8, 9, "Delete");
2816 bOK = newtCompactButton(i += 9, 9, " OK ");
2817 bCancel = newtCompactButton(i += 9, 9, "Cancel");
2818 newtOpenWindow(21, 7, 38, 10, title_of_window);
2819 myForm = newtForm(NULL, NULL, 0);
2820 if (disklist->entries == 0) {
2821 newtFormAddComponents(myForm, headerMsg, bAdd, bDelete, bOK,
2822 bCancel, NULL);
2823 } else {
2824 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2825 bAdd, bDelete, bOK, bCancel, NULL);
2826 }
2827 b_res = newtRunForm(myForm);
2828 if (b_res == bOK || b_res == bCancel) { /* do nothing */
2829// That's OK. At the end of this subroutine (after this do/while loop),
2830// we'll throw away the changes if Cancel was pushed.
2831 } else {
2832 curr_choice = newtListboxGetCurrent(partitionsListbox);
2833 for (i = 0; i < disklist->entries && keylist[i] != curr_choice;
2834 i++);
2835 if (i == disklist->entries && disklist->entries > 0) {
2836 log_to_screen("I don't know what that button does!");
2837 } else {
2838 currline = i;
2839 if (b_res == bAdd) {
2840 log_it("Making list of unallocated RAID slices");
2841 make_list_of_unallocated_raid_partitions
2842 (unallocated_raid_partitions, mountlist_dontedit,
2843 raidlist);
2844 if (unallocated_raid_partitions->entries <= 0) {
2845 popup_and_OK
2846 ("There are no unallocated partitions marked for RAID.");
2847 } else {
2848 log_it
2849 ("Done. The user may add one or more of the above to RAID device");
2850 add_disklist_entry(disklist, raidrec->raid_device,
2851 unallocated_raid_partitions);
2852 log_it("I have finished adding a disklist entry.");
2853 redraw_disklist(disklist, keylist,
2854 partitionsListbox);
2855 }
2856 } else if (b_res == bDelete) {
2857 delete_disklist_entry(disklist, raidrec->raid_device,
2858 currline);
2859 redraw_disklist(disklist, keylist, partitionsListbox);
2860 } else {
2861 sprintf(tmp, "%s's index is %d. What should it be?",
2862 raidrec->raid_device,
2863 disklist->el[currline].index);
2864 sprintf(sz_res, "%d", disklist->el[currline].index);
2865 if (popup_and_get_string("Set index", tmp, sz_res, 10)) {
2866 disklist->el[currline].index = atoi(sz_res);
2867 }
2868 redraw_disklist(disklist, keylist, partitionsListbox);
2869 }
2870 }
2871 }
2872 newtFormDestroy(myForm);
2873 newtPopWindow();
2874 }
2875 newtPopHelpLine();
2876 if (b_res == bCancel) {
2877 memcpy((void *) raidlist, (void *) bkp_raidlist,
2878 sizeof(struct raidlist_itself));
2879 memcpy((void *) raidrec, (void *) bkp_raidrec,
2880 sizeof(struct raid_device_record));
2881 memcpy((void *) disklist, (void *) bkp_disklist,
2882 sizeof(struct list_of_disks));
2883 }
2884 paranoid_free(tmp);
2885 paranoid_free(help_text);
2886 paranoid_free(title_of_window);
2887 paranoid_free(sz_res);
2888 paranoid_free(header_text);
2889 paranoid_free(bkp_raidrec);
2890 paranoid_free(bkp_disklist);
2891 paranoid_free(bkp_raidlist);
2892 paranoid_free(unallocated_raid_partitions);
2893}
2894#endif
2895
2896
2897
2898/**
2899 * Ask the user which restore mode (nuke, interactive, or compare) we should use.
2900 * @return The mode selected: 'I' for interactive, 'N' for nuke, 'C' for compare,
2901 * or 'E' (or any other letter) for exit.
2902 */
2903char which_restore_mode()
2904{
2905
2906 /** char *************************************************************/
2907 char output = '\0';
2908 char tmp[MAX_STR_LEN];
2909
2910 /** newt *************************************************************/
2911
2912 newtComponent b1;
2913 newtComponent b2;
2914 newtComponent b3;
2915 newtComponent b4;
2916 newtComponent b_res;
2917 newtComponent myForm;
2918
2919 if (g_text_mode) {
2920 for (output = 'z'; !strchr("AICE", output); output = tmp[0]) {
2921 printf
2922 ("Which mode - (A)utomatic, (I)nteractive, \n(C)ompare only, or (E)xit to shell?\n--> ");
2923 fgets(tmp, MAX_STR_LEN - 1, stdin);
2924 }
2925 return (output);
2926 }
2927
2928 newtPushHelpLine
2929 (" Do you want to 'nuke' your system, restore interactively, or just compare?");
2930 newtOpenWindow(24, 3, 32, 17, "How should I restore?");
2931 b1 = newtButton(7, 1, "Automatically");
2932 b2 = newtButton(7, 5, "Interactively");
2933 b3 = newtButton(7, 9, "Compare only!");
2934 b4 = newtButton(7, 13, "Exit to shell");
2935 myForm = newtForm(NULL, NULL, 0);
2936 newtFormAddComponents(myForm, b1, b2, b3, b4, NULL);
2937 b_res = newtRunForm(myForm);
2938 newtFormDestroy(myForm);
2939 newtPopWindow();
2940 if (b_res == b1) {
2941 output = 'N';
2942 }
2943 if (b_res == b2) {
2944 output = 'I';
2945 }
2946 if (b_res == b3) {
2947 output = 'C';
2948 }
2949 if (b_res == b4) {
2950 output = 'E';
2951 }
2952 newtPopHelpLine();
2953 return (output);
2954}
Note: See TracBrowser for help on using the repository browser.