source: MondoRescue/branches/2.2.10/mondo/src/mondorestore/mondo-rstr-newt.c@ 2300

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