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

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

Try to add USB support for mondoarchive with new functions

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