source: MondoRescue/branches/3.1/mondo/src/mondorestore/mondo-rstr-newt.c@ 3147

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