source: MondoRescue/branches/2.2.7/mondo/src/common/libmondo-fork.c@ 1978

Last change on this file since 1978 was 1784, checked in by Bruno Cornec, 16 years ago

Fix mindi for Debian x86_64 where /lib64 is a link

  • Property svn:keywords set to Id
File size: 23.3 KB
Line 
1/* libmondo-fork.c
2 $Id: libmondo-fork.c 1784 2007-11-10 22:47:07Z bruno $
3
4- subroutines for handling forking/pthreads/etc.
5
6
701/20/2006
8- replaced partimagehack with ntfsclone
9
1006/20/2004
11- create fifo /var/log/partimagehack-debug.log and empty it
12 to keep ramdisk from filling up
13
1404/13/2004
15- >= should be <= g_loglevel
16
1711/15/2003
18- changed a few []s to char*s
19
2010/12
21- rewrote partimagehack handling (multiple fifos, chunks, etc.)
22
2310/11
24- partimagehack now has debug level of N (set in my-stuff.h)
25
2610/08
27- call to partimagehack when restoring will now log errors to /var/log/....log
28
2910/06
30- cleaned up logging a bit
31
3209/30
33- line 735 - missing char* cmd in sprintf()
34
3509/28
36- added run_external_binary_with_percentage_indicator()
37- rewritten eval_call_to_make_ISO()
38
3909/18
40- call mkstemp instead of mktemp
41
4209/13
43- major NTFS hackage
44
4509/12
46- paranoid_system("rm -f /tmp/ *PARTIMAGE*") before calling partimagehack
47
4809/11
49- forward-ported unbroken feed_*_partimage() subroutines
50 from early August 2003
51
5209/08
53- detect & use partimagehack if it exists
54
5509/05
56- finally finished partimagehack hack :)
57
5807/04
59- added subroutines to wrap around partimagehack
60
6104/27
62- don't echo (...res=%d...) at end of log_it()
63 unnecessarily
64- replace newtFinished() and newtInit() with
65 newtSuspend() and newtResume()
66
6704/24
68- added some assert()'s and log_OS_error()'s
69
7004/09
71- cleaned up run_program_and_log_output()
72
7304/07
74- cleaned up code a bit
75- let run_program_and_log_output() accept -1 (only log if _no error_)
76
7701/02/2003
78- in eval_call_to_make_ISO(), append output to MONDO_LOGFILE
79 instead of a temporary stderr text file
80
8112/10
82- patch by Heiko Schlittermann to handle % chars in issue.net
83
8411/18
85- if mkisofs in eval_call_to_make_ISO() returns an error then return it,
86 whether ISO was created or not
87
8810/30
89- if mkisofs in eval_call_to_make_ISO() returns an error then find out if
90 the output (ISO) file has been created; if it has then return 0 anyway
91
9208/01 - 09/30
93- run_program_and_log_output() now takes boolean operator to specify
94 whether it will log its activities in the event of _success_
95- system() now includes 2>/dev/null
96- enlarged some tmp[]'s
97- added run_program_and_log_to_screen() and run_program_and_log_output()
98
9907/24
100- created
101*/
102
103
104#include "my-stuff.h"
105#include "mondostructures.h"
106#include "libmondo-fork.h"
107#include "libmondo-string-EXT.h"
108#include "libmondo-gui-EXT.h"
109#include "libmondo-files-EXT.h"
110#include "libmondo-tools-EXT.h"
111#include "lib-common-externs.h"
112
113/*@unused@*/
114//static char cvsid[] = "$Id: libmondo-fork.c 1784 2007-11-10 22:47:07Z bruno $";
115
116extern t_bkptype g_backup_media_type;
117extern bool g_text_mode;
118extern char *MONDO_LOGFILE;
119
120/* Reference to global bkpinfo */
121extern struct s_bkpinfo *bkpinfo;
122pid_t g_buffer_pid = 0;
123
124
125/**
126 * Call a program and retrieve its last line of output.
127 * @param call The program to run.
128 * @return The last line of its output.
129 * @note The returned value points to static storage that will be overwritten with each call.
130 */
131char *call_program_and_get_last_line_of_output(char *call)
132{
133 /*@ buffers ***************************************************** */
134 static char result[512];
135 char *tmp;
136
137 /*@ pointers **************************************************** */
138 FILE *fin;
139
140 /*@ initialize data ********************************************* */
141 malloc_string(tmp);
142 result[0] = '\0';
143 tmp[0] = '\0';
144
145 /*@******************************************************************** */
146
147 assert_string_is_neither_NULL_nor_zerolength(call);
148 if ((fin = popen(call, "r"))) {
149 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
150 fgets(tmp, MAX_STR_LEN, fin)) {
151 if (strlen(tmp) > 1) {
152 strcpy(result, tmp);
153 }
154 }
155 paranoid_pclose(fin);
156 } else {
157 log_OS_error("Unable to popen call");
158 }
159 strip_spaces(result);
160 paranoid_free(tmp);
161 return (result);
162}
163
164
165
166
167
168
169#define MONDO_POPMSG "Your PC will not retract the CD tray automatically. Please call mondoarchive with the -m (manual CD tray) flag."
170
171/**
172 * Call mkisofs to create an ISO image.
173 * @param bkpinfo The backup information structure. Fields used:
174 * - @c bkpinfo->manual_cd_tray
175 * - @c bkpinfo->backup_media_type
176 * - @c bkpinfo->please_dont_eject_when_restoring
177 * @param basic_call The call to mkisofs. May contain tokens that will be resolved to actual data. The tokens are:
178 * - @c _ISO_ will become the ISO file (@p isofile)
179 * - @c _CD#_ becomes the CD number (@p cd_no)
180 * - @c _ERR_ becomes the logfile (@p g_logfile)
181 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
182 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
183 * @param logstub Unused.
184 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
185 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
186 * @bug @p logstub is unused.
187 */
188int
189eval_call_to_make_ISO(char *basic_call, char *isofile,
190 int cd_no, char *logstub, char *what_i_am_doing)
191{
192
193 /*@ int's *** */
194 int retval = 0;
195
196
197 /*@ buffers *** */
198 char *midway_call, *ultimate_call, *tmp, *command, *incoming,
199 *old_stderr, *cd_number_str;
200 char *p;
201
202/*@*********** End Variables ***************************************/
203
204 log_msg(3, "Starting");
205 assert(bkpinfo != NULL);
206 // BERLIOS: doesn't work even if the string is correct !
207 //assert_string_is_neither_NULL_nor_zerolength(basic_call);
208 assert_string_is_neither_NULL_nor_zerolength(isofile);
209 assert_string_is_neither_NULL_nor_zerolength(logstub);
210 if (!(midway_call = malloc(1200))) {
211 fatal_error("Cannot malloc midway_call");
212 }
213 if (!(ultimate_call = malloc(1200))) {
214 fatal_error("Cannot malloc ultimate_call");
215 }
216 if (!(tmp = malloc(1200))) {
217 fatal_error("Cannot malloc tmp");
218 }
219 if (!(command = malloc(1200))) {
220 fatal_error("Cannot malloc command");
221 }
222 malloc_string(incoming);
223 malloc_string(old_stderr);
224 malloc_string(cd_number_str);
225
226 incoming[0] = '\0';
227 old_stderr[0] = '\0';
228
229 sprintf(cd_number_str, "%d", cd_no);
230 resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
231 resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
232 resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
233 log_msg(4, "basic call = '%s'", basic_call);
234 log_msg(4, "midway_call = '%s'", midway_call);
235 log_msg(4, "tmp = '%s'", tmp);
236 log_msg(4, "ultimate call = '%s'", ultimate_call);
237 sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
238
239 log_to_screen
240 ("Please be patient. Do not be alarmed by on-screen inactivity.");
241 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
242 what_i_am_doing);
243 strcpy(tmp, command);
244 if (bkpinfo->manual_cd_tray) {
245 p = strstr(tmp, "2>>");
246 if (p) {
247 sprintf(p, " ");
248 while (*p == ' ') {
249 p++;
250 }
251 for (; *p != ' '; p++) {
252 *p = ' ';
253 }
254 }
255 strcpy(command, tmp);
256#ifndef _XWIN
257 if (!g_text_mode) {
258 newtSuspend();
259 }
260#endif
261 log_msg(1, "command = '%s'", command);
262 retval += system(command);
263 if (!g_text_mode) {
264 newtResume();
265 }
266 if (retval) {
267 log_msg(2, "Basic call '%s' returned an error.", basic_call);
268 popup_and_OK("Press ENTER to continue.");
269 popup_and_OK
270 ("mkisofs and/or cdrecord returned an error. CD was not created");
271 }
272 }
273 /* if text mode then do the above & RETURN; if not text mode, do this... */
274 else {
275 log_msg(3, "command = '%s'", command);
276// yes_this_is_a_goto:
277 retval =
278 run_external_binary_with_percentage_indicator_NEW
279 (what_i_am_doing, command);
280 }
281
282 paranoid_free(midway_call);
283 paranoid_free(ultimate_call);
284 paranoid_free(tmp);
285 paranoid_free(command);
286 paranoid_free(incoming);
287 paranoid_free(old_stderr);
288 paranoid_free(cd_number_str);
289 return (retval);
290}
291
292
293/**
294 * Call copy of data to create an USB image.
295 * @param bkpinfo The backup information structure. Fields used:
296 * - @c bkpinfo->backup_media_type
297 * @return Exit code of @c copy (0 is success, anything else indicates failure).
298 */
299int
300eval_call_to_make_USB(char *command, char *what_i_am_doing) {
301
302 /*@ int's *** */
303 int retval = 0;
304
305
306/*@*********** End Variables ***************************************/
307
308 log_msg(3, "Starting");
309 assert(bkpinfo != NULL);
310
311 log_to_screen
312 ("Please be patient. Do not be alarmed by on-screen inactivity.");
313 log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
314 what_i_am_doing);
315
316 if (!g_text_mode) {
317 newtSuspend();
318 }
319 log_msg(1, "command = '%s'", command);
320 if (!g_text_mode) {
321 retval = run_external_binary_with_percentage_indicator_NEW
322 (what_i_am_doing, command);
323 } else {
324 retval += system(command);
325 }
326 if (!g_text_mode) {
327 newtResume();
328 }
329
330 return (retval);
331}
332
333
334
335
336/**
337 * Run a program and log its output (stdout and stderr) to the logfile.
338 * @param program The program to run. Passed to the shell, so you can use pipes etc.
339 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
340 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
341 */
342int run_program_and_log_output(char *program, int debug_level)
343{
344 /*@ buffer ****************************************************** */
345 char callstr[MAX_STR_LEN * 2];
346 char incoming[MAX_STR_LEN * 2];
347 char tmp[MAX_STR_LEN * 2];
348 char initial_label[MAX_STR_LEN * 2];
349
350 /*@ int ********************************************************* */
351 int res;
352 int i;
353 int len;
354 bool log_if_failure = FALSE;
355 bool log_if_success = FALSE;
356
357 /*@ pointers *************************************************** */
358 FILE *fin;
359 char *p;
360
361 /*@ end vars *************************************************** */
362
363 assert(program != NULL);
364 if (!program[0]) {
365 log_msg(2, "Warning - asked to run zerolength program");
366 return (1);
367 }
368// if (debug_level == TRUE) { debug_level=5; }
369
370 // assert_string_is_neither_NULL_nor_zerolength(program);
371
372 if (debug_level <= g_loglevel) {
373 log_if_success = TRUE;
374 log_if_failure = TRUE;
375 }
376 sprintf(callstr, "%s > %s/mondo-run-prog-thing.tmp 2> %s/mondo-run-prog-thing.err",
377 program, bkpinfo->tmpdir, bkpinfo->tmpdir);
378 while ((p = strchr(callstr, '\r'))) {
379 *p = ' ';
380 }
381 while ((p = strchr(callstr, '\n'))) {
382 *p = ' ';
383 } /* single '=' is intentional */
384
385
386 len = (int) strlen(program);
387 for (i = 0; i < 35 - len / 2; i++) {
388 tmp[i] = '-';
389 }
390 tmp[i] = '\0';
391 strcat(tmp, " ");
392 strcat(tmp, program);
393 strcat(tmp, " ");
394 for (i = 0; i < 35 - len / 2; i++) {
395 strcat(tmp, "-");
396 }
397 strcpy(initial_label, tmp);
398 res = system(callstr);
399 if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
400 log_msg(0, "running: %s", callstr);
401 log_msg(0,
402 "--------------------------------start of output-----------------------------");
403 }
404 sprintf(callstr, "cat %s/mondo-run-prog-thing.err >> %s/mondo-run-prog-thing.tmp 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
405 if (log_if_failure && system(callstr)) {
406 log_OS_error("Command failed");
407 }
408 sprintf(tmp, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
409 unlink(tmp);
410 sprintf(tmp, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
411 fin = fopen(tmp, "r");
412 if (fin) {
413 for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
414 fgets(incoming, MAX_STR_LEN, fin)) {
415 /* patch by Heiko Schlittermann */
416 p = incoming;
417 while (p && *p) {
418 if ((p = strchr(p, '%'))) {
419 memmove(p, p + 1, strlen(p) + 1);
420 p += 2;
421 }
422 }
423 /* end of patch */
424 strip_spaces(incoming);
425 if ((res == 0 && log_if_success)
426 || (res != 0 && log_if_failure)) {
427 log_msg(0, incoming);
428 }
429 }
430 paranoid_fclose(fin);
431 }
432 unlink(tmp);
433 if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
434 log_msg(0,
435 "--------------------------------end of output------------------------------");
436 if (res) {
437 log_msg(0, "...ran with res=%d", res);
438 } else {
439 log_msg(0, "...ran just fine. :-)");
440 }
441 }
442// else
443// { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
444 return (res);
445}
446
447
448
449/**
450 * Run a program and log its output to the screen.
451 * @param basic_call The program to run.
452 * @param what_i_am_doing The title of the evalcall form.
453 * @return The return value of the command (varies, but 0 almost always means success).
454 * @see run_program_and_log_output
455 * @see log_to_screen
456 */
457int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
458{
459 /*@ int ******************************************************** */
460 int retval = 0;
461 int res = 0;
462 int i;
463
464 /*@ pointers **************************************************** */
465 FILE *fin;
466
467 /*@ buffers **************************************************** */
468 char tmp[MAX_STR_LEN * 2];
469 char command[MAX_STR_LEN * 2];
470 char lockfile[MAX_STR_LEN];
471
472 /*@ end vars *************************************************** */
473
474 assert_string_is_neither_NULL_nor_zerolength(basic_call);
475
476 sprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
477
478 sprintf(command,
479 "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
480 lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
481 open_evalcall_form(what_i_am_doing);
482 sprintf(tmp, "Executing %s", basic_call);
483 log_msg(2, tmp);
484 if (!(fin = popen(command, "r"))) {
485 log_OS_error("Unable to popen-in command");
486 sprintf(tmp, "Failed utterly to call '%s'", command);
487 log_to_screen(tmp);
488 return (1);
489 }
490 if (!does_file_exist(lockfile)) {
491 log_to_screen("Waiting for external binary to start");
492 for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
493 log_msg(3, "Waiting for lockfile %s to exist", lockfile);
494 }
495 }
496#ifdef _XWIN
497 /* This only can update when newline goes into the file,
498 but it's *much* prettier/faster on Qt. */
499 while (does_file_exist(lockfile)) {
500 while (!feof(fin)) {
501 if (!fgets(tmp, 512, fin))
502 break;
503 log_to_screen(tmp);
504 }
505 usleep(500000);
506 }
507#else
508 /* This works on Newt, and it gives quicker updates. */
509 for (; does_file_exist(lockfile); sleep(1)) {
510 log_file_end_to_screen(MONDO_LOGFILE, "");
511 update_evalcall_form(1);
512 }
513#endif
514 /* Evaluate the status returned by pclose to get the exit code of the called program. */
515 errno = 0;
516 res = pclose(fin);
517 /* Log actual pclose errors. */
518 if (errno) log_msg(5, "pclose err: %d", errno);
519 /* Check if we have a valid status. If we do, extract the called program's exit code. */
520 /* If we don't, highlight this fact by returning -1. */
521 if (WIFEXITED(res)) {
522 retval = WEXITSTATUS(res);
523 } else {
524 retval = -1;
525 }
526 close_evalcall_form();
527 unlink(lockfile);
528 return (retval);
529}
530
531
532
533
534/**
535 * Apparently unused. @bug This has a purpose, but what?
536 */
537#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
538#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
539
540
541
542
543int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
544{
545// if dir=='w' then copy from orig to archived
546// if dir=='r' then copy from archived to orig
547 char *tmp;
548 char *buf;
549 char filestr[MAX_STR_LEN];
550 long int bytes_to_be_read, bytes_read_in, bytes_written_out =
551 0, bufcap, subsliceno = 0;
552 int retval = 0;
553 FILE *fin;
554 FILE *fout;
555 FILE *ftmp;
556
557 log_msg(5, "Opening.");
558 malloc_string(tmp);
559 tmp[0] = '\0';
560 bufcap = 256L * 1024L;
561 if (!(buf = malloc(bufcap))) {
562 fatal_error("Failed to malloc() buf");
563 }
564
565 if (direction == 'w') {
566 fin = f_orig;
567 fout = f_archived;
568 sprintf(tmp, "%-64s", PIMP_START_SZ);
569 if (fwrite(tmp, 1, 64, fout) != 64) {
570 fatal_error("Can't write the introductory block");
571 }
572 while (1) {
573 bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
574 if (bytes_read_in == 0) {
575 break;
576 }
577 sprintf(tmp, "%-64ld", bytes_read_in);
578 if (fwrite(tmp, 1, 64, fout) != 64) {
579 fatal_error("Cannot write introductory block");
580 }
581 log_msg(7,
582 "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
583 subsliceno, bytes_read_in, bytes_to_be_read);
584 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
585 sprintf(tmp, "%-64ld", subsliceno);
586 if (fwrite(tmp, 1, 64, fout) != 64) {
587 fatal_error("Cannot write post-thingy block");
588 }
589 log_msg(7, "Subslice #%d written OK", subsliceno);
590 subsliceno++;
591 }
592 sprintf(tmp, "%-64ld", 0L);
593 if (fwrite(tmp, 1, 64L, fout) != 64L) {
594 fatal_error("Cannot write final introductory block");
595 }
596 } else {
597 fin = f_archived;
598 fout = f_orig;
599 if (fread(tmp, 1, 64L, fin) != 64L) {
600 fatal_error("Cannot read the introductory block");
601 }
602 log_msg(5, "tmp is %s", tmp);
603 if (!strstr(tmp, PIMP_START_SZ)) {
604 fatal_error("Can't find intro blk");
605 }
606 if (fread(tmp, 1, 64L, fin) != 64L) {
607 fatal_error("Cannot read introductory blk");
608 }
609 bytes_to_be_read = atol(tmp);
610 while (bytes_to_be_read > 0) {
611 log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
612 bytes_to_be_read);
613 bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
614 if (bytes_read_in != bytes_to_be_read) {
615 fatal_error
616 ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
617 }
618 bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
619 if (fread(tmp, 1, 64, fin) != 64) {
620 fatal_error("Cannot read post-thingy block");
621 }
622 if (atol(tmp) != subsliceno) {
623 log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
624 subsliceno, atol(tmp), tmp);
625 }
626 log_msg(7, "Subslice #%ld read OK", subsliceno);
627 subsliceno++;
628 if (fread(tmp, 1, 64, fin) != 64) {
629 fatal_error("Cannot read introductory block");
630 }
631 bytes_to_be_read = atol(tmp);
632 }
633 }
634
635// log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
636
637 if (direction == 'w') {
638 sprintf(tmp, "%-64s", PIMP_END_SZ);
639 if (fwrite(tmp, 1, 64, fout) != 64) {
640 fatal_error("Can't write the final block");
641 }
642 } else {
643 log_msg(1, "tmpA is %s", tmp);
644 if (!strstr(tmp, PIMP_END_SZ)) {
645 if (fread(tmp, 1, 64, fin) != 64) {
646 fatal_error("Can't read the final block");
647 }
648 log_msg(5, "tmpB is %s", tmp);
649 if (!strstr(tmp, PIMP_END_SZ)) {
650 sprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
651 ftmp = fopen(filestr, "w");
652 bytes_read_in = fread(tmp, 1, 64, fin);
653 log_msg(1, "bytes_read_in = %ld", bytes_read_in);
654// if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
655 fwrite(tmp, 1, bytes_read_in, ftmp);
656 sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
657// log_msg(0, tmp);
658 fread(tmp, 1, 512, fin);
659 log_msg(0, "tmp = '%s'", tmp);
660 fwrite(tmp, 1, 512, ftmp);
661 fclose(ftmp);
662 fatal_error("Missing terminating block");
663 }
664 }
665 }
666
667 paranoid_free(buf);
668 paranoid_free(tmp);
669 log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
670 return (retval);
671}
672
673
674
675
676/**
677 * Feed @p input_device through ntfsclone to @p output_fname.
678 * @param input_device The device to image.
679 * @param output_fname The file to write.
680 * @return 0 for success, nonzero for failure.
681 */
682int feed_into_ntfsprog(char *input_device, char *output_fname)
683{
684// BACKUP
685 int res = -1;
686 char*command;
687
688 if (!does_file_exist(input_device)) {
689 fatal_error("input device does not exist");
690 }
691 if ( !find_home_of_exe("ntfsclone")) {
692 fatal_error("ntfsclone not found");
693 }
694 malloc_string(command);
695 sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
696 res = run_program_and_log_output(command, 5);
697 paranoid_free(command);
698 unlink(output_fname);
699 return (res);
700}
701
702
703
704
705
706int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
707{
708
709 /*@ int *************************************************************** */
710 int res = 0;
711 int percentage = 0;
712 int maxpc = 0;
713 int pcno = 0;
714 int last_pcno = 0;
715
716 /*@ buffers *********************************************************** */
717 char *command;
718 char *tempfile;
719 char *title;
720 /*@ pointers ********************************************************** */
721 FILE *pin;
722
723 malloc_string(title);
724 malloc_string(command);
725 malloc_string(tempfile);
726 assert_string_is_neither_NULL_nor_zerolength(cmd);
727 assert_string_is_neither_NULL_nor_zerolength(title);
728
729 strcpy(title, tt);
730 sprintf(tempfile, "%s/mondo.binperc", bkpinfo->tmpdir);
731 sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
732 tempfile);
733 log_msg(3, command);
734 open_evalcall_form(title);
735 if (!(pin = popen(command, "r"))) {
736 log_OS_error("fmt err");
737 return (1);
738 }
739 maxpc = 100;
740// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
741 for (sleep(1); does_file_exist(tempfile); sleep(1)) {
742 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
743 if (pcno < 0 || pcno > 100) {
744 log_msg(5, "Weird pc#");
745 continue;
746 }
747 percentage = pcno * 100 / maxpc;
748 if (pcno <= 5 && last_pcno > 40) {
749 close_evalcall_form();
750 strcpy(title, "Verifying...");
751 open_evalcall_form(title);
752 }
753 last_pcno = pcno;
754 update_evalcall_form(percentage);
755 }
756// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
757 close_evalcall_form();
758 if (pclose(pin)) {
759 res++;
760 log_OS_error("Unable to pclose");
761 }
762 unlink(tempfile);
763 paranoid_free(command);
764 paranoid_free(tempfile);
765 paranoid_free(title);
766 return (res);
767}
768
769
770
771
772void *run_prog_in_bkgd_then_exit(void *info)
773{
774 char *sz_command;
775 static int res = 4444;
776
777 res = 999;
778 sz_command = (char *) info;
779 log_msg(4, "sz_command = '%s'", sz_command);
780 res = system(sz_command);
781 if (res > 256 && res != 4444) {
782 res = res / 256;
783 }
784 log_msg(4, "child res = %d", res);
785 sz_command[0] = '\0';
786 pthread_exit((void *) (&res));
787}
788
789
790
791
792int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
793{
794
795 /*@ int *************************************************************** */
796 int res = 0;
797 int percentage = 0;
798 int maxpc = 100;
799 int pcno = 0;
800 int last_pcno = 0;
801 int counter = 0;
802
803 /*@ buffers *********************************************************** */
804 char *command;
805 char *title;
806 /*@ pointers ********************************************************** */
807 static int chldres = 0;
808 int *pchild_result;
809 pthread_t childthread;
810
811 pchild_result = &chldres;
812 assert_string_is_neither_NULL_nor_zerolength(cmd);
813 assert_string_is_neither_NULL_nor_zerolength(tt);
814 *pchild_result = 999;
815
816 malloc_string(title);
817 malloc_string(command);
818 strcpy(title, tt);
819 sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
820 log_msg(3, "command = '%s'", command);
821 if ((res =
822 pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
823 (void *) command))) {
824 fatal_error("Unable to create an archival thread");
825 }
826
827 log_msg(8, "Parent running");
828 open_evalcall_form(title);
829 for (sleep(1); command[0] != '\0'; sleep(1)) {
830 pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
831 if (pcno <= 0 || pcno > 100) {
832 log_msg(8, "Weird pc#");
833 continue;
834 }
835 percentage = pcno * 100 / maxpc;
836 if (pcno <= 5 && last_pcno >= 40) {
837 close_evalcall_form();
838 strcpy(title, "Verifying...");
839 open_evalcall_form(title);
840 }
841 if (counter++ >= 5) {
842 counter = 0;
843 log_file_end_to_screen(MONDO_LOGFILE, "");
844 }
845 last_pcno = pcno;
846 update_evalcall_form(percentage);
847 }
848 log_file_end_to_screen(MONDO_LOGFILE, "");
849 close_evalcall_form();
850 pthread_join(childthread, (void *) (&pchild_result));
851 if (pchild_result) {
852 res = *pchild_result;
853 } else {
854 res = 666;
855 }
856 log_msg(3, "Parent res = %d", res);
857 paranoid_free(command);
858 paranoid_free(title);
859 return (res);
860}
861
862
863
864
865/**
866 * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
867 * @param input_fifo The ntfsclone file to read.
868 * @param output_device Where to put the output.
869 * @return The return value of ntfsclone (0 for success).
870 */
871int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
872{
873// RESTORE
874 int res = -1;
875 char *command;
876
877 if ( !find_home_of_exe("ntfsclone")) {
878 fatal_error("ntfsclone not found");
879 }
880 malloc_string(command);
881 sprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
882 res = run_program_and_log_output(command, 5);
883 paranoid_free(command);
884 return (res);
885}
Note: See TracBrowser for help on using the repository browser.