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

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

Fix interface of evaluate_mountlist (remove 2nd param useless) and fix nuke mode which wasn't working.

  • Property svn:keywords set to Id
File size: 87.0 KB
Line 
1/***************************************************************************
2 * $Id: mondo-rstr-newt.c 2394 2009-09-12 00:53:02Z 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 2394 2009-09-12 00:53:02Z 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 currline = 0;
1936 int finished = FALSE;
1937
1938 /** buffers **********************************************************/
1939 char tmp[MAX_STR_LEN];
1940 char *flaws_str = NULL;
1941 char *flaws_str_A = NULL;
1942 char *flaws_str_B = NULL;
1943 char *flaws_str_C = NULL;
1944
1945 assert(mountlist != NULL);
1946 assert(raidlist != NULL);
1947
1948 if (mountlist->entries > ARBITRARY_MAXIMUM) {
1949 log_to_screen("Arbitrary limits suck, man!");
1950 finish(1);
1951 }
1952 newtPushHelpLine
1953 (" Please edit the mountlist to your satisfaction, then click OK or Cancel.");
1954 i = 4;
1955 bAdd = newtCompactButton(i, 17, " Add ");
1956 bEdit = newtCompactButton(i += 11, 17, " Edit ");
1957 bDelete = newtCompactButton(i += 12, 17, "Delete");
1958 bReload = newtCompactButton(i += 12, 17, "Reload");
1959 bCancel = newtCompactButton(i += 12, 17, "Cancel");
1960 bOK = newtCompactButton(i += 12, 17, " OK ");
1961 sprintf(tmp, "%-24s %-24s %-8s %s", "Device", "Mountpoint", "Format",
1962 "Size (MB)");
1963 headerMsg = newtLabel(2, 1, tmp);
1964 flawsLabelA = newtLabel(2, 13, " ");
1965 flawsLabelB = newtLabel(2, 14, " ");
1966 flawsLabelC = newtLabel(2, 15, " ");
1967 partitionsListbox =
1968 newtListbox(2, 2, 10, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1969 redraw_mountlist(mountlist, keylist, partitionsListbox);
1970 newtOpenWindow(1, 3, 77, 18, "Editing mountlist");
1971 myForm = newtForm(NULL, NULL, 0);
1972 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
1973 flawsLabelA, flawsLabelB, flawsLabelC, bAdd,
1974 bEdit, bDelete, bReload, bCancel, bOK, NULL);
1975 while (!finished) {
1976 flaws_str = evaluate_mountlist(mountlist);
1977 if (strlen(flaws_str) > 0) {
1978 mr_asprintf(&flaws_str_A, "%s", flaws_str + 1);
1979 } else {
1980 mr_asprintf(&flaws_str_A, "");
1981 }
1982 if (strlen(flaws_str_A) >= 74) {
1983 for (i = 74; flaws_str_A[i] != ' '; i--);
1984 mr_asprintf(&flaws_str_B, "%s", flaws_str_A + i + 1);
1985 flaws_str_A[i] = '\0';
1986 } else {
1987 mr_asprintf(&flaws_str_B, "");
1988 }
1989 if (strlen(flaws_str_B) >= 74) {
1990 for (i = 74; flaws_str_B[i] != ' '; i--);
1991 mr_asprintf(&flaws_str_C, "%s", flaws_str_B + i + 1);
1992 flaws_str_B[i] = '\0';
1993 } else {
1994 mr_asprintf(&flaws_str_C, "");
1995 }
1996 mr_free(flaws_str);
1997
1998 newtLabelSetText(flawsLabelA, flaws_str_A);
1999 newtLabelSetText(flawsLabelB, flaws_str_B);
2000 newtLabelSetText(flawsLabelC, flaws_str_C);
2001 b_res = newtRunForm(myForm);
2002 mr_free(flaws_str_A);
2003 mr_free(flaws_str_B);
2004 mr_free(flaws_str_C);
2005
2006 if (b_res == bOK) {
2007 flaws_str = evaluate_mountlist(mountlist);
2008 if (flaws_str != NULL) {
2009 mr_free(flaws_str);
2010 finished =
2011 ask_me_yes_or_no
2012 ("Your mountlist might not work. Continue anyway?");
2013 } else {
2014 finished =
2015 ask_me_yes_or_no
2016 ("Are you sure you want to save your mountlist and continue? (No changes will be made to your partition table at this time.)");
2017 }
2018 } else if (b_res == bCancel) {
2019 finished = TRUE;
2020 } else if (b_res == bReload) {
2021 if (ask_me_yes_or_no("Reload original mountlist?")) {
2022/*
2023This would be really dumb. RAIDTAB_FNAME is #define'd. --- Hugo, 2003/04/24
2024 if (!RAIDTAB_FNAME[0])
2025 {
2026 strcpy(RAIDTAB_FNAME, "/etc/raidtab");
2027 log_it("Warning - raidtab_fname is blank. Assuming %s", g_raidtab_fname);
2028 }
2029*/
2030 load_mountlist(mountlist, mountlist_fname);
2031 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
2032 redraw_mountlist(mountlist, keylist, partitionsListbox);
2033 }
2034 } else {
2035 curr_choice = newtListboxGetCurrent(partitionsListbox);
2036 for (i = 0;
2037 i < mountlist->entries && keylist[i] != curr_choice; i++);
2038 if (i == mountlist->entries && mountlist->entries > 0) {
2039 log_to_screen("I don't know what that button does!");
2040 } else {
2041 currline = i;
2042 if (b_res == bAdd) {
2043 add_mountlist_entry(mountlist, raidlist,
2044 partitionsListbox, currline,
2045 keylist);
2046 } else if (b_res == bDelete) {
2047 delete_mountlist_entry(mountlist, raidlist,
2048 partitionsListbox, currline,
2049 keylist);
2050 } else {
2051 if (mountlist->entries > 0) {
2052 edit_mountlist_entry(mountlist, raidlist,
2053 partitionsListbox, currline,
2054 keylist);
2055 } else {
2056 popup_and_OK
2057 ("Please add an entry. Then press ENTER to edit it.");
2058 }
2059 }
2060 }
2061 }
2062 }
2063 newtFormDestroy(myForm);
2064 newtPopWindow();
2065 newtPopHelpLine();
2066 if (b_res == bOK) {
2067 log_it("You pushed 'OK'. I shall now continue.");
2068 return (0);
2069 } else {
2070 /* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
2071 return (1);
2072 }
2073}
2074
2075
2076
2077/**
2078 * Edit the mountlist.
2079 * @param mountlist The mountlist to edit.
2080 * @param raidlist The raidlist that goes with @p mountlist.
2081 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
2082 */
2083int
2084edit_mountlist(char *mountlist_fname, struct mountlist_itself *mountlist,
2085 struct raidlist_itself *raidlist)
2086{
2087 int res = 0;
2088
2089 log_it("entering eml");
2090
2091 if (g_text_mode) {
2092 fatal_error("Don't call edit_mountlist() in text mode");
2093 } else {
2094 log_it
2095 ("I'm in GUI mode, so I shall edit mountlist using edit_mountlist()");
2096 res = edit_mountlist_in_newt(mountlist_fname, mountlist, raidlist);
2097 }
2098 log_it("leaving eml");
2099 return (res);
2100}
2101
2102
2103
2104
2105#ifndef __FreeBSD__
2106/**
2107 * Edit the additional RAID variables in @p raidrec.
2108 * @param raidrec The RAID device record to edit the RAID variables in.
2109 * @ingroup restoreGuiVarslist
2110 */
2111void edit_raidrec_additional_vars(struct raid_device_record *raidrec)
2112{
2113
2114 /** structure *********************************************************/
2115 struct raid_device_record bkp_raidrec;
2116
2117 /** newt **************************************************************/
2118 newtComponent myForm;
2119 newtComponent bAdd;
2120 newtComponent bEdit;
2121 newtComponent bDelete;
2122 newtComponent bOK;
2123 newtComponent bCancel;
2124 newtComponent b_res;
2125 newtComponent varsListbox;
2126 newtComponent headerMsg;
2127
2128 /** ?? ***************************************************************/
2129 void *keylist[ARBITRARY_MAXIMUM], *curr_choice;
2130
2131 /** buffers **********************************************************/
2132 char title_of_window[MAX_STR_LEN];
2133
2134 /** int **************************************************************/
2135 int i = 0;
2136 int currline = 0;
2137
2138
2139 assert(raidrec != NULL);
2140
2141 memcpy((void *) &bkp_raidrec, (void *) raidrec,
2142 sizeof(struct raid_device_record));
2143 sprintf(title_of_window, "Additional variables");
2144 newtPushHelpLine
2145 (" Edit the additional fields to your heart's content, then click OK or Cancel.");
2146 headerMsg = newtLabel(1, 1, "Label Value");
2147 varsListbox =
2148 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2149 i = 1;
2150 bAdd = newtCompactButton(i, 9, " Add ");
2151 bEdit = newtCompactButton(i += 8, 9, " Edit ");
2152 bDelete = newtCompactButton(i += 9, 9, "Delete");
2153 bOK = newtCompactButton(i += 9, 9, " OK ");
2154 bCancel = newtCompactButton(i += 9, 9, "Cancel");
2155 newtOpenWindow(17, 7, 46, 10, title_of_window);
2156 myForm = newtForm(NULL, NULL, 0);
2157 newtFormAddComponents(myForm, headerMsg, varsListbox, bAdd, bEdit,
2158 bDelete, bOK, bCancel, NULL);
2159 insert_essential_additionalvars(raidrec);
2160 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2161 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
2162 b_res = newtRunForm(myForm);
2163 curr_choice = newtListboxGetCurrent(varsListbox);
2164 for (currline = 0;
2165 currline < raidrec->additional_vars.entries
2166 && keylist[currline] != curr_choice; currline++);
2167 if (currline == raidrec->additional_vars.entries
2168 && raidrec->additional_vars.entries > 0) {
2169 log_it("Warning - I don't know what this button does");
2170 }
2171 if (b_res == bOK) { /* do nothing */
2172 } else if (b_res == bCancel) { /* do nothing */
2173 } else if (b_res == bAdd) {
2174 add_varslist_entry(raidrec);
2175 } else if (b_res == bDelete) {
2176 delete_varslist_entry(raidrec, currline);
2177 } else {
2178 edit_varslist_entry(raidrec, currline);
2179 }
2180 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2181 }
2182 remove_essential_additionalvars(raidrec);
2183 newtFormDestroy(myForm);
2184 newtPopWindow();
2185 newtPopHelpLine();
2186 if (b_res == bCancel) {
2187 memcpy((void *) raidrec, (void *) &bkp_raidrec,
2188 sizeof(struct raid_device_record));
2189 }
2190 return;
2191}
2192#endif
2193
2194
2195/**
2196 * Find the next free location to place a disk in @p disklist.
2197 * @param disklist The disklist to operate on.
2198 * @return The next free location (starting from 0).
2199 * @ingroup restoreGuiDisklist
2200 */
2201int find_next_free_index_in_disklist(struct list_of_disks *disklist)
2202{
2203
2204 /** int ***************************************************************/
2205 int index = -1;
2206 int pos = 0;
2207
2208 /** bool **************************************************************/
2209 bool done;
2210
2211 assert(disklist != NULL);
2212
2213 for (done = FALSE; !done;) {
2214 for (pos = 0;
2215 pos < disklist->entries && disklist->el[pos].index <= index;
2216 pos++);
2217 if (pos >= disklist->entries) {
2218 done = TRUE;
2219 } else {
2220 index = disklist->el[pos].index;
2221 }
2222 }
2223 return (index + 1);
2224}
2225
2226
2227
2228/**
2229 * Locate @p device in @p raidlist.
2230 * @param raidlist The raidlist ot search in.
2231 * @param device The RAID device to search for.
2232 * @return The index of the device, or -1 if it could not be found.
2233 * @ingroup restoreGuiMountlist
2234 */
2235int
2236find_raid_device_in_raidlist(struct raidlist_itself *raidlist,
2237 char *device)
2238{
2239
2240 /** int ***************************************************************/
2241 int i = 0;
2242#ifdef __FreeBSD__
2243 char vdev[64];
2244#else
2245// Linux
2246#endif
2247
2248 assert(raidlist != NULL);
2249 assert_string_is_neither_NULL_nor_zerolength(device);
2250
2251#ifdef __FreeBSD__
2252 for (i = 0; i < raidlist->entries; i++) {
2253 sprintf(vdev, "/dev/vinum/%s", raidlist->el[i].volname);
2254 if (!strcmp(device, vdev))
2255 break;
2256 }
2257#else
2258
2259 for (i = 0;
2260 strcmp(raidlist->el[i].raid_device, device)
2261 && i < raidlist->entries; i++);
2262#endif
2263 if (i == raidlist->entries) {
2264 return (-1);
2265 } else {
2266 return (i);
2267 }
2268}
2269
2270
2271/**
2272 * Get information about the location of ISO images from the user.
2273 * @param isodir_device Where to put the device (e.g. /dev/hda4) the user enters.
2274 * @param isodir_format Where to put the format (e.g. ext2) the user enters.
2275 * @param isodir_path Where to put the path (e.g. /var/cache/mondo) the user enters.
2276 * @param nuke_me_please Whether we're planning on nuking or not.
2277 * @return TRUE if OK was pressed, FALSE otherwise.
2278 */
2279bool
2280get_isodir_info(char *isodir_device, char *isodir_format,
2281 char *isodir_path, bool nuke_me_please)
2282{
2283
2284 /** initialize ********************************************************/
2285
2286 assert(isodir_device != NULL);
2287 assert(isodir_format != NULL);
2288 assert(isodir_path != NULL);
2289
2290 log_it("isodir_path = %s", isodir_path);
2291 isodir_format[0] = '\0';
2292 if (isodir_device[0] == '\0') {
2293 strcpy(isodir_device, "/dev/");
2294 }
2295 if (isodir_path[0] == '\0') {
2296 strcpy(isodir_path, "/");
2297 }
2298 if (does_file_exist("/tmp/NETFS-SERVER-PATH")) {
2299 strcpy(isodir_device, last_line_of_file("/tmp/NETFS-SERVER-MOUNT"));
2300 strcpy(isodir_format, "netfs");
2301 strcpy(isodir_path, last_line_of_file("/tmp/NETFS-SERVER-PATH"));
2302 }
2303 if (nuke_me_please) {
2304 return (TRUE);
2305 }
2306
2307 if (popup_and_get_string
2308 ("ISO Mode - device", "On what device do the ISO files live?",
2309 isodir_device, MAX_STR_LEN / 4)) {
2310 if (popup_and_get_string
2311 ("ISO Mode - format",
2312 "What is the disk format of the device? (Hit ENTER if you don't know.)",
2313 isodir_format, 16)) {
2314 if (popup_and_get_string
2315 ("ISO Mode - path",
2316 "At what path on this device can the ISO files be found?",
2317 isodir_path, MAX_STR_LEN / 4)) {
2318 strip_spaces(isodir_device);
2319 strip_spaces(isodir_format);
2320 strip_spaces(isodir_path);
2321 log_it("isodir_device = %s - isodir_format = %s - isodir_path = %s", isodir_device, isodir_format, isodir_path);
2322 return (TRUE);
2323 }
2324 }
2325 }
2326 return (FALSE);
2327}
2328
2329
2330/**
2331 * Create a new raidtab entry for @p device in @p raidlist.
2332 * @param raidlist The raidlist to add the device to.
2333 * @param mountlist The mountlist containing information about the user's partitions.
2334 * @param currline The selected line in the mountlist.
2335 * @param device The RAID device (e.g. /dev/md0) to use.
2336 * @ingroup restoreGuiMountlist
2337 */
2338void
2339initiate_new_raidlist_entry(struct raidlist_itself *raidlist,
2340 struct mountlist_itself *mountlist,
2341 int currline, char *device)
2342{
2343
2344 /** structure *********************************************************/
2345 struct OSSWAP (raid_device_record, vinum_volume) * raidrec;
2346
2347 /** int ***************************************************************/
2348 int pos_in_raidlist = 0;
2349
2350 assert(raidlist != NULL);
2351 assert(mountlist != NULL);
2352 assert_string_is_neither_NULL_nor_zerolength(device);
2353
2354 pos_in_raidlist =
2355 find_raid_device_in_raidlist(raidlist,
2356 mountlist->el[currline].device);
2357 if (pos_in_raidlist >= 0) {
2358 fatal_error("Sorry, that RAID device already exists. Weird.");
2359 }
2360 pos_in_raidlist = raidlist->entries++;
2361 raidrec = &raidlist->el[pos_in_raidlist];
2362 initialize_raidrec(raidrec);
2363 strcpy(raidrec->OSSWAP(raid_device, volname),
2364 OSSWAP(device, basename(device)));
2365#ifndef __FreeBSD__
2366 choose_raid_level(raidrec);
2367 select_raid_disks(mountlist, raidlist, raidrec, "data",
2368 &raidrec->data_disks);
2369#endif
2370 edit_raidlist_entry(mountlist, raidlist, raidrec, currline);
2371}
2372
2373
2374#ifndef __FreeBSD__
2375/**
2376 * Insert the RAID variables not stored in the "additional RAID variables" list there too.
2377 * @param raidrec The RAID device record to operate on.
2378 * @ingroup restoreGuiVarslist
2379 */
2380void insert_essential_additionalvars(struct raid_device_record *raidrec)
2381{
2382
2383 /** int **************************************************************/
2384 int items = 0;
2385
2386 assert(raidrec != NULL);
2387
2388 items = raidrec->additional_vars.entries;
2389 write_variableINT_to_raid_var_line(raidrec, items++,
2390 "persistent-superblock",
2391 raidrec->persistent_superblock);
2392 write_variableINT_to_raid_var_line(raidrec, items++, "chunk-size",
2393 raidrec->chunk_size);
2394 raidrec->additional_vars.entries = items;
2395}
2396
2397#endif
2398
2399/**
2400 * Dummy function that proves that we can get to the point where Mondo is run.
2401 */
2402void nuke_mode_dummy()
2403{
2404
2405 /** newt *************************************************************/
2406 newtComponent myForm;
2407 newtComponent b1;
2408 newtComponent b2;
2409 newtComponent b3;
2410 newtComponent b_res;
2411
2412
2413 newtPushHelpLine
2414 ("This is where I nuke your hard drives. Mhahahahaha. No-one can stop Mojo Jojo!");
2415 newtOpenWindow(24, 3, 32, 13, "Nuking");
2416 b1 = newtButton(7, 1, "Slowly");
2417 b2 = newtButton(7, 5, "Medium");
2418 b3 = newtButton(7, 9, "Quickly");
2419 myForm = newtForm(NULL, NULL, 0);
2420 newtFormAddComponents(myForm, b1, b2, b3, NULL);
2421 b_res = newtRunForm(myForm);
2422 newtFormDestroy(myForm);
2423 newtPopWindow();
2424 newtPopHelpLine();
2425}
2426
2427
2428
2429/**
2430 * Redraw the disklist.
2431 * @param disklist The disklist to read from.
2432 * @param keylist The list of keys for @p listbox.
2433 * @param listbox The Newt listbox component to redraw.
2434 * @ingroup restoreGuiDisklist
2435 */
2436void
2437redraw_disklist(struct list_of_disks *disklist,
2438 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2439{
2440
2441 /** long **************************************************************/
2442 long i = 0;
2443
2444 assert(disklist != NULL);
2445 assert(keylist != NULL);
2446 assert(listbox != NULL);
2447
2448 newtListboxClear(listbox);
2449
2450 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2451 keylist[i] = (void *) i;
2452 }
2453 for (i = 0; i < disklist->entries; i++) {
2454 newtListboxAppendEntry(listbox,
2455 disklist_entry_to_string(disklist, i),
2456 keylist[i]);
2457 }
2458}
2459
2460
2461/**
2462 * Redraw the mountlist.
2463 * @param mountlist The mountlist to read from.
2464 * @param keylist The list of keys for @p listbox.
2465 * @param listbox The Newt listbox component to redraw.
2466 * @ingroup restoreGuiMountlist
2467 */
2468void
2469redraw_mountlist(struct mountlist_itself *mountlist,
2470 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2471{
2472
2473 /** long **************************************************************/
2474 long i = 0;
2475
2476 assert(mountlist != NULL);
2477 assert(keylist != NULL);
2478 assert(listbox != NULL);
2479
2480 newtListboxClear(listbox);
2481// sort_mountlist_by_device (mountlist);
2482 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2483 keylist[i] = (void *) i;
2484 }
2485 for (i = 0; i < mountlist->entries; i++) {
2486 newtListboxAppendEntry(listbox,
2487 mountlist_entry_to_string(mountlist, i),
2488 keylist[i]);
2489 }
2490}
2491
2492
2493
2494
2495/**
2496 * Redraw the list of unallocated RAID partitions.
2497 * @param unallocated_raid_partitions The mountlist containing unallocated RAID partitions.
2498 * @param keylist The list of keys for @p listbox.
2499 * @param listbox The Newt listbox component to redraw.
2500 * @ingroup restoreGuiDisklist
2501 */
2502void redraw_unallocpartnslist(struct mountlist_itself
2503 *unallocated_raid_partitions,
2504 void *keylist[ARBITRARY_MAXIMUM],
2505 newtComponent listbox)
2506{
2507
2508 /** long *************************************************************/
2509 long i = 0;
2510
2511 /** buffers **********************************************************/
2512 char tmp[MAX_STR_LEN];
2513
2514 assert(unallocated_raid_partitions != NULL);
2515 assert(keylist != NULL);
2516 assert(listbox != NULL);
2517
2518 newtListboxClear(listbox);
2519 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2520 keylist[i] = (void *) i;
2521 }
2522 for (i = 0; i < unallocated_raid_partitions->entries; i++) {
2523 sprintf(tmp, "%-22s %8lld",
2524 unallocated_raid_partitions->el[i].device,
2525 unallocated_raid_partitions->el[i].size / 1024L);
2526 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2527 }
2528}
2529
2530#ifndef __FreeBSD__
2531/**
2532 * Redraw the list of additional RAID variables.
2533 * @param additional_vars The list of additional RAID varibals.
2534 * @param keylist The list of keys for @p listbox.
2535 * @param listbox The Newt listbox component to redraw.
2536 * @ingroup restoreGuiVarslist
2537 */
2538void
2539redraw_varslist(struct additional_raid_variables *additional_vars,
2540 void *keylist[], newtComponent listbox)
2541{
2542 /** long ************************************************************/
2543 long i = 0;
2544
2545 /** buffers *********************************************************/
2546 char tmp[MAX_STR_LEN];
2547
2548 assert(additional_vars != NULL);
2549 assert(keylist != NULL);
2550 assert(listbox != NULL);
2551
2552 newtListboxClear(listbox);
2553
2554 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2555 keylist[i] = (void *) i;
2556 }
2557 for (i = 0; i < additional_vars->entries; i++) {
2558 sprintf(tmp, "%-32s %-8s", additional_vars->el[i].label,
2559 additional_vars->el[i].value);
2560 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2561 }
2562}
2563
2564
2565/**
2566 * Remove variable @p label from the RAID variables list in @p raidrec.
2567 * @param raidrec The RAID device record to remove the variable from.
2568 * @param label The variable name to remove.
2569 * @return The value of the variable removed.
2570 * @ingroup restoreUtilityGroup
2571 */
2572int
2573read_variableINT_and_remove_from_raidvars(struct
2574 OSSWAP (raid_device_record,
2575 vinum_volume) * raidrec,
2576 char *label)
2577{
2578 /** int ***************************************************************/
2579 int i = 0;
2580 int res = 0;
2581
2582
2583 assert(raidrec != NULL);
2584 assert(label != NULL);
2585
2586 for (i = 0;
2587 i < raidrec->additional_vars.entries
2588 && strcmp(raidrec->additional_vars.el[i].label, label); i++);
2589 if (i == raidrec->additional_vars.entries) {
2590 res = -1;
2591 } else {
2592 res = atoi(raidrec->additional_vars.el[i].value);
2593 for (i++; i < raidrec->additional_vars.entries; i++) {
2594 memcpy((void *) &raidrec->additional_vars.el[i - 1],
2595 (void *) &raidrec->additional_vars.el[i],
2596 sizeof(struct raid_var_line));
2597 }
2598 raidrec->additional_vars.entries--;
2599 }
2600 return (res);
2601}
2602#endif
2603
2604/**
2605 * Change all RAID devices to use @p new_dev instead of @p old_dev.
2606 * @param raidlist The raidlist to make the changes in.
2607 * @param old_dev The old name of the device (what it used to be).
2608 * @param new_dev The new name of the device (what it is now).
2609 * @ingroup restoreGuiMountlist
2610 */
2611void rejig_partition_name_in_raidlist_if_necessary(struct raidlist_itself
2612 *raidlist,
2613 char *old_dev,
2614 char *new_dev)
2615{
2616 /** buffers ********************************************************/
2617 char tmp[MAX_STR_LEN];
2618
2619 /** int ************************************************************/
2620 int pos = 0;
2621 int j = 0;
2622
2623 assert(raidlist != NULL);
2624 assert_string_is_neither_NULL_nor_zerolength(old_dev);
2625 assert_string_is_neither_NULL_nor_zerolength(new_dev);
2626
2627 pos = which_raid_device_is_using_this_partition(raidlist, old_dev);
2628 if (pos < 0) {
2629 sprintf(tmp, "No need to rejig %s in raidlist: it's not listed.",
2630 old_dev);
2631 log_it(tmp);
2632 } else {
2633 if ((j =
2634 where_in_drivelist_is_drive(&raidlist->
2635 OSSWAP(el[pos].data_disks, disks),
2636 old_dev)) >= 0) {
2637 strcpy(raidlist->OSSWAP(el[pos].data_disks, disks).el[j].
2638 device, new_dev);
2639 } else
2640 if ((j =
2641 where_in_drivelist_is_drive(&raidlist->
2642 OSSWAP(el[pos].spare_disks,
2643 spares),
2644 old_dev)) >= 0) {
2645 strcpy(raidlist->OSSWAP(el[pos].spare_disks, spares).el[j].
2646 device, new_dev);
2647 }
2648#ifndef __FreeBSD__
2649 else if ((j =
2650 where_in_drivelist_is_drive(&raidlist->el[pos].
2651 parity_disks,
2652 old_dev)) >= 0) {
2653 strcpy(raidlist->el[pos].parity_disks.el[j].device, new_dev);
2654 } else
2655 if ((j =
2656 where_in_drivelist_is_drive(&raidlist->el[pos].
2657 failed_disks,
2658 old_dev)) >= 0) {
2659 strcpy(raidlist->el[pos].failed_disks.el[j].device, new_dev);
2660 }
2661#endif
2662 else {
2663 sprintf(tmp,
2664 "%s is supposed to be listed in this raid dev but it's not...",
2665 old_dev);
2666 log_it(tmp);
2667 }
2668 }
2669}
2670
2671
2672#ifndef __FreeBSD__
2673/**
2674 * Remove the essential RAID variables from the "additional variables" section.
2675 * If they have been changed, set them in their normal locations too.
2676 * @param raidrec The RAID device record to operate on.
2677 * @ingroup restoreUtilityVarslist
2678 */
2679void remove_essential_additionalvars(struct raid_device_record *raidrec)
2680{
2681
2682 /** int **************************************************************/
2683 int res = 0;
2684
2685 assert(raidrec != NULL);
2686
2687 res =
2688 read_variableINT_and_remove_from_raidvars(raidrec,
2689 "persistent-superblock");
2690 if (res > 0) {
2691 raidrec->persistent_superblock = res;
2692 }
2693 res = read_variableINT_and_remove_from_raidvars(raidrec, "chunk-size");
2694 if (res > 0) {
2695 raidrec->chunk_size = res;
2696 }
2697 res = read_variableINT_and_remove_from_raidvars(raidrec, "block-size");
2698}
2699
2700/**
2701 * Select the RAID disks to use in @p raidrec.
2702 * @param mountlist_dontedit The mountlist (will not be edited).
2703 * @param raidlist The raidlist to modify.
2704 * @param raidrec The RAID device record in @p raidlist to work on.
2705 * @param description_of_list The type of disks we're selecting (e.g. "data").
2706 * @param disklist The disklist to put the user-selected disks in.
2707 * @ingroup restoreGuiMountlist
2708 */
2709void
2710select_raid_disks(struct mountlist_itself *mountlist_dontedit,
2711 struct raidlist_itself *raidlist,
2712 struct raid_device_record *raidrec,
2713 char *description_of_list,
2714 struct list_of_disks *disklist)
2715{
2716 void *curr_choice;
2717
2718 /** ??? ***************************************************************/
2719
2720 /** structures ********************************************************/
2721 struct raidlist_itself *bkp_raidlist;
2722 struct raid_device_record *bkp_raidrec;
2723 struct list_of_disks *bkp_disklist;
2724 struct mountlist_itself *unallocated_raid_partitions;
2725
2726 /** newt **************************************************************/
2727 newtComponent myForm = NULL;
2728 newtComponent bAdd = NULL;
2729 newtComponent bDelete = NULL;
2730 newtComponent bOK = NULL;
2731 newtComponent bCancel = NULL;
2732 newtComponent b_res = NULL;
2733 newtComponent partitionsListbox = NULL;
2734 newtComponent headerMsg = NULL;
2735
2736 /** buffers **********************************************************/
2737 void *keylist[ARBITRARY_MAXIMUM];
2738 char *tmp;
2739 char *help_text;
2740 char *title_of_window;
2741 char *sz_res;
2742 char *header_text;
2743
2744 /** int **************************************************************/
2745 int i = 0;
2746 int currline = 0;
2747
2748 assert(mountlist_dontedit != NULL);
2749 assert(raidlist != NULL);
2750 assert(raidrec != NULL);
2751 assert(description_of_list != NULL);
2752 assert(disklist != NULL);
2753
2754 log_it("malloc'ing");
2755 malloc_string(tmp);
2756 malloc_string(help_text);
2757 malloc_string(title_of_window);
2758 malloc_string(sz_res);
2759 malloc_string(header_text);
2760 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
2761 fatal_error("Cannot malloc space for raidrec");
2762 }
2763 if (!(bkp_disklist = malloc(sizeof(struct list_of_disks)))) {
2764 fatal_error("Cannot malloc space for disklist");
2765 }
2766 if (!(bkp_raidlist = malloc(sizeof(struct raidlist_itself)))) {
2767 fatal_error("Cannot malloc space for raidlist");
2768 }
2769 if (!
2770 (unallocated_raid_partitions =
2771 malloc(sizeof(struct mountlist_itself)))) {
2772 fatal_error("Cannot malloc space for unallocated_raid_partitions");
2773 }
2774
2775 memcpy((void *) bkp_raidlist, (void *) raidlist,
2776 sizeof(struct raidlist_itself));
2777 memcpy((void *) bkp_raidrec, (void *) raidrec,
2778 sizeof(struct raid_device_record));
2779 memcpy((void *) bkp_disklist, (void *) disklist,
2780 sizeof(struct list_of_disks));
2781
2782 log_it("Post-malloc");
2783 strcpy(help_text,
2784 " Edit this RAID device's list of partitions. Choose OK or Cancel when done.");
2785 sprintf(header_text, "%-24s %s", "Device", "Index");
2786 sprintf(title_of_window, "%s contains...", raidrec->raid_device);
2787 newtPushHelpLine(help_text);
2788 for (b_res = (newtComponent) 12345; b_res != bOK && b_res != bCancel;) {
2789 headerMsg = newtLabel(1, 1, header_text);
2790 partitionsListbox =
2791 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2792 redraw_disklist(disklist, keylist, partitionsListbox);
2793 i = 1;
2794 bAdd = newtCompactButton(i, 9, " Add ");
2795 bDelete = newtCompactButton(i += 8, 9, "Delete");
2796 bOK = newtCompactButton(i += 9, 9, " OK ");
2797 bCancel = newtCompactButton(i += 9, 9, "Cancel");
2798 newtOpenWindow(21, 7, 38, 10, title_of_window);
2799 myForm = newtForm(NULL, NULL, 0);
2800 if (disklist->entries == 0) {
2801 newtFormAddComponents(myForm, headerMsg, bAdd, bDelete, bOK,
2802 bCancel, NULL);
2803 } else {
2804 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2805 bAdd, bDelete, bOK, bCancel, NULL);
2806 }
2807 b_res = newtRunForm(myForm);
2808 if (b_res == bOK || b_res == bCancel) { /* do nothing */
2809// That's OK. At the end of this subroutine (after this do/while loop),
2810// we'll throw away the changes if Cancel was pushed.
2811 } else {
2812 curr_choice = newtListboxGetCurrent(partitionsListbox);
2813 for (i = 0; i < disklist->entries && keylist[i] != curr_choice;
2814 i++);
2815 if (i == disklist->entries && disklist->entries > 0) {
2816 log_to_screen("I don't know what that button does!");
2817 } else {
2818 currline = i;
2819 if (b_res == bAdd) {
2820 log_it("Making list of unallocated RAID slices");
2821 make_list_of_unallocated_raid_partitions
2822 (unallocated_raid_partitions, mountlist_dontedit,
2823 raidlist);
2824 if (unallocated_raid_partitions->entries <= 0) {
2825 popup_and_OK
2826 ("There are no unallocated partitions marked for RAID.");
2827 } else {
2828 log_it
2829 ("Done. The user may add one or more of the above to RAID device");
2830 add_disklist_entry(disklist, raidrec->raid_device,
2831 unallocated_raid_partitions);
2832 log_it("I have finished adding a disklist entry.");
2833 redraw_disklist(disklist, keylist,
2834 partitionsListbox);
2835 }
2836 } else if (b_res == bDelete) {
2837 delete_disklist_entry(disklist, raidrec->raid_device,
2838 currline);
2839 redraw_disklist(disklist, keylist, partitionsListbox);
2840 } else {
2841 sprintf(tmp, "%s's index is %d. What should it be?",
2842 raidrec->raid_device,
2843 disklist->el[currline].index);
2844 sprintf(sz_res, "%d", disklist->el[currline].index);
2845 if (popup_and_get_string("Set index", tmp, sz_res, 10)) {
2846 disklist->el[currline].index = atoi(sz_res);
2847 }
2848 redraw_disklist(disklist, keylist, partitionsListbox);
2849 }
2850 }
2851 }
2852 newtFormDestroy(myForm);
2853 newtPopWindow();
2854 }
2855 newtPopHelpLine();
2856 if (b_res == bCancel) {
2857 memcpy((void *) raidlist, (void *) bkp_raidlist,
2858 sizeof(struct raidlist_itself));
2859 memcpy((void *) raidrec, (void *) bkp_raidrec,
2860 sizeof(struct raid_device_record));
2861 memcpy((void *) disklist, (void *) bkp_disklist,
2862 sizeof(struct list_of_disks));
2863 }
2864 paranoid_free(tmp);
2865 paranoid_free(help_text);
2866 paranoid_free(title_of_window);
2867 paranoid_free(sz_res);
2868 paranoid_free(header_text);
2869 paranoid_free(bkp_raidrec);
2870 paranoid_free(bkp_disklist);
2871 paranoid_free(bkp_raidlist);
2872 paranoid_free(unallocated_raid_partitions);
2873}
2874#endif
2875
2876
2877
2878/**
2879 * Ask the user which restore mode (nuke, interactive, or compare) we should use.
2880 * @return The mode selected: 'I' for interactive, 'N' for nuke, 'C' for compare,
2881 * or 'E' (or any other letter) for exit.
2882 */
2883char which_restore_mode()
2884{
2885
2886 /** char *************************************************************/
2887 char output = '\0';
2888 char tmp[MAX_STR_LEN];
2889
2890 /** newt *************************************************************/
2891
2892 newtComponent b1;
2893 newtComponent b2;
2894 newtComponent b3;
2895 newtComponent b4;
2896 newtComponent b_res;
2897 newtComponent myForm;
2898
2899 if (g_text_mode) {
2900 for (output = 'z'; !strchr("AICE", output); output = tmp[0]) {
2901 printf
2902 ("Which mode - (A)utomatic, (I)nteractive, \n(C)ompare only, or (E)xit to shell?\n--> ");
2903 fgets(tmp, MAX_STR_LEN - 1, stdin);
2904 }
2905 return (output);
2906 }
2907
2908 newtPushHelpLine
2909 (" Do you want to 'nuke' your system, restore interactively, or just compare?");
2910 newtOpenWindow(24, 3, 32, 17, "How should I restore?");
2911 b1 = newtButton(7, 1, "Automatically");
2912 b2 = newtButton(7, 5, "Interactively");
2913 b3 = newtButton(7, 9, "Compare only!");
2914 b4 = newtButton(7, 13, "Exit to shell");
2915 myForm = newtForm(NULL, NULL, 0);
2916 newtFormAddComponents(myForm, b1, b2, b3, b4, NULL);
2917 b_res = newtRunForm(myForm);
2918 newtFormDestroy(myForm);
2919 newtPopWindow();
2920 if (b_res == b1) {
2921 output = 'N';
2922 }
2923 if (b_res == b2) {
2924 output = 'I';
2925 }
2926 if (b_res == b3) {
2927 output = 'C';
2928 }
2929 if (b_res == b4) {
2930 output = 'E';
2931 }
2932 newtPopHelpLine();
2933 return (output);
2934}
Note: See TracBrowser for help on using the repository browser.