source: MondoRescue/branches/3.2/mondo/src/mondorestore/mondo-rstr-newt.c@ 3614

Last change on this file since 3614 was 3614, checked in by Bruno Cornec, 7 years ago

mr_strip_spaces now returns a dynamically allocated string

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