source: MondoRescue/branches/3.0/mondo/src/mondorestore/mondo-rstr-newt.c@ 3185

Last change on this file since 3185 was 3185, checked in by Bruno Cornec, 11 years ago

Simplify the interface of mr_getline and mr_asprintf. With 3.1 compatibility now will allow backports from this branch into 3.0

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