source: MondoRescue/branches/2.2.8/mondo/src/mondorestore/mondo-rstr-newt.c@ 2080

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