source: MondoRescue/branches/2.2.9/mondo/src/mondorestore/mondo-rstr-newt.c@ 2209

Last change on this file since 2209 was 2209, checked in by Bruno Cornec, 15 years ago
  • Use new libmr.a in build process
  • Use mr_strcat to fix a memory allocation bug when mountlist contains lots of references unavailable at restore time, creating a error when memory is statically allocated (more usage is expected now).
  • Property svn:keywords set to Id
File size: 86.6 KB
Line 
1/***************************************************************************
2 * $Id: mondo-rstr-newt.c 2209 2009-05-15 02:56:31Z 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 2209 2009-05-15 02:56:31Z 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 = NULL;
1946 char *flaws_str_B = NULL;
1947 char *flaws_str_C = NULL;
1948
1949 assert(mountlist != NULL);
1950 assert(raidlist != NULL);
1951
1952 asprintf(&flaws_str_A, "%s", "xxxxxxxxx");
1953 asprintf(&flaws_str_B, "%s", "xxxxxxxxx");
1954 asprintf(&flaws_str_C, "%s", "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 paranoid_free(flaws_str_A);
2046 paranoid_free(flaws_str_B);
2047 paranoid_free(flaws_str_C);
2048 newtFormDestroy(myForm);
2049 newtPopWindow();
2050 newtPopHelpLine();
2051 if (b_res == bOK) {
2052 log_it("You pushed 'OK'. I shall now continue.");
2053 return (0);
2054 } else {
2055 /* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
2056 return (1);
2057 }
2058}
2059
2060
2061
2062/**
2063 * Edit the mountlist.
2064 * @param mountlist The mountlist to edit.
2065 * @param raidlist The raidlist that goes with @p mountlist.
2066 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
2067 */
2068int
2069edit_mountlist(char *mountlist_fname, struct mountlist_itself *mountlist,
2070 struct raidlist_itself *raidlist)
2071{
2072 int res = 0;
2073
2074 iamhere("entering eml");
2075
2076 if (g_text_mode) {
2077 fatal_error("Don't call edit_mountlist() in text mode");
2078 } else {
2079 log_it
2080 ("I'm in GUI mode, so I shall edit mountlist using edit_mountlist()");
2081 res = edit_mountlist_in_newt(mountlist_fname, mountlist, raidlist);
2082 }
2083 iamhere("leaving eml");
2084 return (res);
2085}
2086
2087
2088
2089
2090#ifndef __FreeBSD__
2091/**
2092 * Edit the additional RAID variables in @p raidrec.
2093 * @param raidrec The RAID device record to edit the RAID variables in.
2094 * @ingroup restoreGuiVarslist
2095 */
2096void edit_raidrec_additional_vars(struct raid_device_record *raidrec)
2097{
2098
2099 /** structure *********************************************************/
2100 struct raid_device_record bkp_raidrec;
2101
2102 /** newt **************************************************************/
2103 newtComponent myForm;
2104 newtComponent bAdd;
2105 newtComponent bEdit;
2106 newtComponent bDelete;
2107 newtComponent bOK;
2108 newtComponent bCancel;
2109 newtComponent b_res;
2110 newtComponent varsListbox;
2111 newtComponent headerMsg;
2112
2113 /** ?? ***************************************************************/
2114 void *keylist[ARBITRARY_MAXIMUM], *curr_choice;
2115
2116 /** buffers **********************************************************/
2117 char title_of_window[MAX_STR_LEN];
2118
2119 /** int **************************************************************/
2120 int i = 0;
2121 int currline = 0;
2122
2123
2124 assert(raidrec != NULL);
2125
2126 memcpy((void *) &bkp_raidrec, (void *) raidrec,
2127 sizeof(struct raid_device_record));
2128 sprintf(title_of_window, "Additional variables");
2129 newtPushHelpLine
2130 (" Edit the additional fields to your heart's content, then click OK or Cancel.");
2131 headerMsg = newtLabel(1, 1, "Label Value");
2132 varsListbox =
2133 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2134 i = 1;
2135 bAdd = newtCompactButton(i, 9, " Add ");
2136 bEdit = newtCompactButton(i += 8, 9, " Edit ");
2137 bDelete = newtCompactButton(i += 9, 9, "Delete");
2138 bOK = newtCompactButton(i += 9, 9, " OK ");
2139 bCancel = newtCompactButton(i += 9, 9, "Cancel");
2140 newtOpenWindow(17, 7, 46, 10, title_of_window);
2141 myForm = newtForm(NULL, NULL, 0);
2142 newtFormAddComponents(myForm, headerMsg, varsListbox, bAdd, bEdit,
2143 bDelete, bOK, bCancel, NULL);
2144 insert_essential_additionalvars(raidrec);
2145 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2146 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
2147 b_res = newtRunForm(myForm);
2148 curr_choice = newtListboxGetCurrent(varsListbox);
2149 for (currline = 0;
2150 currline < raidrec->additional_vars.entries
2151 && keylist[currline] != curr_choice; currline++);
2152 if (currline == raidrec->additional_vars.entries
2153 && raidrec->additional_vars.entries > 0) {
2154 log_it("Warning - I don't know what this button does");
2155 }
2156 if (b_res == bOK) { /* do nothing */
2157 } else if (b_res == bCancel) { /* do nothing */
2158 } else if (b_res == bAdd) {
2159 add_varslist_entry(raidrec);
2160 } else if (b_res == bDelete) {
2161 delete_varslist_entry(raidrec, currline);
2162 } else {
2163 edit_varslist_entry(raidrec, currline);
2164 }
2165 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2166 }
2167 remove_essential_additionalvars(raidrec);
2168 newtFormDestroy(myForm);
2169 newtPopWindow();
2170 newtPopHelpLine();
2171 if (b_res == bCancel) {
2172 memcpy((void *) raidrec, (void *) &bkp_raidrec,
2173 sizeof(struct raid_device_record));
2174 }
2175 return;
2176}
2177#endif
2178
2179
2180/**
2181 * Find the next free location to place a disk in @p disklist.
2182 * @param disklist The disklist to operate on.
2183 * @return The next free location (starting from 0).
2184 * @ingroup restoreGuiDisklist
2185 */
2186int find_next_free_index_in_disklist(struct list_of_disks *disklist)
2187{
2188
2189 /** int ***************************************************************/
2190 int index = -1;
2191 int pos = 0;
2192
2193 /** bool **************************************************************/
2194 bool done;
2195
2196 assert(disklist != NULL);
2197
2198 for (done = FALSE; !done;) {
2199 for (pos = 0;
2200 pos < disklist->entries && disklist->el[pos].index <= index;
2201 pos++);
2202 if (pos >= disklist->entries) {
2203 done = TRUE;
2204 } else {
2205 index = disklist->el[pos].index;
2206 }
2207 }
2208 return (index + 1);
2209}
2210
2211
2212
2213/**
2214 * Locate @p device in @p raidlist.
2215 * @param raidlist The raidlist ot search in.
2216 * @param device The RAID device to search for.
2217 * @return The index of the device, or -1 if it could not be found.
2218 * @ingroup restoreGuiMountlist
2219 */
2220int
2221find_raid_device_in_raidlist(struct raidlist_itself *raidlist,
2222 char *device)
2223{
2224
2225 /** int ***************************************************************/
2226 int i = 0;
2227#ifdef __FreeBSD__
2228 char vdev[64];
2229#else
2230// Linux
2231#endif
2232
2233 assert(raidlist != NULL);
2234 assert_string_is_neither_NULL_nor_zerolength(device);
2235
2236#ifdef __FreeBSD__
2237 for (i = 0; i < raidlist->entries; i++) {
2238 sprintf(vdev, "/dev/vinum/%s", raidlist->el[i].volname);
2239 if (!strcmp(device, vdev))
2240 break;
2241 }
2242#else
2243
2244 for (i = 0;
2245 strcmp(raidlist->el[i].raid_device, device)
2246 && i < raidlist->entries; i++);
2247#endif
2248 if (i == raidlist->entries) {
2249 return (-1);
2250 } else {
2251 return (i);
2252 }
2253}
2254
2255
2256/**
2257 * Get information about the location of ISO images from the user.
2258 * @param isodir_device Where to put the device (e.g. /dev/hda4) the user enters.
2259 * @param isodir_format Where to put the format (e.g. ext2) the user enters.
2260 * @param isodir_path Where to put the path (e.g. /var/cache/mondo) the user enters.
2261 * @param nuke_me_please Whether we're planning on nuking or not.
2262 * @return TRUE if OK was pressed, FALSE otherwise.
2263 */
2264bool
2265get_isodir_info(char *isodir_device, char *isodir_format,
2266 char *isodir_path, bool nuke_me_please)
2267{
2268
2269 /** initialize ********************************************************/
2270
2271 assert(isodir_device != NULL);
2272 assert(isodir_format != NULL);
2273 assert(isodir_path != NULL);
2274
2275 log_it("isodir_path = %s", isodir_path);
2276 isodir_format[0] = '\0';
2277 if (isodir_device[0] == '\0') {
2278 strcpy(isodir_device, "/dev/");
2279 }
2280 if (isodir_path[0] == '\0') {
2281 strcpy(isodir_path, "/");
2282 }
2283 if (does_file_exist("/tmp/NFS-SERVER-PATH")) {
2284 strcpy(isodir_device, last_line_of_file("/tmp/NFS-SERVER-MOUNT"));
2285 strcpy(isodir_format, "nfs");
2286 strcpy(isodir_path, last_line_of_file("/tmp/NFS-SERVER-PATH"));
2287 }
2288 if (nuke_me_please) {
2289 return (TRUE);
2290 }
2291
2292 if (popup_and_get_string
2293 ("ISO Mode - device", "On what device do the ISO files live?",
2294 isodir_device, MAX_STR_LEN / 4)) {
2295 if (popup_and_get_string
2296 ("ISO Mode - format",
2297 "What is the disk format of the device? (Hit ENTER if you don't know.)",
2298 isodir_format, 16)) {
2299 if (popup_and_get_string
2300 ("ISO Mode - path",
2301 "At what path on this device can the ISO files be found?",
2302 isodir_path, MAX_STR_LEN / 4)) {
2303 strip_spaces(isodir_device);
2304 strip_spaces(isodir_format);
2305 strip_spaces(isodir_path);
2306 log_it("isodir_device = %s - isodir_format = %s - isodir_path = %s", isodir_device, isodir_format, isodir_path);
2307 return (TRUE);
2308 }
2309 }
2310 }
2311 return (FALSE);
2312}
2313
2314
2315/**
2316 * Create a new raidtab entry for @p device in @p raidlist.
2317 * @param raidlist The raidlist to add the device to.
2318 * @param mountlist The mountlist containing information about the user's partitions.
2319 * @param currline The selected line in the mountlist.
2320 * @param device The RAID device (e.g. /dev/md0) to use.
2321 * @ingroup restoreGuiMountlist
2322 */
2323void
2324initiate_new_raidlist_entry(struct raidlist_itself *raidlist,
2325 struct mountlist_itself *mountlist,
2326 int currline, char *device)
2327{
2328
2329 /** structure *********************************************************/
2330 struct OSSWAP (raid_device_record, vinum_volume) * raidrec;
2331
2332 /** int ***************************************************************/
2333 int pos_in_raidlist = 0;
2334
2335 assert(raidlist != NULL);
2336 assert(mountlist != NULL);
2337 assert_string_is_neither_NULL_nor_zerolength(device);
2338
2339 pos_in_raidlist =
2340 find_raid_device_in_raidlist(raidlist,
2341 mountlist->el[currline].device);
2342 if (pos_in_raidlist >= 0) {
2343 fatal_error("Sorry, that RAID device already exists. Weird.");
2344 }
2345 pos_in_raidlist = raidlist->entries++;
2346 raidrec = &raidlist->el[pos_in_raidlist];
2347 initialize_raidrec(raidrec);
2348 strcpy(raidrec->OSSWAP(raid_device, volname),
2349 OSSWAP(device, basename(device)));
2350#ifndef __FreeBSD__
2351 choose_raid_level(raidrec);
2352 select_raid_disks(mountlist, raidlist, raidrec, "data",
2353 &raidrec->data_disks);
2354#endif
2355 edit_raidlist_entry(mountlist, raidlist, raidrec, currline);
2356}
2357
2358
2359#ifndef __FreeBSD__
2360/**
2361 * Insert the RAID variables not stored in the "additional RAID variables" list there too.
2362 * @param raidrec The RAID device record to operate on.
2363 * @ingroup restoreGuiVarslist
2364 */
2365void insert_essential_additionalvars(struct raid_device_record *raidrec)
2366{
2367
2368 /** int **************************************************************/
2369 int items = 0;
2370
2371 assert(raidrec != NULL);
2372
2373 items = raidrec->additional_vars.entries;
2374 write_variableINT_to_raid_var_line(raidrec, items++,
2375 "persistent-superblock",
2376 raidrec->persistent_superblock);
2377 write_variableINT_to_raid_var_line(raidrec, items++, "chunk-size",
2378 raidrec->chunk_size);
2379 raidrec->additional_vars.entries = items;
2380}
2381
2382#endif
2383
2384/**
2385 * Dummy function that proves that we can get to the point where Mondo is run.
2386 */
2387void nuke_mode_dummy()
2388{
2389
2390 /** newt *************************************************************/
2391 newtComponent myForm;
2392 newtComponent b1;
2393 newtComponent b2;
2394 newtComponent b3;
2395 newtComponent b_res;
2396
2397
2398 newtPushHelpLine
2399 ("This is where I nuke your hard drives. Mhahahahaha. No-one can stop Mojo Jojo!");
2400 newtOpenWindow(24, 3, 32, 13, "Nuking");
2401 b1 = newtButton(7, 1, "Slowly");
2402 b2 = newtButton(7, 5, "Medium");
2403 b3 = newtButton(7, 9, "Quickly");
2404 myForm = newtForm(NULL, NULL, 0);
2405 newtFormAddComponents(myForm, b1, b2, b3, NULL);
2406 b_res = newtRunForm(myForm);
2407 newtFormDestroy(myForm);
2408 newtPopWindow();
2409 newtPopHelpLine();
2410}
2411
2412
2413
2414/**
2415 * Redraw the disklist.
2416 * @param disklist The disklist to read from.
2417 * @param keylist The list of keys for @p listbox.
2418 * @param listbox The Newt listbox component to redraw.
2419 * @ingroup restoreGuiDisklist
2420 */
2421void
2422redraw_disklist(struct list_of_disks *disklist,
2423 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2424{
2425
2426 /** long **************************************************************/
2427 long i = 0;
2428
2429 assert(disklist != NULL);
2430 assert(keylist != NULL);
2431 assert(listbox != NULL);
2432
2433 newtListboxClear(listbox);
2434
2435 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2436 keylist[i] = (void *) i;
2437 }
2438 for (i = 0; i < disklist->entries; i++) {
2439 newtListboxAppendEntry(listbox,
2440 disklist_entry_to_string(disklist, i),
2441 keylist[i]);
2442 }
2443}
2444
2445
2446/**
2447 * Redraw the mountlist.
2448 * @param mountlist The mountlist to read from.
2449 * @param keylist The list of keys for @p listbox.
2450 * @param listbox The Newt listbox component to redraw.
2451 * @ingroup restoreGuiMountlist
2452 */
2453void
2454redraw_mountlist(struct mountlist_itself *mountlist,
2455 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2456{
2457
2458 /** long **************************************************************/
2459 long i = 0;
2460
2461 assert(mountlist != NULL);
2462 assert(keylist != NULL);
2463 assert(listbox != NULL);
2464
2465 newtListboxClear(listbox);
2466// sort_mountlist_by_device (mountlist);
2467 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2468 keylist[i] = (void *) i;
2469 }
2470 for (i = 0; i < mountlist->entries; i++) {
2471 newtListboxAppendEntry(listbox,
2472 mountlist_entry_to_string(mountlist, i),
2473 keylist[i]);
2474 }
2475}
2476
2477
2478
2479
2480/**
2481 * Redraw the list of unallocated RAID partitions.
2482 * @param unallocated_raid_partitions The mountlist containing unallocated RAID partitions.
2483 * @param keylist The list of keys for @p listbox.
2484 * @param listbox The Newt listbox component to redraw.
2485 * @ingroup restoreGuiDisklist
2486 */
2487void redraw_unallocpartnslist(struct mountlist_itself
2488 *unallocated_raid_partitions,
2489 void *keylist[ARBITRARY_MAXIMUM],
2490 newtComponent listbox)
2491{
2492
2493 /** long *************************************************************/
2494 long i = 0;
2495
2496 /** buffers **********************************************************/
2497 char tmp[MAX_STR_LEN];
2498
2499 assert(unallocated_raid_partitions != NULL);
2500 assert(keylist != NULL);
2501 assert(listbox != NULL);
2502
2503 newtListboxClear(listbox);
2504 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2505 keylist[i] = (void *) i;
2506 }
2507 for (i = 0; i < unallocated_raid_partitions->entries; i++) {
2508 sprintf(tmp, "%-22s %8lld",
2509 unallocated_raid_partitions->el[i].device,
2510 unallocated_raid_partitions->el[i].size / 1024L);
2511 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2512 }
2513}
2514
2515#ifndef __FreeBSD__
2516/**
2517 * Redraw the list of additional RAID variables.
2518 * @param additional_vars The list of additional RAID varibals.
2519 * @param keylist The list of keys for @p listbox.
2520 * @param listbox The Newt listbox component to redraw.
2521 * @ingroup restoreGuiVarslist
2522 */
2523void
2524redraw_varslist(struct additional_raid_variables *additional_vars,
2525 void *keylist[], newtComponent listbox)
2526{
2527 /** long ************************************************************/
2528 long i = 0;
2529
2530 /** buffers *********************************************************/
2531 char tmp[MAX_STR_LEN];
2532
2533 assert(additional_vars != NULL);
2534 assert(keylist != NULL);
2535 assert(listbox != NULL);
2536
2537 newtListboxClear(listbox);
2538
2539 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2540 keylist[i] = (void *) i;
2541 }
2542 for (i = 0; i < additional_vars->entries; i++) {
2543 sprintf(tmp, "%-32s %-8s", additional_vars->el[i].label,
2544 additional_vars->el[i].value);
2545 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2546 }
2547}
2548
2549
2550/**
2551 * Remove variable @p label from the RAID variables list in @p raidrec.
2552 * @param raidrec The RAID device record to remove the variable from.
2553 * @param label The variable name to remove.
2554 * @return The value of the variable removed.
2555 * @ingroup restoreUtilityGroup
2556 */
2557int
2558read_variableINT_and_remove_from_raidvars(struct
2559 OSSWAP (raid_device_record,
2560 vinum_volume) * raidrec,
2561 char *label)
2562{
2563 /** int ***************************************************************/
2564 int i = 0;
2565 int res = 0;
2566
2567
2568 assert(raidrec != NULL);
2569 assert(label != NULL);
2570
2571 for (i = 0;
2572 i < raidrec->additional_vars.entries
2573 && strcmp(raidrec->additional_vars.el[i].label, label); i++);
2574 if (i == raidrec->additional_vars.entries) {
2575 res = -1;
2576 } else {
2577 res = atoi(raidrec->additional_vars.el[i].value);
2578 for (i++; i < raidrec->additional_vars.entries; i++) {
2579 memcpy((void *) &raidrec->additional_vars.el[i - 1],
2580 (void *) &raidrec->additional_vars.el[i],
2581 sizeof(struct raid_var_line));
2582 }
2583 raidrec->additional_vars.entries--;
2584 }
2585 return (res);
2586}
2587#endif
2588
2589/**
2590 * Change all RAID devices to use @p new_dev instead of @p old_dev.
2591 * @param raidlist The raidlist to make the changes in.
2592 * @param old_dev The old name of the device (what it used to be).
2593 * @param new_dev The new name of the device (what it is now).
2594 * @ingroup restoreGuiMountlist
2595 */
2596void rejig_partition_name_in_raidlist_if_necessary(struct raidlist_itself
2597 *raidlist,
2598 char *old_dev,
2599 char *new_dev)
2600{
2601 /** buffers ********************************************************/
2602 char tmp[MAX_STR_LEN];
2603
2604 /** int ************************************************************/
2605 int pos = 0;
2606 int j = 0;
2607
2608 assert(raidlist != NULL);
2609 assert_string_is_neither_NULL_nor_zerolength(old_dev);
2610 assert_string_is_neither_NULL_nor_zerolength(new_dev);
2611
2612 pos = which_raid_device_is_using_this_partition(raidlist, old_dev);
2613 if (pos < 0) {
2614 sprintf(tmp, "No need to rejig %s in raidlist: it's not listed.",
2615 old_dev);
2616 log_it(tmp);
2617 } else {
2618 if ((j =
2619 where_in_drivelist_is_drive(&raidlist->
2620 OSSWAP(el[pos].data_disks, disks),
2621 old_dev)) >= 0) {
2622 strcpy(raidlist->OSSWAP(el[pos].data_disks, disks).el[j].
2623 device, new_dev);
2624 } else
2625 if ((j =
2626 where_in_drivelist_is_drive(&raidlist->
2627 OSSWAP(el[pos].spare_disks,
2628 spares),
2629 old_dev)) >= 0) {
2630 strcpy(raidlist->OSSWAP(el[pos].spare_disks, spares).el[j].
2631 device, new_dev);
2632 }
2633#ifndef __FreeBSD__
2634 else if ((j =
2635 where_in_drivelist_is_drive(&raidlist->el[pos].
2636 parity_disks,
2637 old_dev)) >= 0) {
2638 strcpy(raidlist->el[pos].parity_disks.el[j].device, new_dev);
2639 } else
2640 if ((j =
2641 where_in_drivelist_is_drive(&raidlist->el[pos].
2642 failed_disks,
2643 old_dev)) >= 0) {
2644 strcpy(raidlist->el[pos].failed_disks.el[j].device, new_dev);
2645 }
2646#endif
2647 else {
2648 sprintf(tmp,
2649 "%s is supposed to be listed in this raid dev but it's not...",
2650 old_dev);
2651 log_it(tmp);
2652 }
2653 }
2654}
2655
2656
2657#ifndef __FreeBSD__
2658/**
2659 * Remove the essential RAID variables from the "additional variables" section.
2660 * If they have been changed, set them in their normal locations too.
2661 * @param raidrec The RAID device record to operate on.
2662 * @ingroup restoreUtilityVarslist
2663 */
2664void remove_essential_additionalvars(struct raid_device_record *raidrec)
2665{
2666
2667 /** int **************************************************************/
2668 int res = 0;
2669
2670 assert(raidrec != NULL);
2671
2672 res =
2673 read_variableINT_and_remove_from_raidvars(raidrec,
2674 "persistent-superblock");
2675 if (res > 0) {
2676 raidrec->persistent_superblock = res;
2677 }
2678 res = read_variableINT_and_remove_from_raidvars(raidrec, "chunk-size");
2679 if (res > 0) {
2680 raidrec->chunk_size = res;
2681 }
2682 res = read_variableINT_and_remove_from_raidvars(raidrec, "block-size");
2683}
2684
2685/**
2686 * Select the RAID disks to use in @p raidrec.
2687 * @param mountlist_dontedit The mountlist (will not be edited).
2688 * @param raidlist The raidlist to modify.
2689 * @param raidrec The RAID device record in @p raidlist to work on.
2690 * @param description_of_list The type of disks we're selecting (e.g. "data").
2691 * @param disklist The disklist to put the user-selected disks in.
2692 * @ingroup restoreGuiMountlist
2693 */
2694void
2695select_raid_disks(struct mountlist_itself *mountlist_dontedit,
2696 struct raidlist_itself *raidlist,
2697 struct raid_device_record *raidrec,
2698 char *description_of_list,
2699 struct list_of_disks *disklist)
2700{
2701 void *curr_choice;
2702
2703 /** ??? ***************************************************************/
2704
2705 /** structures ********************************************************/
2706 struct raidlist_itself *bkp_raidlist;
2707 struct raid_device_record *bkp_raidrec;
2708 struct list_of_disks *bkp_disklist;
2709 struct mountlist_itself *unallocated_raid_partitions;
2710
2711 /** newt **************************************************************/
2712 newtComponent myForm = NULL;
2713 newtComponent bAdd = NULL;
2714 newtComponent bDelete = NULL;
2715 newtComponent bOK = NULL;
2716 newtComponent bCancel = NULL;
2717 newtComponent b_res = NULL;
2718 newtComponent partitionsListbox = NULL;
2719 newtComponent headerMsg = NULL;
2720
2721 /** buffers **********************************************************/
2722 void *keylist[ARBITRARY_MAXIMUM];
2723 char *tmp;
2724 char *help_text;
2725 char *title_of_window;
2726 char *sz_res;
2727 char *header_text;
2728
2729 /** int **************************************************************/
2730 int i = 0;
2731 int currline = 0;
2732
2733 assert(mountlist_dontedit != NULL);
2734 assert(raidlist != NULL);
2735 assert(raidrec != NULL);
2736 assert(description_of_list != NULL);
2737 assert(disklist != NULL);
2738
2739 iamhere("malloc'ing");
2740 malloc_string(tmp);
2741 malloc_string(help_text);
2742 malloc_string(title_of_window);
2743 malloc_string(sz_res);
2744 malloc_string(header_text);
2745 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
2746 fatal_error("Cannot malloc space for raidrec");
2747 }
2748 if (!(bkp_disklist = malloc(sizeof(struct list_of_disks)))) {
2749 fatal_error("Cannot malloc space for disklist");
2750 }
2751 if (!(bkp_raidlist = malloc(sizeof(struct raidlist_itself)))) {
2752 fatal_error("Cannot malloc space for raidlist");
2753 }
2754 if (!
2755 (unallocated_raid_partitions =
2756 malloc(sizeof(struct mountlist_itself)))) {
2757 fatal_error("Cannot malloc space for unallocated_raid_partitions");
2758 }
2759
2760 memcpy((void *) bkp_raidlist, (void *) raidlist,
2761 sizeof(struct raidlist_itself));
2762 memcpy((void *) bkp_raidrec, (void *) raidrec,
2763 sizeof(struct raid_device_record));
2764 memcpy((void *) bkp_disklist, (void *) disklist,
2765 sizeof(struct list_of_disks));
2766
2767 iamhere("Post-malloc");
2768 strcpy(help_text,
2769 " Edit this RAID device's list of partitions. Choose OK or Cancel when done.");
2770 sprintf(header_text, "%-24s %s", "Device", "Index");
2771 sprintf(title_of_window, "%s contains...", raidrec->raid_device);
2772 newtPushHelpLine(help_text);
2773 for (b_res = (newtComponent) 12345; b_res != bOK && b_res != bCancel;) {
2774 headerMsg = newtLabel(1, 1, header_text);
2775 partitionsListbox =
2776 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2777 redraw_disklist(disklist, keylist, partitionsListbox);
2778 i = 1;
2779 bAdd = newtCompactButton(i, 9, " Add ");
2780 bDelete = newtCompactButton(i += 8, 9, "Delete");
2781 bOK = newtCompactButton(i += 9, 9, " OK ");
2782 bCancel = newtCompactButton(i += 9, 9, "Cancel");
2783 newtOpenWindow(21, 7, 38, 10, title_of_window);
2784 myForm = newtForm(NULL, NULL, 0);
2785 if (disklist->entries == 0) {
2786 newtFormAddComponents(myForm, headerMsg, bAdd, bDelete, bOK,
2787 bCancel, NULL);
2788 } else {
2789 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2790 bAdd, bDelete, bOK, bCancel, NULL);
2791 }
2792 b_res = newtRunForm(myForm);
2793 if (b_res == bOK || b_res == bCancel) { /* do nothing */
2794// That's OK. At the end of this subroutine (after this do/while loop),
2795// we'll throw away the changes if Cancel was pushed.
2796 } else {
2797 curr_choice = newtListboxGetCurrent(partitionsListbox);
2798 for (i = 0; i < disklist->entries && keylist[i] != curr_choice;
2799 i++);
2800 if (i == disklist->entries && disklist->entries > 0) {
2801 log_to_screen("I don't know what that button does!");
2802 } else {
2803 currline = i;
2804 if (b_res == bAdd) {
2805 log_it("Making list of unallocated RAID slices");
2806 make_list_of_unallocated_raid_partitions
2807 (unallocated_raid_partitions, mountlist_dontedit,
2808 raidlist);
2809 if (unallocated_raid_partitions->entries <= 0) {
2810 popup_and_OK
2811 ("There are no unallocated partitions marked for RAID.");
2812 } else {
2813 log_it
2814 ("Done. The user may add one or more of the above to RAID device");
2815 add_disklist_entry(disklist, raidrec->raid_device,
2816 unallocated_raid_partitions);
2817 log_it("I have finished adding a disklist entry.");
2818 redraw_disklist(disklist, keylist,
2819 partitionsListbox);
2820 }
2821 } else if (b_res == bDelete) {
2822 delete_disklist_entry(disklist, raidrec->raid_device,
2823 currline);
2824 redraw_disklist(disklist, keylist, partitionsListbox);
2825 } else {
2826 sprintf(tmp, "%s's index is %d. What should it be?",
2827 raidrec->raid_device,
2828 disklist->el[currline].index);
2829 sprintf(sz_res, "%d", disklist->el[currline].index);
2830 if (popup_and_get_string("Set index", tmp, sz_res, 10)) {
2831 disklist->el[currline].index = atoi(sz_res);
2832 }
2833 redraw_disklist(disklist, keylist, partitionsListbox);
2834 }
2835 }
2836 }
2837 newtFormDestroy(myForm);
2838 newtPopWindow();
2839 }
2840 newtPopHelpLine();
2841 if (b_res == bCancel) {
2842 memcpy((void *) raidlist, (void *) bkp_raidlist,
2843 sizeof(struct raidlist_itself));
2844 memcpy((void *) raidrec, (void *) bkp_raidrec,
2845 sizeof(struct raid_device_record));
2846 memcpy((void *) disklist, (void *) bkp_disklist,
2847 sizeof(struct list_of_disks));
2848 }
2849 paranoid_free(tmp);
2850 paranoid_free(help_text);
2851 paranoid_free(title_of_window);
2852 paranoid_free(sz_res);
2853 paranoid_free(header_text);
2854 paranoid_free(bkp_raidrec);
2855 paranoid_free(bkp_disklist);
2856 paranoid_free(bkp_raidlist);
2857 paranoid_free(unallocated_raid_partitions);
2858}
2859#endif
2860
2861
2862
2863/**
2864 * Ask the user which restore mode (nuke, interactive, or compare) we should use.
2865 * @return The mode selected: 'I' for interactive, 'N' for nuke, 'C' for compare,
2866 * or 'E' (or any other letter) for exit.
2867 */
2868char which_restore_mode()
2869{
2870
2871 /** char *************************************************************/
2872 char output = '\0';
2873 char tmp[MAX_STR_LEN];
2874
2875 /** newt *************************************************************/
2876
2877 newtComponent b1;
2878 newtComponent b2;
2879 newtComponent b3;
2880 newtComponent b4;
2881 newtComponent b_res;
2882 newtComponent myForm;
2883
2884 if (g_text_mode) {
2885 for (output = 'z'; !strchr("AICE", output); output = tmp[0]) {
2886 printf
2887 ("Which mode - (A)utomatic, (I)nteractive, \n(C)ompare only, or (E)xit to shell?\n--> ");
2888 fgets(tmp, MAX_STR_LEN - 1, stdin);
2889 }
2890 return (output);
2891 }
2892
2893 newtPushHelpLine
2894 (" Do you want to 'nuke' your system, restore interactively, or just compare?");
2895 newtOpenWindow(24, 3, 32, 17, "How should I restore?");
2896 b1 = newtButton(7, 1, "Automatically");
2897 b2 = newtButton(7, 5, "Interactively");
2898 b3 = newtButton(7, 9, "Compare only!");
2899 b4 = newtButton(7, 13, "Exit to shell");
2900 myForm = newtForm(NULL, NULL, 0);
2901 newtFormAddComponents(myForm, b1, b2, b3, b4, NULL);
2902 b_res = newtRunForm(myForm);
2903 newtFormDestroy(myForm);
2904 newtPopWindow();
2905 if (b_res == b1) {
2906 output = 'N';
2907 }
2908 if (b_res == b2) {
2909 output = 'I';
2910 }
2911 if (b_res == b3) {
2912 output = 'C';
2913 }
2914 if (b_res == b4) {
2915 output = 'E';
2916 }
2917 newtPopHelpLine();
2918 return (output);
2919}
Note: See TracBrowser for help on using the repository browser.