source: MondoRescue/branches/2.2.10/mondo/src/common/newt-specific.c@ 2320

Last change on this file since 2320 was 2320, checked in by Bruno Cornec, 15 years ago

r3331@localhost: bruno | 2009-08-06 01:34:32 +0200

  • bkpinfo->scratchdir is now dynamically allocated
  • mondoarchive tests made in text+newt env. Seems OK. Valgrind errors to be fixed
  • Property svn:keywords set to Id
File size: 44.6 KB
Line 
1/* newt-specific.c
2 $Id: newt-specific.c 2320 2009-08-18 12:37:39Z bruno $
3
4 subroutines which do display-type things
5 and use the newt library to do them
6*/
7
8
9/**
10 * @file
11 * Functions for doing display-type things with the Newt library.
12 */
13
14#define MAX_NEWT_COMMENT_LEN 200
15
16#if __cplusplus
17extern "C" {
18#endif
19
20#include "my-stuff.h"
21#include "mr_mem.h"
22#include "mondostructures.h"
23#include "newt-specific.h"
24#include "libmondo-string-EXT.h"
25#include "libmondo-files-EXT.h"
26#include "libmondo-devices-EXT.h"
27#include "libmondo-tools-EXT.h"
28#include "libmondo-fork-EXT.h"
29#include "libmondo-gui-EXT.h"
30#include "lib-common-externs.h"
31
32extern char *MONDO_LOGFILE;
33
34extern struct s_bkpinfo *bkpinfo;
35
36/*@unused@*/
37//static char cvsid[] = "$Id: newt-specific.c 2320 2009-08-18 12:37:39Z bruno $";
38
39 extern pid_t g_mastermind_pid;
40 /*
41 extern char *g_tmpfs_mountpt;
42 */
43 extern char *g_boot_mountpt;
44 extern char *ps_options;
45 extern char *ps_proc_id;
46
47 extern void set_signals(int);
48
49/**
50 * @addtogroup globalGroup
51 * @{
52 */
53/**
54 * Whether we are currently in a nested call of fatal_error().
55 */
56 bool g_exiting = FALSE;
57
58/**
59 * Padding below the Newt components, to overcome bugs in Newt.
60 */
61 char g_haharrrrr[500];
62
63
64 newtComponent g_timeline = NULL, ///< The line of the progress form that shows the time elapsed/remaining
65 g_percentline = NULL, ///< The line of the progress form that shows the percent completed/remaining
66 g_scale = NULL, ///< The progress bar component in the progress form
67 g_progressForm = NULL, ///< The progress form component itself
68 g_blurb1 = NULL, ///< The component for line 1 of the blurb in the progress form
69 g_blurb2 = NULL, ///< The component for line 2 of the blurb in the progress form
70 g_blurb3 = NULL, ///< The component for line 3 (updated continuously) of the blurb in the progress form
71 g_label = NULL; ///< ????? @bug ?????
72
73/**
74 * Padding above the Newt components, to overcome bugs in Newt.
75 */
76 char g_jim_lad_yarr[500];
77 char **err_log_lines = NULL, ///< The list of log lines to show on the screen.
78 g_blurb_str_1[MAX_NEWT_COMMENT_LEN] = "", ///< The string for line 1 of the blurb in the progress form
79 g_blurb_str_2[MAX_NEWT_COMMENT_LEN] = "", ///< The string for line 2 of the blurb in the progress form
80 g_blurb_str_3[MAX_NEWT_COMMENT_LEN] = ""; ///< The string for line 3 (updated continuously) of the blurb in the progress form
81 newtComponent g_isoform_main = NULL, ///< The evalcall form component itself
82 g_isoform_header = NULL, ///< The component for the evalcall form title
83 g_isoform_scale = NULL, ///< The progress bar component in the evalcall form
84 g_isoform_timeline = NULL, ///< The line of the evalcall form that shows the time elapsed/remaining
85 g_isoform_pcline = NULL; ///< The line of the evalcall form that shows the percent completed/remaining
86 long g_isoform_starttime; ///< The time (in seconds since the epoch) that the evalcall form was opened.
87 int g_isoform_old_progress = -1; ///< The most recent progress update of the evalcall form (percent).
88 char g_isoform_header_str[MAX_STR_LEN] = " "; ///< The string for the evalcall form title.
89 int g_mysterious_dot_counter; ///< The counter for the twirling baton (/ | \\ - ...) on percentage less than 3
90 int g_noof_log_lines = 6; ///< The number of lines to show in the log at the bottom of the screen.
91 int g_noof_rows = 25; ///< The number of rows on the screen.
92
93 int g_currentY = 3; ///< The row to write background progress messages to. Incremented each time a message is written.
94 extern int g_current_media_number;
95 pid_t g_main_pid = 0; ///< The PID of the main Mondo process.
96 long g_maximum_progress = 999; ///< The maximum amount of progress (100%) for the currently opened progress form.
97 long g_current_progress = -999; ///< The current amount of progress (filelist #, etc.) for the currently opened progress form.
98 long g_start_time = 0L; ///< The time (in seconds since the epoch) that the progress form was opened.
99 bool g_text_mode = TRUE; ///< If FALSE, use a newt interface; if TRUE, use an ugly (but more compatible) dumb terminal interface.
100 char g_xmondo_stdin[MAX_NEWT_COMMENT_LEN], ///< ... @bug Unneeded w/current XMondo.
101 g_xmondo_stdout[MAX_NEWT_COMMENT_LEN]; ///< .... @bug Unneeded w/current XMondo.
102 bool g_called_by_xmondo = FALSE; ///< @bug Unneeded w/current XMondo.
103 char *g_selfmounted_isodir; ///< Holds the NFS mountpoint if mounted via mondoarchive.
104
105/* @} - end of globalGroup */
106
107//int g_fd_in=-1, g_fd_out=-1;
108
109 void popup_and_OK(char *);
110
111
112/**
113 * @addtogroup guiGroup
114 * @{
115 */
116/**
117 * Ask the user a yes/no question.
118 * @param prompt The question to ask the user.
119 * @return TRUE for yes; FALSE for no.
120 */
121 bool ask_me_yes_or_no(char *prompt) {
122
123 /*@ buffers ********************************************************** */
124 int i;
125
126 assert_string_is_neither_NULL_nor_zerolength(prompt);
127
128 if (g_text_mode) {
129
130 char *tmp;
131 tmp = malloc(MAX_NEWT_COMMENT_LEN);
132
133 while (1) {
134 system("sync");
135 printf
136 ("---promptdialogYN---1--- %s\n---promptdialogYN---Q--- [yes] [no] ---\n--> ",
137 prompt);
138 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, stdin);
139 if (tmp[strlen(tmp) - 1] == '\n')
140 tmp[strlen(tmp) - 1] = '\0';
141
142 i = (int) strlen(tmp);
143 if (i > 0 && tmp[i - 1] < 32) {
144 tmp[i - 1] = '\0';
145 }
146 if (strstr("yesYES", tmp)) {
147 paranoid_free(tmp);
148 return (TRUE);
149 } else if (strstr("NOno", tmp)) {
150 paranoid_free(tmp);
151 return (FALSE);
152 } else {
153 system("sync");
154 printf
155 ("Please enter either YES or NO (or yes or no, or y or n, or...)\n");
156 }
157 }
158 } else {
159 return (popup_with_buttons(prompt, "Yes", "No"));
160 }
161 }
162
163
164/**
165 * Give the user the opportunity to continue the current operation (OK)
166 * or cancel it (Cancel).
167 * @param prompt The string to be displayed.
168 * @return TRUE for OK, FALSE for Cancel.
169 */
170 bool ask_me_OK_or_cancel(char *prompt) {
171
172 /*@ buffer *********************************************************** */
173 char *tmp;
174 int i;
175
176 tmp = malloc(MAX_NEWT_COMMENT_LEN);
177 assert_string_is_neither_NULL_nor_zerolength(prompt);
178 if (g_text_mode) {
179 system("sync");
180 printf
181 ("---promptdialogOKC---1--- %s\n---promptdialogOKC---Q--- [OK] [Cancel] ---\n--> ",
182 prompt);
183 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, stdin);
184 if (tmp[strlen(tmp) - 1] == '\n')
185 tmp[strlen(tmp) - 1] = '\0';
186
187 i = (int) strlen(tmp);
188 if (i > 0 && tmp[i - 1] < 32) {
189 tmp[i - 1] = '\0';
190 }
191 if (strstr("okOKOkYESyes", tmp)) {
192 paranoid_free(tmp);
193 return (TRUE);
194 } else {
195 paranoid_free(tmp);
196 return (FALSE);
197 }
198 } else {
199 paranoid_free(tmp);
200 return (popup_with_buttons(prompt, " Okay ", "Cancel"));
201 }
202 }
203
204
205
206/**
207 * Close the currently opened evalcall form.
208 */
209 void
210 close_evalcall_form(void) {
211 if (g_text_mode) {
212 return;
213 }
214 if (g_isoform_main == NULL) {
215 return;
216 }
217 update_evalcall_form(100);
218 usleep(500000);
219 if (g_text_mode) {
220 log_msg(2, "Closing evalcall form");
221 return;
222 }
223 newtPopHelpLine();
224 newtFormDestroy(g_isoform_main);
225 newtPopWindow();
226
227 /* Reset globals */
228 g_isoform_main = NULL;
229 g_isoform_old_progress = -1;
230 g_isoform_header = NULL;
231 g_isoform_scale = NULL;
232 g_isoform_timeline = NULL;
233 g_isoform_pcline = NULL;
234}
235
236/**
237 * Close the currently opened progress form.
238 */
239 void
240 close_progress_form() {
241 if (g_text_mode) {
242 return;
243 }
244 if (g_current_progress == -999) {
245 log_msg(2,
246 "Trying to close the progress form when it ain't open!");
247 return;
248 }
249 g_current_progress = g_maximum_progress;
250 update_progress_form("Complete");
251 sleep(1);
252 if (g_text_mode) {
253 log_msg(2, "Closing progress form");
254 return;
255 }
256 newtPopHelpLine();
257 newtFormDestroy(g_progressForm);
258 newtPopWindow();
259
260 /* Reset globals */
261 g_progressForm = NULL;
262 g_current_progress = -999;
263 g_timeline = NULL;
264 g_percentline = NULL;
265 g_scale = NULL;
266
267 }
268
269/**
270 * Kill any process containing the string @p str surrounded by spaces in its commandline.
271 */
272void kill_anything_like_this(char *str) {
273
274char *tmp = NULL;
275char *tmp1 = NULL;
276
277 mr_asprintf(&tmp,"ps %s | grep \" %s \" | awk '{print %s;}' | grep -v \"grep\"", ps_options, str , ps_proc_id);
278 run_program_and_log_output(tmp, TRUE);
279 if (strlen(tmp) > 0) {
280 mr_asprintf(&tmp1,"kill `%s`", tmp);
281 run_program_and_log_output(tmp1, TRUE);
282 paranoid_free(tmp1);
283 }
284 paranoid_free(tmp);
285}
286
287
288/**
289 * Exit Mondo with a fatal error.
290 * @param error_string The error message to present to the user before exiting.
291 * @note This function never returns.
292 */
293 void
294 fatal_error(char *error_string) {
295 /*@ buffers ***************************************************** */
296 char *command = NULL;
297 static bool already_exiting = FALSE;
298
299 /*@ end vars **************************************************** */
300
301 set_signals(FALSE); // link to external func
302 g_exiting = TRUE;
303 log_msg(1, "Fatal error received - '%s'", error_string);
304 printf("Fatal error... %s\n", error_string);
305 if (getpid() == g_mastermind_pid) {
306 log_msg(2, "mastermind %d is exiting", (int) getpid());
307 kill(g_main_pid, SIGTERM);
308 finish(1);
309 }
310
311 if (getpid() != g_main_pid) {
312 if (g_mastermind_pid != 0 && getpid() != g_mastermind_pid) {
313 log_msg(2, "non-m/m %d is exiting", (int) getpid());
314 kill(g_main_pid, SIGTERM);
315 finish(1);
316 }
317 }
318
319 log_msg(3, "OK, I think I'm the main PID.");
320 if (already_exiting) {
321 log_msg(3, "...I'm already exiting. Give me time, Julian!");
322 finish(1);
323 }
324
325 already_exiting = TRUE;
326 log_msg(2, "I'm going to do some cleaning up now.");
327 paranoid_system("killall mindi 2> /dev/null");
328 kill_anything_like_this("/mondo/do-not");
329 kill_anything_like_this("tmp.mondo");
330 kill_anything_like_this("ntfsclone");
331 sync();
332
333 chdir("/");
334 if (g_selfmounted_isodir) {
335 mr_asprintf(&command, "umount %s", g_selfmounted_isodir);
336 run_program_and_log_output(command, 5);
337 mr_free(command);
338 mr_asprintf(&command, "rmdir %s", g_selfmounted_isodir);
339 run_program_and_log_output(command, 5);
340 mr_free(command);
341 }
342
343 if (!g_text_mode) {
344 log_msg(0, "-------FATAL ERROR---------");
345 log_msg(0, error_string);
346 newtFinished();
347 }
348
349 printf("---FATALERROR--- %s\n", error_string);
350 printf("If you require technical support, please contact the mailing list.\n");
351 printf("See http://www.mondorescue.org for details.\n");
352 printf("The list's members can help you, if you attach that file to your e-mail.\n");
353 printf("Log file: %s\n", MONDO_LOGFILE);
354 printf("Mondo has aborted.\n");
355 if (!g_main_pid) {
356 log_msg(3, "FYI - g_main_pid is blank");
357 }
358 finish(254);
359 }
360
361/* Free memory allocated for newt */
362void free_newt_stuff() {
363
364int i;
365
366for (i = 0; i < g_noof_log_lines; i++) {
367 mr_free(err_log_lines[i]);
368}
369mr_free(err_log_lines);
370}
371
372
373
374/**
375 * Exit Mondo normally.
376 * @param signal The exit code (0 indicates a successful backup; 1 for Mondo means the
377 * user aborted; 254 means a fatal error occured).
378 * @note This function never returns.
379 */
380 void
381 finish(int signal) {
382 char *command = NULL;
383
384 chdir("/");
385 run_program_and_log_output("umount " MNT_CDROM, FALSE);
386 if (g_selfmounted_isodir) {
387 mr_asprintf(&command, "umount %s", g_selfmounted_isodir);
388 run_program_and_log_output(command, 1);
389 mr_free(command);
390
391 mr_asprintf(&command, "rmdir %s", g_selfmounted_isodir);
392 run_program_and_log_output(command, 1);
393 mr_free(command);
394 }
395 if (!g_text_mode) {
396 if (does_file_exist("/THIS-IS-A-RAMDISK")) {
397 log_msg(1, "Calling newtFinished()");
398 newtFinished();
399 } else {
400 log_msg(1, "Calling newtSuspend()");
401 newtSuspend();
402 }
403 }
404 printf("Execution run ended; result=%d\n", signal);
405 printf("Type 'less %s' to see the output log\n", MONDO_LOGFILE);
406 if (bkpinfo->tmpdir) {
407 log_msg(8,"tempdir is %s",bkpinfo->tmpdir);
408 if (strstr(bkpinfo->tmpdir ,"mondo.tmp.") != NULL) {
409 log_msg(8,"erasing tempdir");
410 mr_asprintf(&command, "rm -Rf %s", bkpinfo->tmpdir);
411 system(command);
412 mr_free(command);
413 }
414 }
415 if (bkpinfo->scratchdir) {
416 log_msg(8,"scratchdir is %s", bkpinfo->scratchdir);
417 if (strstr(bkpinfo->scratchdir ,"mondo.scratch.") != NULL) {
418 log_msg(8,"erasing scratchdir");
419 mr_asprintf(&command, "rm -Rf %s", bkpinfo->scratchdir);
420 system(command);
421 mr_free(command);
422 }
423 }
424 /* Free all allocated strings in bkpinfo */
425 mr_free(bkpinfo->nfs_user);
426 mr_free(bkpinfo->exclude_paths);
427 mr_free(bkpinfo->include_paths);
428 mr_free(bkpinfo->scratchdir);
429
430 /* Then free the structure */
431 mr_free(bkpinfo);
432 free_libmondo_global_strings();
433 free_newt_stuff();
434 exit(signal);
435 }
436
437
438
439
440
441/**
442 * Log the last @p g_noof_log_lines lines of @p filename that match @p
443 * grep_for_me to the screen.
444 * @param filename The file to give the end of.
445 * @param grep_for_me If not "", then only give lines in @p filename that match this regular expression.
446 */
447 void
448 log_file_end_to_screen(char *filename, char *grep_for_me) {
449
450 /*@ buffers ********************************************************** */
451 char *command;
452 char *tmp;
453
454 /*@ pointers ********************************************************* */
455 FILE *fin;
456
457 /*@ int ************************************************************** */
458 int i = 0;
459
460 malloc_string(command);
461 malloc_string(tmp);
462 assert_string_is_neither_NULL_nor_zerolength(filename);
463 assert(grep_for_me != NULL);
464
465 if (!does_file_exist(filename)) {
466 paranoid_free(command);
467 paranoid_free(tmp);
468 return;
469 }
470 if (grep_for_me[0] != '\0') {
471 sprintf(command, "grep '%s' %s | tail -n%d",
472 grep_for_me, filename, g_noof_log_lines);
473 } else {
474 sprintf(command, "tail -n%d %s", g_noof_log_lines,
475 filename);
476 }
477 fin = popen(command, "r");
478 if (!fin) {
479 log_OS_error(command);
480 } else {
481 for (i = 0; i < g_noof_log_lines; i++) {
482 for (err_log_lines[i][0] = '\0';
483 strlen(err_log_lines[i]) < 2 && !feof(fin);) {
484 (void) fgets(err_log_lines[i], MAX_NEWT_COMMENT_LEN,
485 fin);
486 /* Commented to make valgrind happy and avoid crash
487 strip_spaces(err_log_lines[i]);
488 */
489 if (!strncmp(err_log_lines[i], "root:", 5)) {
490 strcpy(tmp, err_log_lines[i] + 6);
491 strcpy(err_log_lines[i], tmp);
492 }
493 if (feof(fin)) {
494 break;
495 }
496 }
497 }
498 paranoid_pclose(fin);
499 }
500 refresh_log_screen();
501 paranoid_free(command);
502 paranoid_free(tmp);
503 }
504
505
506/**
507 * Log a message to the screen.
508 * @param fmt A printf-style format string to write. The following parameters are its arguments.
509 * @note The message is also written to the logfile.
510 */
511 void
512 log_to_screen(const char *fmt, ...) {
513
514 /*@ int ************************************************************** */
515 int i = 0;
516 int j = 0;
517 va_list args;
518
519 /*@ buffers ********************************************************** */
520 char *output;
521
522 malloc_string(output);
523
524 va_start(args, fmt);
525 vsprintf(output, fmt, args);
526 log_msg(0, output);
527 output[80] = '\0';
528 va_end(args);
529 i = (int) strlen(output);
530 if (i > 0 && output[i - 1] < 32) {
531 output[i - 1] = '\0';
532 }
533
534 if (err_log_lines) {
535 for (i = 1; i < g_noof_log_lines; i++) {
536 strcpy(err_log_lines[i - 1],
537 " ");
538 strcpy(err_log_lines[i - 1], err_log_lines[i]);
539 }
540 }
541 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
542 output[strlen(output) - 1] = '\0';
543 }
544 for (j = 0; j < (int) strlen(output); j++) {
545 if (output[j] < 32) {
546 output[j] = ' ';
547 }
548 }
549 if (err_log_lines)
550 strcpy(err_log_lines[g_noof_log_lines - 1], output);
551 if (g_text_mode) {
552 printf("%s\n", output);
553 } else {
554 refresh_log_screen();
555 }
556 paranoid_free(output);
557 }
558
559
560
561
562/**
563 * Write a string to the root window at (@p x, @p y) and also to the logfile.
564 * @param y The row to write the string to.
565 * @param x The column to write the string to.
566 * @param output The string to write.
567 */
568 void
569 mvaddstr_and_log_it(int y, int x, char *output) {
570 assert_string_is_neither_NULL_nor_zerolength(output);
571 log_msg(0, output);
572 if (g_text_mode) {
573 printf("%s\n", output);
574 } else {
575 newtDrawRootText(x, y, output);
576 newtRefresh();
577 }
578 }
579
580
581
582
583/**
584 * Open an evalcall form with title @p ttl.
585 * @param ttl The title to use for the evalcall form.
586 */
587 void
588 open_evalcall_form(char *ttl) {
589
590 /*@ buffers ********************************************************* */
591 char *title;
592 char *tmp;
593
594 /*@ initialize ****************************************************** */
595 g_isoform_old_progress = -1;
596 g_mysterious_dot_counter = 0;
597 malloc_string(title);
598 malloc_string(tmp);
599
600 assert(ttl != NULL);
601 strcpy(title, ttl);
602 strcpy(g_isoform_header_str, title);
603// center_string (title, 80);
604 if (g_text_mode) {
605 log_msg(0, title);
606 } else {
607 strcpy(tmp, title);
608 center_string(tmp, 80);
609 newtPushHelpLine(tmp);
610 }
611 center_string(g_isoform_header_str, 36);
612 g_isoform_starttime = get_time();
613 if (g_text_mode) {
614 log_msg(0, g_isoform_header_str);
615 } else {
616 g_isoform_header = newtLabel(1, 1, g_isoform_header_str);
617 g_isoform_scale = newtScale(3, 3, 34, 100);
618 newtCenteredWindow(40, 7, title);
619 g_isoform_main = newtForm(NULL, NULL, 0);
620 g_isoform_timeline = newtLabel(1, 5, "This is the timeline");
621 g_isoform_pcline = newtLabel(1, 6, "This is the pcline");
622 newtFormAddComponents(g_isoform_main, g_isoform_timeline,
623 g_isoform_pcline, g_isoform_header,
624 g_isoform_scale, NULL);
625 newtRefresh();
626 }
627 update_evalcall_form(0);
628 paranoid_free(tmp);
629 paranoid_free(title);
630 }
631
632
633
634/**
635 * Open a progress form with title @p title.
636 * @param title The title to use for the progress form (will be put in the title bar on Newt).
637 * @param b1 The first line of the blurb; generally static.
638 * @param b2 The second line of the blurb; generally static.
639 * @param b3 The third line of the blurb; generally dynamic (it is passed
640 * to update_evalcall_form() every time).
641 * @param max_val The maximum amount of progress (number of filesets, etc.)
642 */
643 void
644 open_progress_form(char *title, char *b1, char *b2, char *b3,
645 long max_val) {
646
647 /*@ buffers ********************************************************* */
648 char *b1c;
649 char *blurb1;
650 char *blurb2;
651 char *blurb3;
652
653 /*@ initialize ****************************************************** */
654 g_mysterious_dot_counter = 0;
655
656 malloc_string(b1c);
657 malloc_string(blurb1);
658 malloc_string(blurb2);
659 malloc_string(blurb3);
660
661 assert(title != NULL);
662 assert(b1 != NULL);
663 assert(b2 != NULL);
664 assert(b3 != NULL);
665
666 strcpy(blurb1, b1);
667 strcpy(blurb2, b2);
668 strcpy(blurb3, b3);
669 strcpy(b1c, b1);
670 center_string(b1c, 80);
671 if (max_val <= 0) {
672 max_val = 1;
673 }
674
675 g_start_time = get_time();
676 g_maximum_progress = max_val;
677 g_current_progress = 0;
678 strcpy(g_blurb_str_1, blurb1);
679 strcpy(g_blurb_str_2, blurb3);
680 strcpy(g_blurb_str_3, blurb2);
681 if (g_text_mode) {
682 log_msg(0, blurb1);
683 log_msg(0, blurb2);
684 log_msg(0, blurb3);
685 } else {
686 g_blurb1 = newtLabel(2, 1, blurb1);
687 g_blurb2 = newtLabel(2, 2, blurb3);
688 g_blurb3 = newtLabel(2, 4, blurb2);
689 // newtOpenWindow (10, 4, 60, 11, title);
690 newtCenteredWindow(60, 11, title);
691 g_scale = newtScale(3, 6, 54, g_maximum_progress);
692 g_progressForm = newtForm(NULL, NULL, 0);
693 g_percentline = newtLabel(10, 9, "This is the percentline");
694 g_timeline = newtLabel(10, 8, "This is the timeline");
695 newtFormAddComponents(g_progressForm, g_percentline,
696 g_timeline, g_scale, g_blurb1, g_blurb3,
697 g_blurb2, NULL);
698 newtPushHelpLine(b1c);
699 newtRefresh();
700 }
701 update_progress_form_full(blurb1, blurb2, blurb3);
702 paranoid_free(b1c);
703 paranoid_free(blurb1);
704 paranoid_free(blurb2);
705 paranoid_free(blurb3);
706 }
707
708/**
709 * Give a message to the user in the form of a dialog box (under Newt).
710 * @param prompt The message.
711 */
712 void
713 popup_and_OK(char *prompt) {
714 char ch;
715
716 assert_string_is_neither_NULL_nor_zerolength(prompt);
717
718 log_msg(0, prompt);
719 if (g_text_mode) {
720 printf
721 ("---promptpopup---1--- %s\n---promptpopup---Q--- [OK] ---\n--> ",
722 prompt);
723 while (((ch = getchar()) != '\n') && (ch != EOF));
724 } else {
725 (void) popup_with_buttons(prompt, " OK ", "");
726 }
727 }
728
729/**
730 * Ask the user to enter a value.
731 * @param title The title of the dialog box.
732 * @param b The blurb (e.g. what you want the user to enter).
733 * @param input The string given originaly as default
734 * @return output string if the user pressed OK, NULL if they pressed Cancel.
735 */
736 char *popup_and_get_string(char *title, char *b, char *input) {
737
738 /*@ newt ************************************************************ */
739 newtComponent myForm;
740 newtComponent b_1;
741 newtComponent b_2;
742 newtComponent b_res;
743 newtComponent text;
744 newtComponent type_here;
745
746 /*@ pointers ********************************************************* */
747 char *entry_value;
748
749 /*@ buffers ********************************************************** */
750 char *blurb = NULL;
751 char *output = NULL;
752
753 assert_string_is_neither_NULL_nor_zerolength(title);
754 assert(b != NULL);
755
756 if (g_text_mode) {
757 printf("---promptstring---1--- %s\n---promptstring---2--- %s\n---promptstring---Q--- [%s]\n--> ", title, b, input);
758 mr_getline(&output, stdin);
759 if (! strcmp(output,"\n")) {
760 /* take default if ENTER was typed */
761 mr_free(output);
762 mr_asprintf(&output, "%s", input);
763 }
764 if (output[strlen(output) - 1] == '\n')
765 output[strlen(output) - 1] = '\0';
766 return (output);
767 }
768
769 mr_asprintf(&blurb, "%s", b);
770 text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0);
771 type_here =
772 newtEntry(2, newtTextboxGetNumLines(text) + 2,
773 input, 50,
774#ifdef __cplusplus
775 0, NEWT_FLAG_RETURNEXIT
776#else
777 (void *) &entry_value, NEWT_FLAG_RETURNEXIT
778#endif
779 );
780 b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, " OK ");
781 b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, "Cancel");
782 newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title);
783 myForm = newtForm(NULL, NULL, 0);
784 newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL);
785 center_string(blurb, 80);
786 newtPushHelpLine(blurb);
787 b_res = newtRunForm(myForm);
788
789 if (b_res == b_1) {
790 mr_asprintf(&output, "%s", newtEntryGetValue(type_here));
791 }
792 newtPopHelpLine();
793 newtFormDestroy(myForm);
794 newtPopWindow();
795 mr_free(blurb);
796
797 return (output);
798 }
799
800
801/**
802 * Pop up a dialog box with user-defined buttons.
803 * @param p The text to put in the dialog box.
804 * @param button1 The label on the first button.
805 * @param button2 The label on the second button, or "" if you only want one button.
806 * @return TRUE if @p button1 was pushed, FALSE otherwise.
807 */
808 bool popup_with_buttons(char *p, char *button1, char *button2) {
809
810 /*@ buffers *********************************************************** */
811 char *prompt;
812
813 /*@ newt ************************************************************** */
814 newtComponent myForm;
815 newtComponent b_1;
816 newtComponent b_2;
817 newtComponent b_res;
818 newtComponent text;
819
820 assert_string_is_neither_NULL_nor_zerolength(p);
821 assert(button1 != NULL);
822 assert(button2 != NULL);
823 prompt = malloc(MAX_NEWT_COMMENT_LEN);
824 if (g_text_mode) {
825 if (strlen(button2) == 0) {
826 printf("%s (%s) --> ", p, button1);
827 } else {
828 printf("%s (%s or %s) --> ", p, button1, button2);
829 }
830 for (prompt[0] = '\0';
831 strcmp(prompt, button1) && (strlen(button2) == 0
832 || strcmp(prompt, button2));) {
833 printf("--> ");
834 (void) fgets(prompt, MAX_NEWT_COMMENT_LEN, stdin);
835 }
836 if (!strcmp(prompt, button1)) {
837 paranoid_free(prompt);
838 return (TRUE);
839 } else {
840 paranoid_free(prompt);
841 return (FALSE);
842 }
843 }
844
845 strncpy(prompt, p, MAX_NEWT_COMMENT_LEN - 1);
846 prompt[MAX_NEWT_COMMENT_LEN - 1] = '\0';
847 text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0);
848 b_1 =
849 newtButton(20 -
850 ((button2[0] !=
851 '\0') ? strlen(button1) +
852 2 : strlen(button1) / 2),
853 newtTextboxGetNumLines(text) + 3, button1);
854 if (button2[0] != '\0') {
855 b_2 =
856 newtButton(24, newtTextboxGetNumLines(text) + 3, button2);
857 } else {
858 b_2 = NULL;
859 }
860 // newtOpenWindow (25, 5, 46, newtTextboxGetNumLines (text) + 7, "Alert");
861 newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, "Alert");
862 myForm = newtForm(NULL, NULL, 0);
863 newtFormAddComponents(myForm, text, b_1, b_2, NULL);
864 center_string(prompt, 80);
865 newtPushHelpLine(prompt);
866 b_res = newtRunForm(myForm);
867 newtPopHelpLine();
868 newtFormDestroy(myForm);
869 newtPopWindow();
870 if (b_res == b_1) {
871 paranoid_free(prompt);
872 return (TRUE);
873 } else {
874 paranoid_free(prompt);
875 return (FALSE);
876 }
877 }
878
879
880
881
882/**
883 * Synchronize the log messages stored in @p err_log_lines with those shown
884 * on the screen.
885 */
886 void
887 refresh_log_screen() {
888
889 /*@ int *********************************************************** */
890 int i = 0;
891
892
893 if (g_text_mode || !err_log_lines) {
894 return;
895 }
896 for (i = g_noof_log_lines - 1; i >= 0; i--) {
897 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
898 " ");
899 }
900 newtRefresh();
901 for (i = g_noof_log_lines - 1; i >= 0; i--) {
902 err_log_lines[i][79] = '\0';
903 newtDrawRootText(0, i + g_noof_rows - 1 - g_noof_log_lines,
904 err_log_lines[i]);
905 }
906 newtRefresh();
907 }
908
909
910/**
911 * Set up the Newt graphical environment. If @p g_text_mode is TRUE, then
912 * only allocate some memory.
913 */
914 void
915 setup_newt_stuff() {
916
917 /*@ int *********************************************************** */
918 int i = 0;
919 int cols;
920
921 if (!g_text_mode) {
922 newtInit();
923 newtCls();
924 newtPushHelpLine
925 ("Welcome to Mondo Rescue, by Dev Team and the Internet. All rights reversed.");
926 /* newtDrawRootText(28,0,"Welcome to Mondo Rescue"); */
927 newtDrawRootText(18, 0, WELCOME_STRING);
928 newtRefresh();
929 newtGetScreenSize(&cols, &g_noof_rows);
930 g_noof_log_lines = (g_noof_rows / 5) + 1;
931 }
932
933 err_log_lines = (char **) mr_malloc(sizeof(char *) * g_noof_log_lines);
934
935 for (i = 0; i < g_noof_log_lines; i++) {
936 err_log_lines[i] = (char *) mr_malloc(MAX_NEWT_COMMENT_LEN);
937 }
938
939 for (i = 0; i < g_noof_log_lines; i++) {
940 err_log_lines[i][0] = '\0';
941 }
942 }
943
944
945/**
946 * Update the evalcall form to show (<tt>num</tt>/<tt>denom</tt>)*100 %.
947 * @param num The numerator of the ratio.
948 * @param denom The denomenator of the ratio.
949 */
950 void
951 update_evalcall_form_ratio(int num, int denom) {
952
953 /*@ long ************************************************************ */
954 long current_time = 0L;
955 long time_taken = 0L;
956 long time_total_est = 0L;
957 long time_remaining = 0L;
958
959 /*@ buffers ********************************************************** */
960 char *timeline_str = NULL;
961 char *pcline_str = NULL;
962 char *taskprogress = NULL;
963
964 /*@ int ************************************************************** */
965 int percentage = 0;
966 int i = 0;
967 int j = 0;
968
969 if (num * 100 < denom) {
970 percentage = 1;
971 } else {
972 percentage = (num * 100 + denom / 2) / denom;
973 }
974
975 current_time = get_time();
976 time_taken = current_time - g_isoform_starttime;
977 if (num) {
978 time_total_est = time_taken * denom / num;
979 time_remaining = time_total_est - time_taken;
980 } else {
981 time_remaining = 0L;
982 }
983 if (!g_text_mode) {
984 if (g_isoform_header) {
985 newtLabelSetText(g_isoform_header, g_isoform_header_str);
986 }
987 }
988 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
989 if ((percentage < 3 && g_isoform_old_progress < 3)
990 || percentage > g_isoform_old_progress) {
991 g_isoform_old_progress = percentage;
992 mr_asprintf(&timeline_str, "%2ld:%02ld taken %2ld:%02ld remaining", (long) time_taken / 60, (long) time_taken % 60, (long) time_remaining / 60, (long) time_remaining % 60);
993 if (percentage < 3) {
994 mr_asprintf(&pcline_str, " Working");
995 for (j = 0; j < g_mysterious_dot_counter; j++) {
996 mr_strcat(pcline_str, ".");
997 }
998 for (; j < 27; j++) {
999 mr_strcat(pcline_str, " ");
1000 }
1001 mr_strcat(pcline_str, " %c", special_dot_char(g_mysterious_dot_counter));
1002 } else {
1003 mr_asprintf(&pcline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
1004 }
1005 if (g_text_mode) {
1006 mr_asprintf(&taskprogress, "TASK: [");
1007 for (i = 0; i < percentage; i += 5) {
1008 mr_strcat(taskprogress, "*");
1009 }
1010 for (; i < 100; i += 5) {
1011 mr_strcat(taskprogress, ".");
1012 }
1013 if (percentage >= 3) {
1014 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, time_remaining / 60, time_remaining % 60);
1015 printf("---evalcall---1--- %s\n", g_isoform_header_str);
1016 printf("---evalcall---2--- %s\n", taskprogress);
1017 printf("---evalcall---E---\n");
1018 }
1019 mr_free(taskprogress);
1020 } else {
1021 newtScaleSet(g_isoform_scale,
1022 (unsigned long long) percentage);
1023 if (g_isoform_pcline) {
1024 newtLabelSetText(g_isoform_pcline, pcline_str);
1025 }
1026 if ((percentage >= 3) && (g_isoform_timeline)) {
1027 newtLabelSetText(g_isoform_timeline, timeline_str);
1028 }
1029 }
1030 mr_free(pcline_str);
1031 mr_free(timeline_str);
1032 }
1033 if (!g_text_mode) {
1034 newtRefresh();
1035 }
1036 }
1037
1038
1039
1040/**
1041 * Update the evalcall form to show @p curr %.
1042 * @param curr The current amount of progress (percentage) in the evalcall form.
1043 */
1044 void
1045 update_evalcall_form(int curr) {
1046 update_evalcall_form_ratio(curr, 100);
1047 }
1048
1049
1050
1051/**
1052 * Update the progress form to show @p blurb3 and the current value of
1053 * @p g_maximum_progress.
1054 * @param blurb3 The new third line of the blurb; use @p g_blurb_str_2 (no, that's not a typo) to keep it the same.
1055 */
1056 void
1057 update_progress_form(char *blurb3) {
1058 if (g_current_progress == -999) {
1059 return;
1060 }
1061 strcpy(g_blurb_str_2, blurb3);
1062 update_progress_form_full(g_blurb_str_1, g_blurb_str_2,
1063 g_blurb_str_3);
1064 }
1065
1066
1067/**
1068 * Update the progress form's complete blurb and show @p g_current_progress.
1069 * @param blurb1 The first line of the blurb. Use @p g_blurb_str_1 to keep it unchanged.
1070 * @param blurb2 The second line of the blurb. Use @p g_blurb_str_3 (no, that's not a typo) to keep it the same.
1071 * @param blurb3 The third line of the blurb. Use @p g_blurb_str_2 (no, that's not a typo either) to keep it the same.
1072 */
1073 void
1074 update_progress_form_full(char *blurb1, char *blurb2, char *blurb3) {
1075 /*@ long ***************************************************** */
1076 long current_time = 0L;
1077 long time_taken = 0L;
1078 long time_remaining = 0L;
1079 long time_total_est = 0L;
1080
1081 /*@ int ******************************************************* */
1082 int percentage = 0;
1083 int i = 0;
1084
1085 /*@ buffers *************************************************** */
1086 char *percentline_str = NULL;
1087 char *timeline_str = NULL;
1088 char *taskprogress = NULL;
1089 char *tmp = NULL;
1090
1091 if (!g_text_mode) {
1092 assert(blurb1 != NULL);
1093 assert(blurb2 != NULL);
1094 assert(blurb3 != NULL);
1095 assert(g_timeline != NULL);
1096 }
1097
1098 current_time = get_time();
1099 time_taken = current_time - g_start_time;
1100 if (g_maximum_progress == 0) {
1101 percentage = 0;
1102 } else {
1103 if (g_current_progress > g_maximum_progress) {
1104 log_msg(0, "update_progress_form_full(%s,%s,%s) --- g_current_progress=%ld; g_maximum_progress=%ld", blurb1, blurb2, blurb3, g_current_progress, g_maximum_progress);
1105 g_current_progress = g_maximum_progress;
1106 }
1107 percentage = (int) ((g_current_progress * 100L) / g_maximum_progress);
1108 }
1109 if (percentage < 1) {
1110 percentage = 1;
1111 }
1112 if (percentage > 100) {
1113 percentage = 100;
1114 }
1115 if (g_current_progress) {
1116 time_total_est = time_taken * (long) g_maximum_progress / (long) g_current_progress;
1117 time_remaining = time_total_est - time_taken;
1118 } else {
1119 time_remaining = 0L;
1120 }
1121 g_mysterious_dot_counter = (g_mysterious_dot_counter + 1) % 27;
1122 if (g_text_mode) {
1123 printf("---progress-form---1--- %s\n", blurb1);
1124 printf("---progress-form---2--- %s\n", blurb2);
1125 printf("---progress-form---3--- %s\n", blurb3);
1126 printf("---progress-form---E---\n");
1127 mr_asprintf(&taskprogress, "TASK: [");
1128 for (i = 0; i < percentage; i += 5) {
1129 mr_strcat(taskprogress, "*");
1130 }
1131 for (; i < 100; i += 5) {
1132 mr_strcat(taskprogress, ".");
1133 }
1134 if (percentage > 100) {
1135 log_msg(2, "percentage = %d", percentage);
1136 }
1137 mr_strcat(taskprogress, "] %3d%% done; %2ld:%02ld to go", percentage, (long) time_remaining / 60, (long) time_remaining % 60);
1138 printf("---progress-form---4--- %s\n", taskprogress);
1139 paranoid_free(taskprogress);
1140 } else {
1141 mr_asprintf(&timeline_str, "%2ld:%02ld taken %2ld:%02ld remaining ", (long) time_taken / 60, (long) time_taken % 60, (long) time_remaining / 60, (long) time_remaining % 60);
1142 mr_asprintf(&percentline_str, " %3d%% done %3d%% to go", percentage, 100 - percentage);
1143
1144 center_string(blurb1, 54);
1145 center_string(blurb2, 54);
1146 center_string(blurb3, 54);
1147 if (g_blurb1) {
1148 newtLabelSetText(g_blurb1, blurb1);
1149 }
1150 if (g_blurb2) {
1151 newtLabelSetText(g_blurb2, blurb3);
1152 }
1153 if (g_blurb3) {
1154 newtLabelSetText(g_blurb3, blurb2);
1155 }
1156 newtScaleSet(g_scale, (unsigned long long) g_current_progress);
1157 if ((percentage >= 2) && (g_timeline)) {
1158 newtLabelSetText(g_timeline, timeline_str);
1159 }
1160 mr_free(timeline_str);
1161
1162 if (g_percentline) {
1163 newtLabelSetText(g_percentline, percentline_str);
1164 }
1165 mr_free(percentline_str);
1166
1167 newtRefresh();
1168 }
1169 }
1170
1171
1172/**
1173 * Ask the user which backup media type they would like to use.
1174 * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd,
1175 * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c nfs,
1176 * and @c iso.
1177 * @param restoring TRUE if we're restoring, FALSE if we're backing up.
1178 * @return The backup type chosen, or @c none if the user chose "Exit to shell".
1179 */
1180 t_bkptype which_backup_media_type(bool restoring) {
1181
1182 /*@ char ************************************************************ */
1183 t_bkptype output;
1184
1185
1186 /*@ newt ************************************************************ */
1187 char *title_sz;
1188 char *minimsg_sz;
1189 static t_bkptype possible_bkptypes[] =
1190 { none, cdr, cdrw, dvd, tape, cdstream, udev, nfs, iso };
1191 static char *possible_responses[] =
1192 { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev",
1193"nfs", "iso", NULL };
1194 char *outstr;
1195 t_bkptype backup_type;
1196 int i;
1197
1198 newtComponent b1;
1199 newtComponent b2;
1200 newtComponent b3;
1201 newtComponent b4;
1202 newtComponent b5;
1203 newtComponent b6;
1204 newtComponent b7;
1205 newtComponent b8;
1206 newtComponent b_res;
1207 newtComponent myForm;
1208
1209 title_sz = malloc(MAX_NEWT_COMMENT_LEN);
1210 minimsg_sz = malloc(MAX_NEWT_COMMENT_LEN);
1211 outstr = malloc(MAX_NEWT_COMMENT_LEN);
1212 if (g_text_mode) {
1213 for (backup_type = none; backup_type == none;) {
1214 printf("Backup type (");
1215 for (i = 0; possible_responses[i]; i++) {
1216 printf("%c%s", (i == 0) ? '\0' : ' ',
1217 possible_responses[i]);
1218 }
1219 printf(")\n--> ");
1220 (void) fgets(outstr, MAX_NEWT_COMMENT_LEN, stdin);
1221 strip_spaces(outstr);
1222 for (i = 0; possible_responses[i]; i++) {
1223 if (!strcmp(possible_responses[i], outstr)) {
1224 backup_type = possible_bkptypes[i];
1225 }
1226 }
1227 }
1228 paranoid_free(title_sz);
1229 paranoid_free(minimsg_sz);
1230 paranoid_free(outstr);
1231 return (backup_type);
1232 }
1233 newtDrawRootText(18, 0, WELCOME_STRING);
1234 if (restoring) {
1235 strcpy(title_sz,
1236 "Please choose the backup media from which you want to read data.");
1237 strcpy(minimsg_sz, "Read from:");
1238 } else {
1239 strcpy(title_sz,
1240 "Please choose the backup media to which you want to archive data.");
1241 strcpy(minimsg_sz, "Backup to:");
1242 }
1243 newtPushHelpLine(title_sz);
1244 newtCenteredWindow(34, 17, minimsg_sz);
1245 b1 = newtButton(1, 1, "CD-R disks ");
1246 b2 = newtButton(17, 1, "CD-RW disks");
1247 b3 = newtButton(1, 9, "Tape drive ");
1248 b4 = newtButton(17, 5, "USB Key/Disk");
1249 b5 = newtButton(1, 5, " DVD disks ");
1250 b6 = newtButton(17, 9, " NFS mount ");
1251 b7 = newtButton(1, 13, " Hard disk ");
1252 b8 = newtButton(17, 13, " Exit ");
1253 myForm = newtForm(NULL, NULL, 0);
1254 newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8,
1255 NULL);
1256 b_res = newtRunForm(myForm);
1257 newtFormDestroy(myForm);
1258 newtPopWindow();
1259 if (b_res == b1) {
1260 output = cdr;
1261 } else if (b_res == b2) {
1262 output = cdrw;
1263 } else if (b_res == b3) {
1264 output = tape;
1265 } else if (b_res == b4) {
1266 output = usb;
1267 } else if (b_res == b5) {
1268 output = dvd;
1269 } else if (b_res == b6) {
1270 output = nfs;
1271 } else if (b_res == b7) {
1272 output = iso;
1273 } else {
1274 output = none;
1275 }
1276 newtPopHelpLine();
1277 paranoid_free(title_sz);
1278 paranoid_free(minimsg_sz);
1279 paranoid_free(outstr);
1280 return (output);
1281 }
1282
1283
1284
1285
1286/**
1287 * Ask the user how much compression they would like to use.
1288 * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9).
1289 * @return The compression level (0-9) chosen, or -1 for "Exit".
1290 */
1291 int
1292 which_compression_level() {
1293
1294 /*@ char ************************************************************ */
1295 int output = none;
1296
1297
1298 /*@ newt ************************************************************ */
1299
1300 newtComponent b1;
1301 newtComponent b2;
1302 newtComponent b3;
1303 newtComponent b4;
1304 newtComponent b5;
1305 newtComponent b_res;
1306 newtComponent myForm;
1307
1308 newtDrawRootText(18, 0, WELCOME_STRING);
1309 newtPushHelpLine
1310 (" Please specify the level of compression that you want.");
1311 // newtOpenWindow (23, 3, 34, 13, "How much compression?");
1312 newtCenteredWindow(34, 13, "How much compression?");
1313 b1 = newtButton(4, 1, "Maximum");
1314 b2 = newtButton(18, 1, "Average");
1315 b3 = newtButton(4, 5, "Minimum");
1316 b4 = newtButton(18, 5, " None ");
1317 b5 = newtButton(4, 9, " Exit ");
1318 myForm = newtForm(NULL, NULL, 0);
1319 newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL);
1320 b_res = newtRunForm(myForm);
1321 newtFormDestroy(myForm);
1322 newtPopWindow();
1323 if (b_res == b1) {
1324 output = 9;
1325 } else if (b_res == b2) {
1326 output = 4;
1327 } else if (b_res == b3) {
1328 output = 1;
1329 } else if (b_res == b4) {
1330 output = 0;
1331 } else if (b_res == b5) {
1332 output = -1;
1333 }
1334 newtPopHelpLine();
1335 return (output);
1336 }
1337
1338
1339
1340
1341
1342/**
1343 * Load @p source_file (a list of files) into @p filelist. There can be no more than
1344 * @p ARBITRARY_MAXIMUM entries.
1345 * @param filelist The filelist structure to load @p source_file into.
1346 * @param source_file The file containing a list of filenames to load into @p filelist.
1347 */
1348 int load_filelist_into_array(struct s_filelist *filelist,
1349 char *source_file) {
1350 int i;
1351 int j = 0;
1352 bool done;
1353 char *tmp;
1354 char *tmp1 = NULL;
1355 FILE *fin, *fout;
1356 struct s_filelist_entry dummy_fle;
1357
1358 assert(filelist != NULL);
1359 assert_string_is_neither_NULL_nor_zerolength(source_file);
1360
1361 log_it("entering");
1362 if (!(fin = fopen(source_file, "r"))) {
1363 log_OS_error(source_file);
1364 log_msg(2, "Can't open %s; therefore, cannot popup list",
1365 source_file);
1366 return (1);
1367 }
1368
1369 mr_asprintf(&tmp1,"%s/icantfindthesefiles.txt",bkpinfo->tmpdir);
1370 if (!(fout = fopen(tmp1, "a"))) {
1371 log_msg(2, "Can't write to %s", tmp1);
1372 return(1);
1373 }
1374
1375 malloc_string(tmp);
1376 log_msg(2, "Loading %s", source_file);
1377 for (filelist->entries = 0; filelist->entries <= ARBITRARY_MAXIMUM;
1378 filelist->entries++) {
1379 god_i_hate_gotos:
1380 if (feof(fin)) {
1381 break;
1382 }
1383 (void) fgets(tmp, MAX_NEWT_COMMENT_LEN, fin);
1384 i = (int) strlen(tmp);
1385 if (i < 2) {
1386 goto god_i_hate_gotos;
1387 }
1388 if (tmp[i - 1] < 32) {
1389 tmp[--i] = '\0';
1390 }
1391 if (i < 2) {
1392 goto god_i_hate_gotos;
1393 }
1394 if (!does_file_exist(tmp) && !feof(fin)) {
1395 j++;
1396 fprintf(fout, "%s\n", tmp);
1397
1398 goto god_i_hate_gotos;
1399 }
1400 filelist->el[filelist->entries].severity =
1401 severity_of_difference(tmp, NULL);
1402 strcpy(filelist->el[filelist->entries].filename, tmp);
1403 if (feof(fin)) {
1404 break;
1405 }
1406 }
1407 paranoid_fclose(fin);
1408 paranoid_fclose(fout);
1409 if (j > 0) {
1410 log_to_screen("%d files listed in %s/changed.files have been deleted since backup was made\nand are referenced in %s", j, bkpinfo->tmpdir,tmp1);
1411 }
1412 paranoid_free(tmp1);
1413 if (filelist->entries >= ARBITRARY_MAXIMUM) {
1414 log_to_screen("Arbitrary limits suck, man!");
1415 paranoid_free(tmp);
1416 return (1);
1417 }
1418 for (done = FALSE; !done;) {
1419 done = TRUE;
1420 for (i = 0; i < filelist->entries - 1; i++) {
1421// if (strcmp(filelist->el[i].filename, filelist->el[i+1].filename) > 0)
1422 if (filelist->el[i].severity < filelist->el[i + 1].severity
1423 || (filelist->el[i].severity ==
1424 filelist->el[i + 1].severity
1425 && strcmp(filelist->el[i].filename,
1426 filelist->el[i + 1].filename) > 0)) {
1427 memcpy((void *) &dummy_fle,
1428 (void *) &(filelist->el[i]),
1429 sizeof(struct s_filelist_entry));
1430 memcpy((void *) &(filelist->el[i]),
1431 (void *) &(filelist->el[i + 1]),
1432 sizeof(struct s_filelist_entry));
1433 memcpy((void *) &(filelist->el[i + 1]),
1434 (void *) &dummy_fle,
1435 sizeof(struct s_filelist_entry));
1436 log_msg(2, "Swapping %s and %s",
1437 filelist->el[i].filename,
1438 filelist->el[i + 1].filename);
1439 done = FALSE;
1440 }
1441 }
1442 }
1443 paranoid_free(tmp);
1444 log_it("leaving");
1445 return (0);
1446 }
1447
1448
1449
1450/**
1451 * Generate a pretty string based on @p flentry.
1452 * @param flentry The filelist entry to stringify.
1453 * @return The string form of @p flentry.
1454 * @note The returned value points to static storage that will be overwritten with each call.
1455 */
1456 char *filelist_entry_to_string(struct s_filelist_entry *flentry) {
1457 static char comment[100];
1458 char *tmp = NULL;
1459
1460 log_it("entering");
1461 assert(flentry != NULL);
1462 if (flentry->severity == 0) {
1463 mr_asprintf(&tmp, "0 ");
1464 } else if (flentry->severity == 1) {
1465 mr_asprintf(&tmp, "low ");
1466 } else if (flentry->severity == 2) {
1467 mr_asprintf(&tmp, "med ");
1468 } else {
1469 mr_asprintf(&tmp, "high");
1470 }
1471 mr_strcat(tmp, " ");
1472 mr_strcat(tmp, flentry->filename);
1473 strncpy(comment, tmp, 99);
1474 paranoid_free(tmp);
1475
1476 log_it("leaving");
1477 return (comment);
1478 }
1479
1480
1481
1482
1483
1484/**
1485 * Pop up a list containing the filenames in @p source_file and the severity if they have changed since the
1486 * last backup. There can be no more than @p ARBITRARY_MAXIMUM files in @p source_file.
1487 * @param source_file The file containing a list of changed files.
1488 */
1489 void popup_changelist_from_file(char *source_file) {
1490 char *reason;
1491 newtComponent myForm;
1492 newtComponent bClose;
1493 newtComponent bSelect;
1494 newtComponent b_res;
1495 newtComponent fileListbox;
1496 newtComponent headerMsg;
1497
1498 /*@ ???? ************************************************************ */
1499 void *curr_choice;
1500 void *keylist[ARBITRARY_MAXIMUM];
1501
1502 /*@ int ************************************************************* */
1503 int currline = 0;
1504 int finished = FALSE;
1505
1506 /*@ long ************************************************************ */
1507 long i = 0;
1508 long lng = 0;
1509
1510 /*@ buffers ********************************************************* */
1511 char *tmp = NULL;
1512 char *differ_sz = NULL;
1513
1514 struct s_filelist *filelist;
1515 assert_string_is_neither_NULL_nor_zerolength(source_file);
1516 if (g_text_mode) {
1517 log_msg(2, "Text mode. Therefore, no popup list.");
1518 return;
1519 }
1520 log_msg(2, "Examining file %s", source_file);
1521
1522 lng = count_lines_in_file(source_file);
1523 if (lng < 1) {
1524 log_msg(2, "No lines in file. Therefore, no popup list.");
1525 return;
1526 } else if (lng >= ARBITRARY_MAXIMUM) {
1527 log_msg(2, "Too many files differ for me to list.");
1528 return;
1529 }
1530
1531 filelist = (struct s_filelist *) malloc(sizeof(struct s_filelist));
1532 fileListbox =
1533 newtListbox(2, 2, 12, NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
1534 newtListboxClear(fileListbox);
1535
1536 if (load_filelist_into_array(filelist, source_file)) {
1537 log_msg(2, "Can't open %s; therefore, cannot popup list", source_file);
1538 return;
1539 }
1540 log_msg(2, "%d files loaded into filelist array",
1541 filelist->entries);
1542 for (i = 0; i < filelist->entries; i++) {
1543 keylist[i] = (void *) i;
1544 newtListboxAppendEntry(fileListbox,
1545 filelist_entry_to_string(&
1546 (filelist->
1547 el[i])),
1548 keylist[i]);
1549 }
1550 mr_asprintf(&differ_sz, " %ld files differ. Hit 'Select' to pick a file. Hit 'Close' to quit the list.", i);
1551 newtPushHelpLine(differ_sz);
1552 mr_free(differ_sz);
1553
1554 bClose = newtCompactButton(10, 15, " Close ");
1555 bSelect = newtCompactButton(30, 15, " Select ");
1556 mr_asprintf(&tmp, "%-10s %-20s", "Priority", "Filename");
1557 headerMsg = newtLabel(2, 1, tmp);
1558 mr_free(tmp);
1559
1560 newtOpenWindow(5, 4, 70, 16, "Non-matching files");
1561 myForm = newtForm(NULL, NULL, 0);
1562 newtFormAddComponents(myForm, headerMsg, fileListbox, bClose,
1563 bSelect, NULL);
1564
1565 malloc_string(reason);
1566 while (!finished) {
1567 b_res = newtRunForm(myForm);
1568 if (b_res == bClose) {
1569 finished = TRUE;
1570 } else {
1571 curr_choice = newtListboxGetCurrent(fileListbox);
1572 for (i = 0;
1573 i < filelist->entries && keylist[i] != curr_choice;
1574 i++);
1575 if (i == filelist->entries && filelist->entries > 0) {
1576 log_to_screen("I don't know what that button does!");
1577 } else {
1578 currline = i;
1579 if (filelist->entries > 0) {
1580 severity_of_difference(filelist->el[currline]. filename, reason);
1581 mr_asprintf(&tmp, "%s --- %s", filelist->el[currline].filename, reason);
1582
1583 popup_and_OK(tmp);
1584 mr_free(tmp);
1585 }
1586 }
1587 }
1588 }
1589 paranoid_free(reason);
1590
1591 newtFormDestroy(myForm);
1592 newtPopWindow();
1593 newtPopHelpLine();
1594 return;
1595 }
1596
1597/* @} - end of guiGroup */
1598
1599
1600#if __cplusplus
1601} /* extern "C" */
1602#endif
1603
1604
1605void wait_until_software_raids_are_prepped(char *mdstat_file,
1606 int wait_for_percentage);
Note: See TracBrowser for help on using the repository browser.