source: MondoRescue/branches/3.3/mondo/src/mondorestore/mondo-rstr-newt.c@ 3879

Last change on this file since 3879 was 3879, checked in by Bruno Cornec, 3 months ago

Fix all remaining compiler errors

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