source: MondoRescue/branches/3.1/mondo/src/common/newt-specific.c@ 2937

Last change on this file since 2937 was 2937, checked in by Bruno Cornec, 12 years ago

svn merge -r 2849:2935 ../3.0

  • Adds 3 binaries called potentially by udev o support USB key mount at restore time (Victor Gattegno)
  • mindi only sets the boot flag of a USB device if none is present already (should avoid flip/flop situation)
  • Fix a compatibility bug in analyze-my-lvm whete exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Precise that -J is not supported by mondorestore yet.
  • Fix a compatibility bug in mindi where exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Install an MBR on USB devices when possible
  • Improve error message in case mondorestore isn't found
  • Adds support for more virtio devices for KVM restore
  • Improve mbr.bin handling
  • Fix an issue in test of FindKernel for Xen and non Xen cases
    • Fix #492 by removing an extra useless fi
    • Move fvat nd fat modules to CDROM_MODS in order to have them in the initrd to support saving logs to a USB device e.g.
    • Fix an error when dmidecode reports comments before system name
    • Fix previous broken patch on comment management for bkphw
    • Improve msgs printed during kbd analysis
    • Fix a small remaining pb with new comment support
    • Try to again improve the mount/umount stuff in mondo -Adds mention to OEL, CentOS, ScientifLinux as RHEL clones
  • Fix a panadoid_free done on the wrong variable (solve a memory leak)
  • Solve #495 by skipping the potential (auto-read-only) when analyzin /proc/mdstat result
  • Fix some markup in man pages
  • adds the post-nuke script sample for Debian (Guillaume Pernot/Andrée Leidenfrost)
  • Avoid a useless umount when changing of afio set, let insist_on_cd check and perform if needed
  • Copy everything under .ssh of root account to allow for sshfs mount without passwd at restore time.
  • Adds support for bnx2i driver
  • Try to fix #496 by supporting the protocol in the output of the mount command (sshfs#...)
  • Uses the -d option of the mount command to avoid leaving loop devices in use. Probably provoked by the usage of a more recent busybox, but anyway that option doesn't hurt, and does nothing for non-loop devices. Should fix the problems reported on the ML with NFS and more than 8 ISO images (8 being the default number of loop devices available)
  • Attempt to fix #436 by adding some newtDrawForm where appropriate.
  • Fix bugs in the kernel detection routine of mindi (some echo were done without capturing their output, confusing the return). This should also help the Xen detection issue
  • Change the logging function to be nearer from 2.2.10, and start using some
  • Initialize some global fields with an empty string, now that newt Draws more regularly progress forms
  • Mindi progression still not good, but showing now 3% when calling mindi. Would need a specific handling for the progress bar to evolve more afterwards
  • Add compatibility with the latest SSSTK + PSP content for HP ProLiant + branding homogeneous now. We only take conrep out of the SSSTK pending a package availability. Using hpacuscripting now. Review of parameters called following test made.
  • Fix #501 by pushing BOOT_SIZE to 36864 for certain Ubuntu cases
  • Fix some remaining inconsistencis in the ProLiant HW backup. ready for usage.
  • Fix an issue on udevadm call (needs an intermediate variable)
  • Fix a remaining ProLiant tool call
  • Remove again some wrong variable initialization for Net in order to not have strange messages now we call Draw more often
  • Adds support for hwaddr (MAC addresses) at restore time, and map to the correct NIC if it's found at restore time (based on code from Stefan Heijmans)
  • Try to Fix #499 by changing init in order to never exit, and rebooting using sysrq in /proc (not portable). Maybe should move to using a real init, and a separated rcS script.
  • Fixes the shell msg error by using setsid as per Busybox FAQ
  • Fix #491 by correctly checking Xen kernel used for a dom0 env (apollo13)
  • Improves logging in Die()
  • Improve again ProLiant HW support, by adding references to the yet to come SSSTK rpm so that conrep is called at its future right place.
  • Improve mindi support for kernel 3.x
  • Fix #499 for good, by using a real init from busybox and moving our init script to etc/init.d/rcS. We also now call the reboot from busybox to exit, which indeed reset the system correctly (in a VM at least which wasn't the case before). Reauires a new mindi-busybox as well.
  • Attempt to solve #503 by handling /lib64 separately for udev and only if not a link (to /lib) for Ubuntu 64
  • Improve again previous tests for bug #503
  • Improve -d option description for NFS
  • Allow http download for ftp content
  • New italian mirror
  • Fix #505 by documenting environment variables
    • Fix a bug where on OBDR mode compression was turned off to create the OBDR content but not back on (report from the mailing list)
  • solve http://trac.mondorescue.org/ticket/520 by Victor Gattegno
  • Fix #524 as option -U needs a size also
  • Removes useless gendf option
  • Fix #511 now stabgrub is called also after conf file modifications
  • Adds support for SUID binaries
  • remove a useless xenkernelname variable from mindi
  • Should fix #512 by clearing old scratchdir before changing to a new
  • Simplify the function engaging SCSI for Smart Array controllers, and support as many controller as we have
  • Add some binaries used on SLES 10 by udev
  • Fix a syntax issue in newt-specific.c
  • Fix a memory free bug where a used variable was freed to early
  • Fix a memory free bug where a used variable was misused in asprintf/free
  • Fix #498 by using an external program (hpsa_obdr_mode) which can set the mode of the tape to CD-ROM or Sequential at will.

That program will have to be downloaded from http://cciss.sf.net

  • Adds support for the route command in mindi-busybox
  • Force block size back to 32768 in OBDR mode after skipping the 2 tape marks, to read the additional rootfs data corerctly - Fix #498 for good it seems
  • Adds support for a new pata_atiixp driver
  • Fix #525 and improve printing when no /etc/raidtab, but still md devices
  • Exclude /run similarly to /sys and /proc
  • Fix a bug in mindi when having multiple swap partiions with same starting string (/dev/dm-1 and /dev/dm-16). We now add a space in the grep to avoid mischoice.
  • Fix grub installation if we had to go in the hack part which wasn't using the correct variables to make decisions !
  • Remove all mention of mondorescue.com from code
  • Fix some website typo on the download page and precise that we now develop on Mageia
  • Exclude sysfs devpts and tmpfs partitions found in RHEL 6.2 from analyze by mindi (made error messages)
  • Force activation of first partition which should not be optional as everything is destroyed before
  • Fix a bug in analyze-my-lvm where LVs were created in random order (the one of lvscan) instead of using the dm-xxx order, which creates issues at restore time between the mapping created then and the one we had at backup time which could be inconssistent

-mondoarchive now exists if the mt command is missing when needed

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