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

Last change on this file since 764 was 764, checked in by Bruno Cornec, 18 years ago

merge -r728:763 $SVN_M/branches/stable

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