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

Last change on this file was 3893, checked in by Bruno Cornec, 2 months ago

Remove more warnings - switch and size_t/int comparisons

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