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
RevLine 
[1]1/***************************************************************************
[2094]2 * $Id: mondo-rstr-newt.c 3893 2024-03-13 15:49:30Z bruno $
[1]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
[3879]12#include "my-stuff.h"
13#include "mondostructures.h"
[2211]14#include "mr_mem.h"
[3193]15#include "mr_str.h"
[3879]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"
[1]23
[128]24//static char cvsid[] = "$Id: mondo-rstr-newt.c 3893 2024-03-13 15:49:30Z bruno $";
[1]25
[2770]26extern bool popup_with_buttons(char *p, char *button1, char *button2);
[1]27extern char err_log_lines[NOOF_ERR_LINES][MAX_STR_LEN];
28
[3879]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
[1]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/**
[3879]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
[1]63 */
[3879]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
[1]93/**
[3879]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/**
[1]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
[128]136add_disklist_entry(struct list_of_disks *disklist, char *raid_device,
137 struct mountlist_itself *unallocated_raid_partitions)
[1]138{
139 /** buffers ***********************************************************/
[3193]140 char *tmp = NULL;
[1]141
142 /** newt **************************************************************/
[128]143 newtComponent myForm;
[1]144 newtComponent bOK;
145 newtComponent bCancel;
146 newtComponent b_res;
147 newtComponent partitionsListbox;
148 newtComponent headerMsg;
[128]149
[1]150 /** prototypes *********************************************************/
[128]151 void *keylist[ARBITRARY_MAXIMUM];
152 void *curr_choice;
[1]153
154 /** int ****************************************************************/
[128]155 int i = 0;
[1]156 int index = 0;
157 int currline = 0;
158 int items = 0;
159
[128]160 assert(disklist != NULL);
161 assert_string_is_neither_NULL_nor_zerolength(raid_device);
162 assert(unallocated_raid_partitions != NULL);
[1]163
[128]164 newtPushHelpLine
[541]165 (" Add one of the following unallocated RAID partitions to this RAID device.");
[3193]166 mr_asprintf(tmp, "%-26s %s", "Device", "Size");
[128]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;
[541]173 bOK = newtCompactButton(i, 9, " OK ");
174 bCancel = newtCompactButton(i += 9, 9, "Cancel");
175 newtOpenWindow(22, 6, 36, 10, "Unallocated RAID partitions");
[128]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;
[3193]192 strcpy(disklist->el[items].device, unallocated_raid_partitions->el[currline].device);
[128]193 disklist->el[items].index = index;
194 disklist->entries = ++items;
195
196 }
[1]197 }
[128]198 newtFormDestroy(myForm);
[3193]199 mr_free(tmp);
[128]200 newtPopWindow();
201 newtPopHelpLine();
[1]202}
203
204
[3879]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{
[1]216
[3879]217 /** int ***************************************************************/
218 int i = 0;
219#ifdef __FreeBSD__
220 char *vdev = NULL;
221 int res = 0;
222#else
223// Linux
224#endif
[1]225
[3879]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
[1]252/**
[3879]253 * Redraw the disklist.
254 * @param disklist The disklist to read from.
[1]255 * @param keylist The list of keys for @p listbox.
[3879]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.
[1]331 * @ingroup restoreGuiMountlist
332 */
333void
[3879]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)
[1]339{
[3879]340 void *curr_choice;
[1]341
[3879]342 /** ??? ***************************************************************/
[1]343
[3879]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;
[1]349
[3879]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
[1]360 /** buffers **********************************************************/
[3879]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;
[1]367
[3879]368 /** int **************************************************************/
369 int i = 0;
370 int currline = 0;
[1]371
[3879]372 assert(mountlist_dontedit != NULL);
[128]373 assert(raidlist != NULL);
[3879]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);
[128]523 assert(listbox != NULL);
[1]524
[3879]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 */
[3892]617void choose_raid_level(struct OSSWAP (raid_device_record, vinum_plex) * raidrec) {
[3879]618
[1]619#ifdef __FreeBSD__
[3879]620
621 /** int ***************************************************************/
622 int out = 0;
623
624 /** buffers ***********************************************************/
[3892]625 char *tmp = NULL;
626 char *tmp1 = NULL;
[3879]627 char *prompt = NULL;
[3892]628 char *sz = NULL;
[3879]629
630 mr_asprintf(prompt, "Please enter the RAID level you want. (concat, striped, raid5)");
631 if (raidrec->raidlevel == -1) {
[3892]632 mr_asprintf(tmp, "%s", "concat");
[3879]633 } else if (raidrec->raidlevel == 0) {
[3892]634 mr_asprintf(tmp, "%s", "striped");
[3879]635 } else {
[3892]636 mr_asprintf(tmp, "raid%i", raidrec->raidlevel);
[3879]637 }
638 for (out = 999; out == 999;) {
[3892]639 tmp1 = mr_popup_and_get_string("Specify RAID level", prompt, tmp);
640 if (tmp1 == NULL) {
[3879]641 mr_free(prompt);
[3892]642 mr_free(tmp);
[3879]643 return;
644 }
[3892]645 mr_free(tmp);
646 tmp = mr_strip_spaces(tmp1);
647 log_it(tmp);
648
[3879]649 if (tmp[0] == '[' && tmp[strlen(tmp) - 1] == ']') {
[3892]650 sz = tmp;
651 // safe as shorter
652 mr_strncpy(tmp, sz + 1, strlen(sz) - 2);
[3879]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 }
[3892]662 mr_free(tmp);
663
[3879]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;
[3892]670 if (raidrec->raid_level == -1) {
671 mr_asprintf(tmp, "%s", "linear");
672 } else {
673 mr_asprintf(tmp, "%d", raidrec->raid_level);
674 }
[3879]675 }
676 }
677 }
678 mr_free(prompt);
[3892]679 mr_free(tmp);
[3879]680
681 raidrec->raidlevel = out;
[1]682#else
[3879]683 /** buffers ***********************************************************/
[3892]684 char *tmp = NULL;
685 char *tmp1 = NULL;
[3879]686 char *personalities = NULL;
687 char *prompt = NULL;
[3892]688 char *sz;
[3879]689 int out = 0;
690
691
692 assert(raidrec != NULL);
[3892]693 paranoid_system("grep Pers " MDSTAT_FILE " > /tmp/raid-personalities.txt 2> /dev/null");
[3879]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) {
[3892]699 mr_asprintf(tmp, "%s", "linear");
[3879]700 } else {
[3892]701 mr_asprintf(tmp, "%d", raidrec->raid_level);
[3879]702 }
703 for (out = 999;
704 out != -1 && out != 0 && out != 1 && out != 4 && out != 5
705 && out != 10;) {
[3892]706 tmp1 = mr_popup_and_get_string("Specify RAID level", prompt, tmp);
707 if (tmp1 == NULL) {
708 mr_free(tmp);
709 mr_free(prompt);
[3879]710 return;
711 }
[3892]712 mr_free(tmp);
713 tmp = mr_strip_spaces(tmp1);
714 log_it(tmp);
715
[3879]716 if (tmp[0] == '[' && tmp[strlen(tmp) - 1] == ']') {
[3892]717 sz = tmp;
718 // safe as shorter
719 mr_strncpy(tmp, sz + 1, strlen(sz) - 2);
[3879]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 }
[3892]729 mr_free(tmp);
730
[3879]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;
[3892]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 }
[3879]742 }
743 }
744 }
745 mr_free(prompt);
[3892]746 mr_free(tmp);
[3879]747
748 raidrec->raid_level = out;
[1]749#endif
[3879]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);
[128]807 newtPushHelpLine
[3879]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);
[3193]811
[3879]812 for (;;) {
813 int i;
[3193]814
[3879]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:
[3893]826 mr_asprintf(tmp, "unknown (%d)", raidrec->raidlevel);
[3879]827 break;
828 }
829 bLevel = newtCompactButton(2, 2, " RAID level ");
830 sLevel = newtLabel(19, 2, tmp);
831 mr_free(tmp);
[3193]832
[3879]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);
[3193]842
[3879]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);
[128]870 }
[3879]871 if (i < unallocparts->entries) {
872 mr_asprintf(entry, "%-17s", unallocparts->el[i].device);
873 newtListboxAppendEntry(unallocListbox, entry, keylist[i]);
874 mr_free(entry);
[128]875 }
876 }
[3193]877
[3879]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
[3193]897
[3879]898 newtDrawForm(editraidForm);
899 newtRefresh();
900 b_res = newtRunForm(editraidForm);
901 if (b_res == bOK || b_res == bCancel) {
902 break;
903 }
[3193]904
[3879]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}
[3193]947
[3879]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);
[128]978 }
[3879]979 mr_free(header);
980 mr_free(comment);
[1]981}
[3879]982#endif
[1]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 */
[128]991void add_varslist_entry(struct raid_device_record *raidrec)
[1]992{
993
994 /** buffers ***********************************************************/
[128]995 char sz_out[MAX_STR_LEN];
[1]996
997 /** int ****************************************************************/
[128]998 int items = 0;
[1]999 int i = 0;
1000
[128]1001 assert(raidrec != NULL);
[1]1002
[128]1003 sz_out[0] = '\0';
1004 if (popup_and_get_string
[541]1005 ("Add variable", "Enter the name of the variable to add", sz_out,
[128]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
[541]1014 ("No need to add that variable. It is already listed here.");
[128]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 }
[1]1020 }
1021}
1022#endif
1023
[3879]1024
1025#ifndef __FreeBSD__
[1]1026/**
[3879]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/**
[1]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
[128]1164calculate_raid_device_size(struct mountlist_itself *mountlist,
1165 struct raidlist_itself *raidlist,
1166 char *raid_device)
[1]1167{
1168#ifdef __FreeBSD__
[128]1169 /** FreeBSD-specific version of calculate_raid_device_size() **/
[1]1170
1171 /** structures ********************************************************/
[128]1172 struct vinum_volume *raidrec;
[1]1173
1174 /** int ***************************************************************/
[128]1175 int i = 0, j = 0;
[1]1176 int noof_partitions = 0;
1177
1178 /** long **************************************************************/
[128]1179 long total_size = 0;
1180 long plex_size = 0;
1181 long smallest_partition = 999999999;
1182 long smallest_plex = 999999999;
[1]1183 long sp = 0;
1184
1185 /** buffers ***********************************************************/
[3193]1186 char tmp = NULL;
1187 char *devname = NULL;
[1]1188
[128]1189 for (i = 0;
1190 i < raidlist->entries
1191 && strcmp(raidlist->el[i].volname, basename(raid_device)); i++);
1192 if (i == raidlist->entries) {
[3193]1193 log_it("Cannot calc size of raid device %s - cannot find it in raidlist", raid_device);
[128]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) {
[3193]1205 mr_asprintf(devname, "%s", raidrec->plex[j].sd[k].which_device);
[128]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;
[3893]1230 default:
1231 log_msg("Unknown raidlevel %d",raidrec->plex[j].raidlevel);
1232 break;
[128]1233 }
1234 }
1235 }
[3193]1236 mr_free(devname);
[128]1237 }
[1]1238
[128]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;
[1]1253
[128]1254 smallest_partition = 999999999;
1255 }
1256
[3193]1257 log_it("I have calculated %s's real size to be %ld", raid_device, (long) smallest_plex);
[128]1258 return (smallest_plex);
[1]1259#else
[128]1260 /** Linux-specific version of calculate_raid_device_size() **/
[1]1261
1262 /** structures ********************************************************/
[128]1263 struct raid_device_record *raidrec;
[1]1264
1265 /** int ***************************************************************/
[128]1266 int i = 0;
[1]1267 int noof_partitions = 0;
1268
1269 /** long **************************************************************/
[128]1270 long total_size = 0;
[1]1271 long smallest_partition = 999999999;
1272 long sp = 0;
1273
[128]1274 assert(mountlist != NULL);
1275 assert(raidlist != NULL);
1276 assert_string_is_neither_NULL_nor_zerolength(raid_device);
[1]1277
[128]1278 for (i = 0;
1279 i < raidlist->entries
1280 && strcmp(raidlist->el[i].raid_device, raid_device); i++);
1281 if (i == raidlist->entries) {
[3193]1282 log_it("Cannot calc size of raid device %s - cannot find it in raidlist", raid_device);
[128]1283 return (999999999);
[1]1284 }
[128]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);
[1]1304 }
[3193]1305 log_it("I have calculated %s's real size to be %ld", raid_device, (long) total_size);
[128]1306 return (total_size);
[1]1307#endif
1308}
1309
1310
1311
1312/**
[3879]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.
[1]1318 * @ingroup restoreGuiMountlist
1319 */
[3879]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)
[1]1324{
1325
1326#ifdef __FreeBSD__
[3879]1327 /** structures ********************************************************/
1328 struct vinum_volume bkp_raidrec;
[128]1329
[1]1330
1331 /** buffers ***********************************************************/
[3879]1332 char title_of_editraidForm_window[MAX_STR_LEN];
[1]1333
[3879]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 }
[128]1413 }
[3879]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;
[128]1421 }
[3879]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;
[128]1429 }
[3879]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--;
[128]1442 }
[3879]1443 continue;
[128]1444 }
[3879]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);
[1]1454 }
[3879]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;
[3193]1466
[3879]1467
[1]1468 /** buffers ***********************************************************/
[3879]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;
[1]1475
[3879]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;
[1]1487
[3879]1488 assert(mountlist != NULL);
1489 assert(raidlist != NULL);
[128]1490 assert(raidrec != NULL);
[3193]1491
[3879]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);
[128]1537 }
[3879]1538 newtFormDestroy(editraidForm);
1539 if (b_res == bOK || b_res == bCancel) {
1540 break;
[128]1541 }
[3879]1542 mr_free(sz_data_disks);
1543 mr_free(sz_spare_disks);
1544 mr_free(sz_parity_disks);
1545 mr_free(sz_failed_disks);
[1]1546 }
[3879]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);
[1]1556#endif
1557}
1558
1559
1560/**
[3879]1561 * Create a new raidtab entry for @p device in @p raidlist.
1562 * @param raidlist The raidlist to add the device to.
[1]1563 * @param mountlist The mountlist containing information about the user's partitions.
[3879]1564 * @param currline The selected line in the mountlist.
1565 * @param device The RAID device (e.g. /dev/md0) to use.
1566 * @ingroup restoreGuiMountlist
[1]1567 */
1568void
[3879]1569initiate_new_raidlist_entry(struct raidlist_itself *raidlist,
1570 struct mountlist_itself *mountlist,
1571 int currline, char *device)
[1]1572{
1573
[3879]1574 /** structure *********************************************************/
1575 struct OSSWAP (raid_device_record, vinum_volume) * raidrec;
1576
[1]1577 /** int ***************************************************************/
[3879]1578 int pos_in_raidlist = 0;
[1]1579
[3879]1580 assert(raidlist != NULL);
[128]1581 assert(mountlist != NULL);
[3879]1582 assert_string_is_neither_NULL_nor_zerolength(device);
[1]1583
[3879]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.");
[1]1589 }
[3879]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);
[1]1600}
1601
1602
1603/**
[3879]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
[1]1609 */
1610void
[3879]1611redraw_mountlist(struct mountlist_itself *mountlist,
1612 void *keylist[ARBITRARY_MAXIMUM], newtComponent listbox)
[1]1613{
1614
[3879]1615 /** long **************************************************************/
1616 long i = 0;
1617 char * tmp = NULL;
[1]1618
[3879]1619 assert(mountlist != NULL);
1620 assert(keylist != NULL);
1621 assert(listbox != NULL);
[1]1622
[3879]1623 newtListboxClear(listbox);
1624// sort_mountlist_by_device (mountlist);
1625 for (i = 0; i < ARBITRARY_MAXIMUM; i++) {
1626 keylist[i] = (void *) i;
[128]1627 }
[3879]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);
[128]1632 }
[1]1633}
1634
1635
1636
[3879]1637
[1]1638/**
[3879]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.
[1]1644 * @param keylist The list of keys for @p listbox.
1645 * @ingroup restoreGuiMountlist
1646 */
1647void
[3879]1648add_mountlist_entry(struct mountlist_itself *mountlist,
1649 struct raidlist_itself *raidlist,
1650 newtComponent listbox, int currline, void *keylist[])
[1]1651{
1652
[3879]1653 /** int **************************************************************/
1654 int i = 0;
[1]1655
[3879]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;
[1]1669
[3879]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;
[1]1676
[3879]1677 /** pointers *********************************************************/
1678 char *mountpoint_here;
1679 char *size_here;
1680 char *device_here;
1681 char *format_here;
1682
[128]1683 assert(mountlist != NULL);
1684 assert(raidlist != NULL);
1685 assert(listbox != NULL);
1686 assert(keylist != NULL);
[1]1687
[3879]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) {
[128]1742 return;
[1]1743 }
[3879]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);
[3193]1747
[3879]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);
[128]1762 }
[3879]1763 mr_free(device_str);
[3193]1764
[3879]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--;
[128]1804 }
1805 }
[1]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 */
[3879]1816void delete_raidlist_entry(struct mountlist_itself *mountlist,
[128]1817 struct raidlist_itself *raidlist, char *device)
[1]1818{
[128]1819
[1]1820 /** int ***************************************************************/
[128]1821 int i = 0;
1822 int items = 0;
[1]1823
1824 /** bool **************************************************************/
[128]1825 bool delete_partitions_too;
[1]1826
1827 /** buffers ***********************************************************/
[3193]1828 char *tmp = NULL;
[1]1829
[128]1830 assert(mountlist != NULL);
1831 assert(raidlist != NULL);
1832 assert_string_is_neither_NULL_nor_zerolength(device);
[1]1833
[128]1834 i = find_raid_device_in_raidlist(raidlist, device);
1835 if (i < 0) {
1836 return;
1837 }
[3193]1838 mr_asprintf(tmp, "Do you want me to delete %s's partitions, too?", device);
[128]1839 delete_partitions_too = ask_me_yes_or_no(tmp);
1840 if (delete_partitions_too) {
[1]1841#ifdef __FreeBSD__
[128]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)) {
[3193]1851 strcpy(d.el[d.entries].name, raidlist->disks.el[z].name);
1852 strcpy(d.el[d.entries++].device, raidlist->disks.el[z].device);
[128]1853 }
1854 }
1855 }
1856 }
1857
1858 del_partns_listed_in_disklist(mountlist, raidlist, &d);
[1]1859#else
[128]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);
[1]1868#endif
[128]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 }
[3193]1880 mr_free(tmp);
[128]1881 raidlist->entries = items;
[1]1882}
1883
1884
[3879]1885
[1]1886/**
[3879]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
[1]1894 */
[3879]1895void
1896delete_mountlist_entry(struct mountlist_itself *mountlist,
1897 struct raidlist_itself *raidlist,
1898 newtComponent listbox, int currline,
1899 void *keylist[])
[1]1900{
1901
[3879]1902 /** int ***************************************************************/
1903 int pos = 0;
[3193]1904 int res = 0;
[1]1905
[3879]1906 /** buffers ***********************************************************/
1907 char *tmp = NULL;
1908 char *device = NULL;
[1]1909
1910
[3879]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);
[3193]1924 res = ask_me_yes_or_no(tmp);
1925 mr_free(tmp);
1926
[3879]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] = '^';
[128]1991 } else {
[3879]1992 tmpnopath[j++] = tmp[i++];
[128]1993 }
[1]1994 }
[3879]1995 tmpnopath[j] = '\0';
1996
1997 strcpy(tmpprevpath, tmpnopath); /* Make a copy for next iteration */
1998
1999 return (tmpnopath);
[1]2000}
2001
2002
[3879]2003
[1]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
[128]2013redraw_filelist(struct s_node *filelist, void *keylist[ARBITRARY_MAXIMUM],
2014 newtComponent listbox)
[1]2015{
2016
2017 /** int ***************************************************************/
[128]2018 static int lines_in_flist_window = 0;
2019 static int depth = 0;
[1]2020
[738]2021 /** long **************************************************************/
2022 long i = 0;
2023
[1]2024 /** structures *******************************************************/
[128]2025 struct s_node *node;
[1]2026
2027 /** buffers **********************************************************/
[128]2028 static char current_filename[MAX_STR_LEN];
2029 char tmp[MAX_STR_LEN + 2];
[3193]2030 char *tmp1 = NULL;
[1]2031
2032 /** bool *************************************************************/
2033 /* void*dummyptr; */
[128]2034 bool dummybool;
2035 static bool warned_already;
[1]2036
[128]2037 assert(filelist != NULL);
2038 assert(keylist != NULL);
2039 assert(listbox != NULL);
[1]2040
2041
[128]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 }
[1]2049 }
[128]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;
[3193]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);
[128]2064 }
2065 } else {
[3193]2066 strcpy(g_strings_of_flist_window[lines_in_flist_window], current_filename);
[128]2067 g_is_path_selected[lines_in_flist_window] = node->selected;
2068 lines_in_flist_window++;
2069 }
2070 }
[1]2071 }
[128]2072 if (depth == 0) {
2073 if (lines_in_flist_window > ARBITRARY_MAXIMUM) {
2074 lines_in_flist_window = ARBITRARY_MAXIMUM;
[1]2075 }
2076/* do an elementary sort */
[128]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) {
[3193]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
[128]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 }
[1]2092/* write list to screen */
[128]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);
[1]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 */
[128]2114int edit_filelist(struct s_node *filelist)
[1]2115{
2116
2117 /** newt **************************************************************/
[128]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;
[1]2127
2128 /** int ***************************************************************/
2129 int finished = FALSE;
2130 int lines_in_flist_window = 0;
2131 int indexno = 0;
2132 int j = 0;
2133
2134 /** ???? **************************************************************/
[128]2135 void *curr_choice;
2136 void *keylist[ARBITRARY_MAXIMUM];
[1]2137
2138 /** bool **************************************************************/
[128]2139 bool dummybool;
[1]2140
2141/* struct s_node *node; */
2142
[128]2143 assert(filelist != NULL);
[1]2144
[541]2145 log_to_screen("Editing filelist");
[128]2146 newtPushHelpLine
[541]2147 (" Please edit the filelist to your satisfaction, then click OK or Cancel.");
[128]2148 j = 4;
[541]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 ");
[128]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);
[541]2160 newtOpenWindow(1, 3, 77, 18, "Editing filelist");
[128]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
[541]2169 ("Are you happy with your file selection?");
[128]2170 } else if (b_res == bCancel) {
2171 finished = TRUE;
2172 } else if (b_res == bRegex) {
[541]2173 popup_and_OK("I haven't implemented this yet...");
[128]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 }
[3193]2184 log_it("You selected '%s'", g_strings_of_flist_window[indexno]);
2185
[128]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 }
[1]2229 }
2230 }
[128]2231 newtFormDestroy(myForm);
2232 newtPopWindow();
2233 newtPopHelpLine();
2234 if (b_res == bOK) {
2235 return (0);
2236 } else {
[1]2237/* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
[128]2238 return (1);
2239 }
[1]2240}
2241
2242
2243/**
[3879]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/**
[1]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
[128]2311edit_mountlist_entry(struct mountlist_itself *mountlist,
2312 struct raidlist_itself *raidlist,
2313 newtComponent listbox, int currline, void *keylist[])
[1]2314{
2315
2316 /** structures ********************************************************/
[128]2317 static struct raidlist_itself bkp_raidlist;
[1]2318
2319 /** newt **************************************************************/
[128]2320 newtComponent myForm;
[1]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 ***********************************************************/
[3193]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;
[1]2342
2343 /** pointers **********************************************************/
2344 char *device_here;
2345 char *mountpoint_here;
2346 char *size_here;
2347 char *format_here;
2348
2349 /** int ***************************************************************/
[128]2350 int j = 0;
[1]2351
[128]2352 assert(mountlist != NULL);
2353 assert(raidlist != NULL);
2354 assert(listbox != NULL);
2355 assert(keylist != NULL);
[1]2356
[128]2357 memcpy((void *) &bkp_raidlist, (void *) raidlist,
2358 sizeof(struct raidlist_itself));
[3193]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);
[128]2365 newtOpenWindow(20, 5, 48, 10, "Edit entry");
[541]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: ");
[3193]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);
[128]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);
[1]2378 }
[541]2379 bOK = newtButton(2, 6, " OK ");
2380 bCancel = newtButton(14, 6, "Cancel");
[128]2381 if (strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)) {
2382 b_raid = newtButton(26, 6, "RAID..");
[1]2383 }
[128]2384 newtPushHelpLine
[541]2385 (" Edit this partition's mountpoint, size and format; then click 'OK'.");
[128]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);
[3193]2392 mr_free(device_str);
[3614]2393 device_str = mr_strip_spaces(device_here);
[3193]2394
2395 mr_free(mountpoint_str);
[3614]2396 mountpoint_str = mr_strip_spaces(mountpoint_here);
[3193]2397
2398 mr_free(format_str);
[3614]2399 format_str = mr_strip_spaces(format_here);
[3193]2400
[128]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)) {
[541]2404 popup_and_OK("You can't change /dev/mdX to /dev/mdY.");
[128]2405 b_res = NULL;
2406 continue;
2407 } else if (b_res == bOK && !strcmp(mountpoint_str, "image")
2408 && strcmp(mountpt_used_to_be, "image")) {
[541]2409 popup_and_OK("You can't change a regular device to an image.");
[128]2410 b_res = NULL;
2411 continue;
[1]2412 }
[128]2413 if (!strstr(mountlist->el[currline].device, RAID_DEVICE_STUB)
2414 && strcmp(mountlist->el[currline].mountpoint, "image")) {
[3193]2415 mr_free(size_str);
[3614]2416 size_str = mr_strip_spaces(size_here);
[128]2417 } else {
[3193]2418 mr_asprintf(size_str, "%ld", calculate_raid_device_size(mountlist, raidlist, mountlist->el[currline].device) / 1024);
[128]2419 newtLabelSetText(sizeComp, size_str);
[1]2420 }
[128]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
[541]2429 ("You cannot edit the RAID settings until you have OK'd your change to the device node.");
[128]2430 } else {
2431 j = find_raid_device_in_raidlist(raidlist,
2432 mountlist->el[currline].
2433 device);
2434 if (j < 0) {
[3193]2435 mr_asprintf(tmp, "/etc/raidtab does not have an entry for %s; please delete it and add it again", mountlist->el[currline].device);
[128]2436 popup_and_OK(tmp);
[3193]2437 mr_free(tmp);
[128]2438 } else {
[541]2439 log_it("edit_raidlist_entry - calling");
[128]2440 edit_raidlist_entry(mountlist, raidlist,
2441 &raidlist->el[j], currline);
2442 }
2443 }
2444 }
[1]2445 }
[128]2446 newtFormDestroy(myForm);
2447 newtPopHelpLine();
2448 newtPopWindow();
[3193]2449 mr_free(mountpt_used_to_be);
2450
[128]2451 if (b_res == bCancel) {
2452 memcpy((void *) raidlist, (void *) &bkp_raidlist,
2453 sizeof(struct raidlist_itself));
[3193]2454 mr_free(device_str);
2455 mr_free(device_used_to_be);
2456 mr_free(format_str);
2457 mr_free(size_str);
[128]2458 return;
2459 }
2460 strcpy(mountlist->el[currline].device, device_str);
2461 strcpy(mountlist->el[currline].mountpoint, mountpoint_str);
[3193]2462 mr_free(mountpoint_str);
2463
[128]2464 strcpy(mountlist->el[currline].format, format_str);
[3193]2465 mr_free(format_str);
2466
[705]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,
[128]2471 mountlist->el[currline].device);
[705]2472 } else {
[2125]2473 mountlist->el[currline].size = atol(size_str) * 1024L;
[705]2474 }
[128]2475 }
[3193]2476 mr_free(size_str);
[3611]2477
2478 tmp = mountlist_entry_to_string(mountlist, currline);
2479 newtListboxSetEntry(listbox, (long) keylist[currline], tmp);
2480 mr_free(tmp);
2481
[128]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)) {
[3193]2485 initiate_new_raidlist_entry(raidlist, mountlist, currline, device_str);
[128]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 }
[1]2499/* else, moving a RAID to another RAID; bad idea, or so I thought */
[128]2500#ifndef __FreeBSD__ /* It works fine under FBSD. */
2501 else if (strcmp(device_used_to_be, device_str)) {
[3193]2502 popup_and_OK("You are renaming a RAID device as another RAID device. I don't like it but I'll allow it.");
[128]2503 }
[1]2504#endif
[3193]2505 mr_free(device_str);
2506 mr_free(device_used_to_be);
2507
[128]2508 redraw_mountlist(mountlist, keylist, listbox);
[1]2509}
2510
2511
[3892]2512#ifdef __FreeBSD__
[1]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
[128]2522add_raid_subdisk(struct raidlist_itself *raidlist,
2523 struct vinum_plex *raidrec, char *temp)
[1]2524{
[128]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)) {
[3193]2530 strcpy(raidrec->sd[raidrec->subdisks].which_device, raidlist->disks.el[i].name);
[128]2531 found = TRUE;
2532 }
[1]2533 }
[128]2534 if (!found) {
[3193]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);
[128]2537 strcpy(raidlist->disks.el[raidlist->disks.entries++].device, temp);
2538 }
2539 raidrec->subdisks++;
[1]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 */
[128]2552char *find_dev_entry_for_raid_device_name(struct raidlist_itself *raidlist,
2553 char *vinum_name)
[1]2554{
[128]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 }
[1]2560 }
[128]2561 return NULL;
[1]2562}
2563
2564void
[128]2565edit_raidlist_plex(struct mountlist_itself *mountlist,
2566 struct raidlist_itself *raidlist,
2567 struct vinum_plex *raidrec, int currline,
2568 int currline2);
[1]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
[128]2579edit_mountlist_in_newt(char *mountlist_fname,
2580 struct mountlist_itself *mountlist,
2581 struct raidlist_itself *raidlist)
[1]2582{
2583
2584 /** newt **************************************************************/
[128]2585 newtComponent myForm;
[1]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 /** ???? *************************************************************/
[128]2600 void *curr_choice;
2601 void *keylist[ARBITRARY_MAXIMUM];
[1]2602
2603 /** int **************************************************************/
[128]2604 int i = 0;
[1]2605 int currline = 0;
2606 int finished = FALSE;
2607
2608 /** buffers **********************************************************/
[3193]2609 char *tmp = NULL;
[2350]2610 char *flaws_str = NULL;
[2209]2611 char *flaws_str_A = NULL;
2612 char *flaws_str_B = NULL;
2613 char *flaws_str_C = NULL;
[1]2614
[128]2615 assert(mountlist != NULL);
2616 assert(raidlist != NULL);
[1]2617
[128]2618 if (mountlist->entries > ARBITRARY_MAXIMUM) {
[541]2619 log_to_screen("Arbitrary limits suck, man!");
[128]2620 finish(1);
[1]2621 }
[3069]2622 newtPushHelpLine("WARNING: No LVM modification possible here. Edit /tmp/i-want-my-lvm instead");
[128]2623 i = 4;
[541]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 ");
[3193]2630 mr_asprintf(tmp, "%-24s %-24s %-8s %s", "Device", "Mountpoint", "Format", "Size (MB)");
[128]2631 headerMsg = newtLabel(2, 1, tmp);
[3193]2632 mr_free(tmp);
2633
[2350]2634 flawsLabelA = newtLabel(2, 13, " ");
2635 flawsLabelB = newtLabel(2, 14, " ");
2636 flawsLabelC = newtLabel(2, 15, " ");
[3236]2637 partitionsListbox = newtListbox(2, 2, 10, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
[128]2638 redraw_mountlist(mountlist, keylist, partitionsListbox);
[541]2639 newtOpenWindow(1, 3, 77, 18, "Editing mountlist");
[128]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) {
[2394]2645 flaws_str = evaluate_mountlist(mountlist);
[2398]2646 /* flaws_str can be NULL */
2647 if ((flaws_str == NULL) || (strlen(flaws_str) == 0)) {
[3185]2648 mr_asprintf(flaws_str_A, "");
[2398]2649 } else {
[3185]2650 mr_asprintf(flaws_str_A, "%s", flaws_str + 1);
[2355]2651 }
2652 if (strlen(flaws_str_A) >= 74) {
2653 for (i = 74; flaws_str_A[i] != ' '; i--);
[3185]2654 mr_asprintf(flaws_str_B, "%s", flaws_str_A + i + 1);
[2355]2655 flaws_str_A[i] = '\0';
2656 } else {
[3185]2657 mr_asprintf(flaws_str_B, "");
[2355]2658 }
2659 if (strlen(flaws_str_B) >= 74) {
2660 for (i = 74; flaws_str_B[i] != ' '; i--);
[3185]2661 mr_asprintf(flaws_str_C, "%s", flaws_str_B + i + 1);
[2355]2662 flaws_str_B[i] = '\0';
2663 } else {
[3185]2664 mr_asprintf(flaws_str_C, "");
[2355]2665 }
[2350]2666
[128]2667 newtLabelSetText(flawsLabelA, flaws_str_A);
2668 newtLabelSetText(flawsLabelB, flaws_str_B);
2669 newtLabelSetText(flawsLabelC, flaws_str_C);
2670 b_res = newtRunForm(myForm);
[2350]2671 mr_free(flaws_str_A);
2672 mr_free(flaws_str_B);
2673 mr_free(flaws_str_C);
2674
[128]2675 if (b_res == bOK) {
[2394]2676 if (flaws_str != NULL) {
[3236]2677 finished = ask_me_yes_or_no("Your mountlist might not work. Continue anyway?");
[128]2678 } else {
[3236]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.)");
[128]2680 }
2681 } else if (b_res == bCancel) {
2682 finished = TRUE;
2683 } else if (b_res == bReload) {
[541]2684 if (ask_me_yes_or_no("Reload original mountlist?")) {
[128]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);
[3236]2691 for (i = 0; i < mountlist->entries && keylist[i] != curr_choice; i++);
[128]2692 if (i == mountlist->entries && mountlist->entries > 0) {
[541]2693 log_to_screen("I don't know what that button does!");
[128]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
[541]2711 ("Please add an entry. Then press ENTER to edit it.");
[128]2712 }
2713 }
2714 }
[1]2715 }
[2398]2716 mr_free(flaws_str);
[1]2717 }
[128]2718 newtFormDestroy(myForm);
2719 newtPopWindow();
2720 newtPopHelpLine();
2721 if (b_res == bOK) {
[541]2722 log_it("You pushed 'OK'. I shall now continue.");
[128]2723 return (0);
2724 } else {
2725 /* popup_and_OK("You pushed 'cancel'. I shall now abort."); */
2726 return (1);
2727 }
[1]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
[128]2739edit_mountlist(char *mountlist_fname, struct mountlist_itself *mountlist,
2740 struct raidlist_itself *raidlist)
[1]2741{
[128]2742 int res = 0;
[1]2743
[2230]2744 log_it("entering eml");
[1]2745
[128]2746 if (g_text_mode) {
2747 fatal_error("Don't call edit_mountlist() in text mode");
2748 } else {
[3236]2749 log_it("I'm in GUI mode, so I shall edit mountlist using edit_mountlist()");
[128]2750 res = edit_mountlist_in_newt(mountlist_fname, mountlist, raidlist);
2751 }
[2230]2752 log_it("leaving eml");
[128]2753 return (res);
[1]2754}
2755
2756/**
2757 * Dummy function that proves that we can get to the point where Mondo is run.
2758 */
[128]2759void nuke_mode_dummy()
[1]2760{
2761
2762 /** newt *************************************************************/
[128]2763 newtComponent myForm;
[1]2764 newtComponent b1;
2765 newtComponent b2;
2766 newtComponent b3;
2767
2768
[128]2769 newtPushHelpLine
[541]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");
[128]2775 myForm = newtForm(NULL, NULL, 0);
2776 newtFormAddComponents(myForm, b1, b2, b3, NULL);
2777 newtFormDestroy(myForm);
2778 newtPopWindow();
2779 newtPopHelpLine();
[1]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 */
[128]2787char which_restore_mode()
[1]2788{
[128]2789
[1]2790 /** char *************************************************************/
[128]2791 char output = '\0';
[3193]2792 char *tmp = NULL;
[128]2793
[1]2794 /** newt *************************************************************/
2795
[128]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) {
[3193]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);
[128]2809 }
2810 return (output);
[1]2811 }
2812
[3193]2813 newtPushHelpLine(" Do you want to 'nuke' your system, restore interactively, or just compare?");
[541]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");
[128]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);
[1]2838}
Note: See TracBrowser for help on using the repository browser.