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

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

merge -r686:728 $SVN_M/branches/stable

  • Property svn:keywords set to Id
File size: 87.2 KB
Line 
1/***************************************************************************
2 * $Id: mondo-rstr-newt.c 729 2006-07-30 14:06:39Z 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 int i = 0;
886
887 /** structures *******************************************************/
888 struct s_node *node = NULL;
889
890 /** buffers **********************************************************/
891 static char *current_filename = NULL;
892 char *tmp = NULL;
893
894 /** bool *************************************************************/
895 /* void*dummyptr; */
896 bool dummybool;
897 static bool warned_already;
898
899 assert(filelist != NULL);
900 assert(keylist != NULL);
901 assert(listbox != NULL);
902
903 if (depth == 0) {
904 lines_in_flist_window = 0;
905 warned_already = FALSE;
906 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
907 g_strings_of_flist_window[i][0] = '\0';
908 g_is_path_selected[i] = FALSE;
909 }
910 }
911 for (node = filelist; node != NULL; node = node->right) {
912 current_filename[depth] = node->ch;
913 if (node->down) {
914 depth++;
915 i = redraw_filelist(node->down, keylist, listbox);
916 depth--;
917 }
918 if (node->ch == '\0' && node->expanded) {
919 if (lines_in_flist_window == ARBITRARY_MAXIMUM) {
920 if (!warned_already) {
921 warned_already = TRUE;
922 asprintf(&tmp,
923 _
924 ("Too many lines. Displaying first %d entries only. Close a directory to see more."),
925 ARBITRARY_MAXIMUM);
926 popup_and_OK(tmp);
927 paranoid_free(tmp);
928 }
929 } else {
930 strcpy(g_strings_of_flist_window[lines_in_flist_window],
931 current_filename);
932 g_is_path_selected[lines_in_flist_window] = node->selected;
933 lines_in_flist_window++;
934 }
935 }
936 }
937 if (depth == 0) {
938 if (lines_in_flist_window > ARBITRARY_MAXIMUM) {
939 lines_in_flist_window = ARBITRARY_MAXIMUM;
940 }
941/* do an elementary sort */
942 for (i = 1; i < lines_in_flist_window; i++) {
943 if (strcmp
944 (g_strings_of_flist_window[i],
945 g_strings_of_flist_window[i - 1]) < 0) {
946 asprintf(&tmp, g_strings_of_flist_window[i]);
947 strcpy(g_strings_of_flist_window[i],
948 g_strings_of_flist_window[i - 1]);
949 strcpy(g_strings_of_flist_window[i - 1], tmp);
950 paranoid_free(tmp);
951
952 dummybool = g_is_path_selected[i];
953 g_is_path_selected[i] = g_is_path_selected[i - 1];
954 g_is_path_selected[i - 1] = dummybool;
955 i = 0;
956 }
957 }
958/* write list to screen */
959 newtListboxClear(listbox);
960 for (i = 0; i < lines_in_flist_window; i++) {
961 asprintf(&tmp, "%c%c %-80s",
962 (g_is_path_selected[i] ? '*' : ' '),
963 (g_is_path_expanded[i] ? '+' : '-'),
964 strip_path(g_strings_of_flist_window[i]));
965 // BERLIOS: this is dangerous now => Memory leak
966 if (strlen(tmp) > 71) {
967 tmp[70] = '\0';
968 }
969 keylist[i] = (void *) i;
970 newtListboxAppendEntry(listbox, tmp, keylist[i]);
971 paranoid_free(tmp);
972 }
973 return (lines_in_flist_window);
974 } else {
975 return (0);
976 }
977}
978
979
980/**
981 * Strip a path to the bare minimum (^ pointing to the directory above, plus filename).
982 * @param tmp The path to strip.
983 * @return The stripped path.
984 * @author Conor Daly
985 * @ingroup restoreUtilityGroup
986 */
987char *strip_path(char *tmp)
988{
989
990 int i = 0, j = 0, slashcount = 0;
991 int slashloc = 0, lastslashloc = 0;
992
993 while (tmp[i] != '\0') { /* Count the slashes in tmp
994 1 slash per dir */
995 if (tmp[i] == '/') {
996 slashcount++;
997 lastslashloc = slashloc;
998 slashloc = i;
999 if (tmp[i + 1] == '\0') { /* if this slash is last char, back off */
1000 slashcount--;
1001 slashloc = lastslashloc;
1002 }
1003 }
1004 i++;
1005 }
1006 if (slashcount > 0)
1007 slashcount--; /* Keep one slash 'cos Hugh does... */
1008
1009 /* BERLIOS: tmpnopath and prev not defined !! How can this compile ?? */
1010 for (i = 0; i < slashcount; i++) { /* Replace each dir with a space char */
1011 tmpnopath[i] = ' ';
1012 }
1013
1014 i = slashloc;
1015 j = slashcount;
1016 while (tmp[i] != '\0') { /* Now add what's left of tmp */
1017 if ((tmpprevpath[j] == ' ' || tmpprevpath[j] == '^')
1018 && tmp[i] == '/' && tmpnopath[j - 1] != '^' && j != 0) { /* Add a pointer upwards if this is not in the same dir as line above */
1019 tmpnopath[j - 1] = '^';
1020 } else {
1021 tmpnopath[j++] = tmp[i++];
1022 }
1023 }
1024 tmpnopath[j] = '\0';
1025
1026 strcpy(tmpprevpath, tmpnopath); /* Make a copy for next iteration */
1027
1028 return (tmpnopath);
1029}
1030
1031
1032/**
1033 * Allow the user to edit the filelist and choose which files to restore.
1034 * @param filelist The node structure containing the filelist.
1035 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
1036 */
1037int edit_filelist(struct s_node *filelist)
1038{
1039
1040 /** newt **************************************************************/
1041 newtComponent myForm;
1042 newtComponent bLess = NULL;
1043 newtComponent bMore = NULL;
1044 newtComponent bToggle = NULL;
1045 newtComponent bOK = NULL;
1046 newtComponent bCancel = NULL;
1047 newtComponent b_res = NULL;
1048 newtComponent filelistListbox = NULL;
1049 newtComponent bRegex = NULL;
1050
1051 /** int ***************************************************************/
1052 int finished = FALSE;
1053 int lines_in_flist_window = 0;
1054 int indexno = 0;
1055 int j = 0;
1056
1057 /** ???? **************************************************************/
1058 void *curr_choice;
1059 void *keylist[ARBITRARY_MAXIMUM];
1060
1061 /** bool **************************************************************/
1062 bool dummybool;
1063
1064/* struct s_node *node; */
1065
1066 assert(filelist != NULL);
1067
1068 log_to_screen(_("Editing filelist"));
1069 newtPushHelpLine
1070 (_
1071 (" Please edit the filelist to your satisfaction, then click OK or Cancel."));
1072 j = 4;
1073 bLess = newtCompactButton(j, 17, _(" Less "));
1074 bMore = newtCompactButton(j += 12, 17, _(" More "));
1075 bToggle = newtCompactButton(j += 12, 17, _("Toggle"));
1076 bRegex = newtCompactButton(j += 12, 17, _("RegEx"));
1077 bCancel = newtCompactButton(j += 12, 17, _("Cancel"));
1078 bOK = newtCompactButton(j += 12, 17, _(" OK "));
1079 filelistListbox =
1080 newtListbox(2, 1, 15, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1081 toggle_all_root_dirs_on(filelist);
1082 lines_in_flist_window =
1083 redraw_filelist(filelist, keylist, filelistListbox);
1084 newtOpenWindow(1, 3, 77, 18, _("Editing filelist"));
1085 myForm = newtForm(NULL, NULL, 0);
1086 newtFormAddComponents(myForm, filelistListbox, bLess, bMore, bToggle,
1087 bRegex, bCancel, bOK, NULL);
1088 while (!finished) {
1089 b_res = newtRunForm(myForm);
1090 if (b_res == bOK) {
1091 finished =
1092 ask_me_yes_or_no
1093 (_("Are you happy with your file selection?"));
1094 } else if (b_res == bCancel) {
1095 finished = TRUE;
1096 } else if (b_res == bRegex) {
1097 popup_and_OK(_("I haven't implemented this yet..."));
1098 } else {
1099 curr_choice = newtListboxGetCurrent(filelistListbox);
1100 for (indexno = 0;
1101 indexno < lines_in_flist_window
1102 && keylist[indexno] != curr_choice; indexno++);
1103 if (indexno == lines_in_flist_window) {
1104 log_it
1105 ("I don't know what this button does; assuming I am to toggle 1st entry");
1106 indexno = 0;
1107 }
1108 log_it("You selected '%s'",
1109 g_strings_of_flist_window[indexno]);
1110 if (b_res == bMore) {
1111 g_is_path_expanded[indexno] = TRUE;
1112 toggle_path_expandability(filelist,
1113 g_strings_of_flist_window
1114 [indexno], TRUE);
1115 lines_in_flist_window =
1116 redraw_filelist(filelist, keylist, filelistListbox);
1117 newtListboxSetCurrentByKey(filelistListbox, curr_choice);
1118 } else if (b_res == bLess) {
1119 g_is_path_expanded[indexno] = FALSE;
1120 toggle_path_expandability(filelist,
1121 g_strings_of_flist_window
1122 [indexno], FALSE);
1123 lines_in_flist_window =
1124 redraw_filelist(filelist, keylist, filelistListbox);
1125 newtListboxSetCurrentByKey(filelistListbox, curr_choice);
1126 } else {
1127 if (!strcmp(g_strings_of_flist_window[indexno], "/")) {
1128 dummybool = !g_is_path_selected[indexno];
1129 for (j = 1; j < lines_in_flist_window; j++) {
1130 toggle_path_selection(filelist,
1131 g_strings_of_flist_window[j],
1132 dummybool);
1133 }
1134 } else {
1135 toggle_path_selection(filelist,
1136 g_strings_of_flist_window
1137 [indexno],
1138 !g_is_path_selected[indexno]);
1139 lines_in_flist_window =
1140 redraw_filelist(filelist, keylist,
1141 filelistListbox);
1142 }
1143 newtListboxSetCurrentByKey(filelistListbox, curr_choice);
1144 }
1145 for (indexno = 0;
1146 indexno < lines_in_flist_window
1147 && keylist[indexno] != curr_choice; indexno++);
1148 if (indexno == lines_in_flist_window) {
1149 log_it
1150 ("Layout of table has changed. Y pointer is reverting to zero.");
1151 indexno = 0;
1152 }
1153 }
1154 }
1155 newtFormDestroy(myForm);
1156 newtPopWindow();
1157 newtPopHelpLine();
1158 if (b_res == bOK) {
1159 return (0);
1160 } else {
1161/* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
1162 return (1);
1163 }
1164}
1165
1166
1167/**
1168 * Edit an entry in @p mountlist.
1169 * @param mountlist The mountlist containing information about the user's partitions.
1170 * @param raidlist The raidlist to accompany @p mountlist.
1171 * @param listbox The Newt listbox component in the mountlist editor.
1172 * @param currline The selected line (starting from 0) in @p listbox.
1173 * @param keylist The list of keys for @p listbox.
1174 * @ingroup restoreGuiMountlist
1175 */
1176void
1177edit_mountlist_entry(struct mountlist_itself *mountlist,
1178 struct raidlist_itself *raidlist,
1179 newtComponent listbox, int currline, void *keylist[])
1180{
1181
1182 /** structures ********************************************************/
1183 static struct raidlist_itself bkp_raidlist;
1184
1185 /** newt **************************************************************/
1186 newtComponent myForm;
1187 newtComponent bOK;
1188 newtComponent bCancel;
1189 newtComponent b_res;
1190 newtComponent mountpointComp;
1191 newtComponent label0;
1192 newtComponent label1;
1193 newtComponent label2;
1194 newtComponent label3;
1195 newtComponent sizeComp;
1196 newtComponent deviceComp;
1197 newtComponent formatComp;
1198 newtComponent b_raid = NULL;
1199
1200 char *device_str = NULL;
1201 char *mountpoint_str = NULL;
1202 char *size_str = NULL;
1203 char *format_str = NULL;
1204 char *tmp = NULL;
1205 char *device_used_to_be = NULL;
1206 char *mountpt_used_to_be = NULL;
1207 char *device_here = NULL;
1208 char *mountpoint_here = NULL;
1209 char *size_here = NULL;
1210 char *format_here = NULL;
1211
1212 int j = 0;
1213
1214 assert(mountlist != NULL);
1215 assert(raidlist != NULL);
1216 assert(listbox != NULL);
1217 assert(keylist != NULL);
1218
1219 memcpy((void *) &bkp_raidlist, (void *) raidlist,
1220 sizeof(struct raidlist_itself));
1221 asprintf(&device_str, mountlist->el[currline].device);
1222 asprintf(&device_used_to_be, mountlist->el[currline].device);
1223 asprintf(&mountpoint_str, mountlist->el[currline].mountpoint);
1224 asprintf(&mountpt_used_to_be, mountlist->el[currline].mountpoint);
1225 asprintf(&format_str, mountlist->el[currline].format);
1226 asprintf(&size_str, "%lld", mountlist->el[currline].size / 1024);
1227
1228 newtOpenWindow(20, 5, 48, 10, "Edit entry");
1229 label0 = newtLabel(2, 1, _("Device:"));
1230 label1 = newtLabel(2, 2, _("Mountpoint:"));
1231 label2 = newtLabel(2, 3, _("Size (MB): "));
1232 label3 = newtLabel(2, 4, _("Format: "));
1233 deviceComp =
1234 newtEntry(14, 1, device_str, 30, (void *) &device_here, 0);
1235 paranoid_free(device_str);
1236
1237 mountpointComp =
1238 newtEntry(14, 2, mountpoint_str, 30, (void *) &mountpoint_here, 0);
1239 paranoid_free(mountpoint_str);
1240
1241 formatComp =
1242 newtEntry(14, 4, format_str, 15, (void *) &format_here, 0);
1243 paranoid_free(format_str);
1244
1245 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1246 || !strcmp(mountlist->el[currline].mountpoint, "image")) {
1247 sizeComp = newtLabel(14, 3, size_str);
1248 } else {
1249 sizeComp = newtEntry(14, 3, size_str, 10, (void *) &size_here, 0);
1250 }
1251 paranoid_free(size_str);
1252
1253 bOK = newtButton(2, 6, _(" OK "));
1254 bCancel = newtButton(14, 6, _("Cancel"));
1255 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)) {
1256 b_raid = newtButton(26, 6, "RAID..");
1257 }
1258 newtPushHelpLine
1259 (_
1260 (" Edit this partition's mountpoint, size and format; then click 'OK'."));
1261 myForm = newtForm(NULL, NULL, 0);
1262 newtFormAddComponents(myForm, deviceComp, mountpointComp, sizeComp,
1263 formatComp, label0, label1, label2, label3, bOK,
1264 bCancel, b_raid, NULL);
1265 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
1266 b_res = newtRunForm(myForm);
1267
1268 paranoid_free(device_str);
1269 asprintf(&device_str, device_here);
1270 strip_spaces(device_str);
1271
1272 paranoid_free(mountpoint_str);
1273 asprintf(&mountpoint_str, mountpoint_here);
1274 strip_spaces(mountpoint_str);
1275
1276 paranoid_free(format_str);
1277 asprintf(&format_str, format_here);
1278 paranoid_free(format_here);
1279 strip_spaces(format_str);
1280 if (b_res == bOK && strstr(device_str, RAID_DEVICE_STUB)
1281 && strstr(device_used_to_be, RAID_DEVICE_STUB)
1282 && strcmp(device_str, device_used_to_be)) {
1283 popup_and_OK(_("You can't change /dev/mdX to /dev/mdY."));
1284 b_res = NULL;
1285 continue;
1286 } else if (b_res == bOK && !strcmp(mountpoint_str, "image")
1287 && strcmp(mountpt_used_to_be, "image")) {
1288 popup_and_OK(_
1289 ("You can't change a regular device to an image."));
1290 b_res = NULL;
1291 continue;
1292 }
1293 paranoid_free(mountpt_used_to_be);
1294
1295 if (!strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1296 && strcmp(mountlist->el[currline].mountpoint, "image")) {
1297 asprintf(&size_str, size_here);
1298 strip_spaces(size_str);
1299 } else {
1300 asprintf(&size_str, "%ld",
1301 calculate_raid_device_size(mountlist, raidlist,
1302 mountlist->el[currline].
1303 device) / 1024);
1304 newtLabelSetText(sizeComp, size_str);
1305 }
1306 paranoid_free(size_here);
1307
1308 /* do not let user click RAID button if user has changed device_str */
1309 if (b_res == b_raid) {
1310 if (strcmp(device_str, mountlist->el[currline].device)) {
1311 /*
1312 can't change mountlist's entry from /dex/mdX to /dev/mdY: it would ugly
1313 when you try to map the changes over to the raidtab list, trust me
1314 */
1315 popup_and_OK
1316 (_
1317 ("You cannot edit the RAID settings until you have OK'd your change to the device node."));
1318 } else {
1319 j = find_raid_device_in_raidlist(raidlist,
1320 mountlist->el[currline].
1321 device);
1322 if (j < 0) {
1323 sprintf(tmp,
1324 _
1325 ("/etc/raidtab does not have an entry for %s; please delete it and add it again"),
1326 mountlist->el[currline].device);
1327 popup_and_OK(tmp);
1328 } else {
1329 log_it(_("edit_raidlist_entry - calling"));
1330 edit_raidlist_entry(mountlist, raidlist,
1331 &raidlist->el[j], currline);
1332 }
1333 }
1334 }
1335 }
1336 paranoid_free(device_here);
1337 paranoid_free(mountpoint_here);
1338
1339 newtFormDestroy(myForm);
1340 newtPopHelpLine();
1341 newtPopWindow();
1342 if (b_res == bCancel) {
1343 memcpy((void *) raidlist, (void *) &bkp_raidlist,
1344 sizeof(struct raidlist_itself));
1345 return;
1346 }
1347 strcpy(mountlist->el[currline].device, device_str);
1348 strcpy(mountlist->el[currline].mountpoint, mountpoint_str);
1349 paranoid_free(mountpoint_str);
1350
1351 strcpy(mountlist->el[currline].format, format_str);
1352 paranoid_free(format_str);
1353
1354 if (strcmp(mountlist->el[currline].mountpoint, "image")) {
1355 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)) {
1356 mountlist->el[currline].size =
1357 calculate_raid_device_size(mountlist, raidlist,
1358 mountlist->el[currline].device);
1359 } else {
1360 mountlist->el[currline].size = atol(size_str) * 1024;
1361 }
1362 }
1363 paranoid_free(size_str);
1364
1365 newtListboxSetEntry(listbox, (int) keylist[currline],
1366 mountlist_entry_to_string(mountlist, currline));
1367 /* if new /dev/md RAID device then do funky stuff */
1368 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1369 && !strstr(device_used_to_be, RAID_DEVICE_STUB)) {
1370 initiate_new_raidlist_entry(raidlist, mountlist, currline,
1371 device_str);
1372 }
1373 /* if moving from RAID to non-RAID then do funky stuff */
1374 else if (strstr(device_used_to_be, RAID_DEVICE_STUB)
1375 && !strstr(device_str, RAID_DEVICE_STUB)) {
1376 delete_raidlist_entry(mountlist, raidlist, device_str);
1377 }
1378 /* if moving a non-RAID to another non-RAID then re-jig any RAID disks, if necessary */
1379 else if (!strstr(device_used_to_be, RAID_DEVICE_STUB)
1380 && !strstr(device_str, RAID_DEVICE_STUB)) {
1381 rejig_partition_name_in_raidlist_if_necessary(raidlist,
1382 device_used_to_be,
1383 device_str);
1384 }
1385/* else, moving a RAID to another RAID; bad idea, or so I thought */
1386#ifndef __FreeBSD__ /* It works fine under FBSD. */
1387 else if (strcmp(device_used_to_be, device_str)) {
1388 popup_and_OK
1389 (_
1390 ("You are renaming a RAID device as another RAID device. I don't like it but I'll allow it."));
1391 }
1392#endif
1393 redraw_mountlist(mountlist, keylist, listbox);
1394 paranoid_free(device_str);
1395 paranoid_free(device_used_to_be);
1396}
1397
1398
1399#if __FreeBSD__
1400/**
1401 * Add a subdisk to @p raidrec.
1402 * @param raidlist The raidlist containing information about RAID partitions.
1403 * @param raidrec The RAID device record to add the subdisk to.
1404 * @param temp The device name of the RAID disk to add it to.
1405 * @author Joshua Oreman
1406 * @ingroup restoreGuiMountlist
1407 */
1408void
1409add_raid_subdisk(struct raidlist_itself *raidlist,
1410 struct vinum_plex *raidrec, char *temp)
1411{
1412 int i;
1413 bool found = FALSE;
1414
1415 for (i = 0; i < raidlist->disks.entries; ++i) {
1416 if (!strcmp(raidlist->disks.el[i].device, temp)) {
1417 strcpy(raidrec->sd[raidrec->subdisks].which_device,
1418 raidlist->disks.el[i].name);
1419 found = TRUE;
1420 }
1421 }
1422 if (!found) {
1423 sprintf(raidlist->disks.el[raidlist->disks.entries].name,
1424 "drive%i", raidlist->disks.entries);
1425 sprintf(raidrec->sd[raidrec->subdisks].which_device, "drive%i",
1426 raidlist->disks.entries);
1427 strcpy(raidlist->disks.el[raidlist->disks.entries++].device, temp);
1428 }
1429 raidrec->subdisks++;
1430}
1431
1432
1433/**
1434 * Determine the /dev entry for @p vinum_name.
1435 * @param raidlist The raidlist containing information about RAID devices.
1436 * @param vinum_name The name of the Vinum drive to map to a /dev entry.
1437 * @return The /dev entry, or NULL if none was found.
1438 * @note The returned string points to static storage that will be overwritten with each call.
1439 * @author Joshua Oreman
1440 * @ingroup restoreUtilityGroup
1441 */
1442char *find_dev_entry_for_raid_device_name(struct raidlist_itself *raidlist,
1443 char *vinum_name)
1444{
1445 int i;
1446 for (i = 0; i < raidlist->disks.entries; ++i) {
1447 if (!strcmp(raidlist->disks.el[i].name, vinum_name)) {
1448 return raidlist->disks.el[i].device;
1449 }
1450 }
1451 return NULL;
1452}
1453
1454
1455void
1456edit_raidlist_plex(struct mountlist_itself *mountlist,
1457 struct raidlist_itself *raidlist,
1458 struct vinum_plex *raidrec, int currline,
1459 int currline2);
1460
1461#endif
1462
1463
1464/**
1465 * Edit the entry for @p raidrec in @p raidlist.
1466 * @param mountlist The mountlist to get some information from.
1467 * @param raidlist The raidlist containing information about RAID devices.
1468 * @param raidrec The RAID device record for this partition.
1469 * @param currline The line number (starting from 0) in the mountlist of the RAID device.
1470 * @ingroup restoreGuiMountlist
1471 */
1472void
1473edit_raidlist_entry(struct mountlist_itself *mountlist,
1474 struct raidlist_itself *raidlist,
1475 struct OSSWAP (raid_device_record,
1476 vinum_volume) * raidrec, int currline)
1477{
1478
1479#ifdef __FreeBSD__
1480 /** structures ********************************************************/
1481 struct vinum_volume bkp_raidrec;
1482
1483
1484 /** buffers ***********************************************************/
1485 char *title_of_editraidForm_window;
1486
1487 /** newt **************************************************************/
1488 newtComponent editraidForm;
1489 newtComponent bOK;
1490 newtComponent bCancel;
1491 newtComponent bEdit;
1492 newtComponent bAdd;
1493 newtComponent bDelete;
1494 newtComponent b_res;
1495 newtComponent plexesListbox;
1496 newtComponent plexesHeader;
1497
1498 void *keylist[10];
1499 void *curr_choice;
1500
1501 int currline2 = 0;
1502 char *pname = NULL;
1503 char *raidlevel = NULL;
1504 char *chunksize = NULL;
1505 char *entry = NULL;
1506 char *msg = NULL;
1507 int i = 0;
1508 char *headerstr = NULL;
1509
1510 log_it(_("Started edit_raidlist_entry"));
1511 memcpy((void *) &bkp_raidrec, (void *) raidrec,
1512 sizeof(struct vinum_volume));
1513 asprintf(&title_of_editraidForm_window, _("Plexes on %s"),
1514 raidrec->volname);
1515 newtPushHelpLine(_(" Please select a plex to edit"));
1516 newtOpenWindow(13, 5, 54, 15, title_of_editraidForm_window);
1517 paranoid_free(title_of_editraidForm_window);
1518
1519 for (;;) {
1520 asprintf(&headerstr, "%-14s %-8s %11s %8s", _("Plex"), _("Level",) _("Stripe Size"), _("Subdisks"));
1521
1522 bOK = newtCompactButton(2, 13, _(" OK "));
1523 bCancel = newtCompactButton(12, 13, _("Cancel"));
1524 bAdd = newtCompactButton(22, 13, _(" Add "));
1525 bEdit = newtCompactButton(32, 13, _(" Edit "));
1526 bDelete = newtCompactButton(42, 13, _("Delete"));
1527
1528 plexesListbox =
1529 newtListbox(2, 3, 9, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1530 plexesHeader = newtLabel(2, 2, headerstr);
1531 paranoid_free(headerstr);
1532
1533 editraidForm = newtForm(NULL, NULL, 0);
1534
1535 newtListboxClear(plexesListbox);
1536 for (i = 0; i < 10; ++i) {
1537 keylist[i] = (void *) i;
1538 if (i < raidrec->plexes) {
1539 switch (raidrec->plex[i].raidlevel) {
1540 case -1:
1541 asprintf(&raidlevel, "concat");
1542 break;
1543 case 0:
1544 asprintf(&raidlevel, "striped");
1545 break;
1546 case 5:
1547 asprintf(&raidlevel, "raid5");
1548 break;
1549 default:
1550 asprintf(&raidlevel, "raid%i",
1551 raidrec->plex[i].raidlevel);
1552 break;
1553 }
1554
1555 if (raidrec->plex[i].raidlevel == -1) {
1556 asprintf(&chunksize, "N/A");
1557 } else {
1558 asprintf(&chunksize, "%dk", raidrec->plex[i].stripesize);
1559 }
1560 asprintf(&pname, "%s.p%i", raidrec->volname, i);
1561 asprintf(&entry, "%-14s %-8s %11s %8d",
1562 pname, raidlevel, chunksize,
1563 raidrec->plex[i].subdisks);
1564 paranoid_free(pname);
1565 paranoid_free(chunksize);
1566 paranoid_free(raidlevel);
1567 newtListboxAppendEntry(plexesListbox, entry, keylist[i]);
1568 paranoid_free(entry);
1569 }
1570 }
1571
1572 newtFormAddComponents(editraidForm, bOK, bCancel, bAdd, bEdit,
1573 bDelete, plexesListbox, plexesHeader, NULL);
1574
1575 b_res = newtRunForm(editraidForm);
1576 if (b_res == bOK || b_res == bCancel) {
1577 break;
1578 }
1579
1580 curr_choice = newtListboxGetCurrent(plexesListbox);
1581 for (currline2 = 0; currline2 < raidrec->plexes; ++currline2) {
1582 if (currline2 > 9)
1583 break;
1584 if (keylist[currline2] == curr_choice)
1585 break;
1586 }
1587
1588 if (b_res == bDelete) {
1589 asprintf(&msg, _("Are you sure you want to delete %s.p%i?"),
1590 raidrec->volname, currline2);
1591 if (ask_me_yes_or_no(msg)) {
1592 log_it(_("Deleting RAID plex"));
1593 memcpy((void *) &raidrec->plex[currline2],
1594 (void *) &raidrec->plex[raidrec->plexes - 1],
1595 sizeof(struct vinum_plex));
1596 raidrec->plexes--;
1597 }
1598 paranoid_free(msg);
1599 continue;
1600 }
1601 if (b_res == bAdd) {
1602 raidrec->plex[raidrec->plexes].raidlevel = 0;
1603 raidrec->plex[raidrec->plexes].stripesize = 279;
1604 raidrec->plex[raidrec->plexes].subdisks = 0;
1605 currline2 = raidrec->plexes++;
1606 }
1607 edit_raidlist_plex(mountlist, raidlist, &raidrec->plex[currline2],
1608 currline, currline2);
1609 newtFormDestroy(editraidForm);
1610 }
1611 if (b_res == bCancel) {
1612 memcpy((void *) raidrec, (void *) &bkp_raidrec,
1613 sizeof(struct vinum_volume));
1614 }
1615 newtPopHelpLine();
1616 newtPopWindow();
1617 mountlist->el[currline].size =
1618 calculate_raid_device_size(mountlist, raidlist, raidrec->volname);
1619#else
1620 /** structures ********************************************************/
1621 struct raid_device_record *bkp_raidrec = NULL;
1622
1623
1624 /** buffers ***********************************************************/
1625 char *title_of_editraidForm_window = NULL;
1626 char *sz_raid_level = NULL;
1627 char *sz_data_disks = NULL;
1628 char *sz_spare_disks = NULL;
1629 char *sz_parity_disks = NULL;
1630 char *sz_failed_disks = NULL;
1631
1632 /** newt **************************************************************/
1633 newtComponent editraidForm;
1634 newtComponent bOK;
1635 newtComponent bCancel;
1636 newtComponent bAdditional;
1637 newtComponent bChangeRaid;
1638 newtComponent bSelectData;
1639 newtComponent bSelectSpare;
1640 newtComponent bSelectParity;
1641 newtComponent bSelectFailed;
1642 newtComponent b_res;
1643
1644 assert(mountlist != NULL);
1645 assert(raidlist != NULL);
1646 assert(raidrec != NULL);
1647
1648 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
1649 fatal_error("Cannot malloc space for raidrec");
1650 }
1651
1652 log_it("Started edit_raidlist_entry");
1653
1654 memcpy((void *) bkp_raidrec, (void *) raidrec,
1655 sizeof(struct raid_device_record));
1656 asprintf(&title_of_editraidForm_window, _("Edit %s"), raidrec->raid_device);
1657 log_msg(2, "Opening newt window");
1658 newtOpenWindow(20, 5, 40, 14, title_of_editraidForm_window);
1659 paranoid_free(title_of_editraidForm_window);
1660
1661 for (;;) {
1662 log_msg(2, "Main loop");
1663 asprintf(&sz_raid_level,
1664 turn_raid_level_number_to_string(raidrec->raid_level));
1665 asprintf(&sz_data_disks,
1666 number_of_disks_as_string(raidrec->data_disks.entries,
1667 _("data")));
1668 asprintf(&sz_spare_disks,
1669 number_of_disks_as_string(raidrec->spare_disks.entries,
1670 _("spare")));
1671 asprintf(&sz_parity_disks,
1672 number_of_disks_as_string(raidrec->parity_disks.entries,
1673 _("parity")));
1674 asprintf(&sz_failed_disks,
1675 number_of_disks_as_string(raidrec->failed_disks.entries,
1676 _("failed")));
1677 bSelectData = newtButton(1, 1, sz_data_disks);
1678 bSelectSpare = newtButton(20, 1, sz_spare_disks);
1679 bSelectParity = newtButton(1, 5, sz_parity_disks);
1680 bSelectFailed = newtButton(20, 5, sz_failed_disks);
1681 bChangeRaid = newtButton(1, 9, sz_raid_level);
1682 paranoid_free(sz_raid_level);
1683 paranoid_free(sz_data_disks);
1684 paranoid_free(sz_spare_disks);
1685 paranoid_free(sz_parity_disks);
1686 paranoid_free(sz_failed_disks);
1687
1688 bOK = newtButton(16 + (raidrec->raid_level == -1), 9, _(" OK "));
1689 bCancel = newtButton(28, 9, _("Cancel"));
1690 bAdditional =
1691 newtCompactButton(1, 13,
1692 _("Additional settings and information"));
1693 newtPushHelpLine
1694 (_
1695 (" Edit the RAID device's settings to your heart's content, then hit OK/Cancel."));
1696 editraidForm = newtForm(NULL, NULL, 0);
1697 newtFormAddComponents(editraidForm, bSelectData, bSelectParity,
1698 bChangeRaid, bSelectSpare, bSelectFailed,
1699 bOK, bCancel, bAdditional);
1700 b_res = newtRunForm(editraidForm);
1701 if (b_res == bChangeRaid) {
1702 choose_raid_level(raidrec);
1703 } else if (b_res == bSelectData) {
1704 select_raid_disks(mountlist, raidlist, raidrec, _("data"),
1705 &raidrec->data_disks);
1706 } else if (b_res == bSelectSpare) {
1707 select_raid_disks(mountlist, raidlist, raidrec, _("spare"),
1708 &raidrec->spare_disks);
1709 } else if (b_res == bSelectParity) {
1710 select_raid_disks(mountlist, raidlist, raidrec, _("parity"),
1711 &raidrec->parity_disks);
1712 } else if (b_res == bSelectFailed) {
1713 select_raid_disks(mountlist, raidlist, raidrec, _("failed"),
1714 &raidrec->failed_disks);
1715 } else if (b_res == bAdditional) {
1716 edit_raidrec_additional_vars(raidrec);
1717 }
1718 newtFormDestroy(editraidForm);
1719 if (b_res == bOK || b_res == bCancel) {
1720 break;
1721 }
1722 }
1723 if (b_res == bCancel) {
1724 memcpy((void *) raidrec, (void *) bkp_raidrec,
1725 sizeof(struct raid_device_record));
1726 }
1727 newtPopHelpLine();
1728 newtPopWindow();
1729 mountlist->el[currline].size =
1730 calculate_raid_device_size(mountlist, raidlist,
1731 raidrec->raid_device);
1732 paranoid_free(bkp_raidrec);
1733#endif
1734}
1735#ifdef __FreeBSD__
1736
1737
1738/**
1739 * Edit the plex @p raidrec in @p raidlist.
1740 * @param mountlist The mountlist to get some of the information from.
1741 * @param raidlist The raidlist containing information about RAID devices.
1742 * @param raidrec The plex to edit.
1743 * @param currline The line number (starting from 0) of the RAID device in @p mountlist.
1744 * @param currline2 The line number (starting from 0) of the plex within the RAID device.
1745 * @author Joshua Oreman
1746 * @ingroup restoreGuiMountlist
1747 */
1748void
1749edit_raidlist_plex(struct mountlist_itself *mountlist,
1750 struct raidlist_itself *raidlist,
1751 struct vinum_plex *raidrec, int currline, int currline2)
1752{
1753
1754 /** structures ********************************************************/
1755 struct vinum_plex bkp_raidrec;
1756
1757
1758 /** buffers ***********************************************************/
1759 char *title_of_editraidForm_window = NULL;
1760 char *tmp = NULL;
1761 char *entry = NULL;
1762
1763 /** newt **************************************************************/
1764 newtComponent editraidForm;
1765 newtComponent bOK;
1766 newtComponent bCancel;
1767 newtComponent bEdit;
1768 newtComponent bAdd;
1769 newtComponent bDelete;
1770 newtComponent b_res;
1771 newtComponent unallocListbox, allocListbox;
1772 newtComponent bLevel, sLevel;
1773 newtComponent bStripeSize, sStripeSize;
1774 newtComponent bAlloc, bUnalloc;
1775
1776 void *keylist[ARBITRARY_MAXIMUM];
1777 void *curr_choice_a, *curr_choice_u;
1778 int currline_a, currline_u;
1779 int i;
1780
1781 struct mountlist_itself *unallocparts = NULL;
1782
1783 /* BERLIOS: Check return value */
1784 unallocparts = malloc(sizeof(struct mountlist_itself));
1785
1786 log_it("Started edit_raidlist_entry");
1787 memcpy((void *) &bkp_raidrec, (void *) raidrec,
1788 sizeof(struct vinum_plex));
1789 asprintf(&title_of_editraidForm_window, "%s.p%i",
1790 raidlist->el[currline].volname, currline2);
1791 newtPushHelpLine
1792 (_
1793 (" Please select a subdisk to edit, or edit this plex's parameters"));
1794 newtOpenWindow(13, 3, 54, 18, title_of_editraidForm_window);
1795 paranoid_free(title_of_editraidForm_window);
1796
1797 for (;;) {
1798 switch (raidrec->raidlevel) {
1799 case -1:
1800 asprintf(&tmp, "concat");
1801 break;
1802 case 0:
1803 asprintf(&tmp, "striped");
1804 break;
1805 case 5:
1806 asprintf(&tmp, "raid5");
1807 break;
1808 default:
1809 asprintf(&tmp, _("unknown (%i)"), raidrec->raidlevel);
1810 break;
1811 }
1812 bLevel = newtCompactButton(2, 2, _(" RAID level "));
1813 sLevel = newtLabel(19, 2, tmp);
1814 paranoid_free(tmp);
1815
1816 if (raidrec->raidlevel >= 0) {
1817 asprintf(&tmp, "%ik", raidrec->stripesize);
1818 bStripeSize = newtCompactButton(2, 4, _(" Stripe size "));
1819 } else {
1820 asprintf(&tmp, "N/A");
1821 bStripeSize = newtLabel(2, 4, _("Stripe size:"));
1822 }
1823 sStripeSize = newtLabel(19, 4, tmp);
1824 paranoid_free(tmp);
1825
1826 bOK = newtCompactButton(2, 16, _(" OK "));
1827 bCancel = newtCompactButton(12, 16, _("Cancel"));
1828 bAdd = newtCompactButton(22, 16, _(" Add "));
1829 bEdit = newtCompactButton(32, 16, _(" Edit "));
1830 bDelete = newtCompactButton(42, 16, _("Delete"));
1831
1832
1833 // plexesListbox = newtListbox (2, 7, 9, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1834 unallocListbox =
1835 newtListbox(2, 7, 7, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1836 allocListbox =
1837 newtListbox(33, 7, 7, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1838 bAlloc = newtButton(23, 7, " -> ");
1839 bUnalloc = newtButton(23, 11, " <- ");
1840
1841 editraidForm = newtForm(NULL, NULL, 0);
1842
1843 newtListboxClear(allocListbox);
1844 newtListboxClear(unallocListbox);
1845 bzero(unallocparts, sizeof(struct mountlist_itself));
1846 make_list_of_unallocated_raid_partitions(unallocparts, mountlist,
1847 raidlist);
1848 for (i = 0; i < ARBITRARY_MAXIMUM; ++i) {
1849 keylist[i] = (void *) i;
1850 if (i < raidrec->subdisks) {
1851 asprintf(&entry, "%-17s",
1852 find_dev_entry_for_raid_device_name(raidlist,
1853 raidrec->
1854 sd[i].
1855 which_device));
1856 newtListboxAppendEntry(allocListbox, entry, keylist[i]);
1857 paranoid_free(entry);
1858 }
1859 if (i < unallocparts->entries) {
1860 asprintf(&entry, "%-17s", unallocparts->el[i].device);
1861 newtListboxAppendEntry(unallocListbox, entry, keylist[i]);
1862 paranoid_free(entry);
1863 }
1864 }
1865
1866#define COMP(x) newtFormAddComponent (editraidForm, x)
1867#define UCOMP(x) if (unallocparts->entries > 0) COMP(x)
1868#define ACOMP(x) if (raidrec->subdisks > 0) COMP(x)
1869 editraidForm = newtForm(NULL, NULL, 0);
1870 UCOMP(unallocListbox);
1871 UCOMP(bAlloc);
1872 ACOMP(allocListbox);
1873 ACOMP(bUnalloc);
1874 COMP(bOK);
1875 COMP(bCancel);
1876 COMP(bLevel);
1877 COMP(sLevel);
1878 if (raidrec->raidlevel != -1) {
1879 COMP(bStripeSize);
1880 COMP(sStripeSize);
1881 }
1882#undef COMP
1883#undef UCOMP
1884#undef ACOMP
1885
1886 newtRefresh();
1887 b_res = newtRunForm(editraidForm);
1888 if (b_res == bOK || b_res == bCancel) {
1889 break;
1890 }
1891
1892 curr_choice_a = (raidrec->subdisks > 0) ?
1893 newtListboxGetCurrent(allocListbox) : (void *) 1234;
1894 curr_choice_u = (unallocparts->entries > 0) ?
1895 newtListboxGetCurrent(unallocListbox) : (void *) 1234;
1896 for (currline_a = 0; currline_a < raidrec->subdisks; ++currline_a) {
1897 if (currline_a > ARBITRARY_MAXIMUM)
1898 break;
1899 if (keylist[currline_a] == curr_choice_a)
1900 break;
1901 }
1902 for (currline_u = 0; currline_u < unallocparts->entries;
1903 ++currline_u) {
1904 if (currline_u > ARBITRARY_MAXIMUM)
1905 break;
1906 if (keylist[currline_u] == curr_choice_u)
1907 break;
1908 }
1909 if (b_res == bLevel) {
1910 choose_raid_level(raidrec);
1911 } else if (b_res == bStripeSize) {
1912 asprintf(&tmp, "%i", raidrec->stripesize);
1913 if (popup_and_get_string
1914 (_("Stripe size"),
1915 _("Please enter the stripe size in kilobytes."), tmp)) {
1916 raidrec->stripesize = atoi(tmp);
1917 }
1918 paranoid_free(tmp);
1919 } else if ((b_res == bAlloc) || (b_res == unallocListbox)) {
1920 if (currline_u <= unallocparts->entries)
1921 add_raid_subdisk(raidlist, raidrec,
1922 unallocparts->el[currline_u].device);
1923 } else if ((b_res == bUnalloc) || (b_res == allocListbox)) {
1924 if (currline_a <= raidrec->subdisks) {
1925 memcpy((void *) &raidrec->sd[currline_a],
1926 (void *) &raidrec->sd[raidrec->subdisks - 1],
1927 sizeof(struct vinum_subdisk));
1928 raidrec->subdisks--;
1929 }
1930 }
1931#if 0
1932 } else {
1933 edit_raid_subdisk(raidlist, raidrec, &raidrec->sd[currline3],
1934 currline3);
1935 }
1936#endif
1937 newtFormDestroy(editraidForm);
1938 newtRefresh();
1939}
1940
1941if (b_res == bCancel) {
1942 memcpy((void *) raidrec, (void *) &bkp_raidrec,
1943 sizeof(struct vinum_plex));
1944}
1945newtPopWindow();
1946newtPopHelpLine();
1947}
1948#else
1949/**
1950 * Edit additional RAID variable number @p lino.
1951 * @param raidrec The RAID device record to edit the variable in.
1952 * @param lino The line number (starting from 0) of the variable to edit.
1953 * @ingroup restoreGuiVarslist
1954 */
1955void edit_varslist_entry(struct raid_device_record *raidrec, int lino)
1956{
1957
1958 /** buffers ***********************************************************/
1959 char *header = NULL;
1960 char *comment = NULL;
1961 char *sz_out = NULL;
1962
1963 assert(raidrec != 0);
1964 assert(lino >= 0);
1965
1966 asprintf(&sz_out, raidrec->additional_vars.el[lino].value);
1967 asprintf(&header, _("Edit %s"), raidrec->additional_vars.el[lino].label);
1968 asprintf(&comment, _("Please set %s's value (currently '%s')"),
1969 raidrec->additional_vars.el[lino].label, sz_out);
1970 if (popup_and_get_string(header, comment, sz_out)) {
1971 strcpy(raidrec->additional_vars.el[lino].value, sz_out);
1972 }
1973 paranoid_free(header);
1974 paranoid_free(comment);
1975 paranoid_free(sz_out);
1976}
1977#endif
1978
1979
1980/**
1981 * Edit the mountlist using Newt.
1982 * @param mountlist The mountlist to edit.
1983 * @param raidlist The raidlist that goes with @p mountlist.
1984 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
1985 */
1986int
1987edit_mountlist_in_newt(char *mountlist_fname,
1988 struct mountlist_itself *mountlist,
1989 struct raidlist_itself *raidlist)
1990{
1991
1992 /** newt **************************************************************/
1993 newtComponent myForm;
1994 newtComponent bAdd;
1995 newtComponent bEdit;
1996 newtComponent bDelete;
1997 newtComponent bOK;
1998 newtComponent bCancel;
1999 newtComponent b_res = NULL;
2000 newtComponent partitionsListbox;
2001 newtComponent headerMsg;
2002 newtComponent flawsLabelA;
2003 newtComponent flawsLabelB;
2004 newtComponent flawsLabelC;
2005 newtComponent bReload;
2006
2007 /** ???? *************************************************************/
2008 void *curr_choice = NULL;
2009 void *keylist[ARBITRARY_MAXIMUM];
2010
2011 /** int **************************************************************/
2012 int i = 0;
2013 int currline = 0;
2014 int finished = FALSE;
2015
2016 /** buffers **********************************************************/
2017 char *tmp = NULL;
2018 char *flaws_str_A = NULL;
2019 char *flaws_str_B = NULL;
2020 char *flaws_str_C = NULL;
2021
2022 assert(mountlist != NULL);
2023 assert(raidlist != NULL);
2024
2025 asprintf(&flaws_str_A, "xxxxxxxxx");
2026 asprintf(&flaws_str_B, "xxxxxxxxx");
2027 asprintf(&flaws_str_C, "xxxxxxxxx");
2028 if (mountlist->entries > ARBITRARY_MAXIMUM) {
2029 log_to_screen(_("Arbitrary limits suck, man!"));
2030 finish(1);
2031 }
2032 newtPushHelpLine
2033 (_
2034 (" Please edit the mountlist to your satisfaction, then click OK or Cancel."));
2035 i = 4;
2036 bAdd = newtCompactButton(i, 17, _(" Add "));
2037 bEdit = newtCompactButton(i += 11, 17, _(" Edit "));
2038 bDelete = newtCompactButton(i += 12, 17, _("Delete"));
2039 bReload = newtCompactButton(i += 12, 17, _("Reload"));
2040 bCancel = newtCompactButton(i += 12, 17, _("Cancel"));
2041 bOK = newtCompactButton(i += 12, 17, _(" OK "));
2042 asprintf(&tmp, "%-24s %-24s %-8s %s", _("Device"), _("Mountpoint"),
2043 _("Format"), _("Size (MB)"));
2044 headerMsg = newtLabel(2, 1, tmp);
2045 flawsLabelA = newtLabel(2, 13, flaws_str_A);
2046 flawsLabelB = newtLabel(2, 14, flaws_str_B);
2047 flawsLabelC = newtLabel(2, 15, flaws_str_C);
2048 partitionsListbox =
2049 newtListbox(2, 2, 10, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2050 redraw_mountlist(mountlist, keylist, partitionsListbox);
2051 newtOpenWindow(1, 3, 77, 18, _("Editing mountlist"));
2052 myForm = newtForm(NULL, NULL, 0);
2053 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2054 flawsLabelA, flawsLabelB, flawsLabelC, bAdd,
2055 bEdit, bDelete, bReload, bCancel, bOK, NULL);
2056 while (!finished) {
2057 evaluate_mountlist(mountlist, flaws_str_A, flaws_str_B,
2058 flaws_str_C);
2059 newtLabelSetText(flawsLabelA, flaws_str_A);
2060 newtLabelSetText(flawsLabelB, flaws_str_B);
2061 newtLabelSetText(flawsLabelC, flaws_str_C);
2062 b_res = newtRunForm(myForm);
2063 /* BERLIOS: This needs to be re-written */
2064 if (b_res == bOK) {
2065 if (!evaluate_mountlist
2066 (mountlist, flaws_str_A, flaws_str_B, flaws_str_C)) {
2067 finished =
2068 ask_me_yes_or_no
2069 (_("Your mountlist might not work. Continue anyway?"));
2070 } else {
2071 finished =
2072 ask_me_yes_or_no
2073 (_
2074 ("Are you sure you want to save your mountlist and continue? (No changes will be made to your partition table at this time.)"));
2075 }
2076 } else if (b_res == bCancel) {
2077 finished = TRUE;
2078 } else if (b_res == bReload) {
2079 if (ask_me_yes_or_no(_("Reload original mountlist?"))) {
2080 load_mountlist(mountlist, mountlist_fname);
2081 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2082 redraw_mountlist(mountlist, keylist, partitionsListbox);
2083 }
2084 } else {
2085 curr_choice = newtListboxGetCurrent(partitionsListbox);
2086 for (i = 0;
2087 i < mountlist->entries && keylist[i] != curr_choice; i++);
2088 if (i == mountlist->entries && mountlist->entries > 0) {
2089 log_to_screen(_("I don't know what that button does!"));
2090 } else {
2091 currline = i;
2092 if (b_res == bAdd) {
2093 add_mountlist_entry(mountlist, raidlist,
2094 partitionsListbox, currline,
2095 keylist);
2096 } else if (b_res == bDelete) {
2097 delete_mountlist_entry(mountlist, raidlist,
2098 partitionsListbox, currline,
2099 keylist);
2100 } else {
2101 if (mountlist->entries > 0) {
2102 edit_mountlist_entry(mountlist, raidlist,
2103 partitionsListbox, currline,
2104 keylist);
2105 } else {
2106 popup_and_OK
2107 (_
2108 ("Please add an entry. Then press ENTER to edit it."));
2109 }
2110 }
2111 }
2112 }
2113 paranoid_free(flaws_str_A);
2114 paranoid_free(flaws_str_B);
2115 paranoid_free(flaws_str_C);
2116 }
2117 newtFormDestroy(myForm);
2118 newtPopWindow();
2119 newtPopHelpLine();
2120 if (b_res == bOK) {
2121 log_it(_("You pushed 'OK'. I shall now continue."));
2122 return (0);
2123 } else {
2124 /* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
2125 return (1);
2126 }
2127}
2128
2129
2130/**
2131 * Edit the mountlist.
2132 * @param mountlist The mountlist to edit.
2133 * @param raidlist The raidlist that goes with @p mountlist.
2134 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
2135 */
2136int
2137edit_mountlist(char *mountlist_fname, struct mountlist_itself *mountlist,
2138 struct raidlist_itself *raidlist)
2139{
2140 int res = 0;
2141
2142 iamhere("entering eml");
2143
2144 if (g_text_mode) {
2145 fatal_error("Don't call edit_mountlist() in text mode");
2146 } else {
2147 log_it
2148 ("I'm in GUI mode, so I shall edit mountlist using edit_mountlist()");
2149 res = edit_mountlist_in_newt(mountlist_fname, mountlist, raidlist);
2150 }
2151 iamhere("leaving eml");
2152 return (res);
2153}
2154
2155
2156#ifndef __FreeBSD__
2157/**
2158 * Edit the additional RAID variables in @p raidrec.
2159 * @param raidrec The RAID device record to edit the RAID variables in.
2160 * @ingroup restoreGuiVarslist
2161 */
2162void edit_raidrec_additional_vars(struct raid_device_record *raidrec)
2163{
2164
2165 /** structure *********************************************************/
2166 struct raid_device_record bkp_raidrec;
2167
2168 /** newt **************************************************************/
2169 newtComponent myForm;
2170 newtComponent bAdd;
2171 newtComponent bEdit;
2172 newtComponent bDelete;
2173 newtComponent bOK;
2174 newtComponent bCancel;
2175 newtComponent b_res;
2176 newtComponent varsListbox;
2177 newtComponent headerMsg;
2178
2179 /** ?? ***************************************************************/
2180 void *keylist[ARBITRARY_MAXIMUM], *curr_choice = NULL;
2181
2182 /** buffers **********************************************************/
2183 char *title_of_window = NULL;
2184
2185 /** int **************************************************************/
2186 int i = 0;
2187 int currline = 0;
2188
2189
2190 assert(raidrec != NULL);
2191
2192 memcpy((void *) &bkp_raidrec, (void *) raidrec,
2193 sizeof(struct raid_device_record));
2194 asprintf(&title_of_window, "Additional variables");
2195 newtPushHelpLine
2196 (_
2197 (" Edit the additional fields to your heart's content, then click OK or Cancel."));
2198 headerMsg =
2199 newtLabel(1, 1, _("Label Value"));
2200 varsListbox =
2201 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2202 i = 1;
2203 bAdd = newtCompactButton(i, 9, _(" Add "));
2204 bEdit = newtCompactButton(i += 8, 9, _(" Edit "));
2205 bDelete = newtCompactButton(i += 9, 9, _("Delete"));
2206 bOK = newtCompactButton(i += 9, 9, _(" OK "));
2207 bCancel = newtCompactButton(i += 9, 9, _("Cancel"));
2208 newtOpenWindow(17, 7, 46, 10, title_of_window);
2209 paranoid_free(title_of_window);
2210
2211 myForm = newtForm(NULL, NULL, 0);
2212 newtFormAddComponents(myForm, headerMsg, varsListbox, bAdd, bEdit,
2213 bDelete, bOK, bCancel, NULL);
2214 insert_essential_additionalvars(raidrec);
2215 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2216 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
2217 b_res = newtRunForm(myForm);
2218 curr_choice = newtListboxGetCurrent(varsListbox);
2219 for (currline = 0;
2220 currline < raidrec->additional_vars.entries
2221 && keylist[currline] != curr_choice; currline++);
2222 if (currline == raidrec->additional_vars.entries
2223 && raidrec->additional_vars.entries > 0) {
2224 log_it("Warning - I don't know what this button does");
2225 }
2226 if (b_res == bOK) { /* do nothing */
2227 } else if (b_res == bCancel) { /* do nothing */
2228 } else if (b_res == bAdd) {
2229 add_varslist_entry(raidrec);
2230 } else if (b_res == bDelete) {
2231 delete_varslist_entry(raidrec, currline);
2232 } else {
2233 edit_varslist_entry(raidrec, currline);
2234 }
2235 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2236 }
2237 remove_essential_additionalvars(raidrec);
2238 newtFormDestroy(myForm);
2239 newtPopWindow();
2240 newtPopHelpLine();
2241 if (b_res == bCancel) {
2242 memcpy((void *) raidrec, (void *) &bkp_raidrec,
2243 sizeof(struct raid_device_record));
2244 }
2245 return;
2246}
2247#endif
2248
2249
2250/**
2251 * Find the next free location to place a disk in @p disklist.
2252 * @param disklist The disklist to operate on.
2253 * @return The next free location (starting from 0).
2254 * @ingroup restoreGuiDisklist
2255 */
2256int find_next_free_index_in_disklist(struct list_of_disks *disklist)
2257{
2258
2259 /** int ***************************************************************/
2260 int index = -1;
2261 int pos = 0;
2262
2263 /** bool **************************************************************/
2264 bool done;
2265
2266 assert(disklist != NULL);
2267
2268 for (done = FALSE; !done;) {
2269 for (pos = 0;
2270 pos < disklist->entries && disklist->el[pos].index <= index;
2271 pos++);
2272 if (pos >= disklist->entries) {
2273 done = TRUE;
2274 } else {
2275 index = disklist->el[pos].index;
2276 }
2277 }
2278 return (index + 1);
2279}
2280
2281
2282/**
2283 * Locate @p device in @p raidlist.
2284 * @param raidlist The raidlist ot search in.
2285 * @param device The RAID device to search for.
2286 * @return The index of the device, or -1 if it could not be found.
2287 * @ingroup restoreGuiMountlist
2288 */
2289int
2290find_raid_device_in_raidlist(struct raidlist_itself *raidlist,
2291 char *device)
2292{
2293 /** int ***************************************************************/
2294 int i = 0;
2295#ifdef __FreeBSD__
2296 char *vdev = NULL;
2297#endif
2298
2299 assert(raidlist != NULL);
2300 assert_string_is_neither_NULL_nor_zerolength(device);
2301
2302#ifdef __FreeBSD__
2303 for (i = 0; i < raidlist->entries; i++) {
2304 asprintf(&vdev, "/dev/vinum/%s", raidlist->el[i].volname);
2305 if (!strcmp(device, vdev)) {
2306 paranoid_free(vdev);
2307 break;
2308 }
2309 paranoid_free(vdev);
2310 }
2311#else
2312
2313 for (i = 0; strcmp(raidlist->el[i].raid_device, device)
2314 && i < raidlist->entries; i++);
2315#endif
2316 if (i == raidlist->entries) {
2317 return (-1);
2318 } else {
2319 return (i);
2320 }
2321}
2322
2323
2324/**
2325 * Get information about the location of ISO images from the user.
2326 * @param isodir_device Where to put the device (e.g. /dev/hda4) the user enters.
2327 * @param isodir_format Where to put the format (e.g. ext2) the user enters.
2328 * @param isodir_path Where to put the path (e.g. /var/cache/mondo) the user enters.
2329 * @param nuke_me_please Whether we're planning on nuking or not.
2330 * @return TRUE if OK was pressed, FALSE otherwise.
2331 */
2332bool
2333get_isodir_info(char *isodir_device, char *isodir_format,
2334 char *isodir_path, bool nuke_me_please)
2335{
2336
2337 /** initialize ********************************************************/
2338
2339 // %d no var ???
2340 // log_it("%d - AAA - isodir_path = %s", isodir_path);
2341 if (isodir_device == NULL) {
2342 asprintf(&isodir_device, "/dev/");
2343 }
2344 if (isodir_path == NULL) {
2345 asprintf(&isodir_path, "/");
2346 }
2347 if (does_file_exist("/tmp/NFS-SERVER-PATH")) {
2348 paranoid_free(isodir_device);
2349 isodir_device = last_line_of_file("/tmp/NFS-SERVER-MOUNT");
2350 asprintf(&isodir_format, "nfs");
2351 paranoid_free(isodir_path);
2352 isodir_path = last_line_of_file("/tmp/NFS-SERVER-PATH");
2353 }
2354 if (nuke_me_please) {
2355 return (TRUE);
2356 }
2357
2358 if (popup_and_get_string
2359 (_("ISO Mode - device"),
2360 _("On what device do the ISO files live?"), isodir_device)) {
2361 if (popup_and_get_string
2362 (_("ISO Mode - format"),
2363 _
2364 ("What is the disk format of the device? (Hit ENTER if you don't know.)"),
2365 isodir_format)) {
2366 if (popup_and_get_string
2367 (_("ISO Mode - path"),
2368 _
2369 ("At what path on this device can the ISO files be found?"),
2370 isodir_path)) {
2371 // Same pb:
2372 // log_it("%d - BBB - isodir_path = %s", isodir_path);
2373 return (TRUE);
2374 }
2375 }
2376 }
2377 return (FALSE);
2378}
2379
2380
2381/**
2382 * Create a new raidtab entry for @p device in @p raidlist.
2383 * @param raidlist The raidlist to add the device to.
2384 * @param mountlist The mountlist containing information about the user's partitions.
2385 * @param currline The selected line in the mountlist.
2386 * @param device The RAID device (e.g. /dev/md0) to use.
2387 * @ingroup restoreGuiMountlist
2388 */
2389void
2390initiate_new_raidlist_entry(struct raidlist_itself *raidlist,
2391 struct mountlist_itself *mountlist,
2392 int currline, char *device)
2393{
2394 /** structure *********************************************************/
2395 struct OSSWAP (raid_device_record, vinum_volume) * raidrec;
2396
2397 /** int ***************************************************************/
2398 int pos_in_raidlist = 0;
2399
2400 assert(raidlist != NULL);
2401 assert(mountlist != NULL);
2402 assert_string_is_neither_NULL_nor_zerolength(device);
2403
2404 pos_in_raidlist =
2405 find_raid_device_in_raidlist(raidlist,
2406 mountlist->el[currline].device);
2407 if (pos_in_raidlist >= 0) {
2408 fatal_error("Sorry, that RAID device already exists. Weird.");
2409 }
2410 pos_in_raidlist = raidlist->entries++;
2411 raidrec = &raidlist->el[pos_in_raidlist];
2412 initialize_raidrec(raidrec);
2413 strcpy(raidrec->OSSWAP(raid_device, volname),
2414 OSSWAP(device, basename(device)));
2415#ifndef __FreeBSD__
2416 choose_raid_level(raidrec);
2417 select_raid_disks(mountlist, raidlist, raidrec, "data",
2418 &raidrec->data_disks);
2419#endif
2420 edit_raidlist_entry(mountlist, raidlist, raidrec, currline);
2421}
2422
2423
2424#ifndef __FreeBSD__
2425/**
2426 * Insert the RAID variables not stored in the "additional RAID variables" list there too.
2427 * @param raidrec The RAID device record to operate on.
2428 * @ingroup restoreGuiVarslist
2429 */
2430void insert_essential_additionalvars(struct raid_device_record *raidrec)
2431{
2432
2433 /** int **************************************************************/
2434 int items = 0;
2435
2436 assert(raidrec != NULL);
2437
2438 items = raidrec->additional_vars.entries;
2439 write_variableINT_to_raid_var_line(raidrec, items++,
2440 "persistent-superblock",
2441 raidrec->persistent_superblock);
2442 write_variableINT_to_raid_var_line(raidrec, items++, "chunk-size",
2443 raidrec->chunk_size);
2444 raidrec->additional_vars.entries = items;
2445}
2446#endif
2447
2448
2449/**
2450 * Dummy function that proves that we can get to the point where Mondo is run.
2451 */
2452void nuke_mode_dummy()
2453{
2454
2455 /** newt *************************************************************/
2456 newtComponent myForm;
2457 newtComponent b1;
2458 newtComponent b2;
2459 newtComponent b3;
2460 newtComponent b_res;
2461
2462
2463 newtPushHelpLine
2464 (_
2465 ("This is where I nuke your hard drives. Mhahahahaha. No-one can stop Mojo Jojo!"));
2466 newtOpenWindow(24, 3, 32, 13, _("Nuking"));
2467 b1 = newtButton(7, 1, _("Slowly"));
2468 b2 = newtButton(7, 5, _("Medium"));
2469 b3 = newtButton(7, 9, _("Quickly"));
2470 myForm = newtForm(NULL, NULL, 0);
2471 newtFormAddComponents(myForm, b1, b2, b3, NULL);
2472 b_res = newtRunForm(myForm);
2473 newtFormDestroy(myForm);
2474 newtPopWindow();
2475 newtPopHelpLine();
2476}
2477
2478
2479/**
2480 * Redraw the disklist.
2481 * @param disklist The disklist to read from.
2482 * @param keylist The list of keys for @p listbox.
2483 * @param listbox The Newt listbox component to redraw.
2484 * @ingroup restoreGuiDisklist
2485 */
2486void
2487redraw_disklist(struct list_of_disks *disklist,
2488 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2489{
2490
2491 /** int *************************************************************/
2492 int i = 0;
2493
2494 assert(disklist != NULL);
2495 assert(keylist != NULL);
2496 assert(listbox != NULL);
2497
2498 newtListboxClear(listbox);
2499
2500 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2501 keylist[i] = (void *) i;
2502 }
2503 for (i = 0; i < disklist->entries; i++) {
2504 newtListboxAppendEntry(listbox,
2505 disklist_entry_to_string(disklist, i),
2506 keylist[i]);
2507 }
2508}
2509
2510
2511/**
2512 * Redraw the mountlist.
2513 * @param mountlist The mountlist to read from.
2514 * @param keylist The list of keys for @p listbox.
2515 * @param listbox The Newt listbox component to redraw.
2516 * @ingroup restoreGuiMountlist
2517 */
2518void
2519redraw_mountlist(struct mountlist_itself *mountlist,
2520 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2521{
2522
2523 /** int **************************************************************/
2524 int i = 0;
2525
2526 assert(mountlist != NULL);
2527 assert(keylist != NULL);
2528 assert(listbox != NULL);
2529
2530 newtListboxClear(listbox);
2531// sort_mountlist_by_device (mountlist);
2532 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2533 keylist[i] = (void *) i;
2534 }
2535 for (i = 0; i < mountlist->entries; i++) {
2536 newtListboxAppendEntry(listbox,
2537 mountlist_entry_to_string(mountlist, i),
2538 keylist[i]);
2539 }
2540}
2541
2542
2543/**
2544 * Redraw the list of unallocated RAID partitions.
2545 * @param unallocated_raid_partitions The mountlist containing unallocated RAID partitions.
2546 * @param keylist The list of keys for @p listbox.
2547 * @param listbox The Newt listbox component to redraw.
2548 * @ingroup restoreGuiDisklist
2549 */
2550void redraw_unallocpartnslist(struct mountlist_itself
2551 *unallocated_raid_partitions,
2552 void *keylist[ARBITRARY_MAXIMUM],
2553 newtComponent listbox)
2554{
2555
2556 /** int **************************************************************/
2557 int i = 0;
2558
2559 /** buffers **********************************************************/
2560 char *tmp = NULL;
2561
2562 assert(unallocated_raid_partitions != NULL);
2563 assert(keylist != NULL);
2564 assert(listbox != NULL);
2565
2566 newtListboxClear(listbox);
2567 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2568 keylist[i] = (void *) i;
2569 }
2570 for (i = 0; i < unallocated_raid_partitions->entries; i++) {
2571 asprintf(&tmp, "%-22s %8lld",
2572 unallocated_raid_partitions->el[i].device,
2573 unallocated_raid_partitions->el[i].size / 1024);
2574 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2575 paranoid_free(tmp);
2576 }
2577}
2578
2579
2580#ifndef __FreeBSD__
2581/**
2582 * Redraw the list of additional RAID variables.
2583 * @param additional_vars The list of additional RAID varibals.
2584 * @param keylist The list of keys for @p listbox.
2585 * @param listbox The Newt listbox component to redraw.
2586 * @ingroup restoreGuiVarslist
2587 */
2588void
2589redraw_varslist(struct additional_raid_variables *additional_vars,
2590 void *keylist[], newtComponent listbox)
2591{
2592 /** int *************************************************************/
2593 int i = 0;
2594
2595 /** buffers *********************************************************/
2596 char *tmp;
2597
2598 assert(additional_vars != NULL);
2599 assert(keylist != NULL);
2600 assert(listbox != NULL);
2601
2602 newtListboxClear(listbox);
2603
2604 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2605 keylist[i] = (void *) i;
2606 }
2607 for (i = 0; i < additional_vars->entries; i++) {
2608 asprintf(&tmp, "%-32s %-8s", additional_vars->el[i].label,
2609 additional_vars->el[i].value);
2610 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2611 paranoid_free(tmp);
2612 }
2613}
2614
2615
2616/**
2617 * Remove variable @p label from the RAID variables list in @p raidrec.
2618 * @param raidrec The RAID device record to remove the variable from.
2619 * @param label The variable name to remove.
2620 * @return The value of the variable removed.
2621 * @ingroup restoreUtilityGroup
2622 */
2623int read_variableINT_and_remove_from_raidvars(struct
2624 OSSWAP (raid_device_record,
2625 vinum_volume) *
2626 raidrec, char *label)
2627{
2628 /** int ***************************************************************/
2629 int i = 0;
2630 int res = 0;
2631
2632
2633 assert(raidrec != NULL);
2634 assert(label != NULL);
2635
2636 for (i = 0;
2637 i < raidrec->additional_vars.entries
2638 && strcmp(raidrec->additional_vars.el[i].label, label); i++);
2639 if (i == raidrec->additional_vars.entries) {
2640 res = -1;
2641 } else {
2642 res = atoi(raidrec->additional_vars.el[i].value);
2643 for (i++; i < raidrec->additional_vars.entries; i++) {
2644 memcpy((void *) &raidrec->additional_vars.el[i - 1],
2645 (void *) &raidrec->additional_vars.el[i],
2646 sizeof(struct raid_var_line));
2647 }
2648 raidrec->additional_vars.entries--;
2649 }
2650 return (res);
2651}
2652#endif
2653
2654
2655/**
2656 * Change all RAID devices to use @p new_dev instead of @p old_dev.
2657 * @param raidlist The raidlist to make the changes in.
2658 * @param old_dev The old name of the device (what it used to be).
2659 * @param new_dev The new name of the device (what it is now).
2660 * @ingroup restoreGuiMountlist
2661 */
2662void rejig_partition_name_in_raidlist_if_necessary(struct raidlist_itself
2663 *raidlist,
2664 char *old_dev,
2665 char *new_dev)
2666{
2667 /** buffers ********************************************************/
2668 char *tmp;
2669
2670 /** int ************************************************************/
2671 int pos = 0;
2672 int j = 0;
2673
2674 assert(raidlist != NULL);
2675 assert_string_is_neither_NULL_nor_zerolength(old_dev);
2676 assert_string_is_neither_NULL_nor_zerolength(new_dev);
2677
2678 pos = which_raid_device_is_using_this_partition(raidlist, old_dev);
2679 if (pos < 0) {
2680 asprintf(&tmp, "No need to rejig %s in raidlist: it's not listed.",
2681 old_dev);
2682 log_it(tmp);
2683 paranoid_free(tmp);
2684 } else {
2685 if ((j =
2686 where_in_drivelist_is_drive(&raidlist->
2687 OSSWAP(el[pos].data_disks, disks),
2688 old_dev)) >= 0) {
2689 strcpy(raidlist->OSSWAP(el[pos].data_disks, disks).el[j].
2690 device, new_dev);
2691 } else
2692 if ((j =
2693 where_in_drivelist_is_drive(&raidlist->
2694 OSSWAP(el[pos].spare_disks,
2695 spares),
2696 old_dev)) >= 0) {
2697 strcpy(raidlist->OSSWAP(el[pos].spare_disks, spares).el[j].
2698 device, new_dev);
2699 }
2700#ifndef __FreeBSD__
2701 else if ((j =
2702 where_in_drivelist_is_drive(&raidlist->el[pos].
2703 parity_disks,
2704 old_dev)) >= 0) {
2705 strcpy(raidlist->el[pos].parity_disks.el[j].device, new_dev);
2706 } else
2707 if ((j =
2708 where_in_drivelist_is_drive(&raidlist->el[pos].
2709 failed_disks,
2710 old_dev)) >= 0) {
2711 strcpy(raidlist->el[pos].failed_disks.el[j].device, new_dev);
2712 }
2713#endif
2714 else {
2715 asprintf(&tmp,
2716 "%s is supposed to be listed in this raid dev but it's not...",
2717 old_dev);
2718 log_it(tmp);
2719 paranoid_free(tmp);
2720 }
2721 }
2722}
2723
2724
2725#ifndef __FreeBSD__
2726/**
2727 * Remove the essential RAID variables from the "additional variables" section.
2728 * If they have been changed, set them in their normal locations too.
2729 * @param raidrec The RAID device record to operate on.
2730 * @ingroup restoreUtilityVarslist
2731 */
2732void remove_essential_additionalvars(struct raid_device_record *raidrec)
2733{
2734
2735 /** int **************************************************************/
2736 int res = 0;
2737
2738 assert(raidrec != NULL);
2739
2740 res =
2741 read_variableINT_and_remove_from_raidvars(raidrec,
2742 "persistent-superblock");
2743 if (res > 0) {
2744 raidrec->persistent_superblock = res;
2745 }
2746 res = read_variableINT_and_remove_from_raidvars(raidrec, "chunk-size");
2747 if (res > 0) {
2748 raidrec->chunk_size = res;
2749 }
2750 res = read_variableINT_and_remove_from_raidvars(raidrec, "block-size");
2751}
2752
2753
2754/**
2755 * Select the RAID disks to use in @p raidrec.
2756 * @param mountlist_dontedit The mountlist (will not be edited).
2757 * @param raidlist The raidlist to modify.
2758 * @param raidrec The RAID device record in @p raidlist to work on.
2759 * @param description_of_list The type of disks we're selecting (e.g. "data").
2760 * @param disklist The disklist to put the user-selected disks in.
2761 * @ingroup restoreGuiMountlist
2762 */
2763void
2764select_raid_disks(struct mountlist_itself *mountlist_dontedit,
2765 struct raidlist_itself *raidlist,
2766 struct raid_device_record *raidrec,
2767 char *description_of_list,
2768 struct list_of_disks *disklist)
2769{
2770 void *curr_choice = NULL;
2771
2772 /** structures ********************************************************/
2773 struct raidlist_itself *bkp_raidlist = NULL;
2774 struct raid_device_record *bkp_raidrec = NULL;
2775 struct list_of_disks *bkp_disklist = NULL;
2776 struct mountlist_itself *unallocated_raid_partitions = NULL;
2777
2778 /** newt **************************************************************/
2779 newtComponent myForm = NULL;
2780 newtComponent bAdd = NULL;
2781 newtComponent bDelete = NULL;
2782 newtComponent bOK = NULL;
2783 newtComponent bCancel = NULL;
2784 newtComponent b_res = NULL;
2785 newtComponent partitionsListbox = NULL;
2786 newtComponent headerMsg = NULL;
2787
2788 /** buffers **********************************************************/
2789 void *keylist[ARBITRARY_MAXIMUM];
2790 char *tmp = NULL;
2791 char *help_text = NULL;
2792 char *title_of_window = NULL;
2793 char *sz_res = NULL;
2794 char *header_text = NULL;
2795
2796 /** int **************************************************************/
2797 int i = 0;
2798 int currline = 0;
2799
2800 assert(mountlist_dontedit != NULL);
2801 assert(raidlist != NULL);
2802 assert(raidrec != NULL);
2803 assert(description_of_list != NULL);
2804 assert(disklist != NULL);
2805
2806 iamhere("malloc'ing");
2807 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
2808 fatal_error("Cannot malloc space for raidrec");
2809 }
2810 if (!(bkp_disklist = malloc(sizeof(struct list_of_disks)))) {
2811 fatal_error("Cannot malloc space for disklist");
2812 }
2813 if (!(bkp_raidlist = malloc(sizeof(struct raidlist_itself)))) {
2814 fatal_error("Cannot malloc space for raidlist");
2815 }
2816 if (!
2817 (unallocated_raid_partitions =
2818 malloc(sizeof(struct mountlist_itself)))) {
2819 fatal_error("Cannot malloc space for unallocated_raid_partitions");
2820 }
2821
2822 memcpy((void *) bkp_raidlist, (void *) raidlist,
2823 sizeof(struct raidlist_itself));
2824 memcpy((void *) bkp_raidrec, (void *) raidrec,
2825 sizeof(struct raid_device_record));
2826 memcpy((void *) bkp_disklist, (void *) disklist,
2827 sizeof(struct list_of_disks));
2828
2829 iamhere("Post-malloc");
2830 asprintf(&help_text,
2831 _
2832 (" Edit this RAID device's list of partitions. Choose OK or Cancel when done."));
2833 asprintf(&header_text, "%-24s %s", _("Device"), _("Index"));
2834 asprintf(&title_of_window, _("%s contains..."), raidrec->raid_device);
2835 newtPushHelpLine(help_text);
2836 paranoid_free(help_text);
2837 for (b_res = (newtComponent) 12345; b_res != bOK && b_res != bCancel;) {
2838 headerMsg = newtLabel(1, 1, header_text);
2839 partitionsListbox =
2840 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2841 redraw_disklist(disklist, keylist, partitionsListbox);
2842 i = 1;
2843 bAdd = newtCompactButton(i, 9, _(" Add "));
2844 bDelete = newtCompactButton(i += 8, 9, _("Delete"));
2845 bOK = newtCompactButton(i += 9, 9, _(" OK "));
2846 bCancel = newtCompactButton(i += 9, 9, _("Cancel"));
2847 newtOpenWindow(21, 7, 38, 10, title_of_window);
2848 myForm = newtForm(NULL, NULL, 0);
2849 if (disklist->entries == 0) {
2850 newtFormAddComponents(myForm, headerMsg, bAdd, bDelete, bOK,
2851 bCancel, NULL);
2852 } else {
2853 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2854 bAdd, bDelete, bOK, bCancel, NULL);
2855 }
2856 b_res = newtRunForm(myForm);
2857 if (b_res == bOK || b_res == bCancel) { /* do nothing */
2858// That's OK. At the end of this subroutine (after this do/while loop),
2859// we'll throw away the changes if Cancel was pushed.
2860 } else {
2861 curr_choice = newtListboxGetCurrent(partitionsListbox);
2862 for (i = 0; i < disklist->entries && keylist[i] != curr_choice;
2863 i++);
2864 if (i == disklist->entries && disklist->entries > 0) {
2865 log_to_screen(_("I don't know what that button does!"));
2866 } else {
2867 currline = i;
2868 if (b_res == bAdd) {
2869 log_it(_("Making list of unallocated RAID slices"));
2870 make_list_of_unallocated_raid_partitions
2871 (unallocated_raid_partitions, mountlist_dontedit,
2872 raidlist);
2873 if (unallocated_raid_partitions->entries <= 0) {
2874 popup_and_OK
2875 (_
2876 ("There are no unallocated partitions marked for RAID."));
2877 } else {
2878 log_it
2879 (_
2880 ("Done. The user may add one or more of the above to RAID device"));
2881 add_disklist_entry(disklist, raidrec->raid_device,
2882 unallocated_raid_partitions);
2883 log_it(_
2884 ("I have finished adding a disklist entry."));
2885 redraw_disklist(disklist, keylist,
2886 partitionsListbox);
2887 }
2888 } else if (b_res == bDelete) {
2889 delete_disklist_entry(disklist, raidrec->raid_device,
2890 currline);
2891 redraw_disklist(disklist, keylist, partitionsListbox);
2892 } else {
2893 asprintf(&tmp, _("%s's index is %d. What should it be?"),
2894 raidrec->raid_device,
2895 disklist->el[currline].index);
2896 asprintf(&sz_res, "%d", disklist->el[currline].index);
2897 if (popup_and_get_string
2898 (_("Set index"), tmp, sz_res)) {
2899 disklist->el[currline].index = atoi(sz_res);
2900 }
2901 paranoid_free(tmp);
2902 paranoid_free(sz_res);
2903 redraw_disklist(disklist, keylist, partitionsListbox);
2904 }
2905 }
2906 }
2907 newtFormDestroy(myForm);
2908 newtPopWindow();
2909 }
2910 paranoid_free(title_of_window);
2911 paranoid_free(header_text);
2912
2913 newtPopHelpLine();
2914 if (b_res == bCancel) {
2915 memcpy((void *) raidlist, (void *) bkp_raidlist,
2916 sizeof(struct raidlist_itself));
2917 memcpy((void *) raidrec, (void *) bkp_raidrec,
2918 sizeof(struct raid_device_record));
2919 memcpy((void *) disklist, (void *) bkp_disklist,
2920 sizeof(struct list_of_disks));
2921 }
2922 paranoid_free(bkp_raidrec);
2923 paranoid_free(bkp_disklist);
2924 paranoid_free(bkp_raidlist);
2925 paranoid_free(unallocated_raid_partitions);
2926}
2927#endif
2928
2929
2930/**
2931 * Ask the user which restore mode (nuke, interactive, or compare) we should use.
2932 * @return The mode selected: 'I' for interactive, 'N' for nuke, 'C' for compare,
2933 * or 'E' (or any other letter) for exit.
2934 */
2935char which_restore_mode()
2936{
2937
2938 /** char *************************************************************/
2939 char output = '\0';
2940 char *tmp = NULL;
2941 size_t n = 0;
2942
2943 /** newt *************************************************************/
2944
2945 newtComponent b1;
2946 newtComponent b2;
2947 newtComponent b3;
2948 newtComponent b4;
2949 newtComponent b_res;
2950 newtComponent myForm;
2951
2952 if (g_text_mode) {
2953 for (output = 'z'; !strchr("AICE", output); output = tmp[0]) {
2954 printf
2955 (_
2956 ("Which mode - (A)utomatic, (I)nteractive, \n(C)ompare only, or (E)xit to shell?\n--> "));
2957 getline(&tmp, &n, stdin);
2958 }
2959 paranoid_free(tmp);
2960 return (output);
2961 }
2962
2963 newtPushHelpLine
2964 (_
2965 (" Do you want to 'nuke' your system, restore interactively, or just compare?"));
2966 newtOpenWindow(24, 3, 32, 17, _("How should I restore?"));
2967 b1 = newtButton(7, 1, _("Automatically"));
2968 b2 = newtButton(7, 5, _("Interactively"));
2969 b3 = newtButton(7, 9, _("Compare only!"));
2970 b4 = newtButton(7, 13, _("Exit to shell"));
2971 myForm = newtForm(NULL, NULL, 0);
2972 newtFormAddComponents(myForm, b1, b2, b3, b4, NULL);
2973 b_res = newtRunForm(myForm);
2974 newtFormDestroy(myForm);
2975 newtPopWindow();
2976 if (b_res == b1) {
2977 output = 'N';
2978 }
2979 if (b_res == b2) {
2980 output = 'I';
2981 }
2982 if (b_res == b3) {
2983 output = 'C';
2984 }
2985 if (b_res == b4) {
2986 output = 'E';
2987 }
2988 newtPopHelpLine();
2989 return (output);
2990}
Note: See TracBrowser for help on using the repository browser.