source: MondoRescue/branches/stable/mondo/src/mondorestore/mondo-rstr-newt.c@ 1103

Last change on this file since 1103 was 1103, checked in by Bruno Cornec, 17 years ago

Fix again another set of compiler warnings (Michel Loiseleur <mloiseleur_at_linagora.com>)

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