source: MondoRescue/branches/3.2/mondo/src/mondorestore/mondo-rstr-newt.c

Last change on this file was 3614, checked in by Bruno Cornec, 7 years ago

mr_strip_spaces now returns a dynamically allocated string

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