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

Last change on this file since 2355 was 2355, checked in by Bruno Cornec, 15 years ago

Remove function spread_flaws_across_three_lines and expand it at the single place where it was useful, and fix memory allocation issues in it.

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