source: MondoRescue/branches/2.2.9/mondo/src/mondorestore/mondo-rstr-newt.c

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