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

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

Render bkpinfo global (potential issue on thread, but should not be a problem as that structure is indeed static during archive)
Should solve the tmpdir issue from previous rev.
May still not compile

  • Property svn:keywords set to Id
File size: 22.6 KB
Line 
1/* libmondo-fork.c
2   $Id: libmondo-fork.c 1645 2007-09-24 01:04:43Z 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 1645 2007-09-24 01:04:43Z 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/*
291  if (bkpinfo->backup_media_type == dvd && !bkpinfo->please_dont_eject_when_restoring)
292    {
293      log_msg(3, "Ejecting DVD device");
294      eject_device(bkpinfo->media_device);
295    }
296*/
297    return (retval);
298}
299
300
301
302
303/**
304 * Run a program and log its output (stdout and stderr) to the logfile.
305 * @param program The program to run. Passed to the shell, so you can use pipes etc.
306 * @param debug_level If @p g_loglevel is higher than this, do not log the output.
307 * @return The exit code of @p program (depends on the command, but 0 almost always indicates success).
308 */
309int run_program_and_log_output(char *program, int debug_level)
310{
311    /*@ buffer ****************************************************** */
312    char callstr[MAX_STR_LEN * 2];
313    char incoming[MAX_STR_LEN * 2];
314    char tmp[MAX_STR_LEN * 2];
315    char initial_label[MAX_STR_LEN * 2];
316
317    /*@ int ********************************************************* */
318    int res;
319    int i;
320    int len;
321    bool log_if_failure = FALSE;
322    bool log_if_success = FALSE;
323
324    /*@ pointers *************************************************** */
325    FILE *fin;
326    char *p;
327
328    /*@ end vars *************************************************** */
329
330    assert(program != NULL);
331    if (!program[0]) {
332        log_msg(2, "Warning - asked to run zerolength program");
333        return (1);
334    }
335//  if (debug_level == TRUE) { debug_level=5; }
336
337    //  assert_string_is_neither_NULL_nor_zerolength(program);
338
339    if (debug_level <= g_loglevel) {
340        log_if_success = TRUE;
341        log_if_failure = TRUE;
342    }
343    sprintf(callstr, "%s > %s/mondo-run-prog-thing.tmp 2> %s/mondo-run-prog-thing.err",
344             program, bkpinfo->tmpdir, bkpinfo->tmpdir);
345    while ((p = strchr(callstr, '\r'))) {
346        *p = ' ';
347    }
348    while ((p = strchr(callstr, '\n'))) {
349        *p = ' ';
350    }                           /* single '=' is intentional */
351
352
353    len = (int) strlen(program);
354    for (i = 0; i < 35 - len / 2; i++) {
355        tmp[i] = '-';
356    }
357    tmp[i] = '\0';
358    strcat(tmp, " ");
359    strcat(tmp, program);
360    strcat(tmp, " ");
361    for (i = 0; i < 35 - len / 2; i++) {
362        strcat(tmp, "-");
363    }
364    strcpy(initial_label, tmp);
365    res = system(callstr);
366    if (((res == 0) && log_if_success) || ((res != 0) && log_if_failure)) {
367        log_msg(0, "running: %s", callstr);
368        log_msg(0,
369                "--------------------------------start of output-----------------------------");
370    }
371    sprintf(callstr, "cat %s/mondo-run-prog-thing.err >> %s/mondo-run-prog-thing.tmp 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
372    if (log_if_failure && system(callstr)) {
373        log_OS_error("Command failed");
374    }
375    sprintf(tmp, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
376    unlink(tmp);
377    sprintf(tmp, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
378    fin = fopen(tmp, "r");
379    if (fin) {
380        for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
381             fgets(incoming, MAX_STR_LEN, fin)) {
382            /* patch by Heiko Schlittermann */
383            p = incoming;
384            while (p && *p) {
385                if ((p = strchr(p, '%'))) {
386                    memmove(p, p + 1, strlen(p) + 1);
387                    p += 2;
388                }
389            }
390            /* end of patch */
391            strip_spaces(incoming);
392            if ((res == 0 && log_if_success)
393                || (res != 0 && log_if_failure)) {
394                log_msg(0, incoming);
395            }
396        }
397        paranoid_fclose(fin);
398    }
399    unlink(tmp);
400    if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
401        log_msg(0,
402                "--------------------------------end of output------------------------------");
403        if (res) {
404            log_msg(0, "...ran with res=%d", res);
405        } else {
406            log_msg(0, "...ran just fine. :-)");
407        }
408    }
409//  else
410//    { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
411    return (res);
412}
413
414
415
416/**
417 * Run a program and log its output to the screen.
418 * @param basic_call The program to run.
419 * @param what_i_am_doing The title of the evalcall form.
420 * @return The return value of the command (varies, but 0 almost always means success).
421 * @see run_program_and_log_output
422 * @see log_to_screen
423 */
424int run_program_and_log_to_screen(char *basic_call, char *what_i_am_doing)
425{
426    /*@ int ******************************************************** */
427    int retval = 0;
428    int res = 0;
429    int i;
430
431    /*@ pointers **************************************************** */
432    FILE *fin;
433
434    /*@ buffers **************************************************** */
435    char tmp[MAX_STR_LEN * 2];
436    char command[MAX_STR_LEN * 2];
437    char lockfile[MAX_STR_LEN];
438
439    /*@ end vars *************************************************** */
440
441    assert_string_is_neither_NULL_nor_zerolength(basic_call);
442
443    sprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
444
445    sprintf(command,
446            "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
447            lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
448    open_evalcall_form(what_i_am_doing);
449    sprintf(tmp, "Executing %s", basic_call);
450    log_msg(2, tmp);
451    if (!(fin = popen(command, "r"))) {
452        log_OS_error("Unable to popen-in command");
453        sprintf(tmp, "Failed utterly to call '%s'", command);
454        log_to_screen(tmp);
455        return (1);
456    }
457    if (!does_file_exist(lockfile)) {
458        log_to_screen("Waiting for external binary to start");
459        for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
460            log_msg(3, "Waiting for lockfile %s to exist", lockfile);
461        }
462    }
463#ifdef _XWIN
464    /* This only can update when newline goes into the file,
465       but it's *much* prettier/faster on Qt. */
466    while (does_file_exist(lockfile)) {
467        while (!feof(fin)) {
468            if (!fgets(tmp, 512, fin))
469                break;
470            log_to_screen(tmp);
471        }
472        usleep(500000);
473    }
474#else
475    /* This works on Newt, and it gives quicker updates. */
476    for (; does_file_exist(lockfile); sleep(1)) {
477        log_file_end_to_screen(MONDO_LOGFILE, "");
478        update_evalcall_form(1);
479    }
480#endif
481    /* Evaluate the status returned by pclose to get the exit code of the called program. */
482    errno = 0;
483    res = pclose(fin);
484    /* Log actual pclose errors. */
485    if (errno) log_msg(5, "pclose err: %d", errno);
486    /* Check if we have a valid status. If we do, extract the called program's exit code. */
487    /* If we don't, highlight this fact by returning -1. */
488    if (WIFEXITED(res)) {
489        retval = WEXITSTATUS(res);
490    } else {
491        retval = -1;
492    }
493    close_evalcall_form();
494    unlink(lockfile);
495    return (retval);
496}
497
498
499
500
501/**
502 * Apparently unused. @bug This has a purpose, but what?
503 */
504#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
505#define PIMP_END_SZ "ENDENDEND0xBBC10xBBC2T231hc81h42vws89ff3kff9a82gv34r7fghbka"
506
507
508
509
510int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived, char direction)
511{
512// if dir=='w' then copy from orig to archived
513// if dir=='r' then copy from archived to orig
514    char *tmp;
515    char *buf;
516    char filestr[MAX_STR_LEN];
517    long int bytes_to_be_read, bytes_read_in, bytes_written_out =
518        0, bufcap, subsliceno = 0;
519    int retval = 0;
520    FILE *fin;
521    FILE *fout;
522    FILE *ftmp;
523
524    log_msg(5, "Opening.");
525    malloc_string(tmp);
526    tmp[0] = '\0';
527    bufcap = 256L * 1024L;
528    if (!(buf = malloc(bufcap))) {
529        fatal_error("Failed to malloc() buf");
530    }
531
532    if (direction == 'w') {
533        fin = f_orig;
534        fout = f_archived;
535        sprintf(tmp, "%-64s", PIMP_START_SZ);
536        if (fwrite(tmp, 1, 64, fout) != 64) {
537            fatal_error("Can't write the introductory block");
538        }
539        while (1) {
540            bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
541            if (bytes_read_in == 0) {
542                break;
543            }
544            sprintf(tmp, "%-64ld", bytes_read_in);
545            if (fwrite(tmp, 1, 64, fout) != 64) {
546                fatal_error("Cannot write introductory block");
547            }
548            log_msg(7,
549                    "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
550                    subsliceno, bytes_read_in, bytes_to_be_read);
551            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
552            sprintf(tmp, "%-64ld", subsliceno);
553            if (fwrite(tmp, 1, 64, fout) != 64) {
554                fatal_error("Cannot write post-thingy block");
555            }
556            log_msg(7, "Subslice #%d written OK", subsliceno);
557            subsliceno++;
558        }
559        sprintf(tmp, "%-64ld", 0L);
560        if (fwrite(tmp, 1, 64L, fout) != 64L) {
561            fatal_error("Cannot write final introductory block");
562        }
563    } else {
564        fin = f_archived;
565        fout = f_orig;
566        if (fread(tmp, 1, 64L, fin) != 64L) {
567            fatal_error("Cannot read the introductory block");
568        }
569        log_msg(5, "tmp is %s", tmp);
570        if (!strstr(tmp, PIMP_START_SZ)) {
571            fatal_error("Can't find intro blk");
572        }
573        if (fread(tmp, 1, 64L, fin) != 64L) {
574            fatal_error("Cannot read introductory blk");
575        }
576        bytes_to_be_read = atol(tmp);
577        while (bytes_to_be_read > 0) {
578            log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
579                    bytes_to_be_read);
580            bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
581            if (bytes_read_in != bytes_to_be_read) {
582                fatal_error
583                    ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
584            }
585            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
586            if (fread(tmp, 1, 64, fin) != 64) {
587                fatal_error("Cannot read post-thingy block");
588            }
589            if (atol(tmp) != subsliceno) {
590                log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
591                        subsliceno, atol(tmp), tmp);
592            }
593            log_msg(7, "Subslice #%ld read OK", subsliceno);
594            subsliceno++;
595            if (fread(tmp, 1, 64, fin) != 64) {
596                fatal_error("Cannot read introductory block");
597            }
598            bytes_to_be_read = atol(tmp);
599        }
600    }
601
602//  log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
603
604    if (direction == 'w') {
605        sprintf(tmp, "%-64s", PIMP_END_SZ);
606        if (fwrite(tmp, 1, 64, fout) != 64) {
607            fatal_error("Can't write the final block");
608        }
609    } else {
610        log_msg(1, "tmpA is %s", tmp);
611        if (!strstr(tmp, PIMP_END_SZ)) {
612            if (fread(tmp, 1, 64, fin) != 64) {
613                fatal_error("Can't read the final block");
614            }
615            log_msg(5, "tmpB is %s", tmp);
616            if (!strstr(tmp, PIMP_END_SZ)) {
617                sprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
618                ftmp = fopen(filestr, "w");
619                bytes_read_in = fread(tmp, 1, 64, fin);
620                log_msg(1, "bytes_read_in = %ld", bytes_read_in);
621//      if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
622                fwrite(tmp, 1, bytes_read_in, ftmp);
623                sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
624//    log_msg(0, tmp);
625                fread(tmp, 1, 512, fin);
626                log_msg(0, "tmp = '%s'", tmp);
627                fwrite(tmp, 1, 512, ftmp);
628                fclose(ftmp);
629                fatal_error("Missing terminating block");
630            }
631        }
632    }
633
634    paranoid_free(buf);
635    paranoid_free(tmp);
636    log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
637    return (retval);
638}
639
640
641
642
643/**
644 * Feed @p input_device through ntfsclone to @p output_fname.
645 * @param input_device The device to image.
646 * @param output_fname The file to write.
647 * @return 0 for success, nonzero for failure.
648 */
649int feed_into_ntfsprog(char *input_device, char *output_fname)
650{
651// BACKUP
652    int res = -1;
653    char*command;
654
655    if (!does_file_exist(input_device)) {
656        fatal_error("input device does not exist");
657    }
658    if ( !find_home_of_exe("ntfsclone")) {
659        fatal_error("ntfsclone not found");
660    }
661    malloc_string(command);
662    sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
663    res = run_program_and_log_output(command, 5);
664    paranoid_free(command);
665    unlink(output_fname);
666    return (res);
667}
668
669
670
671
672
673int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
674{
675
676    /*@ int *************************************************************** */
677    int res = 0;
678    int percentage = 0;
679    int maxpc = 0;
680    int pcno = 0;
681    int last_pcno = 0;
682
683    /*@ buffers *********************************************************** */
684    char *command;
685    char *tempfile;
686    char *title;
687    /*@ pointers ********************************************************** */
688    FILE *pin;
689
690    malloc_string(title);
691    malloc_string(command);
692    malloc_string(tempfile);
693    assert_string_is_neither_NULL_nor_zerolength(cmd);
694    assert_string_is_neither_NULL_nor_zerolength(title);
695
696    strcpy(title, tt);
697    sprintf(tempfile, "%s/mondo.binperc", bkpinfo->tmpdir);
698    sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
699            tempfile);
700    log_msg(3, command);
701    open_evalcall_form(title);
702    if (!(pin = popen(command, "r"))) {
703        log_OS_error("fmt err");
704        return (1);
705    }
706    maxpc = 100;
707// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
708    for (sleep(1); does_file_exist(tempfile); sleep(1)) {
709        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
710        if (pcno < 0 || pcno > 100) {
711            log_msg(5, "Weird pc#");
712            continue;
713        }
714        percentage = pcno * 100 / maxpc;
715        if (pcno <= 5 && last_pcno > 40) {
716            close_evalcall_form();
717            strcpy(title, "Verifying...");
718            open_evalcall_form(title);
719        }
720        last_pcno = pcno;
721        update_evalcall_form(percentage);
722    }
723// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
724    close_evalcall_form();
725    if (pclose(pin)) {
726        res++;
727        log_OS_error("Unable to pclose");
728    }
729    unlink(tempfile);
730    paranoid_free(command);
731    paranoid_free(tempfile);
732    paranoid_free(title);
733    return (res);
734}
735
736
737
738
739void *run_prog_in_bkgd_then_exit(void *info)
740{
741    char *sz_command;
742    static int res = 4444;
743
744    res = 999;
745    sz_command = (char *) info;
746    log_msg(4, "sz_command = '%s'", sz_command);
747    res = system(sz_command);
748    if (res > 256 && res != 4444) {
749        res = res / 256;
750    }
751    log_msg(4, "child res = %d", res);
752    sz_command[0] = '\0';
753    pthread_exit((void *) (&res));
754}
755
756
757
758
759int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
760{
761
762    /*@ int *************************************************************** */
763    int res = 0;
764    int percentage = 0;
765    int maxpc = 100;
766    int pcno = 0;
767    int last_pcno = 0;
768    int counter = 0;
769
770    /*@ buffers *********************************************************** */
771    char *command;
772    char *title;
773    /*@ pointers ********************************************************** */
774    static int chldres = 0;
775    int *pchild_result;
776    pthread_t childthread;
777
778    pchild_result = &chldres;
779    assert_string_is_neither_NULL_nor_zerolength(cmd);
780    assert_string_is_neither_NULL_nor_zerolength(tt);
781    *pchild_result = 999;
782
783    malloc_string(title);
784    malloc_string(command);
785    strcpy(title, tt);
786    sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
787    log_msg(3, "command = '%s'", command);
788    if ((res =
789         pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
790                        (void *) command))) {
791        fatal_error("Unable to create an archival thread");
792    }
793
794    log_msg(8, "Parent running");
795    open_evalcall_form(title);
796    for (sleep(1); command[0] != '\0'; sleep(1)) {
797        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
798        if (pcno <= 0 || pcno > 100) {
799            log_msg(8, "Weird pc#");
800            continue;
801        }
802        percentage = pcno * 100 / maxpc;
803        if (pcno <= 5 && last_pcno >= 40) {
804            close_evalcall_form();
805            strcpy(title, "Verifying...");
806            open_evalcall_form(title);
807        }
808        if (counter++ >= 5) {
809            counter = 0;
810            log_file_end_to_screen(MONDO_LOGFILE, "");
811        }
812        last_pcno = pcno;
813        update_evalcall_form(percentage);
814    }
815    log_file_end_to_screen(MONDO_LOGFILE, "");
816    close_evalcall_form();
817    pthread_join(childthread, (void *) (&pchild_result));
818    if (pchild_result) {
819        res = *pchild_result;
820    } else {
821        res = 666;
822    }
823    log_msg(3, "Parent res = %d", res);
824    paranoid_free(command);
825    paranoid_free(title);
826    return (res);
827}
828
829
830
831
832/**
833 * Feed @p input_fifo through ntfsclone (restore) to @p output_device.
834 * @param input_fifo The ntfsclone file to read.
835 * @param output_device Where to put the output.
836 * @return The return value of ntfsclone (0 for success).
837 */
838int feed_outfrom_ntfsprog(char *output_device, char *input_fifo)
839{
840// RESTORE
841    int res = -1;
842    char *command;
843
844    if ( !find_home_of_exe("ntfsclone")) {
845        fatal_error("ntfsclone not found");
846    }
847    malloc_string(command);
848    sprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
849    res = run_program_and_log_output(command, 5);
850    paranoid_free(command);
851    return (res);
852}
Note: See TracBrowser for help on using the repository browser.