source: MondoRescue/branches/2.2.5/mondo/src/common/libmondo-fork.c@ 1745

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