source: MondoRescue/branches/2.2.8/mondo/src/mondorestore/mondo-rstr-newt.c@ 2140

Last change on this file since 2140 was 2140, checked in by Bruno Cornec, 15 years ago

Fix a Big Bug aka Seg fault, where a print msg was just plain wrong for ISO restore - Kudos to Conor Daly

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