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

Last change on this file since 817 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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