source: MondoRescue/branches/3.1/mondo/src/mondorestore/mondo-rstr-newt.c@ 3193

Last change on this file since 3193 was 3193, checked in by Bruno Cornec, 11 years ago
  • Finish with backports from 3.1 for now. Still some work to do, but we will now make that version compile and work again and serve as a base

so the gettext patch can be added

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