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

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

r3325@localhost: bruno | 2009-08-01 22:25:29 +0200
number_of_disks_as_string now alloctes memory

  • Property svn:keywords set to Id
File size: 86.4 KB
Line 
1/***************************************************************************
2 * $Id: mondo-rstr-newt.c 2314 2009-08-18 12:29:14Z 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 2314 2009-08-18 12:29:14Z 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 = NULL;
1564 char *sz_spare_disks = NULL;
1565 char *sz_parity_disks = NULL;
1566 char *sz_failed_disks = NULL;
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
1587 bkp_raidrec = mr_malloc(sizeof(struct raid_device_record));
1588 log_it("Started edit_raidlist_entry");
1589
1590 memcpy((void *) bkp_raidrec, (void *) raidrec, sizeof(struct raid_device_record));
1591 sprintf(title_of_editraidForm_window, "%s", raidrec->raid_device);
1592 log_msg(2, "Opening newt window");
1593 newtOpenWindow(20, 5, 40, 14, title_of_editraidForm_window);
1594 for (;;) {
1595 log_msg(2, "Main loop");
1596 sprintf(title_of_editraidForm_window, "Edit %s",
1597 raidrec->raid_device);
1598 strcpy(sz_raid_level,
1599 turn_raid_level_number_to_string(raidrec->raid_level));
1600 /* Those 4 strings are allocated by the function */
1601 sz_data_disks = number_of_disks_as_string(raidrec->data_disks.entries, "data");
1602 sz_spare_disks = number_of_disks_as_string(raidrec->spare_disks.entries, "spare");
1603 sz_parity_disks = number_of_disks_as_string(raidrec->parity_disks.entries, "parity");
1604 sz_failed_disks = number_of_disks_as_string(raidrec->failed_disks.entries, "failed");
1605 bSelectData = newtButton(1, 1, sz_data_disks);
1606 bSelectSpare = newtButton(20, 1, sz_spare_disks);
1607 bSelectParity = newtButton(1, 5, sz_parity_disks);
1608 bSelectFailed = newtButton(20, 5, sz_failed_disks);
1609 bChangeRaid = newtButton(1, 9, sz_raid_level);
1610 bOK = newtButton(16 + (raidrec->raid_level == -1), 9, " OK ");
1611 bCancel = newtButton(28, 9, "Cancel");
1612 bAdditional =
1613 newtCompactButton(1, 13,
1614 "Additional settings and information");
1615 newtPushHelpLine
1616 (" Edit the RAID device's settings to your heart's content, then hit OK/Cancel.");
1617 editraidForm = newtForm(NULL, NULL, 0);
1618 newtFormAddComponents(editraidForm, bSelectData, bSelectParity,
1619 bChangeRaid, bSelectSpare, bSelectFailed,
1620 bOK, bCancel, bAdditional);
1621 b_res = newtRunForm(editraidForm);
1622 if (b_res == bChangeRaid) {
1623 choose_raid_level(raidrec);
1624 } else if (b_res == bSelectData) {
1625 select_raid_disks(mountlist, raidlist, raidrec, "data", &raidrec->data_disks);
1626 } else if (b_res == bSelectSpare) {
1627 select_raid_disks(mountlist, raidlist, raidrec, "spare", &raidrec->spare_disks);
1628 } else if (b_res == bSelectParity) {
1629 select_raid_disks(mountlist, raidlist, raidrec, "parity", &raidrec->parity_disks);
1630 } else if (b_res == bSelectFailed) {
1631 select_raid_disks(mountlist, raidlist, raidrec, "failed", &raidrec->failed_disks);
1632 } else if (b_res == bAdditional) {
1633 edit_raidrec_additional_vars(raidrec);
1634 }
1635 newtFormDestroy(editraidForm);
1636 if (b_res == bOK || b_res == bCancel) {
1637 break;
1638 }
1639 mr_free(sz_data_disks);
1640 mr_free(sz_spare_disks);
1641 mr_free(sz_parity_disks);
1642 mr_free(sz_failed_disks);
1643 }
1644 if (b_res == bCancel) {
1645 memcpy((void *) raidrec, (void *) bkp_raidrec, sizeof(struct raid_device_record));
1646 }
1647 newtPopHelpLine();
1648 newtPopWindow();
1649 mountlist->el[currline].size = calculate_raid_device_size(mountlist, raidlist, raidrec->raid_device);
1650 paranoid_free(title_of_editraidForm_window);
1651 paranoid_free(sz_raid_level);
1652 paranoid_free(bkp_raidrec);
1653#endif
1654}
1655
1656#ifdef __FreeBSD__
1657
1658/**
1659 * Edit the plex @p raidrec in @p raidlist.
1660 * @param mountlist The mountlist to get some of the information from.
1661 * @param raidlist The raidlist containing information about RAID devices.
1662 * @param raidrec The plex to edit.
1663 * @param currline The line number (starting from 0) of the RAID device in @p mountlist.
1664 * @param currline2 The line number (starting from 0) of the plex within the RAID device.
1665 * @author Joshua Oreman
1666 * @ingroup restoreGuiMountlist
1667 */
1668void
1669edit_raidlist_plex(struct mountlist_itself *mountlist,
1670 struct raidlist_itself *raidlist,
1671 struct vinum_plex *raidrec, int currline, int currline2)
1672{
1673
1674 /** structures ********************************************************/
1675 struct vinum_plex bkp_raidrec;
1676
1677
1678 /** buffers ***********************************************************/
1679 char title_of_editraidForm_window[MAX_STR_LEN];
1680
1681 /** newt **************************************************************/
1682 newtComponent editraidForm;
1683 newtComponent bOK;
1684 newtComponent bCancel;
1685 newtComponent bEdit;
1686 newtComponent bAdd;
1687 newtComponent bDelete;
1688 newtComponent b_res;
1689 newtComponent unallocListbox, allocListbox;
1690 newtComponent bLevel, sLevel;
1691 newtComponent bStripeSize, sStripeSize;
1692 newtComponent bAlloc, bUnalloc;
1693
1694 void *keylist[ARBITRARY_MAXIMUM];
1695 void *curr_choice_a, *curr_choice_u;
1696 int currline_a, currline_u;
1697
1698 struct mountlist_itself *unallocparts;
1699
1700 unallocparts = malloc(sizeof(struct mountlist_itself));
1701
1702 log_it("Started edit_raidlist_entry");
1703 memcpy((void *) &bkp_raidrec, (void *) raidrec,
1704 sizeof(struct vinum_plex));
1705 sprintf(title_of_editraidForm_window, "%s.p%i",
1706 raidlist->el[currline].volname, currline2);
1707 newtPushHelpLine
1708 (" Please select a subdisk to edit, or edit this plex's parameters");
1709 newtOpenWindow(13, 3, 54, 18, title_of_editraidForm_window);
1710 for (;;) {
1711 int i;
1712 char headerstr[MAX_STR_LEN];
1713 char tmp[64];
1714 snprintf(headerstr, MAX_STR_LEN, "%-24s %s", "Subdisk", "Device");
1715
1716
1717 switch (raidrec->raidlevel) {
1718 case -1:
1719 strcpy(tmp, "concat");
1720 break;
1721 case 0:
1722 strcpy(tmp, "striped");
1723 break;
1724 case 5:
1725 strcpy(tmp, "raid5");
1726 break;
1727 default:
1728 sprintf(tmp, "unknown (%i)", raidrec->raidlevel);
1729 break;
1730 }
1731 bLevel = newtCompactButton(2, 2, " RAID level ");
1732 sLevel = newtLabel(19, 2, tmp);
1733
1734 if (raidrec->raidlevel >= 0) {
1735 sprintf(tmp, "%ik", raidrec->stripesize);
1736 bStripeSize = newtCompactButton(2, 4, " Stripe size ");
1737 } else {
1738 strcpy(tmp, "N/A");
1739 bStripeSize = newtLabel(2, 4, "Stripe size:");
1740 }
1741 sStripeSize = newtLabel(19, 4, tmp);
1742
1743 bOK = newtCompactButton(2, 16, " OK ");
1744 bCancel = newtCompactButton(12, 16, "Cancel");
1745 bAdd = newtCompactButton(22, 16, " Add ");
1746 bEdit = newtCompactButton(32, 16, " Edit ");
1747 bDelete = newtCompactButton(42, 16, "Delete");
1748
1749
1750 // plexesListbox = newtListbox (2, 7, 9, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1751 // plexesHeader = newtLabel (2, 6, headerstr);
1752 unallocListbox =
1753 newtListbox(2, 7, 7, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1754 allocListbox =
1755 newtListbox(33, 7, 7, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1756 bAlloc = newtButton(23, 7, " -> ");
1757 bUnalloc = newtButton(23, 11, " <- ");
1758
1759 editraidForm = newtForm(NULL, NULL, 0);
1760
1761 newtListboxClear(allocListbox);
1762 newtListboxClear(unallocListbox);
1763 bzero(unallocparts, sizeof(struct mountlist_itself));
1764 make_list_of_unallocated_raid_partitions(unallocparts, mountlist,
1765 raidlist);
1766 for (i = 0; i < ARBITRARY_MAXIMUM; ++i) {
1767 char entry[MAX_STR_LEN];
1768 keylist[i] = (void *) i;
1769 if (i < raidrec->subdisks) {
1770 snprintf(entry, MAX_STR_LEN, "%-17s",
1771 find_dev_entry_for_raid_device_name(raidlist,
1772 raidrec->
1773 sd[i].
1774 which_device));
1775 newtListboxAppendEntry(allocListbox, entry, keylist[i]);
1776 }
1777 if (i < unallocparts->entries) {
1778 snprintf(entry, MAX_STR_LEN, "%-17s",
1779 unallocparts->el[i].device);
1780 newtListboxAppendEntry(unallocListbox, entry, keylist[i]);
1781 }
1782 }
1783
1784#define COMP(x) newtFormAddComponent (editraidForm, x)
1785#define UCOMP(x) if (unallocparts->entries > 0) COMP(x)
1786#define ACOMP(x) if (raidrec->subdisks > 0) COMP(x)
1787 editraidForm = newtForm(NULL, NULL, 0);
1788 UCOMP(unallocListbox);
1789 UCOMP(bAlloc);
1790 ACOMP(allocListbox);
1791 ACOMP(bUnalloc);
1792 COMP(bOK);
1793 COMP(bCancel);
1794 COMP(bLevel);
1795 COMP(sLevel);
1796 if (raidrec->raidlevel != -1) {
1797 COMP(bStripeSize);
1798 COMP(sStripeSize);
1799 }
1800#undef COMP
1801#undef UCOMP
1802#undef ACOMP
1803
1804 newtRefresh();
1805 b_res = newtRunForm(editraidForm);
1806 if (b_res == bOK || b_res == bCancel) {
1807 break;
1808 }
1809
1810 curr_choice_a = (raidrec->subdisks > 0) ?
1811 newtListboxGetCurrent(allocListbox) : (void *) 1234;
1812 curr_choice_u = (unallocparts->entries > 0) ?
1813 newtListboxGetCurrent(unallocListbox) : (void *) 1234;
1814 for (currline_a = 0; currline_a < raidrec->subdisks; ++currline_a) {
1815 if (currline_a > ARBITRARY_MAXIMUM)
1816 break;
1817 if (keylist[currline_a] == curr_choice_a)
1818 break;
1819 }
1820 for (currline_u = 0; currline_u < unallocparts->entries;
1821 ++currline_u) {
1822 if (currline_u > ARBITRARY_MAXIMUM)
1823 break;
1824 if (keylist[currline_u] == curr_choice_u)
1825 break;
1826 }
1827 if (b_res == bLevel) {
1828 choose_raid_level(raidrec);
1829 } else if (b_res == bStripeSize) {
1830 char tmp[64];
1831 sprintf(tmp, "%i", raidrec->stripesize);
1832 if (popup_and_get_string
1833 ("Stripe size",
1834 "Please enter the stripe size in kilobytes.", tmp, 20)) {
1835 raidrec->stripesize = atoi(tmp);
1836 }
1837 } else if ((b_res == bAlloc) || (b_res == unallocListbox)) {
1838 if (currline_u <= unallocparts->entries)
1839 add_raid_subdisk(raidlist, raidrec,
1840 unallocparts->el[currline_u].device);
1841 } else if ((b_res == bUnalloc) || (b_res == allocListbox)) {
1842 if (currline_a <= raidrec->subdisks) {
1843 memcpy((void *) &raidrec->sd[currline_a],
1844 (void *) &raidrec->sd[raidrec->subdisks - 1],
1845 sizeof(struct vinum_subdisk));
1846 raidrec->subdisks--;
1847 }
1848 }
1849 newtFormDestroy(editraidForm);
1850 newtRefresh();
1851}
1852
1853if (b_res == bCancel) {
1854 memcpy((void *) raidrec, (void *) &bkp_raidrec, sizeof(struct vinum_plex));
1855}
1856newtPopWindow();
1857newtPopHelpLine();
1858}
1859#else
1860/**
1861 * Edit additional RAID variable number @p lino.
1862 * @param raidrec The RAID device record to edit the variable in.
1863 * @param lino The line number (starting from 0) of the variable to edit.
1864 * @ingroup restoreGuiVarslist
1865 */
1866void edit_varslist_entry(struct raid_device_record *raidrec, int lino)
1867{
1868
1869 /** buffers ***********************************************************/
1870 char header[MAX_STR_LEN];
1871 char comment[MAX_STR_LEN];
1872 char sz_out[MAX_STR_LEN];
1873
1874 assert(raidrec != 0);
1875 assert(lino >= 0);
1876
1877 strcpy(sz_out, raidrec->additional_vars.el[lino].value);
1878 sprintf(header, "Edit %s", raidrec->additional_vars.el[lino].label);
1879 sprintf(comment, "Please set %s's value (currently '%s')",
1880 raidrec->additional_vars.el[lino].label, sz_out);
1881 if (popup_and_get_string(header, comment, sz_out, MAX_STR_LEN)) {
1882 strip_spaces(sz_out);
1883 strcpy(raidrec->additional_vars.el[lino].value, sz_out);
1884 }
1885}
1886
1887#endif
1888
1889/**
1890 * Edit the mountlist using Newt.
1891 * @param mountlist The mountlist to edit.
1892 * @param raidlist The raidlist that goes with @p mountlist.
1893 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
1894 */
1895int
1896edit_mountlist_in_newt(char *mountlist_fname,
1897 struct mountlist_itself *mountlist,
1898 struct raidlist_itself *raidlist)
1899{
1900
1901 /** newt **************************************************************/
1902 newtComponent myForm;
1903 newtComponent bAdd;
1904 newtComponent bEdit;
1905 newtComponent bDelete;
1906 newtComponent bOK;
1907 newtComponent bCancel;
1908 newtComponent b_res = NULL;
1909 newtComponent partitionsListbox;
1910 newtComponent headerMsg;
1911 newtComponent flawsLabelA;
1912 newtComponent flawsLabelB;
1913 newtComponent flawsLabelC;
1914 newtComponent bReload;
1915
1916 /** ???? *************************************************************/
1917 void *curr_choice;
1918 void *keylist[ARBITRARY_MAXIMUM];
1919
1920 /** int **************************************************************/
1921 int i = 0;
1922 int currline = 0;
1923 int finished = FALSE;
1924
1925 /** buffers **********************************************************/
1926 char *tmp = NULL;
1927 char *flaws_str_A = NULL;
1928 char *flaws_str_B = NULL;
1929 char *flaws_str_C = NULL;
1930
1931 assert(mountlist != NULL);
1932 assert(raidlist != NULL);
1933
1934 mr_asprintf(&flaws_str_A, "%s", " ");
1935 mr_asprintf(&flaws_str_B, "%s", " ");
1936 mr_asprintf(&flaws_str_C, "%s", " ");
1937 if (mountlist->entries > ARBITRARY_MAXIMUM) {
1938 log_to_screen("Arbitrary limits suck, man!");
1939 finish(1);
1940 }
1941 newtPushHelpLine
1942 (" Please edit the mountlist to your satisfaction, then click OK or Cancel.");
1943 i = 4;
1944 bAdd = newtCompactButton(i, 17, " Add ");
1945 bEdit = newtCompactButton(i += 11, 17, " Edit ");
1946 bDelete = newtCompactButton(i += 12, 17, "Delete");
1947 bReload = newtCompactButton(i += 12, 17, "Reload");
1948 bCancel = newtCompactButton(i += 12, 17, "Cancel");
1949 bOK = newtCompactButton(i += 12, 17, " OK ");
1950 mr_asprintf(&tmp, "%-24s %-24s %-8s %s", "Device", "Mountpoint", "Format", "Size (MB)");
1951 headerMsg = newtLabel(2, 1, tmp);
1952 flawsLabelA = newtLabel(2, 13, flaws_str_A);
1953 flawsLabelB = newtLabel(2, 14, flaws_str_B);
1954 flawsLabelC = newtLabel(2, 15, flaws_str_C);
1955 partitionsListbox =
1956 newtListbox(2, 2, 10, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1957 redraw_mountlist(mountlist, keylist, partitionsListbox);
1958 newtOpenWindow(1, 3, 77, 18, "Editing mountlist");
1959 myForm = newtForm(NULL, NULL, 0);
1960 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
1961 flawsLabelA, flawsLabelB, flawsLabelC, bAdd,
1962 bEdit, bDelete, bReload, bCancel, bOK, NULL);
1963 while (!finished) {
1964 evaluate_mountlist(mountlist, flaws_str_A, flaws_str_B,
1965 flaws_str_C);
1966 newtLabelSetText(flawsLabelA, flaws_str_A);
1967 newtLabelSetText(flawsLabelB, flaws_str_B);
1968 newtLabelSetText(flawsLabelC, flaws_str_C);
1969 b_res = newtRunForm(myForm);
1970 if (b_res == bOK) {
1971 if (!evaluate_mountlist
1972 (mountlist, flaws_str_A, flaws_str_B, flaws_str_C)) {
1973 finished =
1974 ask_me_yes_or_no
1975 ("Your mountlist might not work. Continue anyway?");
1976 } else {
1977 finished =
1978 ask_me_yes_or_no
1979 ("Are you sure you want to save your mountlist and continue? (No changes will be made to your partition table at this time.)");
1980 }
1981 } else if (b_res == bCancel) {
1982 finished = TRUE;
1983 } else if (b_res == bReload) {
1984 if (ask_me_yes_or_no("Reload original mountlist?")) {
1985 load_mountlist(mountlist, mountlist_fname);
1986 load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
1987 redraw_mountlist(mountlist, keylist, partitionsListbox);
1988 }
1989 } else {
1990 curr_choice = newtListboxGetCurrent(partitionsListbox);
1991 for (i = 0;
1992 i < mountlist->entries && keylist[i] != curr_choice; i++);
1993 if (i == mountlist->entries && mountlist->entries > 0) {
1994 log_to_screen("I don't know what that button does!");
1995 } else {
1996 currline = i;
1997 if (b_res == bAdd) {
1998 add_mountlist_entry(mountlist, raidlist,
1999 partitionsListbox, currline,
2000 keylist);
2001 } else if (b_res == bDelete) {
2002 delete_mountlist_entry(mountlist, raidlist,
2003 partitionsListbox, currline,
2004 keylist);
2005 } else {
2006 if (mountlist->entries > 0) {
2007 edit_mountlist_entry(mountlist, raidlist,
2008 partitionsListbox, currline,
2009 keylist);
2010 } else {
2011 popup_and_OK
2012 ("Please add an entry. Then press ENTER to edit it.");
2013 }
2014 }
2015 }
2016 }
2017 }
2018 newtFormDestroy(myForm);
2019
2020 mr_free(flaws_str_A);
2021 mr_free(flaws_str_B);
2022 mr_free(flaws_str_C);
2023 mr_free(tmp);
2024
2025 newtPopWindow();
2026 newtPopHelpLine();
2027 if (b_res == bOK) {
2028 log_it("You pushed 'OK'. I shall now continue.");
2029 return (0);
2030 } else {
2031 /* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
2032 return (1);
2033 }
2034}
2035
2036
2037
2038/**
2039 * Edit the mountlist.
2040 * @param mountlist The mountlist to edit.
2041 * @param raidlist The raidlist that goes with @p mountlist.
2042 * @return 0 if the user pressed OK, 1 if they pressed Cancel.
2043 */
2044int
2045edit_mountlist(char *mountlist_fname, struct mountlist_itself *mountlist,
2046 struct raidlist_itself *raidlist)
2047{
2048 int res = 0;
2049
2050 log_it("entering eml");
2051
2052 if (g_text_mode) {
2053 fatal_error("Don't call edit_mountlist() in text mode");
2054 } else {
2055 log_it
2056 ("I'm in GUI mode, so I shall edit mountlist using edit_mountlist()");
2057 res = edit_mountlist_in_newt(mountlist_fname, mountlist, raidlist);
2058 }
2059 log_it("leaving eml");
2060 return (res);
2061}
2062
2063
2064
2065
2066#ifndef __FreeBSD__
2067/**
2068 * Edit the additional RAID variables in @p raidrec.
2069 * @param raidrec The RAID device record to edit the RAID variables in.
2070 * @ingroup restoreGuiVarslist
2071 */
2072void edit_raidrec_additional_vars(struct raid_device_record *raidrec)
2073{
2074
2075 /** structure *********************************************************/
2076 struct raid_device_record bkp_raidrec;
2077
2078 /** newt **************************************************************/
2079 newtComponent myForm;
2080 newtComponent bAdd;
2081 newtComponent bEdit;
2082 newtComponent bDelete;
2083 newtComponent bOK;
2084 newtComponent bCancel;
2085 newtComponent b_res;
2086 newtComponent varsListbox;
2087 newtComponent headerMsg;
2088
2089 /** ?? ***************************************************************/
2090 void *keylist[ARBITRARY_MAXIMUM], *curr_choice;
2091
2092 /** buffers **********************************************************/
2093 char title_of_window[MAX_STR_LEN];
2094
2095 /** int **************************************************************/
2096 int i = 0;
2097 int currline = 0;
2098
2099
2100 assert(raidrec != NULL);
2101
2102 memcpy((void *) &bkp_raidrec, (void *) raidrec,
2103 sizeof(struct raid_device_record));
2104 sprintf(title_of_window, "Additional variables");
2105 newtPushHelpLine
2106 (" Edit the additional fields to your heart's content, then click OK or Cancel.");
2107 headerMsg = newtLabel(1, 1, "Label Value");
2108 varsListbox =
2109 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2110 i = 1;
2111 bAdd = newtCompactButton(i, 9, " Add ");
2112 bEdit = newtCompactButton(i += 8, 9, " Edit ");
2113 bDelete = newtCompactButton(i += 9, 9, "Delete");
2114 bOK = newtCompactButton(i += 9, 9, " OK ");
2115 bCancel = newtCompactButton(i += 9, 9, "Cancel");
2116 newtOpenWindow(17, 7, 46, 10, title_of_window);
2117 myForm = newtForm(NULL, NULL, 0);
2118 newtFormAddComponents(myForm, headerMsg, varsListbox, bAdd, bEdit,
2119 bDelete, bOK, bCancel, NULL);
2120 insert_essential_additionalvars(raidrec);
2121 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2122 for (b_res = NULL; b_res != bOK && b_res != bCancel;) {
2123 b_res = newtRunForm(myForm);
2124 curr_choice = newtListboxGetCurrent(varsListbox);
2125 for (currline = 0;
2126 currline < raidrec->additional_vars.entries
2127 && keylist[currline] != curr_choice; currline++);
2128 if (currline == raidrec->additional_vars.entries
2129 && raidrec->additional_vars.entries > 0) {
2130 log_it("Warning - I don't know what this button does");
2131 }
2132 if (b_res == bOK) { /* do nothing */
2133 } else if (b_res == bCancel) { /* do nothing */
2134 } else if (b_res == bAdd) {
2135 add_varslist_entry(raidrec);
2136 } else if (b_res == bDelete) {
2137 delete_varslist_entry(raidrec, currline);
2138 } else {
2139 edit_varslist_entry(raidrec, currline);
2140 }
2141 redraw_varslist(&raidrec->additional_vars, keylist, varsListbox);
2142 }
2143 remove_essential_additionalvars(raidrec);
2144 newtFormDestroy(myForm);
2145 newtPopWindow();
2146 newtPopHelpLine();
2147 if (b_res == bCancel) {
2148 memcpy((void *) raidrec, (void *) &bkp_raidrec,
2149 sizeof(struct raid_device_record));
2150 }
2151 return;
2152}
2153#endif
2154
2155
2156/**
2157 * Find the next free location to place a disk in @p disklist.
2158 * @param disklist The disklist to operate on.
2159 * @return The next free location (starting from 0).
2160 * @ingroup restoreGuiDisklist
2161 */
2162int find_next_free_index_in_disklist(struct list_of_disks *disklist)
2163{
2164
2165 /** int ***************************************************************/
2166 int index = -1;
2167 int pos = 0;
2168
2169 /** bool **************************************************************/
2170 bool done;
2171
2172 assert(disklist != NULL);
2173
2174 for (done = FALSE; !done;) {
2175 for (pos = 0;
2176 pos < disklist->entries && disklist->el[pos].index <= index;
2177 pos++);
2178 if (pos >= disklist->entries) {
2179 done = TRUE;
2180 } else {
2181 index = disklist->el[pos].index;
2182 }
2183 }
2184 return (index + 1);
2185}
2186
2187
2188
2189/**
2190 * Locate @p device in @p raidlist.
2191 * @param raidlist The raidlist ot search in.
2192 * @param device The RAID device to search for.
2193 * @return The index of the device, or -1 if it could not be found.
2194 * @ingroup restoreGuiMountlist
2195 */
2196int
2197find_raid_device_in_raidlist(struct raidlist_itself *raidlist,
2198 char *device)
2199{
2200
2201 /** int ***************************************************************/
2202 int i = 0;
2203#ifdef __FreeBSD__
2204 char *vdev = NULL;
2205 int res = 0;
2206#else
2207// Linux
2208#endif
2209
2210 assert(raidlist != NULL);
2211 assert_string_is_neither_NULL_nor_zerolength(device);
2212
2213#ifdef __FreeBSD__
2214 for (i = 0; i < raidlist->entries; i++) {
2215 mr_asprintf(&vdev, "/dev/vinum/%s", raidlist->el[i].volname);
2216 res = strcmp(device, vdev);
2217 mr_free(vdev);
2218
2219 if (!res)
2220 break;
2221 }
2222#else
2223
2224 for (i = 0;
2225 strcmp(raidlist->el[i].raid_device, device)
2226 && i < raidlist->entries; i++);
2227#endif
2228 if (i == raidlist->entries) {
2229 return (-1);
2230 } else {
2231 return (i);
2232 }
2233}
2234
2235
2236/**
2237 * Get information about the location of ISO images from the user.
2238 * @param isodir_device Where to put the device (e.g. /dev/hda4) the user enters.
2239 * @param isodir_format Where to put the format (e.g. ext2) the user enters.
2240 * @param isodir_path Where to put the path (e.g. /var/cache/mondo) the user enters.
2241 * @param nuke_me_please Whether we're planning on nuking or not.
2242 * @return TRUE if OK was pressed, FALSE otherwise.
2243 */
2244bool
2245get_isodir_info(char *isodir_device, char *isodir_format,
2246 char *isodir_path, bool nuke_me_please)
2247{
2248
2249 /** initialize ********************************************************/
2250
2251 assert(isodir_device != NULL);
2252 assert(isodir_format != NULL);
2253 assert(isodir_path != NULL);
2254
2255 log_it("isodir_path = %s", isodir_path);
2256 isodir_format[0] = '\0';
2257 if (isodir_device[0] == '\0') {
2258 strcpy(isodir_device, "/dev/");
2259 }
2260 if (isodir_path[0] == '\0') {
2261 strcpy(isodir_path, "/");
2262 }
2263 if (does_file_exist("/tmp/NFS-SERVER-PATH")) {
2264 strcpy(isodir_device, last_line_of_file("/tmp/NFS-SERVER-MOUNT"));
2265 strcpy(isodir_format, "nfs");
2266 strcpy(isodir_path, last_line_of_file("/tmp/NFS-SERVER-PATH"));
2267 }
2268 if (nuke_me_please) {
2269 return (TRUE);
2270 }
2271
2272 if (popup_and_get_string
2273 ("ISO Mode - device", "On what device do the ISO files live?",
2274 isodir_device, MAX_STR_LEN / 4)) {
2275 if (popup_and_get_string
2276 ("ISO Mode - format",
2277 "What is the disk format of the device? (Hit ENTER if you don't know.)",
2278 isodir_format, 16)) {
2279 if (popup_and_get_string
2280 ("ISO Mode - path",
2281 "At what path on this device can the ISO files be found?",
2282 isodir_path, MAX_STR_LEN / 4)) {
2283 strip_spaces(isodir_device);
2284 strip_spaces(isodir_format);
2285 strip_spaces(isodir_path);
2286 log_it("isodir_device = %s - isodir_format = %s - isodir_path = %s", isodir_device, isodir_format, isodir_path);
2287 return (TRUE);
2288 }
2289 }
2290 }
2291 return (FALSE);
2292}
2293
2294
2295/**
2296 * Create a new raidtab entry for @p device in @p raidlist.
2297 * @param raidlist The raidlist to add the device to.
2298 * @param mountlist The mountlist containing information about the user's partitions.
2299 * @param currline The selected line in the mountlist.
2300 * @param device The RAID device (e.g. /dev/md0) to use.
2301 * @ingroup restoreGuiMountlist
2302 */
2303void
2304initiate_new_raidlist_entry(struct raidlist_itself *raidlist,
2305 struct mountlist_itself *mountlist,
2306 int currline, char *device)
2307{
2308
2309 /** structure *********************************************************/
2310 struct OSSWAP (raid_device_record, vinum_volume) * raidrec;
2311
2312 /** int ***************************************************************/
2313 int pos_in_raidlist = 0;
2314
2315 assert(raidlist != NULL);
2316 assert(mountlist != NULL);
2317 assert_string_is_neither_NULL_nor_zerolength(device);
2318
2319 pos_in_raidlist =
2320 find_raid_device_in_raidlist(raidlist,
2321 mountlist->el[currline].device);
2322 if (pos_in_raidlist >= 0) {
2323 fatal_error("Sorry, that RAID device already exists. Weird.");
2324 }
2325 pos_in_raidlist = raidlist->entries++;
2326 raidrec = &raidlist->el[pos_in_raidlist];
2327 initialize_raidrec(raidrec);
2328 strcpy(raidrec->OSSWAP(raid_device, volname),
2329 OSSWAP(device, basename(device)));
2330#ifndef __FreeBSD__
2331 choose_raid_level(raidrec);
2332 select_raid_disks(mountlist, raidlist, raidrec, "data",
2333 &raidrec->data_disks);
2334#endif
2335 edit_raidlist_entry(mountlist, raidlist, raidrec, currline);
2336}
2337
2338
2339#ifndef __FreeBSD__
2340/**
2341 * Insert the RAID variables not stored in the "additional RAID variables" list there too.
2342 * @param raidrec The RAID device record to operate on.
2343 * @ingroup restoreGuiVarslist
2344 */
2345void insert_essential_additionalvars(struct raid_device_record *raidrec)
2346{
2347
2348 /** int **************************************************************/
2349 int items = 0;
2350
2351 assert(raidrec != NULL);
2352
2353 items = raidrec->additional_vars.entries;
2354 write_variableINT_to_raid_var_line(raidrec, items++,
2355 "persistent-superblock",
2356 raidrec->persistent_superblock);
2357 write_variableINT_to_raid_var_line(raidrec, items++, "chunk-size",
2358 raidrec->chunk_size);
2359 raidrec->additional_vars.entries = items;
2360}
2361
2362#endif
2363
2364/**
2365 * Dummy function that proves that we can get to the point where Mondo is run.
2366 */
2367void nuke_mode_dummy()
2368{
2369
2370 /** newt *************************************************************/
2371 newtComponent myForm;
2372 newtComponent b1;
2373 newtComponent b2;
2374 newtComponent b3;
2375 newtComponent b_res;
2376
2377
2378 newtPushHelpLine
2379 ("This is where I nuke your hard drives. Mhahahahaha. No-one can stop Mojo Jojo!");
2380 newtOpenWindow(24, 3, 32, 13, "Nuking");
2381 b1 = newtButton(7, 1, "Slowly");
2382 b2 = newtButton(7, 5, "Medium");
2383 b3 = newtButton(7, 9, "Quickly");
2384 myForm = newtForm(NULL, NULL, 0);
2385 newtFormAddComponents(myForm, b1, b2, b3, NULL);
2386 b_res = newtRunForm(myForm);
2387 newtFormDestroy(myForm);
2388 newtPopWindow();
2389 newtPopHelpLine();
2390}
2391
2392
2393
2394/**
2395 * Redraw the disklist.
2396 * @param disklist The disklist to read from.
2397 * @param keylist The list of keys for @p listbox.
2398 * @param listbox The Newt listbox component to redraw.
2399 * @ingroup restoreGuiDisklist
2400 */
2401void
2402redraw_disklist(struct list_of_disks *disklist,
2403 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2404{
2405
2406 /** long **************************************************************/
2407 long i = 0;
2408
2409 assert(disklist != NULL);
2410 assert(keylist != NULL);
2411 assert(listbox != NULL);
2412
2413 newtListboxClear(listbox);
2414
2415 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2416 keylist[i] = (void *) i;
2417 }
2418 for (i = 0; i < disklist->entries; i++) {
2419 newtListboxAppendEntry(listbox,
2420 disklist_entry_to_string(disklist, i),
2421 keylist[i]);
2422 }
2423}
2424
2425
2426/**
2427 * Redraw the mountlist.
2428 * @param mountlist The mountlist to read from.
2429 * @param keylist The list of keys for @p listbox.
2430 * @param listbox The Newt listbox component to redraw.
2431 * @ingroup restoreGuiMountlist
2432 */
2433void
2434redraw_mountlist(struct mountlist_itself *mountlist,
2435 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
2436{
2437
2438 /** long **************************************************************/
2439 long i = 0;
2440
2441 assert(mountlist != NULL);
2442 assert(keylist != NULL);
2443 assert(listbox != NULL);
2444
2445 newtListboxClear(listbox);
2446// sort_mountlist_by_device (mountlist);
2447 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2448 keylist[i] = (void *) i;
2449 }
2450 for (i = 0; i < mountlist->entries; i++) {
2451 newtListboxAppendEntry(listbox,
2452 mountlist_entry_to_string(mountlist, i),
2453 keylist[i]);
2454 }
2455}
2456
2457
2458
2459
2460/**
2461 * Redraw the list of unallocated RAID partitions.
2462 * @param unallocated_raid_partitions The mountlist containing unallocated RAID partitions.
2463 * @param keylist The list of keys for @p listbox.
2464 * @param listbox The Newt listbox component to redraw.
2465 * @ingroup restoreGuiDisklist
2466 */
2467void redraw_unallocpartnslist(struct mountlist_itself
2468 *unallocated_raid_partitions,
2469 void *keylist[ARBITRARY_MAXIMUM],
2470 newtComponent listbox)
2471{
2472
2473 /** long *************************************************************/
2474 long i = 0;
2475
2476 /** buffers **********************************************************/
2477 char tmp[MAX_STR_LEN];
2478
2479 assert(unallocated_raid_partitions != NULL);
2480 assert(keylist != NULL);
2481 assert(listbox != NULL);
2482
2483 newtListboxClear(listbox);
2484 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2485 keylist[i] = (void *) i;
2486 }
2487 for (i = 0; i < unallocated_raid_partitions->entries; i++) {
2488 sprintf(tmp, "%-22s %8lld",
2489 unallocated_raid_partitions->el[i].device,
2490 unallocated_raid_partitions->el[i].size / 1024L);
2491 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2492 }
2493}
2494
2495#ifndef __FreeBSD__
2496/**
2497 * Redraw the list of additional RAID variables.
2498 * @param additional_vars The list of additional RAID varibals.
2499 * @param keylist The list of keys for @p listbox.
2500 * @param listbox The Newt listbox component to redraw.
2501 * @ingroup restoreGuiVarslist
2502 */
2503void
2504redraw_varslist(struct additional_raid_variables *additional_vars,
2505 void *keylist[], newtComponent listbox)
2506{
2507 /** long ************************************************************/
2508 long i = 0;
2509
2510 /** buffers *********************************************************/
2511 char tmp[MAX_STR_LEN];
2512
2513 assert(additional_vars != NULL);
2514 assert(keylist != NULL);
2515 assert(listbox != NULL);
2516
2517 newtListboxClear(listbox);
2518
2519 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
2520 keylist[i] = (void *) i;
2521 }
2522 for (i = 0; i < additional_vars->entries; i++) {
2523 sprintf(tmp, "%-32s %-8s", additional_vars->el[i].label,
2524 additional_vars->el[i].value);
2525 newtListboxAppendEntry(listbox, tmp, keylist[i]);
2526 }
2527}
2528
2529
2530/**
2531 * Remove variable @p label from the RAID variables list in @p raidrec.
2532 * @param raidrec The RAID device record to remove the variable from.
2533 * @param label The variable name to remove.
2534 * @return The value of the variable removed.
2535 * @ingroup restoreUtilityGroup
2536 */
2537int
2538read_variableINT_and_remove_from_raidvars(struct
2539 OSSWAP (raid_device_record,
2540 vinum_volume) * raidrec,
2541 char *label)
2542{
2543 /** int ***************************************************************/
2544 int i = 0;
2545 int res = 0;
2546
2547
2548 assert(raidrec != NULL);
2549 assert(label != NULL);
2550
2551 for (i = 0;
2552 i < raidrec->additional_vars.entries
2553 && strcmp(raidrec->additional_vars.el[i].label, label); i++);
2554 if (i == raidrec->additional_vars.entries) {
2555 res = -1;
2556 } else {
2557 res = atoi(raidrec->additional_vars.el[i].value);
2558 for (i++; i < raidrec->additional_vars.entries; i++) {
2559 memcpy((void *) &raidrec->additional_vars.el[i - 1],
2560 (void *) &raidrec->additional_vars.el[i],
2561 sizeof(struct raid_var_line));
2562 }
2563 raidrec->additional_vars.entries--;
2564 }
2565 return (res);
2566}
2567#endif
2568
2569/**
2570 * Change all RAID devices to use @p new_dev instead of @p old_dev.
2571 * @param raidlist The raidlist to make the changes in.
2572 * @param old_dev The old name of the device (what it used to be).
2573 * @param new_dev The new name of the device (what it is now).
2574 * @ingroup restoreGuiMountlist
2575 */
2576void rejig_partition_name_in_raidlist_if_necessary(struct raidlist_itself
2577 *raidlist,
2578 char *old_dev,
2579 char *new_dev)
2580{
2581 /** buffers ********************************************************/
2582 char *tmp = NULL;
2583
2584 /** int ************************************************************/
2585 int pos = 0;
2586 int j = 0;
2587
2588 assert(raidlist != NULL);
2589 assert_string_is_neither_NULL_nor_zerolength(old_dev);
2590 assert_string_is_neither_NULL_nor_zerolength(new_dev);
2591
2592 pos = which_raid_device_is_using_this_partition(raidlist, old_dev);
2593 if (pos < 0) {
2594 mr_asprintf(&tmp, "No need to rejig %s in raidlist: it's not listed.", old_dev);
2595 log_it(tmp);
2596 mr_free(tmp);
2597 } else {
2598 if ((j =
2599 where_in_drivelist_is_drive(&raidlist->
2600 OSSWAP(el[pos].data_disks, disks),
2601 old_dev)) >= 0) {
2602 strcpy(raidlist->OSSWAP(el[pos].data_disks, disks).el[j].
2603 device, new_dev);
2604 } else
2605 if ((j =
2606 where_in_drivelist_is_drive(&raidlist->
2607 OSSWAP(el[pos].spare_disks,
2608 spares),
2609 old_dev)) >= 0) {
2610 strcpy(raidlist->OSSWAP(el[pos].spare_disks, spares).el[j].
2611 device, new_dev);
2612 }
2613#ifndef __FreeBSD__
2614 else if ((j =
2615 where_in_drivelist_is_drive(&raidlist->el[pos].
2616 parity_disks,
2617 old_dev)) >= 0) {
2618 strcpy(raidlist->el[pos].parity_disks.el[j].device, new_dev);
2619 } else
2620 if ((j =
2621 where_in_drivelist_is_drive(&raidlist->el[pos].
2622 failed_disks,
2623 old_dev)) >= 0) {
2624 strcpy(raidlist->el[pos].failed_disks.el[j].device, new_dev);
2625 }
2626#endif
2627 else {
2628 mr_asprintf(&tmp, "%s is supposed to be listed in this raid dev but it's not...", old_dev);
2629 log_it(tmp);
2630 mr_free(tmp);
2631 }
2632 }
2633}
2634
2635
2636#ifndef __FreeBSD__
2637/**
2638 * Remove the essential RAID variables from the "additional variables" section.
2639 * If they have been changed, set them in their normal locations too.
2640 * @param raidrec The RAID device record to operate on.
2641 * @ingroup restoreUtilityVarslist
2642 */
2643void remove_essential_additionalvars(struct raid_device_record *raidrec)
2644{
2645
2646 /** int **************************************************************/
2647 int res = 0;
2648
2649 assert(raidrec != NULL);
2650
2651 res =
2652 read_variableINT_and_remove_from_raidvars(raidrec,
2653 "persistent-superblock");
2654 if (res > 0) {
2655 raidrec->persistent_superblock = res;
2656 }
2657 res = read_variableINT_and_remove_from_raidvars(raidrec, "chunk-size");
2658 if (res > 0) {
2659 raidrec->chunk_size = res;
2660 }
2661 res = read_variableINT_and_remove_from_raidvars(raidrec, "block-size");
2662}
2663
2664/**
2665 * Select the RAID disks to use in @p raidrec.
2666 * @param mountlist_dontedit The mountlist (will not be edited).
2667 * @param raidlist The raidlist to modify.
2668 * @param raidrec The RAID device record in @p raidlist to work on.
2669 * @param description_of_list The type of disks we're selecting (e.g. "data").
2670 * @param disklist The disklist to put the user-selected disks in.
2671 * @ingroup restoreGuiMountlist
2672 */
2673void
2674select_raid_disks(struct mountlist_itself *mountlist_dontedit,
2675 struct raidlist_itself *raidlist,
2676 struct raid_device_record *raidrec,
2677 char *description_of_list,
2678 struct list_of_disks *disklist)
2679{
2680 void *curr_choice;
2681
2682 /** ??? ***************************************************************/
2683
2684 /** structures ********************************************************/
2685 struct raidlist_itself *bkp_raidlist;
2686 struct raid_device_record *bkp_raidrec;
2687 struct list_of_disks *bkp_disklist;
2688 struct mountlist_itself *unallocated_raid_partitions;
2689
2690 /** newt **************************************************************/
2691 newtComponent myForm = NULL;
2692 newtComponent bAdd = NULL;
2693 newtComponent bDelete = NULL;
2694 newtComponent bOK = NULL;
2695 newtComponent bCancel = NULL;
2696 newtComponent b_res = NULL;
2697 newtComponent partitionsListbox = NULL;
2698 newtComponent headerMsg = NULL;
2699
2700 /** buffers **********************************************************/
2701 void *keylist[ARBITRARY_MAXIMUM];
2702 char *tmp;
2703 char *help_text;
2704 char *title_of_window;
2705 char *sz_res;
2706 char *header_text;
2707
2708 /** int **************************************************************/
2709 int i = 0;
2710 int currline = 0;
2711
2712 assert(mountlist_dontedit != NULL);
2713 assert(raidlist != NULL);
2714 assert(raidrec != NULL);
2715 assert(description_of_list != NULL);
2716 assert(disklist != NULL);
2717
2718 log_it("malloc'ing");
2719 malloc_string(tmp);
2720 malloc_string(help_text);
2721 malloc_string(title_of_window);
2722 malloc_string(sz_res);
2723 malloc_string(header_text);
2724 if (!(bkp_raidrec = malloc(sizeof(struct raid_device_record)))) {
2725 fatal_error("Cannot malloc space for raidrec");
2726 }
2727 if (!(bkp_disklist = malloc(sizeof(struct list_of_disks)))) {
2728 fatal_error("Cannot malloc space for disklist");
2729 }
2730 if (!(bkp_raidlist = malloc(sizeof(struct raidlist_itself)))) {
2731 fatal_error("Cannot malloc space for raidlist");
2732 }
2733 if (!
2734 (unallocated_raid_partitions =
2735 malloc(sizeof(struct mountlist_itself)))) {
2736 fatal_error("Cannot malloc space for unallocated_raid_partitions");
2737 }
2738
2739 memcpy((void *) bkp_raidlist, (void *) raidlist,
2740 sizeof(struct raidlist_itself));
2741 memcpy((void *) bkp_raidrec, (void *) raidrec,
2742 sizeof(struct raid_device_record));
2743 memcpy((void *) bkp_disklist, (void *) disklist,
2744 sizeof(struct list_of_disks));
2745
2746 log_it("Post-malloc");
2747 strcpy(help_text,
2748 " Edit this RAID device's list of partitions. Choose OK or Cancel when done.");
2749 sprintf(header_text, "%-24s %s", "Device", "Index");
2750 sprintf(title_of_window, "%s contains...", raidrec->raid_device);
2751 newtPushHelpLine(help_text);
2752 for (b_res = (newtComponent) 12345; b_res != bOK && b_res != bCancel;) {
2753 headerMsg = newtLabel(1, 1, header_text);
2754 partitionsListbox =
2755 newtListbox(1, 2, 6, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
2756 redraw_disklist(disklist, keylist, partitionsListbox);
2757 i = 1;
2758 bAdd = newtCompactButton(i, 9, " Add ");
2759 bDelete = newtCompactButton(i += 8, 9, "Delete");
2760 bOK = newtCompactButton(i += 9, 9, " OK ");
2761 bCancel = newtCompactButton(i += 9, 9, "Cancel");
2762 newtOpenWindow(21, 7, 38, 10, title_of_window);
2763 myForm = newtForm(NULL, NULL, 0);
2764 if (disklist->entries == 0) {
2765 newtFormAddComponents(myForm, headerMsg, bAdd, bDelete, bOK,
2766 bCancel, NULL);
2767 } else {
2768 newtFormAddComponents(myForm, headerMsg, partitionsListbox,
2769 bAdd, bDelete, bOK, bCancel, NULL);
2770 }
2771 b_res = newtRunForm(myForm);
2772 if (b_res == bOK || b_res == bCancel) { /* do nothing */
2773// That's OK. At the end of this subroutine (after this do/while loop),
2774// we'll throw away the changes if Cancel was pushed.
2775 } else {
2776 curr_choice = newtListboxGetCurrent(partitionsListbox);
2777 for (i = 0; i < disklist->entries && keylist[i] != curr_choice;
2778 i++);
2779 if (i == disklist->entries && disklist->entries > 0) {
2780 log_to_screen("I don't know what that button does!");
2781 } else {
2782 currline = i;
2783 if (b_res == bAdd) {
2784 log_it("Making list of unallocated RAID slices");
2785 make_list_of_unallocated_raid_partitions
2786 (unallocated_raid_partitions, mountlist_dontedit,
2787 raidlist);
2788 if (unallocated_raid_partitions->entries <= 0) {
2789 popup_and_OK
2790 ("There are no unallocated partitions marked for RAID.");
2791 } else {
2792 log_it
2793 ("Done. The user may add one or more of the above to RAID device");
2794 add_disklist_entry(disklist, raidrec->raid_device,
2795 unallocated_raid_partitions);
2796 log_it("I have finished adding a disklist entry.");
2797 redraw_disklist(disklist, keylist,
2798 partitionsListbox);
2799 }
2800 } else if (b_res == bDelete) {
2801 delete_disklist_entry(disklist, raidrec->raid_device,
2802 currline);
2803 redraw_disklist(disklist, keylist, partitionsListbox);
2804 } else {
2805 sprintf(tmp, "%s's index is %d. What should it be?",
2806 raidrec->raid_device,
2807 disklist->el[currline].index);
2808 sprintf(sz_res, "%d", disklist->el[currline].index);
2809 if (popup_and_get_string("Set index", tmp, sz_res, 10)) {
2810 disklist->el[currline].index = atoi(sz_res);
2811 }
2812 redraw_disklist(disklist, keylist, partitionsListbox);
2813 }
2814 }
2815 }
2816 newtFormDestroy(myForm);
2817 newtPopWindow();
2818 }
2819 newtPopHelpLine();
2820 if (b_res == bCancel) {
2821 memcpy((void *) raidlist, (void *) bkp_raidlist,
2822 sizeof(struct raidlist_itself));
2823 memcpy((void *) raidrec, (void *) bkp_raidrec,
2824 sizeof(struct raid_device_record));
2825 memcpy((void *) disklist, (void *) bkp_disklist,
2826 sizeof(struct list_of_disks));
2827 }
2828 paranoid_free(tmp);
2829 paranoid_free(help_text);
2830 paranoid_free(title_of_window);
2831 paranoid_free(sz_res);
2832 paranoid_free(header_text);
2833 paranoid_free(bkp_raidrec);
2834 paranoid_free(bkp_disklist);
2835 paranoid_free(bkp_raidlist);
2836 paranoid_free(unallocated_raid_partitions);
2837}
2838#endif
2839
2840
2841
2842/**
2843 * Ask the user which restore mode (nuke, interactive, or compare) we should use.
2844 * @return The mode selected: 'I' for interactive, 'N' for nuke, 'C' for compare,
2845 * or 'E' (or any other letter) for exit.
2846 */
2847char which_restore_mode()
2848{
2849
2850 /** char *************************************************************/
2851 char output = '\0';
2852 char tmp[MAX_STR_LEN];
2853
2854 /** newt *************************************************************/
2855
2856 newtComponent b1;
2857 newtComponent b2;
2858 newtComponent b3;
2859 newtComponent b4;
2860 newtComponent b_res;
2861 newtComponent myForm;
2862
2863 if (g_text_mode) {
2864 for (output = 'z'; !strchr("AICE", output); output = tmp[0]) {
2865 printf
2866 ("Which mode - (A)utomatic, (I)nteractive, \n(C)ompare only, or (E)xit to shell?\n--> ");
2867 fgets(tmp, MAX_STR_LEN - 1, stdin);
2868 }
2869 return (output);
2870 }
2871
2872 newtPushHelpLine
2873 (" Do you want to 'nuke' your system, restore interactively, or just compare?");
2874 newtOpenWindow(24, 3, 32, 17, "How should I restore?");
2875 b1 = newtButton(7, 1, "Automatically");
2876 b2 = newtButton(7, 5, "Interactively");
2877 b3 = newtButton(7, 9, "Compare only!");
2878 b4 = newtButton(7, 13, "Exit to shell");
2879 myForm = newtForm(NULL, NULL, 0);
2880 newtFormAddComponents(myForm, b1, b2, b3, b4, NULL);
2881 b_res = newtRunForm(myForm);
2882 newtFormDestroy(myForm);
2883 newtPopWindow();
2884 if (b_res == b1) {
2885 output = 'N';
2886 }
2887 if (b_res == b2) {
2888 output = 'I';
2889 }
2890 if (b_res == b3) {
2891 output = 'C';
2892 }
2893 if (b_res == b4) {
2894 output = 'E';
2895 }
2896 newtPopHelpLine();
2897 return (output);
2898}
Note: See TracBrowser for help on using the repository browser.