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

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

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

  • Property svn:keywords set to Id
File size: 87.2 KB
Line 
1/***************************************************************************
2 * $Id: mondo-rstr-newt.c 688 2006-07-17 13:44:46Z bcornec $
3**/
4
5
6/**
7 * @file
8 * Functions for handling GUI interfaces in the restore.
9 */
10
11#ifdef __FreeBSD__
12#define OSSWAP(linux,fbsd) fbsd
13#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 (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1355 || !strcmp(mountlist->el[currline].mountpoint, "image")) {
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 paranoid_free(size_str);
1363
1364 newtListboxSetEntry(listbox, (int) keylist[currline],
1365 mountlist_entry_to_string(mountlist, currline));
1366 /* if new /dev/md RAID device then do funky stuff */
1367 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
1368 && !strstr(device_used_to_be, RAID_DEVICE_STUB)) {
1369 initiate_new_raidlist_entry(raidlist, mountlist, currline,
1370 device_str);
1371 }
1372 /* if moving from RAID to non-RAID then do funky stuff */
1373 else if (strstr(device_used_to_be, RAID_DEVICE_STUB)
1374 && !strstr(device_str, RAID_DEVICE_STUB)) {
1375 delete_raidlist_entry(mountlist, raidlist, device_str);
1376 }
1377 /* if moving a non-RAID to another non-RAID then re-jig any RAID disks, if necessary */
1378 else if (!strstr(device_used_to_be, RAID_DEVICE_STUB)
1379 && !strstr(device_str, RAID_DEVICE_STUB)) {
1380 rejig_partition_name_in_raidlist_if_necessary(raidlist,
1381 device_used_to_be,
1382 device_str);
1383 }
1384/* else, moving a RAID to another RAID; bad idea, or so I thought */
1385#ifndef __FreeBSD__ /* It works fine under FBSD. */
1386 else if (strcmp(device_used_to_be, device_str)) {
1387 popup_and_OK
1388 (_
1389 ("You are renaming a RAID device as another RAID device. I don't like it but I'll allow it."));
1390 }
1391#endif
1392 redraw_mountlist(mountlist, keylist, listbox);
1393 paranoid_free(device_str);
1394 paranoid_free(device_used_to_be);
1395}
1396
1397
1398#if __FreeBSD__
1399/**
1400 * Add a subdisk to @p raidrec.
1401 * @param raidlist The raidlist containing information about RAID partitions.
1402 * @param raidrec The RAID device record to add the subdisk to.
1403 * @param temp The device name of the RAID disk to add it to.
1404 * @author Joshua Oreman
1405 * @ingroup restoreGuiMountlist
1406 */
1407void
1408add_raid_subdisk(struct raidlist_itself *raidlist,
1409 struct vinum_plex *raidrec, char *temp)
1410{
1411 int i;
1412 bool found = FALSE;
1413
1414 for (i = 0; i < raidlist->disks.entries; ++i) {
1415 if (!strcmp(raidlist->disks.el[i].device, temp)) {
1416 strcpy(raidrec->sd[raidrec->subdisks].which_device,
1417 raidlist->disks.el[i].name);
1418 found = TRUE;
1419 }
1420 }
1421 if (!found) {
1422 sprintf(raidlist->disks.el[raidlist->disks.entries].name,
1423 "drive%i", raidlist->disks.entries);
1424 sprintf(raidrec->sd[raidrec->subdisks].which_device, "drive%i",
1425 raidlist->disks.entries);
1426 strcpy(raidlist->disks.el[raidlist->disks.entries++].device, temp);
1427 }
1428 raidrec->subdisks++;
1429}
1430
1431
1432/**
1433 * Determine the /dev entry for @p vinum_name.
1434 * @param raidlist The raidlist containing information about RAID devices.
1435 * @param vinum_name The name of the Vinum drive to map to a /dev entry.
1436 * @return The /dev entry, or NULL if none was found.
1437 * @note The returned string points to static storage that will be overwritten with each call.
1438 * @author Joshua Oreman
1439 * @ingroup restoreUtilityGroup
1440 */
1441char *find_dev_entry_for_raid_device_name(struct raidlist_itself *raidlist,
1442 char *vinum_name)
1443{
1444 int i;
1445 for (i = 0; i < raidlist->disks.entries; ++i) {
1446 if (!strcmp(raidlist->disks.el[i].name, vinum_name)) {
1447 return raidlist->disks.el[i].device;
1448 }
1449 }
1450 return NULL;
1451}
1452
1453
1454void
1455edit_raidlist_plex(struct mountlist_itself *mountlist,
1456 struct raidlist_itself *raidlist,
1457 struct vinum_plex *raidrec, int currline,
1458 int currline2);
1459
1460#endif
1461
1462
1463/**
1464 * Edit the entry for @p raidrec in @p raidlist.
1465 * @param mountlist The mountlist to get some information from.
1466 * @param raidlist The raidlist containing information about RAID devices.
1467 * @param raidrec The RAID device record for this partition.
1468 * @param currline The line number (starting from 0) in the mountlist of the RAID device.
1469 * @ingroup restoreGuiMountlist
1470 */
1471void
1472edit_raidlist_entry(struct mountlist_itself *mountlist,
1473 struct raidlist_itself *raidlist,
1474 struct OSSWAP (raid_device_record,
1475 vinum_volume) * raidrec, int currline)
1476{
1477
1478#ifdef __FreeBSD__
1479 /** structures ********************************************************/
1480 struct vinum_volume bkp_raidrec;
1481
1482
1483 /** buffers ***********************************************************/
1484 char *title_of_editraidForm_window;
1485
1486 /** newt **************************************************************/
1487 newtComponent editraidForm;
1488 newtComponent bOK;
1489 newtComponent bCancel;
1490 newtComponent bEdit;
1491 newtComponent bAdd;
1492 newtComponent bDelete;
1493 newtComponent b_res;
1494 newtComponent plexesListbox;
1495 newtComponent plexesHeader;
1496
1497 void *keylist[10];
1498 void *curr_choice;
1499
1500 int currline2 = 0;
1501 char *pname = NULL;
1502 char *raidlevel = NULL;
1503 char *chunksize = NULL;
1504 char *entry = NULL;
1505 char *msg = NULL;
1506 int i = 0;
1507 char *headerstr = NULL;
1508
1509 log_it(_("Started edit_raidlist_entry"));
1510 memcpy((void *) &bkp_raidrec, (void *) raidrec,
1511 sizeof(struct vinum_volume));
1512 asprintf(&title_of_editraidForm_window, _("Plexes on %s"),
1513 raidrec->volname);
1514 newtPushHelpLine(_(" Please select a plex to edit"));
1515 newtOpenWindow(13, 5, 54, 15, title_of_editraidForm_window);
1516 paranoid_free(title_of_editraidForm_window);
1517
1518 for (;;) {
1519 asprintf(&headerstr, "%-14s %-8s %11s %8s", _("Plex"), _("Level",) _("Stripe Size"), _("Subdisks"));
1520
1521 bOK = newtCompactButton(2, 13, _(" OK "));
1522 bCancel = newtCompactButton(12, 13, _("Cancel"));
1523 bAdd = newtCompactButton(22, 13, _(" Add "));
1524 bEdit = newtCompactButton(32, 13, _(" Edit "));
1525 bDelete = newtCompactButton(42, 13, _("Delete"));
1526
1527 plexesListbox =
1528 newtListbox(2, 3, 9, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1529 plexesHeader = newtLabel(2, 2, headerstr);
1530 paranoid_free(headerstr);
1531
1532 editraidForm = newtForm(NULL, NULL, 0);
1533
1534 newtListboxClear(plexesListbox);
1535 for (i = 0; i < 10; ++i) {
1536 keylist[i] = (void *) i;
1537 if (i < raidrec->plexes) {
1538 switch (raidrec->plex[i].raidlevel) {
1539 case -1:
1540 asprintf(&raidlevel, "concat");
1541 break;
1542 case 0:
1543 asprintf(&raidlevel, "striped");
1544 break;
1545 case 5:
1546 asprintf(&raidlevel, "raid5");
1547 break;
1548 default:
1549 asprintf(&raidlevel, "raid%i",
1550 raidrec->plex[i].raidlevel);
1551 break;
1552 }
1553
1554 if (raidrec->plex[i].raidlevel == -1) {
1555 asprintf(&chunksize, "N/A");
1556 } else {
1557 asprintf(&chunksize, "%dk", raidrec->plex[i].stripesize);
1558 }
1559 asprintf(&pname, "%s.p%i", raidrec->volname, i);
1560 asprintf(&entry, "%-14s %-8s %11s %8d",
1561 pname, raidlevel, chunksize,
1562 raidrec->plex[i].subdisks);
1563 paranoid_free(pname);
1564 paranoid_free(chunksize);
1565 paranoid_free(raidlevel);
1566 newtListboxAppendEntry(plexesListbox, entry, keylist[i]);
1567 paranoid_free(entry);
1568 }
1569 }
1570
1571 newtFormAddComponents(editraidForm, bOK, bCancel, bAdd, bEdit,
1572 bDelete, plexesListbox, plexesHeader, NULL);
1573
1574 b_res = newtRunForm(editraidForm);
1575 if (b_res == bOK || b_res == bCancel) {
1576 break;
1577 }
1578
1579 curr_choice = newtListboxGetCurrent(plexesListbox);
1580 for (currline2 = 0; currline2 < raidrec->plexes; ++currline2) {
1581 if (currline2 > 9)
1582 break;
1583 if (keylist[currline2] == curr_choice)
1584 break;
1585 }
1586
1587 if (b_res == bDelete) {
1588 asprintf(&msg, _("Are you sure you want to delete %s.p%i?"),
1589 raidrec->volname, currline2);
1590 if (ask_me_yes_or_no(msg)) {
1591 log_it(_("Deleting RAID plex"));
1592 memcpy((void *) &raidrec->plex[currline2],
1593 (void *) &raidrec->plex[raidrec->plexes - 1],
1594 sizeof(struct vinum_plex));
1595 raidrec->plexes--;
1596 }
1597 paranoid_free(msg);
1598 continue;
1599 }
1600 if (b_res == bAdd) {
1601 raidrec->plex[raidrec->plexes].raidlevel = 0;
1602 raidrec->plex[raidrec->plexes].stripesize = 279;
1603 raidrec->plex[raidrec->plexes].subdisks = 0;
1604 currline2 = raidrec->plexes++;
1605 }
1606 edit_raidlist_plex(mountlist, raidlist, &raidrec->plex[currline2],
1607 currline, currline2);
1608 newtFormDestroy(editraidForm);
1609 }
1610 if (b_res == bCancel) {
1611 memcpy((void *) raidrec, (void *) &bkp_raidrec,
1612 sizeof(struct vinum_volume));
1613 }
1614 newtPopHelpLine();
1615 newtPopWindow();
1616 mountlist->el[currline].size =
1617 calculate_raid_device_size(mountlist, raidlist, raidrec->volname);
1618#else
1619 /** structures ********************************************************/
1620 struct raid_device_record *bkp_raidrec = NULL;
1621
1622
1623 /** buffers ***********************************************************/
1624 char *title_of_editraidForm_window = NULL;
1625 char *sz_raid_level = NULL;
1626 char *sz_data_disks = NULL;
1627 char *sz_spare_disks = NULL;
1628 char *sz_parity_disks = NULL;
1629 char *sz_failed_disks = NULL;
1630
1631 /** newt **************************************************************/
1632 newtComponent editraidForm;
1633 newtComponent bOK;
1634 newtComponent bCancel;
1635 newtComponent bAdditional;
1636 newtComponent bChangeRaid;
1637 newtComponent bSelectData;
1638 newtComponent bSelectSpare;
1639 newtComponent bSelectParity;
1640 newtComponent bSelectFailed;
1641 newtComponent b_res;
1642
1643 assert(mountlist != NULL);
1644 assert(raidlist != NULL);
1645 assert(raidrec != NULL);
1646
1647 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
1648 fatal_error("Cannot malloc space for raidrec");
1649 }
1650
1651 log_it("Started edit_raidlist_entry");
1652
1653 memcpy((void *) bkp_raidrec, (void *) raidrec,
1654 sizeof(struct raid_device_record));
1655 asprintf(&title_of_editraidForm_window, _("Edit %s"), raidrec->raid_device);
1656 log_msg(2, "Opening newt window");
1657 newtOpenWindow(20, 5, 40, 14, title_of_editraidForm_window);
1658 paranoid_free(title_of_editraidForm_window);
1659
1660 for (;;) {
1661 log_msg(2, "Main loop");
1662 asprintf(&sz_raid_level,
1663 turn_raid_level_number_to_string(raidrec->raid_level));
1664 asprintf(&sz_data_disks,
1665 number_of_disks_as_string(raidrec->data_disks.entries,
1666 _("data")));
1667 asprintf(&sz_spare_disks,
1668 number_of_disks_as_string(raidrec->spare_disks.entries,
1669 _("spare")));
1670 asprintf(&sz_parity_disks,
1671 number_of_disks_as_string(raidrec->parity_disks.entries,
1672 _("parity")));
1673 asprintf(&sz_failed_disks,
1674 number_of_disks_as_string(raidrec->failed_disks.entries,
1675 _("failed")));
1676 bSelectData = newtButton(1, 1, sz_data_disks);
1677 bSelectSpare = newtButton(20, 1, sz_spare_disks);
1678 bSelectParity = newtButton(1, 5, sz_parity_disks);
1679 bSelectFailed = newtButton(20, 5, sz_failed_disks);
1680 bChangeRaid = newtButton(1, 9, sz_raid_level);
1681 paranoid_free(sz_raid_level);
1682 paranoid_free(sz_data_disks);
1683 paranoid_free(sz_spare_disks);
1684 paranoid_free(sz_parity_disks);
1685 paranoid_free(sz_failed_disks);
1686
1687 bOK = newtButton(16 + (raidrec->raid_level == -1), 9, _(" OK "));
1688 bCancel = newtButton(28, 9, _("Cancel"));
1689 bAdditional =
1690 newtCompactButton(1, 13,
1691 _("Additional settings and information"));
1692 newtPushHelpLine
1693 (_
1694 (" Edit the RAID device's settings to your heart's content, then hit OK/Cancel."));
1695 editraidForm = newtForm(NULL, NULL, 0);
1696 newtFormAddComponents(editraidForm, bSelectData, bSelectParity,
1697 bChangeRaid, bSelectSpare, bSelectFailed,
1698 bOK, bCancel, bAdditional);
1699 b_res = newtRunForm(editraidForm);
1700 if (b_res == bChangeRaid) {
1701 choose_raid_level(raidrec);
1702 } else if (b_res == bSelectData) {
1703 select_raid_disks(mountlist, raidlist, raidrec, _("data"),
1704 &raidrec->data_disks);
1705 } else if (b_res == bSelectSpare) {
1706 select_raid_disks(mountlist, raidlist, raidrec, _("spare"),
1707 &raidrec->spare_disks);
1708 } else if (b_res == bSelectParity) {
1709 select_raid_disks(mountlist, raidlist, raidrec, _("parity"),
1710 &raidrec->parity_disks);
1711 } else if (b_res == bSelectFailed) {
1712 select_raid_disks(mountlist, raidlist, raidrec, _("failed"),
1713 &raidrec->failed_disks);
1714 } else if (b_res == bAdditional) {
1715 edit_raidrec_additional_vars(raidrec);
1716 }
1717 newtFormDestroy(editraidForm);
1718 if (b_res == bOK || b_res == bCancel) {
1719 break;
1720 }
1721 }
1722 if (b_res == bCancel) {
1723 memcpy((void *) raidrec, (void *) bkp_raidrec,
1724 sizeof(struct raid_device_record));
1725 }
1726 newtPopHelpLine();
1727 newtPopWindow();
1728 mountlist->el[currline].size =
1729 calculate_raid_device_size(mountlist, raidlist,
1730 raidrec->raid_device);
1731 paranoid_free(bkp_raidrec);
1732#endif
1733}
1734#ifdef __FreeBSD__
1735
1736
1737/**
1738 * Edit the plex @p raidrec in @p raidlist.
1739 * @param mountlist The mountlist to get some of the information from.
1740 * @param raidlist The raidlist containing information about RAID devices.
1741 * @param raidrec The plex to edit.
1742 * @param currline The line number (starting from 0) of the RAID device in @p mountlist.
1743 * @param currline2 The line number (starting from 0) of the plex within the RAID device.
1744 * @author Joshua Oreman
1745 * @ingroup restoreGuiMountlist
1746 */
1747void
1748edit_raidlist_plex(struct mountlist_itself *mountlist,
1749 struct raidlist_itself *raidlist,
1750 struct vinum_plex *raidrec, int currline, int currline2)
1751{
1752
1753 /** structures ********************************************************/
1754 struct vinum_plex bkp_raidrec;
1755
1756
1757 /** buffers ***********************************************************/
1758 char *title_of_editraidForm_window = NULL;
1759 char *tmp = NULL;
1760 char *entry = NULL;
1761
1762 /** newt **************************************************************/
1763 newtComponent editraidForm;
1764 newtComponent bOK;
1765 newtComponent bCancel;
1766 newtComponent bEdit;
1767 newtComponent bAdd;
1768 newtComponent bDelete;
1769 newtComponent b_res;
1770 newtComponent unallocListbox, allocListbox;
1771 newtComponent bLevel, sLevel;
1772 newtComponent bStripeSize, sStripeSize;
1773 newtComponent bAlloc, bUnalloc;
1774
1775 void *keylist[ARBITRARY_MAXIMUM];
1776 void *curr_choice_a, *curr_choice_u;
1777 int currline_a, currline_u;
1778 int i;
1779
1780 struct mountlist_itself *unallocparts = NULL;
1781
1782 /* BERLIOS: Check return value */
1783 unallocparts = malloc(sizeof(struct mountlist_itself));
1784
1785 log_it("Started edit_raidlist_entry");
1786 memcpy((void *) &bkp_raidrec, (void *) raidrec,
1787 sizeof(struct vinum_plex));
1788 asprintf(&title_of_editraidForm_window, "%s.p%i",
1789 raidlist->el[currline].volname, currline2);
1790 newtPushHelpLine
1791 (_
1792 (" Please select a subdisk to edit, or edit this plex's parameters"));
1793 newtOpenWindow(13, 3, 54, 18, title_of_editraidForm_window);
1794 paranoid_free(title_of_editraidForm_window);
1795
1796 for (;;) {
1797 switch (raidrec->raidlevel) {
1798 case -1:
1799 asprintf(&tmp, "concat");
1800 break;
1801 case 0:
1802 asprintf(&tmp, "striped");
1803 break;
1804 case 5:
1805 asprintf(&tmp, "raid5");
1806 break;
1807 default:
1808 asprintf(&tmp, _("unknown (%i)"), raidrec->raidlevel);
1809 break;
1810 }
1811 bLevel = newtCompactButton(2, 2, _(" RAID level "));
1812 sLevel = newtLabel(19, 2, tmp);
1813 paranoid_free(tmp);
1814
1815 if (raidrec->raidlevel >= 0) {
1816 asprintf(&tmp, "%ik", raidrec->stripesize);
1817 bStripeSize = newtCompactButton(2, 4, _(" Stripe size "));
1818 } else {
1819 asprintf(&tmp, "N/A");
1820 bStripeSize = newtLabel(2, 4, _("Stripe size:"));
1821 }
1822 sStripeSize = newtLabel(19, 4, tmp);
1823 paranoid_free(tmp);
1824
1825 bOK = newtCompactButton(2, 16, _(" OK "));
1826 bCancel = newtCompactButton(12, 16, _("Cancel"));
1827 bAdd = newtCompactButton(22, 16, _(" Add "));
1828 bEdit = newtCompactButton(32, 16, _(" Edit "));
1829 bDelete = newtCompactButton(42, 16, _("Delete"));
1830
1831
1832 // plexesListbox = newtListbox (2, 7, 9, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1833 unallocListbox =
1834 newtListbox(2, 7, 7, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1835 allocListbox =
1836 newtListbox(33, 7, 7, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1837 bAlloc = newtButton(23, 7, " -> ");
1838 bUnalloc = newtButton(23, 11, " <- ");
1839
1840 editraidForm = newtForm(NULL, NULL, 0);
1841
1842 newtListboxClear(allocListbox);
1843 newtListboxClear(unallocListbox);
1844 bzero(unallocparts, sizeof(struct mountlist_itself));
1845 make_list_of_unallocated_raid_partitions(unallocparts, mountlist,
1846 raidlist);
1847 for (i = 0; i < ARBITRARY_MAXIMUM; ++i) {
1848 keylist[i] = (void *) i;
1849 if (i < raidrec->subdisks) {
1850 asprintf(&entry, "%-17s",
1851 find_dev_entry_for_raid_device_name(raidlist,
1852 raidrec->
1853 sd[i].
1854 which_device));
1855 newtListboxAppendEntry(allocListbox, entry, keylist[i]);
1856 paranoid_free(entry);
1857 }
1858 if (i < unallocparts->entries) {
1859 asprintf(&entry, "%-17s", unallocparts->el[i].device);
1860 newtListboxAppendEntry(unallocListbox, entry, keylist[i]);
1861 paranoid_free(entry);
1862 }
1863 }
1864
1865#define COMP(x) newtFormAddComponent (editraidForm, x)
1866#define UCOMP(x) if (unallocparts->entries > 0) COMP(x)
1867#define ACOMP(x) if (raidrec->subdisks > 0) COMP(x)
1868 editraidForm = newtForm(NULL, NULL, 0);
1869 UCOMP(unallocListbox);
1870 UCOMP(bAlloc);
1871 ACOMP(allocListbox);
1872 ACOMP(bUnalloc);
1873 COMP(bOK);
1874 COMP(bCancel);
1875 COMP(bLevel);
1876 COMP(sLevel);
1877 if (raidrec->raidlevel != -1) {
1878 COMP(bStripeSize);
1879 COMP(sStripeSize);
1880 }
1881#undef COMP
1882#undef UCOMP
1883#undef ACOMP
1884
1885 newtRefresh();
1886 b_res = newtRunForm(editraidForm);
1887 if (b_res == bOK || b_res == bCancel) {
1888 break;
1889 }
1890
1891 curr_choice_a = (raidrec->subdisks > 0) ?
1892 newtListboxGetCurrent(allocListbox) : (void *) 1234;
1893 curr_choice_u = (unallocparts->entries > 0) ?
1894 newtListboxGetCurrent(unallocListbox) : (void *) 1234;
1895 for (currline_a = 0; currline_a < raidrec->subdisks; ++currline_a) {
1896 if (currline_a > ARBITRARY_MAXIMUM)
1897 break;
1898 if (keylist[currline_a] == curr_choice_a)
1899 break;
1900 }
1901 for (currline_u = 0; currline_u < unallocparts->entries;
1902 ++currline_u) {
1903 if (currline_u > ARBITRARY_MAXIMUM)
1904 break;
1905 if (keylist[currline_u] == curr_choice_u)
1906 break;
1907 }
1908 if (b_res == bLevel) {
1909 choose_raid_level(raidrec);
1910 } else if (b_res == bStripeSize) {
1911 asprintf(&tmp, "%i", raidrec->stripesize);
1912 if (popup_and_get_string
1913 (_("Stripe size"),
1914 _("Please enter the stripe size in kilobytes."), tmp)) {
1915 raidrec->stripesize = atoi(tmp);
1916 }
1917 paranoid_free(tmp);
1918 } else if ((b_res == bAlloc) || (b_res == unallocListbox)) {
1919 if (currline_u <= unallocparts->entries)
1920 add_raid_subdisk(raidlist, raidrec,
1921 unallocparts->el[currline_u].device);
1922 } else if ((b_res == bUnalloc) || (b_res == allocListbox)) {
1923 if (currline_a <= raidrec->subdisks) {
1924 memcpy((void *) &raidrec->sd[currline_a],
1925 (void *) &raidrec->sd[raidrec->subdisks - 1],
1926 sizeof(struct vinum_subdisk));
1927 raidrec->subdisks--;
1928 }
1929 }
1930#if 0
1931 } else {
1932 edit_raid_subdisk(raidlist, raidrec, &raidrec->sd[currline3],
1933 currline3);
1934 }
1935#endif
1936 newtFormDestroy(editraidForm);
1937 newtRefresh();
1938}
1939
1940if (b_res == bCancel) {
1941 memcpy((void *) raidrec, (void *) &bkp_raidrec,
1942 sizeof(struct vinum_plex));
1943}
1944newtPopWindow();
1945newtPopHelpLine();
1946}
1947#else
1948/**
1949 * Edit additional RAID variable number @p lino.
1950 * @param raidrec The RAID device record to edit the variable in.
1951 * @param lino The line number (starting from 0) of the variable to edit.
1952 * @ingroup restoreGuiVarslist
1953 */
1954void edit_varslist_entry(struct raid_device_record *raidrec, int lino)
1955{
1956
1957 /** buffers ***********************************************************/
1958 char *header = NULL;
1959 char *comment = NULL;
1960 char *sz_out = NULL;
1961
1962 assert(raidrec != 0);
1963 assert(lino >= 0);
1964
1965 asprintf(&sz_out, raidrec->additional_vars.el[lino].value);
1966 asprintf(&header, _("Edit %s"), raidrec->additional_vars.el[lino].label);
1967 asprintf(&comment, _("Please set %s's value (currently '%s')"),
1968 raidrec->additional_vars.el[lino].label, sz_out);
1969 if (popup_and_get_string(header, comment, sz_out)) {
1970 strcpy(raidrec->additional_vars.el[lino].value, sz_out);
1971 }
1972 paranoid_free(header);
1973 paranoid_free(comment);
1974 paranoid_free(sz_out);
1975}
1976#endif
1977
1978
1979/**
1980 * Edit the mountlist using Newt.
1981 * @param mountlist The mountlist to edit.
1982 * @param raidlist The raidlist that goes with @p mountlist.
1983 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
1984 */
1985int
1986edit_mountlist_in_newt(char *mountlist_fname,
1987 struct mountlist_itself *mountlist,
1988 struct raidlist_itself *raidlist)
1989{
1990
1991 /** newt **************************************************************/
1992 newtComponent myForm;
1993 newtComponent bAdd;
1994 newtComponent bEdit;
1995 newtComponent bDelete;
1996 newtComponent bOK;
1997 newtComponent bCancel;
1998 newtComponent b_res = NULL;
1999 newtComponent partitionsListbox;
2000 newtComponent headerMsg;
2001 newtComponent flawsLabelA;
2002 newtComponent flawsLabelB;
2003 newtComponent flawsLabelC;
2004 newtComponent bReload;
2005
2006 /** ???? *************************************************************/
2007 void *curr_choice = NULL;
2008 void *keylist[ARBITRARY_MAXIMUM];
2009
2010 /** int **************************************************************/
2011 int i = 0;
2012 int currline = 0;
2013 int finished = FALSE;
2014
2015 /** buffers **********************************************************/
2016 char *tmp = NULL;
2017 char *flaws_str_A = NULL;
2018 char *flaws_str_B = NULL;
2019 char *flaws_str_C = NULL;
2020
2021 assert(mountlist != NULL);
2022 assert(raidlist != NULL);
2023
2024 asprintf(&flaws_str_A, "xxxxxxxxx");
2025 asprintf(&flaws_str_B, "xxxxxxxxx");
2026 asprintf(&flaws_str_C, "xxxxxxxxx");
2027 if (mountlist->entries > ARBITRARY_MAXIMUM) {
2028 log_to_screen(_("Arbitrary limits suck, man!"));
2029 finish(1);
2030 }
2031 newtPushHelpLine
2032 (_
2033 (" Please edit the mountlist to your satisfaction, then click OK or Cancel."));
2034 i = 4;
2035 bAdd = newtCompactButton(i, 17, _(" Add "));
2036 bEdit = newtCompactButton(i += 11, 17, _(" Edit "));
2037 bDelete = newtCompactButton(i += 12, 17, _("Delete"));
2038 bReload = newtCompactButton(i += 12, 17, _("Reload"));
2039 bCancel = newtCompactButton(i += 12, 17, _("Cancel"));
2040 bOK = newtCompactButton(i += 12, 17, _(" OK "));
2041 asprintf(&tmp, "%-24s %-24s %-8s %s", _("Device"), _("Mountpoint"),
2042 _("Format"), _("Size (MB)"));
2043 headerMsg = newtLabel(2, 1, tmp);
2044 flawsLabelA = newtLabel(2, 13, flaws_str_A);
2045 flawsLabelB = newtLabel(2, 14, flaws_str_B);
2046 flawsLabelC = newtLabel(2, 15, flaws_str_C);
2047 partitionsListbox =
2048 newtListbox(2, 2, 10, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2049 redraw_mountlist(mountlist, keylist, partitionsListbox);
2050 newtOpenWindow(1, 3, 77, 18, _("Editing mountlist"));
2051 myForm = newtForm(NULL, NULL, 0);
2052 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2053 flawsLabelA, flawsLabelB, flawsLabelC, bAdd,
2054 bEdit, bDelete, bReload, bCancel, bOK, NULL);
2055 while (!finished) {
2056 evaluate_mountlist(mountlist, flaws_str_A, flaws_str_B,
2057 flaws_str_C);
2058 newtLabelSetText(flawsLabelA, flaws_str_A);
2059 newtLabelSetText(flawsLabelB, flaws_str_B);
2060 newtLabelSetText(flawsLabelC, flaws_str_C);
2061 b_res = newtRunForm(myForm);
2062 /* BERLIOS: This needs to be re-written */
2063 if (b_res == bOK) {
2064 if (!evaluate_mountlist
2065 (mountlist, flaws_str_A, flaws_str_B, flaws_str_C)) {
2066 finished =
2067 ask_me_yes_or_no
2068 (_("Your mountlist might not work. Continue anyway?"));
2069 } else {
2070 finished =
2071 ask_me_yes_or_no
2072 (_
2073 ("Are you sure you want to save your mountlist and continue? (No changes will be made to your partition table at this time.)"));
2074 }
2075 } else if (b_res == bCancel) {
2076 finished = TRUE;
2077 } else if (b_res == bReload) {
2078 if (ask_me_yes_or_no(_("Reload original mountlist?"))) {
2079 load_mountlist(mountlist, mountlist_fname);
2080 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2081 redraw_mountlist(mountlist, keylist, partitionsListbox);
2082 }
2083 } else {
2084 curr_choice = newtListboxGetCurrent(partitionsListbox);
2085 for (i = 0;
2086 i < mountlist->entries && keylist[i] != curr_choice; i++);
2087 if (i == mountlist->entries && mountlist->entries > 0) {
2088 log_to_screen(_("I don't know what that button does!"));
2089 } else {
2090 currline = i;
2091 if (b_res == bAdd) {
2092 add_mountlist_entry(mountlist, raidlist,
2093 partitionsListbox, currline,
2094 keylist);
2095 } else if (b_res == bDelete) {
2096 delete_mountlist_entry(mountlist, raidlist,
2097 partitionsListbox, currline,
2098 keylist);
2099 } else {
2100 if (mountlist->entries > 0) {
2101 edit_mountlist_entry(mountlist, raidlist,
2102 partitionsListbox, currline,
2103 keylist);
2104 } else {
2105 popup_and_OK
2106 (_
2107 ("Please add an entry. Then press ENTER to edit it."));
2108 }
2109 }
2110 }
2111 }
2112 paranoid_free(flaws_str_A);
2113 paranoid_free(flaws_str_B);
2114 paranoid_free(flaws_str_C);
2115 }
2116 newtFormDestroy(myForm);
2117 newtPopWindow();
2118 newtPopHelpLine();
2119 if (b_res == bOK) {
2120 log_it(_("You pushed 'OK'. I shall now continue."));
2121 return (0);
2122 } else {
2123 /* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
2124 return (1);
2125 }
2126}
2127
2128
2129/**
2130 * Edit the mountlist.
2131 * @param mountlist The mountlist to edit.
2132 * @param raidlist The raidlist that goes with @p mountlist.
2133 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
2134 */
2135int
2136edit_mountlist(char *mountlist_fname, struct mountlist_itself *mountlist,
2137 struct raidlist_itself *raidlist)
2138{
2139 int res = 0;
2140
2141 iamhere("entering eml");
2142
2143 if (g_text_mode) {
2144 fatal_error("Don't call edit_mountlist() in text mode");
2145 } else {
2146 log_it
2147 ("I'm in GUI mode, so I shall edit mountlist using edit_mountlist()");
2148 res = edit_mountlist_in_newt(mountlist_fname, mountlist, raidlist);
2149 }
2150 iamhere("leaving eml");
2151 return (res);
2152}
2153
2154
2155#ifndef __FreeBSD__
2156/**
2157 * Edit the additional RAID variables in @p raidrec.
2158 * @param raidrec The RAID device record to edit the RAID variables in.
2159 * @ingroup restoreGuiVarslist
2160 */
2161void edit_raidrec_additional_vars(struct raid_device_record *raidrec)
2162{
2163
2164 /** structure *********************************************************/
2165 struct raid_device_record bkp_raidrec;
2166
2167 /** newt **************************************************************/
2168 newtComponent myForm;
2169 newtComponent bAdd;
2170 newtComponent bEdit;
2171 newtComponent bDelete;
2172 newtComponent bOK;
2173 newtComponent bCancel;
2174 newtComponent b_res;
2175 newtComponent varsListbox;
2176 newtComponent headerMsg;
2177
2178 /** ?? ***************************************************************/
2179 void *keylist[ARBITRARY_MAXIMUM], *curr_choice = NULL;
2180
2181 /** buffers **********************************************************/
2182 char *title_of_window = NULL;
2183
2184 /** int **************************************************************/
2185 int i = 0;
2186 int currline = 0;
2187
2188
2189 assert(raidrec != NULL);
2190
2191 memcpy((void *) &bkp_raidrec, (void *) raidrec,
2192 sizeof(struct raid_device_record));
2193 asprintf(&title_of_window, "Additional variables");
2194 newtPushHelpLine
2195 (_
2196 (" Edit the additional fields to your heart's content, then click OK or Cancel."));
2197 headerMsg =
2198 newtLabel(1, 1, _("Label Value"));
2199 varsListbox =
2200 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2201 i = 1;
2202 bAdd = newtCompactButton(i, 9, _(" Add "));
2203 bEdit = newtCompactButton(i += 8, 9, _(" Edit "));
2204 bDelete = newtCompactButton(i += 9, 9, _("Delete"));
2205 bOK = newtCompactButton(i += 9, 9, _(" OK "));
2206 bCancel = newtCompactButton(i += 9, 9, _("Cancel"));
2207 newtOpenWindow(17, 7, 46, 10, title_of_window);
2208 paranoid_free(title_of_window);
2209
2210 myForm = newtForm(NULL, NULL, 0);
2211 newtFormAddComponents(myForm, headerMsg, varsListbox, bAdd, bEdit,
2212 bDelete, bOK, bCancel, NULL);
2213 insert_essential_additionalvars(raidrec);
2214 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2215 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
2216 b_res = newtRunForm(myForm);
2217 curr_choice = newtListboxGetCurrent(varsListbox);
2218 for (currline = 0;
2219 currline < raidrec->additional_vars.entries
2220 && keylist[currline] != curr_choice; currline++);
2221 if (currline == raidrec->additional_vars.entries
2222 && raidrec->additional_vars.entries > 0) {
2223 log_it("Warning - I don't know what this button does");
2224 }
2225 if (b_res == bOK) { /* do nothing */
2226 } else if (b_res == bCancel) { /* do nothing */
2227 } else if (b_res == bAdd) {
2228 add_varslist_entry(raidrec);
2229 } else if (b_res == bDelete) {
2230 delete_varslist_entry(raidrec, currline);
2231 } else {
2232 edit_varslist_entry(raidrec, currline);
2233 }
2234 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2235 }
2236 remove_essential_additionalvars(raidrec);
2237 newtFormDestroy(myForm);
2238 newtPopWindow();
2239 newtPopHelpLine();
2240 if (b_res == bCancel) {
2241 memcpy((void *) raidrec, (void *) &bkp_raidrec,
2242 sizeof(struct raid_device_record));
2243 }
2244 return;
2245}
2246#endif
2247
2248
2249/**
2250 * Find the next free location to place a disk in @p disklist.
2251 * @param disklist The disklist to operate on.
2252 * @return The next free location (starting from 0).
2253 * @ingroup restoreGuiDisklist
2254 */
2255int find_next_free_index_in_disklist(struct list_of_disks *disklist)
2256{
2257
2258 /** int ***************************************************************/
2259 int index = -1;
2260 int pos = 0;
2261
2262 /** bool **************************************************************/
2263 bool done;
2264
2265 assert(disklist != NULL);
2266
2267 for (done = FALSE; !done;) {
2268 for (pos = 0;
2269 pos < disklist->entries && disklist->el[pos].index <= index;
2270 pos++);
2271 if (pos >= disklist->entries) {
2272 done = TRUE;
2273 } else {
2274 index = disklist->el[pos].index;
2275 }
2276 }
2277 return (index + 1);
2278}
2279
2280
2281/**
2282 * Locate @p device in @p raidlist.
2283 * @param raidlist The raidlist ot search in.
2284 * @param device The RAID device to search for.
2285 * @return The index of the device, or -1 if it could not be found.
2286 * @ingroup restoreGuiMountlist
2287 */
2288int
2289find_raid_device_in_raidlist(struct raidlist_itself *raidlist,
2290 char *device)
2291{
2292 /** int ***************************************************************/
2293 int i = 0;
2294#ifdef __FreeBSD__
2295 char *vdev = NULL;
2296#endif
2297
2298 assert(raidlist != NULL);
2299 assert_string_is_neither_NULL_nor_zerolength(device);
2300
2301#ifdef __FreeBSD__
2302 for (i = 0; i < raidlist->entries; i++) {
2303 asprintf(&vdev, "/dev/vinum/%s", raidlist->el[i].volname);
2304 if (!strcmp(device, vdev)) {
2305 paranoid_free(vdev);
2306 break;
2307 }
2308 paranoid_free(vdev);
2309 }
2310#else
2311
2312 for (i = 0; strcmp(raidlist->el[i].raid_device, device)
2313 && i < raidlist->entries; i++);
2314#endif
2315 if (i == raidlist->entries) {
2316 return (-1);
2317 } else {
2318 return (i);
2319 }
2320}
2321
2322
2323/**
2324 * Get information about the location of ISO images from the user.
2325 * @param isodir_device Where to put the device (e.g. /dev/hda4) the user enters.
2326 * @param isodir_format Where to put the format (e.g. ext2) the user enters.
2327 * @param isodir_path Where to put the path (e.g. /var/cache/mondo) the user enters.
2328 * @param nuke_me_please Whether we're planning on nuking or not.
2329 * @return TRUE if OK was pressed, FALSE otherwise.
2330 */
2331bool
2332get_isodir_info(char *isodir_device, char *isodir_format,
2333 char *isodir_path, bool nuke_me_please)
2334{
2335
2336 /** initialize ********************************************************/
2337
2338 // %d no var ???
2339 // log_it("%d - AAA - isodir_path = %s", isodir_path);
2340 if (isodir_device == NULL) {
2341 asprintf(&isodir_device, "/dev/");
2342 }
2343 if (isodir_path == NULL) {
2344 asprintf(&isodir_path, "/");
2345 }
2346 if (does_file_exist("/tmp/NFS-SERVER-PATH")) {
2347 paranoid_free(isodir_device);
2348 isodir_device = last_line_of_file("/tmp/NFS-SERVER-MOUNT");
2349 asprintf(&isodir_format, "nfs");
2350 paranoid_free(isodir_path);
2351 isodir_path = last_line_of_file("/tmp/NFS-SERVER-PATH");
2352 }
2353 if (nuke_me_please) {
2354 return (TRUE);
2355 }
2356
2357 if (popup_and_get_string
2358 (_("ISO Mode - device"),
2359 _("On what device do the ISO files live?"), isodir_device)) {
2360 if (popup_and_get_string
2361 (_("ISO Mode - format"),
2362 _
2363 ("What is the disk format of the device? (Hit ENTER if you don't know.)"),
2364 isodir_format)) {
2365 if (popup_and_get_string
2366 (_("ISO Mode - path"),
2367 _
2368 ("At what path on this device can the ISO files be found?"),
2369 isodir_path)) {
2370 // Same pb:
2371 // log_it("%d - BBB - isodir_path = %s", isodir_path);
2372 return (TRUE);
2373 }
2374 }
2375 }
2376 return (FALSE);
2377}
2378
2379
2380/**
2381 * Create a new raidtab entry for @p device in @p raidlist.
2382 * @param raidlist The raidlist to add the device to.
2383 * @param mountlist The mountlist containing information about the user's partitions.
2384 * @param currline The selected line in the mountlist.
2385 * @param device The RAID device (e.g. /dev/md0) to use.
2386 * @ingroup restoreGuiMountlist
2387 */
2388void
2389initiate_new_raidlist_entry(struct raidlist_itself *raidlist,
2390 struct mountlist_itself *mountlist,
2391 int currline, char *device)
2392{
2393 /** structure *********************************************************/
2394 struct OSSWAP (raid_device_record, vinum_volume) * raidrec;
2395
2396 /** int ***************************************************************/
2397 int pos_in_raidlist = 0;
2398
2399 assert(raidlist != NULL);
2400 assert(mountlist != NULL);
2401 assert_string_is_neither_NULL_nor_zerolength(device);
2402
2403 pos_in_raidlist =
2404 find_raid_device_in_raidlist(raidlist,
2405 mountlist->el[currline].device);
2406 if (pos_in_raidlist >= 0) {
2407 fatal_error("Sorry, that RAID device already exists. Weird.");
2408 }
2409 pos_in_raidlist = raidlist->entries++;
2410 raidrec = &raidlist->el[pos_in_raidlist];
2411 initialize_raidrec(raidrec);
2412 strcpy(raidrec->OSSWAP(raid_device, volname),
2413 OSSWAP(device, basename(device)));
2414#ifndef __FreeBSD__
2415 choose_raid_level(raidrec);
2416 select_raid_disks(mountlist, raidlist, raidrec, "data",
2417 &raidrec->data_disks);
2418#endif
2419 edit_raidlist_entry(mountlist, raidlist, raidrec, currline);
2420}
2421
2422
2423#ifndef __FreeBSD__
2424/**
2425 * Insert the RAID variables not stored in the "additional RAID variables" list there too.
2426 * @param raidrec The RAID device record to operate on.
2427 * @ingroup restoreGuiVarslist
2428 */
2429void insert_essential_additionalvars(struct raid_device_record *raidrec)
2430{
2431
2432 /** int **************************************************************/
2433 int items = 0;
2434
2435 assert(raidrec != NULL);
2436
2437 items = raidrec->additional_vars.entries;
2438 write_variableINT_to_raid_var_line(raidrec, items++,
2439 "persistent-superblock",
2440 raidrec->persistent_superblock);
2441 write_variableINT_to_raid_var_line(raidrec, items++, "chunk-size",
2442 raidrec->chunk_size);
2443 raidrec->additional_vars.entries = items;
2444}
2445#endif
2446
2447
2448/**
2449 * Dummy function that proves that we can get to the point where Mondo is run.
2450 */
2451void nuke_mode_dummy()
2452{
2453
2454 /** newt *************************************************************/
2455 newtComponent myForm;
2456 newtComponent b1;
2457 newtComponent b2;
2458 newtComponent b3;
2459 newtComponent b_res;
2460
2461
2462 newtPushHelpLine
2463 (_
2464 ("This is where I nuke your hard drives. Mhahahahaha. No-one can stop Mojo Jojo!"));
2465 newtOpenWindow(24, 3, 32, 13, _("Nuking"));
2466 b1 = newtButton(7, 1, _("Slowly"));
2467 b2 = newtButton(7, 5, _("Medium"));
2468 b3 = newtButton(7, 9, _("Quickly"));
2469 myForm = newtForm(NULL, NULL, 0);
2470 newtFormAddComponents(myForm, b1, b2, b3, NULL);
2471 b_res = newtRunForm(myForm);
2472 newtFormDestroy(myForm);
2473 newtPopWindow();
2474 newtPopHelpLine();
2475}
2476
2477
2478/**
2479 * Redraw the disklist.
2480 * @param disklist The disklist to read from.
2481 * @param keylist The list of keys for @p listbox.
2482 * @param listbox The Newt listbox component to redraw.
2483 * @ingroup restoreGuiDisklist
2484 */
2485void
2486redraw_disklist(struct list_of_disks *disklist,
2487 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2488{
2489
2490 /** int *************************************************************/
2491 int i = 0;
2492
2493 assert(disklist != NULL);
2494 assert(keylist != NULL);
2495 assert(listbox != NULL);
2496
2497 newtListboxClear(listbox);
2498
2499 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2500 keylist[i] = (void *) i;
2501 }
2502 for (i = 0; i < disklist->entries; i++) {
2503 newtListboxAppendEntry(listbox,
2504 disklist_entry_to_string(disklist, i),
2505 keylist[i]);
2506 }
2507}
2508
2509
2510/**
2511 * Redraw the mountlist.
2512 * @param mountlist The mountlist to read from.
2513 * @param keylist The list of keys for @p listbox.
2514 * @param listbox The Newt listbox component to redraw.
2515 * @ingroup restoreGuiMountlist
2516 */
2517void
2518redraw_mountlist(struct mountlist_itself *mountlist,
2519 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2520{
2521
2522 /** int **************************************************************/
2523 int i = 0;
2524
2525 assert(mountlist != NULL);
2526 assert(keylist != NULL);
2527 assert(listbox != NULL);
2528
2529 newtListboxClear(listbox);
2530// sort_mountlist_by_device (mountlist);
2531 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2532 keylist[i] = (void *) i;
2533 }
2534 for (i = 0; i < mountlist->entries; i++) {
2535 newtListboxAppendEntry(listbox,
2536 mountlist_entry_to_string(mountlist, i),
2537 keylist[i]);
2538 }
2539}
2540
2541
2542/**
2543 * Redraw the list of unallocated RAID partitions.
2544 * @param unallocated_raid_partitions The mountlist containing unallocated RAID partitions.
2545 * @param keylist The list of keys for @p listbox.
2546 * @param listbox The Newt listbox component to redraw.
2547 * @ingroup restoreGuiDisklist
2548 */
2549void redraw_unallocpartnslist(struct mountlist_itself
2550 *unallocated_raid_partitions,
2551 void *keylist[ARBITRARY_MAXIMUM],
2552 newtComponent listbox)
2553{
2554
2555 /** int **************************************************************/
2556 int i = 0;
2557
2558 /** buffers **********************************************************/
2559 char *tmp = NULL;
2560
2561 assert(unallocated_raid_partitions != NULL);
2562 assert(keylist != NULL);
2563 assert(listbox != NULL);
2564
2565 newtListboxClear(listbox);
2566 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2567 keylist[i] = (void *) i;
2568 }
2569 for (i = 0; i < unallocated_raid_partitions->entries; i++) {
2570 asprintf(&tmp, "%-22s %8lld",
2571 unallocated_raid_partitions->el[i].device,
2572 unallocated_raid_partitions->el[i].size / 1024);
2573 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2574 paranoid_free(tmp);
2575 }
2576}
2577
2578
2579#ifndef __FreeBSD__
2580/**
2581 * Redraw the list of additional RAID variables.
2582 * @param additional_vars The list of additional RAID varibals.
2583 * @param keylist The list of keys for @p listbox.
2584 * @param listbox The Newt listbox component to redraw.
2585 * @ingroup restoreGuiVarslist
2586 */
2587void
2588redraw_varslist(struct additional_raid_variables *additional_vars,
2589 void *keylist[], newtComponent listbox)
2590{
2591 /** int *************************************************************/
2592 int i = 0;
2593
2594 /** buffers *********************************************************/
2595 char *tmp;
2596
2597 assert(additional_vars != NULL);
2598 assert(keylist != NULL);
2599 assert(listbox != NULL);
2600
2601 newtListboxClear(listbox);
2602
2603 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2604 keylist[i] = (void *) i;
2605 }
2606 for (i = 0; i < additional_vars->entries; i++) {
2607 asprintf(&tmp, "%-32s %-8s", additional_vars->el[i].label,
2608 additional_vars->el[i].value);
2609 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2610 paranoid_free(tmp);
2611 }
2612}
2613
2614
2615/**
2616 * Remove variable @p label from the RAID variables list in @p raidrec.
2617 * @param raidrec The RAID device record to remove the variable from.
2618 * @param label The variable name to remove.
2619 * @return The value of the variable removed.
2620 * @ingroup restoreUtilityGroup
2621 */
2622int read_variableINT_and_remove_from_raidvars(struct
2623 OSSWAP (raid_device_record,
2624 vinum_volume) *
2625 raidrec, char *label)
2626{
2627 /** int ***************************************************************/
2628 int i = 0;
2629 int res = 0;
2630
2631
2632 assert(raidrec != NULL);
2633 assert(label != NULL);
2634
2635 for (i = 0;
2636 i < raidrec->additional_vars.entries
2637 && strcmp(raidrec->additional_vars.el[i].label, label); i++);
2638 if (i == raidrec->additional_vars.entries) {
2639 res = -1;
2640 } else {
2641 res = atoi(raidrec->additional_vars.el[i].value);
2642 for (i++; i < raidrec->additional_vars.entries; i++) {
2643 memcpy((void *) &raidrec->additional_vars.el[i - 1],
2644 (void *) &raidrec->additional_vars.el[i],
2645 sizeof(struct raid_var_line));
2646 }
2647 raidrec->additional_vars.entries--;
2648 }
2649 return (res);
2650}
2651#endif
2652
2653
2654/**
2655 * Change all RAID devices to use @p new_dev instead of @p old_dev.
2656 * @param raidlist The raidlist to make the changes in.
2657 * @param old_dev The old name of the device (what it used to be).
2658 * @param new_dev The new name of the device (what it is now).
2659 * @ingroup restoreGuiMountlist
2660 */
2661void rejig_partition_name_in_raidlist_if_necessary(struct raidlist_itself
2662 *raidlist,
2663 char *old_dev,
2664 char *new_dev)
2665{
2666 /** buffers ********************************************************/
2667 char *tmp;
2668
2669 /** int ************************************************************/
2670 int pos = 0;
2671 int j = 0;
2672
2673 assert(raidlist != NULL);
2674 assert_string_is_neither_NULL_nor_zerolength(old_dev);
2675 assert_string_is_neither_NULL_nor_zerolength(new_dev);
2676
2677 pos = which_raid_device_is_using_this_partition(raidlist, old_dev);
2678 if (pos < 0) {
2679 asprintf(&tmp, "No need to rejig %s in raidlist: it's not listed.",
2680 old_dev);
2681 log_it(tmp);
2682 paranoid_free(tmp);
2683 } else {
2684 if ((j =
2685 where_in_drivelist_is_drive(&raidlist->
2686 OSSWAP(el[pos].data_disks, disks),
2687 old_dev)) >= 0) {
2688 strcpy(raidlist->OSSWAP(el[pos].data_disks, disks).el[j].
2689 device, new_dev);
2690 } else
2691 if ((j =
2692 where_in_drivelist_is_drive(&raidlist->
2693 OSSWAP(el[pos].spare_disks,
2694 spares),
2695 old_dev)) >= 0) {
2696 strcpy(raidlist->OSSWAP(el[pos].spare_disks, spares).el[j].
2697 device, new_dev);
2698 }
2699#ifndef __FreeBSD__
2700 else if ((j =
2701 where_in_drivelist_is_drive(&raidlist->el[pos].
2702 parity_disks,
2703 old_dev)) >= 0) {
2704 strcpy(raidlist->el[pos].parity_disks.el[j].device, new_dev);
2705 } else
2706 if ((j =
2707 where_in_drivelist_is_drive(&raidlist->el[pos].
2708 failed_disks,
2709 old_dev)) >= 0) {
2710 strcpy(raidlist->el[pos].failed_disks.el[j].device, new_dev);
2711 }
2712#endif
2713 else {
2714 asprintf(&tmp,
2715 "%s is supposed to be listed in this raid dev but it's not...",
2716 old_dev);
2717 log_it(tmp);
2718 paranoid_free(tmp);
2719 }
2720 }
2721}
2722
2723
2724#ifndef __FreeBSD__
2725/**
2726 * Remove the essential RAID variables from the "additional variables" section.
2727 * If they have been changed, set them in their normal locations too.
2728 * @param raidrec The RAID device record to operate on.
2729 * @ingroup restoreUtilityVarslist
2730 */
2731void remove_essential_additionalvars(struct raid_device_record *raidrec)
2732{
2733
2734 /** int **************************************************************/
2735 int res = 0;
2736
2737 assert(raidrec != NULL);
2738
2739 res =
2740 read_variableINT_and_remove_from_raidvars(raidrec,
2741 "persistent-superblock");
2742 if (res > 0) {
2743 raidrec->persistent_superblock = res;
2744 }
2745 res = read_variableINT_and_remove_from_raidvars(raidrec, "chunk-size");
2746 if (res > 0) {
2747 raidrec->chunk_size = res;
2748 }
2749 res = read_variableINT_and_remove_from_raidvars(raidrec, "block-size");
2750}
2751
2752
2753/**
2754 * Select the RAID disks to use in @p raidrec.
2755 * @param mountlist_dontedit The mountlist (will not be edited).
2756 * @param raidlist The raidlist to modify.
2757 * @param raidrec The RAID device record in @p raidlist to work on.
2758 * @param description_of_list The type of disks we're selecting (e.g. "data").
2759 * @param disklist The disklist to put the user-selected disks in.
2760 * @ingroup restoreGuiMountlist
2761 */
2762void
2763select_raid_disks(struct mountlist_itself *mountlist_dontedit,
2764 struct raidlist_itself *raidlist,
2765 struct raid_device_record *raidrec,
2766 char *description_of_list,
2767 struct list_of_disks *disklist)
2768{
2769 void *curr_choice = NULL;
2770
2771 /** structures ********************************************************/
2772 struct raidlist_itself *bkp_raidlist = NULL;
2773 struct raid_device_record *bkp_raidrec = NULL;
2774 struct list_of_disks *bkp_disklist = NULL;
2775 struct mountlist_itself *unallocated_raid_partitions = NULL;
2776
2777 /** newt **************************************************************/
2778 newtComponent myForm = NULL;
2779 newtComponent bAdd = NULL;
2780 newtComponent bDelete = NULL;
2781 newtComponent bOK = NULL;
2782 newtComponent bCancel = NULL;
2783 newtComponent b_res = NULL;
2784 newtComponent partitionsListbox = NULL;
2785 newtComponent headerMsg = NULL;
2786
2787 /** buffers **********************************************************/
2788 void *keylist[ARBITRARY_MAXIMUM];
2789 char *tmp = NULL;
2790 char *help_text = NULL;
2791 char *title_of_window = NULL;
2792 char *sz_res = NULL;
2793 char *header_text = NULL;
2794
2795 /** int **************************************************************/
2796 int i = 0;
2797 int currline = 0;
2798
2799 assert(mountlist_dontedit != NULL);
2800 assert(raidlist != NULL);
2801 assert(raidrec != NULL);
2802 assert(description_of_list != NULL);
2803 assert(disklist != NULL);
2804
2805 iamhere("malloc'ing");
2806 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
2807 fatal_error("Cannot malloc space for raidrec");
2808 }
2809 if (!(bkp_disklist = malloc(sizeof(struct list_of_disks)))) {
2810 fatal_error("Cannot malloc space for disklist");
2811 }
2812 if (!(bkp_raidlist = malloc(sizeof(struct raidlist_itself)))) {
2813 fatal_error("Cannot malloc space for raidlist");
2814 }
2815 if (!
2816 (unallocated_raid_partitions =
2817 malloc(sizeof(struct mountlist_itself)))) {
2818 fatal_error("Cannot malloc space for unallocated_raid_partitions");
2819 }
2820
2821 memcpy((void *) bkp_raidlist, (void *) raidlist,
2822 sizeof(struct raidlist_itself));
2823 memcpy((void *) bkp_raidrec, (void *) raidrec,
2824 sizeof(struct raid_device_record));
2825 memcpy((void *) bkp_disklist, (void *) disklist,
2826 sizeof(struct list_of_disks));
2827
2828 iamhere("Post-malloc");
2829 asprintf(&help_text,
2830 _
2831 (" Edit this RAID device's list of partitions. Choose OK or Cancel when done."));
2832 asprintf(&header_text, "%-24s %s", _("Device"), _("Index"));
2833 asprintf(&title_of_window, _("%s contains..."), raidrec->raid_device);
2834 newtPushHelpLine(help_text);
2835 paranoid_free(help_text);
2836 for (b_res = (newtComponent) 12345; b_res != bOK && b_res != bCancel;) {
2837 headerMsg = newtLabel(1, 1, header_text);
2838 partitionsListbox =
2839 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2840 redraw_disklist(disklist, keylist, partitionsListbox);
2841 i = 1;
2842 bAdd = newtCompactButton(i, 9, _(" Add "));
2843 bDelete = newtCompactButton(i += 8, 9, _("Delete"));
2844 bOK = newtCompactButton(i += 9, 9, _(" OK "));
2845 bCancel = newtCompactButton(i += 9, 9, _("Cancel"));
2846 newtOpenWindow(21, 7, 38, 10, title_of_window);
2847 myForm = newtForm(NULL, NULL, 0);
2848 if (disklist->entries == 0) {
2849 newtFormAddComponents(myForm, headerMsg, bAdd, bDelete, bOK,
2850 bCancel, NULL);
2851 } else {
2852 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2853 bAdd, bDelete, bOK, bCancel, NULL);
2854 }
2855 b_res = newtRunForm(myForm);
2856 if (b_res == bOK || b_res == bCancel) { /* do nothing */
2857// That's OK. At the end of this subroutine (after this do/while loop),
2858// we'll throw away the changes if Cancel was pushed.
2859 } else {
2860 curr_choice = newtListboxGetCurrent(partitionsListbox);
2861 for (i = 0; i < disklist->entries && keylist[i] != curr_choice;
2862 i++);
2863 if (i == disklist->entries && disklist->entries > 0) {
2864 log_to_screen(_("I don't know what that button does!"));
2865 } else {
2866 currline = i;
2867 if (b_res == bAdd) {
2868 log_it(_("Making list of unallocated RAID slices"));
2869 make_list_of_unallocated_raid_partitions
2870 (unallocated_raid_partitions, mountlist_dontedit,
2871 raidlist);
2872 if (unallocated_raid_partitions->entries <= 0) {
2873 popup_and_OK
2874 (_
2875 ("There are no unallocated partitions marked for RAID."));
2876 } else {
2877 log_it
2878 (_
2879 ("Done. The user may add one or more of the above to RAID device"));
2880 add_disklist_entry(disklist, raidrec->raid_device,
2881 unallocated_raid_partitions);
2882 log_it(_
2883 ("I have finished adding a disklist entry."));
2884 redraw_disklist(disklist, keylist,
2885 partitionsListbox);
2886 }
2887 } else if (b_res == bDelete) {
2888 delete_disklist_entry(disklist, raidrec->raid_device,
2889 currline);
2890 redraw_disklist(disklist, keylist, partitionsListbox);
2891 } else {
2892 asprintf(&tmp, _("%s's index is %d. What should it be?"),
2893 raidrec->raid_device,
2894 disklist->el[currline].index);
2895 asprintf(&sz_res, "%d", disklist->el[currline].index);
2896 if (popup_and_get_string
2897 (_("Set index"), tmp, sz_res)) {
2898 disklist->el[currline].index = atoi(sz_res);
2899 }
2900 paranoid_free(tmp);
2901 paranoid_free(sz_res);
2902 redraw_disklist(disklist, keylist, partitionsListbox);
2903 }
2904 }
2905 }
2906 newtFormDestroy(myForm);
2907 newtPopWindow();
2908 }
2909 paranoid_free(title_of_window);
2910 paranoid_free(header_text);
2911
2912 newtPopHelpLine();
2913 if (b_res == bCancel) {
2914 memcpy((void *) raidlist, (void *) bkp_raidlist,
2915 sizeof(struct raidlist_itself));
2916 memcpy((void *) raidrec, (void *) bkp_raidrec,
2917 sizeof(struct raid_device_record));
2918 memcpy((void *) disklist, (void *) bkp_disklist,
2919 sizeof(struct list_of_disks));
2920 }
2921 paranoid_free(bkp_raidrec);
2922 paranoid_free(bkp_disklist);
2923 paranoid_free(bkp_raidlist);
2924 paranoid_free(unallocated_raid_partitions);
2925}
2926#endif
2927
2928
2929/**
2930 * Ask the user which restore mode (nuke, interactive, or compare) we should use.
2931 * @return The mode selected: 'I' for interactive, 'N' for nuke, 'C' for compare,
2932 * or 'E' (or any other letter) for exit.
2933 */
2934char which_restore_mode()
2935{
2936
2937 /** char *************************************************************/
2938 char output = '\0';
2939 char *tmp = NULL;
2940 size_t n = 0;
2941
2942 /** newt *************************************************************/
2943
2944 newtComponent b1;
2945 newtComponent b2;
2946 newtComponent b3;
2947 newtComponent b4;
2948 newtComponent b_res;
2949 newtComponent myForm;
2950
2951 if (g_text_mode) {
2952 for (output = 'z'; !strchr("AICE", output); output = tmp[0]) {
2953 printf
2954 (_
2955 ("Which mode - (A)utomatic, (I)nteractive, \n(C)ompare only, or (E)xit to shell?\n--> "));
2956 getline(&tmp, &n, stdin);
2957 }
2958 paranoid_free(tmp);
2959 return (output);
2960 }
2961
2962 newtPushHelpLine
2963 (_
2964 (" Do you want to 'nuke' your system, restore interactively, or just compare?"));
2965 newtOpenWindow(24, 3, 32, 17, _("How should I restore?"));
2966 b1 = newtButton(7, 1, _("Automatically"));
2967 b2 = newtButton(7, 5, _("Interactively"));
2968 b3 = newtButton(7, 9, _("Compare only!"));
2969 b4 = newtButton(7, 13, _("Exit to shell"));
2970 myForm = newtForm(NULL, NULL, 0);
2971 newtFormAddComponents(myForm, b1, b2, b3, b4, NULL);
2972 b_res = newtRunForm(myForm);
2973 newtFormDestroy(myForm);
2974 newtPopWindow();
2975 if (b_res == b1) {
2976 output = 'N';
2977 }
2978 if (b_res == b2) {
2979 output = 'I';
2980 }
2981 if (b_res == b3) {
2982 output = 'C';
2983 }
2984 if (b_res == b4) {
2985 output = 'E';
2986 }
2987 newtPopHelpLine();
2988 return (output);
2989}
Note: See TracBrowser for help on using the repository browser.