source: MondoRescue/branches/2.2.5/mondo/src/mondorestore/mondo-rstr-newt.c@ 1547

Last change on this file since 1547 was 738, checked in by andree, 18 years ago

Fixed compiler warnings on amd64 (and likely 64bit in general):

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