Ignore:
Timestamp:
Sep 25, 2013, 9:03:25 AM (11 years ago)
Author:
Bruno Cornec
Message:
  • revert r3188 & r3189 as I started to work on branch 3.0 before deciding it was much better to do it in 3.2. This will allow some small maintenance work on 3.0 if needed, while pushing all the rest to 3.2 and providing early access to it.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.0/mondo/src/common/libmondo-fork.c

    r3188 r3192  
    88#include "my-stuff.h"
    99#include "mr_mem.h"
    10 #include "mr_str.h"
    1110#include "mondostructures.h"
    1211#include "libmondo-fork.h"
     
    3938    /*@ buffers ***************************************************** */
    4039    static char result[MAX_STR_LEN];
    41     char *tmp = NULL;
     40    char *tmp;
     41    char *p;
    4242
    4343    /*@ pointers **************************************************** */
    44     FILE *fin = NULL;
    45     int res = 0;
     44    FILE *fin;
    4645
    4746    /*@ initialize data ********************************************* */
     47    malloc_string(tmp);
    4848    result[0] = '\0';
     49    tmp[0] = '\0';
    4950
    5051    /*@******************************************************************** */
     
    5253    assert_string_is_neither_NULL_nor_zerolength(call);
    5354    if ((fin = popen(call, "r"))) {
    54         for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
     55        for (p = fgets(tmp, MAX_STR_LEN, fin); !feof(fin) && (p != NULL);
     56             p = fgets(tmp, MAX_STR_LEN, fin)) {
    5557            if (strlen(tmp) > 1) {
    5658                strcpy(result, tmp);
    5759            }
    58             mr_free(tmp);
    5960        }
    6061        paranoid_pclose(fin);
    6162    } else {
    62         log_OS_error("Unable to open resulting file");
     63        log_OS_error("Unable to popen call");
    6364    }
    6465    strip_spaces(result);
    65     mr_free(tmp);
    66     return(result);
    67 }
     66    paranoid_free(tmp);
     67    return (result);
     68}
     69
     70
     71
     72
    6873
    6974
     
    8186 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
    8287 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
     88 * @param logstub Unused.
    8389 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
    8490 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
     91 * @bug @p logstub is unused.
    8592 */
    8693int
     
    94101
    95102    /*@ buffers      *** */
    96     char *midway_call = NULL;
    97     char *ultimate_call = NULL;
    98     char *tmp = NULL;
    99        
    100     char *cd_number_str = NULL;
    101     char *command = NULL;
    102     char *p= NULL;
     103    char *midway_call, *ultimate_call, *tmp, *command, *incoming,
     104        *old_stderr, *cd_number_str;
     105    char *p;
    103106    char *tmp1 = NULL;
    104     char *tmp2 = NULL;
    105107
    106108/*@***********   End Variables ***************************************/
     
    108110    log_msg(3, "Starting");
    109111    assert(bkpinfo != NULL);
     112    // BERLIOS: doesn't work even if the string is correct !
     113    //assert_string_is_neither_NULL_nor_zerolength(basic_call);
    110114    assert_string_is_neither_NULL_nor_zerolength(isofile);
    111 
     115    assert_string_is_neither_NULL_nor_zerolength(logstub);
    112116    if (!(midway_call = malloc(1200))) {
    113117        fatal_error("Cannot malloc midway_call");
     
    119123        fatal_error("Cannot malloc tmp");
    120124    }
     125    if (!(command = malloc(1200))) {
     126        fatal_error("Cannot malloc command");
     127    }
     128    malloc_string(incoming);
     129    malloc_string(old_stderr);
     130    malloc_string(cd_number_str);
     131
     132    incoming[0] = '\0';
     133    old_stderr[0] = '\0';
    121134
    122135    if ((bkpinfo->netfs_user) && (strstr(bkpinfo->netfs_proto,"nfs"))) {
     
    126139    }
    127140
    128     mr_asprintf(cd_number_str, "%d", cd_no);
     141    sprintf(cd_number_str, "%d", cd_no);
    129142    resolve_naff_tokens(midway_call, tmp1, isofile, "_ISO_");
     143    resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
     144    resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
    130145    log_msg(4, "basic call = '%s'", tmp1);
    131     mr_free(tmp1);
    132 
    133     resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
    134146    log_msg(4, "midway_call = '%s'", midway_call);
    135     mr_free(cd_number_str);
    136 
    137     resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
     147    log_msg(4, "tmp = '%s'", tmp);
    138148    log_msg(4, "ultimate call = '%s'", ultimate_call);
    139     mr_asprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
    140 
    141     log_to_screen("Please be patient. Do not be alarmed by on-screen inactivity.");
    142     log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'", what_i_am_doing);
     149    sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
     150
     151    log_to_screen
     152        ("Please be patient. Do not be alarmed by on-screen inactivity.");
     153    log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
     154            what_i_am_doing);
     155    strcpy(tmp, command);
    143156    if (bkpinfo->manual_cd_tray) {
    144         mr_asprintf(tmp2, "%s", command);
    145         p = strstr(tmp2, "2>>");
     157        p = strstr(tmp, "2>>");
    146158        if (p) {
    147159            sprintf(p, "   ");
     
    149161                p++;
    150162            }
    151             for (; (*p != ' ') && (*p != '\0'); p++) {
     163            for (; *p != ' '; p++) {
    152164                *p = ' ';
    153165            }
    154166        }
    155         mr_free(command);
    156         command = tmp2;
     167        strcpy(command, tmp);
     168#ifndef _XWIN
    157169        if (!g_text_mode) {
    158170            newtSuspend();
    159171        }
     172#endif
    160173        log_msg(1, "command = '%s'", command);
    161174        retval += system(command);
     
    164177        }
    165178        if (retval) {
    166             popup_and_OK("mkisofs and/or cdrecord returned an error. CD was not created");
     179            log_msg(2, "Basic call '%s' returned an error.", tmp1);
     180            popup_and_OK("Press ENTER to continue.");
     181            popup_and_OK
     182                ("mkisofs and/or cdrecord returned an error. CD was not created");
    167183        }
    168184    }
     
    170186    else {
    171187        log_msg(3, "command = '%s'", command);
    172         retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
    173     }
    174     mr_free(command);
     188//      yes_this_is_a_goto:
     189        retval =
     190            run_external_binary_with_percentage_indicator_NEW
     191            (what_i_am_doing, command);
     192    }
     193
     194    mr_free(tmp1);
    175195    paranoid_free(midway_call);
    176196    paranoid_free(ultimate_call);
    177197    paranoid_free(tmp);
     198    paranoid_free(command);
     199    paranoid_free(incoming);
     200    paranoid_free(old_stderr);
     201    paranoid_free(cd_number_str);
    178202    return (retval);
    179203}
     
    208232    log_msg(1, "command = '%s'", command);
    209233    if (!g_text_mode) {
    210         retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
     234        retval = run_external_binary_with_percentage_indicator_NEW
     235            (what_i_am_doing, command);
    211236    } else {
    212237        retval += system(command);
     
    252277        return (1);
    253278    }
     279//  if (debug_level == TRUE) { debug_level=5; }
     280
     281    //  assert_string_is_neither_NULL_nor_zerolength(program);
    254282
    255283    if (debug_level <= g_loglevel) {
     
    294322                }
    295323            }
     324            /* end of patch */
    296325            strip_spaces(incoming);
    297326            if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
     
    313342        }
    314343    }
     344//  else
     345//    { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
    315346    return (res);
    316347}
     
    337368
    338369    /*@ buffers **************************************************** */
     370    char *tmp = NULL;
    339371    char *command = NULL;
    340     char *lockfile = NULL;
     372    char lockfile[MAX_STR_LEN];
    341373
    342374    /*@ end vars *************************************************** */
     
    344376    assert_string_is_neither_NULL_nor_zerolength(basic_call);
    345377
    346     mr_asprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
    347 
    348     mr_asprintf(command, "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res", lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
     378    sprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
     379
     380    mr_asprintf(command,
     381            "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
     382            lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
    349383    open_evalcall_form(what_i_am_doing);
    350     log_msg(2, "Executing %s", basic_call);
     384    mr_asprintf(tmp, "Executing %s", basic_call);
     385    log_msg(2, tmp);
     386    mr_free(tmp);
    351387
    352388    if (!(fin = popen(command, "r"))) {
    353389        log_OS_error("Unable to popen-in command");
    354         log_to_screen("Failed utterly to call '%s'", command);
     390        mr_asprintf(tmp, "Failed utterly to call '%s'", command);
     391        log_to_screen(tmp);
     392        mr_free(tmp);
    355393        mr_free(command);
    356         mr_free(lockfile);
    357394        return (1);
    358395    }
    359 
    360396    if (!does_file_exist(lockfile)) {
    361397        log_to_screen("Waiting for '%s' to start",command);
     
    365401    }
    366402    mr_free(command);
    367 
     403#ifdef _XWIN
     404    /* This only can update when newline goes into the file,
     405       but it's *much* prettier/faster on Qt. */
     406    while (does_file_exist(lockfile)) {
     407        while (!feof(fin)) {
     408            /* TODO: Dead and wrong code */
     409            if (!fgets(tmp, 512, fin))
     410                break;
     411            log_to_screen(tmp);
     412        }
     413        usleep(500000);
     414    }
     415#else
     416    /* This works on Newt, and it gives quicker updates. */
    368417    for (; does_file_exist(lockfile); sleep(1)) {
    369418        log_file_end_to_screen(MONDO_LOGFILE, "");
    370419        update_evalcall_form(1);
    371420    }
    372 
     421#endif
    373422    /* Evaluate the status returned by pclose to get the exit code of the called program. */
    374423    errno = 0;
     
    385434    close_evalcall_form();
    386435    unlink(lockfile);
    387     mr_free(lockfile);
    388 
    389436    return (retval);
    390437}
     
    406453// if dir=='w' then copy from orig to archived
    407454// if dir=='r' then copy from archived to orig
    408     char *tmp = NULL;
    409     char *tmp1 = NULL;
    410     char *buf = NULL;
    411     char *filestr = NULL;
     455    char *tmp;
     456    char *buf;
     457    char filestr[MAX_STR_LEN];
    412458    long int bytes_to_be_read, bytes_read_in, bytes_written_out =
    413459        0, bufcap, subsliceno = 0;
     
    419465
    420466    log_msg(5, "Opening.");
    421 
     467    if (!(tmp = malloc(tmpcap))) {
     468        fatal_error("Failed to malloc() tmp");
     469    }
     470    tmp[0] = '\0';
    422471    bufcap = 256L * 1024L;
    423472    if (!(buf = malloc(bufcap))) {
     
    428477        fin = f_orig;
    429478        fout = f_archived;
    430         mr_asprintf(tmp, "%-64s", PIMP_START_SZ);
     479        sprintf(tmp, "%-64s", PIMP_START_SZ);
    431480        if (fwrite(tmp, 1, 64, fout) != 64) {
    432             mr_free(tmp);
    433481            fatal_error("Can't write the introductory block");
    434482        }
    435         mr_free(tmp);
    436 
    437483        while (1) {
    438484            bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
     
    440486                break;
    441487            }
    442             mr_asprintf(tmp, "%-64ld", bytes_read_in);
     488            sprintf(tmp, "%-64ld", bytes_read_in);
    443489            if (fwrite(tmp, 1, 64, fout) != 64) {
    444                 mr_free(tmp);
    445490                fatal_error("Cannot write introductory block");
    446491            }
    447             mr_free(tmp);
    448 
    449492            log_msg(7,
    450493                    "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
    451494                    subsliceno, bytes_read_in, bytes_to_be_read);
    452495            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
    453             mr_asprintf(tmp, "%-64ld", subsliceno);
     496            sprintf(tmp, "%-64ld", subsliceno);
    454497            if (fwrite(tmp, 1, 64, fout) != 64) {
    455                 mr_free(tmp);
    456498                fatal_error("Cannot write post-thingy block");
    457499            }
    458             mr_free(tmp);
    459500            log_msg(7, "Subslice #%d written OK", subsliceno);
    460501            subsliceno++;
    461502        }
    462         mr_asprintf(tmp, "%-64ld", 0L);
     503        sprintf(tmp, "%-64ld", 0L);
    463504        if (fwrite(tmp, 1, 64L, fout) != 64L) {
    464             mr_free(tmp);
    465505            fatal_error("Cannot write final introductory block");
    466506        }
    467         mr_free(tmp);
    468 
    469         mr_asprintf(tmp, "%-64s", PIMP_END_SZ);
    470         if (fwrite(tmp, 1, 64, fout) != 64) {
    471             mr_free(tmp);
    472             fatal_error("Can't write the final block");
    473         }
    474         mr_free(tmp);
    475507    } else {
    476508        fin = f_archived;
    477509        fout = f_orig;
    478         if (!(tmp1 = malloc(tmpcap))) {
    479             fatal_error("Failed to malloc() tmp");
    480         }
    481         if (fread(tmp1, 1, 64L, fin) != 64L) {
    482             mr_free(tmp1);
     510        if (fread(tmp, 1, 64L, fin) != 64L) {
    483511            fatal_error("Cannot read the introductory block");
    484512        }
    485         log_msg(5, "tmp1 is %s", tmp1);
    486         if (!strstr(tmp1, PIMP_START_SZ)) {
    487             mr_free(tmp1);
     513        log_msg(5, "tmp is %s", tmp);
     514        if (!strstr(tmp, PIMP_START_SZ)) {
    488515            fatal_error("Can't find intro blk");
    489516        }
    490         if (fread(tmp1, 1, 64L, fin) != 64L) {
    491             mr_free(tmp1);
     517        if (fread(tmp, 1, 64L, fin) != 64L) {
    492518            fatal_error("Cannot read introductory blk");
    493519        }
    494         bytes_to_be_read = atol(tmp1);
     520        bytes_to_be_read = atol(tmp);
    495521        while (bytes_to_be_read > 0) {
    496             log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, bytes_to_be_read);
     522            log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
     523                    bytes_to_be_read);
    497524            bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
    498525            if (bytes_read_in != bytes_to_be_read) {
    499                 mr_free(tmp1);
    500                 fatal_error("Danger, WIll Robinson. Failed to read whole subvol from archives.");
     526                fatal_error
     527                    ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
    501528            }
    502529            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
    503             if (fread(tmp1, 1, 64, fin) != 64) {
    504                 mr_free(tmp1);
     530            if (fread(tmp, 1, 64, fin) != 64) {
    505531                fatal_error("Cannot read post-thingy block");
    506532            }
    507             if (atol(tmp1) != subsliceno) {
    508                 log_msg(1, "Wanted subslice %ld but got %ld ('%s')", subsliceno, atol(tmp1), tmp1);
     533            if (atol(tmp) != subsliceno) {
     534                log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
     535                        subsliceno, atol(tmp), tmp);
    509536            }
    510537            log_msg(7, "Subslice #%ld read OK", subsliceno);
    511538            subsliceno++;
    512             if (fread(tmp1, 1, 64, fin) != 64) {
    513                 mr_free(tmp1);
     539            if (fread(tmp, 1, 64, fin) != 64) {
    514540                fatal_error("Cannot read introductory block");
    515541            }
    516             bytes_to_be_read = atol(tmp1);
    517         }
    518     }
     542            bytes_to_be_read = atol(tmp);
     543        }
     544    }
     545
     546//  log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
    519547
    520548    if (direction == 'w') {
    521         mr_asprintf(tmp, "%-64s", PIMP_END_SZ);
     549        sprintf(tmp, "%-64s", PIMP_END_SZ);
    522550        if (fwrite(tmp, 1, 64, fout) != 64) {
    523             mr_free(tmp);
    524551            fatal_error("Can't write the final block");
    525552        }
    526         mr_free(tmp);
    527553    } else {
    528         log_msg(1, "tmpA is %s", tmp1);
    529         if (!strstr(tmp1, PIMP_END_SZ)) {
    530             if (fread(tmp1, 1, 64, fin) != 64) {
    531                 mr_free(tmp1);
     554        log_msg(1, "tmpA is %s", tmp);
     555        if (!strstr(tmp, PIMP_END_SZ)) {
     556            if (fread(tmp, 1, 64, fin) != 64) {
    532557                fatal_error("Can't read the final block");
    533558            }
    534             log_msg(5, "tmpB is %s", tmp1);
    535             if (!strstr(tmp1, PIMP_END_SZ)) {
    536                 mr_asprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
     559            log_msg(5, "tmpB is %s", tmp);
     560            if (!strstr(tmp, PIMP_END_SZ)) {
     561                sprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
    537562                ftmp = fopen(filestr, "w");
    538                 mr_free(filestr);
    539 
    540                 bytes_read_in = fread(tmp1, 1, 64, fin);
     563                bytes_read_in = fread(tmp, 1, 64, fin);
    541564                log_msg(1, "bytes_read_in = %ld", bytes_read_in);
    542 
     565//      if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
    543566                if (fwrite(tmp, 1, bytes_read_in, ftmp)) {
    544567                    fatal_error("Can't fwrite here");
    545568                }
    546 
    547                 mr_asprintf(tmp, "I am here - %lld", (long long)ftello(fin));
     569                sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
     570//    log_msg(0, tmp);
    548571                if (fread(tmp, 1, tmpcap, fin)) {
    549                     mr_free(tmp);
    550572                    fatal_error("Can't fread here");
    551573                }
    552574                log_msg(0, "tmp = '%s'", tmp);
    553575                if (fwrite(tmp, 1, tmpcap, ftmp)) {
    554                     mr_free(tmp);
    555576                    fatal_error("Can't fwrite there");
    556577                }
    557578                fclose(ftmp);
    558                 mr_free(tmp);
    559579                fatal_error("Missing terminating block");
    560580            }
    561581        }
    562582    }
    563     mr_free(tmp1);
    564583
    565584    paranoid_free(buf);
     585    paranoid_free(tmp);
    566586    log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
    567587    return (retval);
     
    581601// BACKUP
    582602    int res = -1;
    583     char *command = NULL;
     603    char*command;
    584604
    585605    if (!does_file_exist(input_device)) {
     
    589609        fatal_error("ntfsclone not found");
    590610    }
    591     mr_asprintf(command, "ntfsclone --rescue --force --save-image --overwrite %s %s", output_fname, input_device);
     611    malloc_string(command);
     612    sprintf(command, "ntfsclone --rescue --force --save-image --overwrite %s %s", output_fname, input_device);
    592613    res = run_program_and_log_output(command, 5);
    593     mr_free(command);
    594 
     614    paranoid_free(command);
    595615    unlink(output_fname);
    596616    return (res);
     
    618638
    619639
    620 int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd) {
     640int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
     641{
    621642
    622643    /*@ int *************************************************************** */
     
    629650
    630651    /*@ buffers *********************************************************** */
    631     char *command = NULL;
     652    char *command;
     653    char *title;
    632654    /*@ pointers ********************************************************** */
    633655    static int chldres = 0;
     
    640662    *pchild_result = 999;
    641663
    642     mr_asprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
     664    malloc_string(title);
     665    malloc_string(command);
     666    strcpy(title, tt);
     667    sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
    643668    log_msg(3, "command = '%s'", command);
    644     if ((res = pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, (void *) command))) {
     669    if ((res =
     670         pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
     671                        (void *) command))) {
    645672        fatal_error("Unable to create an archival thread");
    646673    }
    647674
    648675    log_msg(8, "Parent running");
    649     open_evalcall_form(tt);
    650 
     676    open_evalcall_form(title);
    651677    for (sleep(1); command[0] != '\0'; sleep(1)) {
    652678        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
    653         if (pcno < 0 || pcno > 100) {
    654             log_msg(8, "Weird pc# %d", pcno);
     679        if (pcno <= 0 || pcno > 100) {
     680            log_msg(8, "Weird pc#");
    655681            continue;
    656682        }
     
    658684        if (pcno <= 5 && last_pcno >= 40) {
    659685            close_evalcall_form();
    660             open_evalcall_form("Verifying...");
     686            strcpy(title, "Verifying...");
     687            open_evalcall_form(title);
    661688        }
    662689        if (counter++ >= 5) {
     
    667694        update_evalcall_form(percentage);
    668695    }
    669     mr_free(command);
    670 
    671696    log_file_end_to_screen(MONDO_LOGFILE, "");
    672697    close_evalcall_form();
     
    678703    }
    679704    log_msg(3, "Parent res = %d", res);
     705    paranoid_free(command);
     706    paranoid_free(title);
    680707    return (res);
    681708}
     709
     710
    682711
    683712
     
    692721// RESTORE
    693722    int res = -1;
    694     char *command = NULL;
     723    char *command;
    695724
    696725    if ( !find_home_of_exe("ntfsclone")) {
    697726        fatal_error("ntfsclone not found");
    698727    }
    699     mr_asprintf(command, "ntfsclone --rescue --force --restore-image --overwrite %s %s", output_device, input_fifo);
     728    malloc_string(command);
     729    sprintf(command, "ntfsclone --rescue --force --restore-image --overwrite %s %s", output_device, input_fifo);
    700730    res = run_program_and_log_output(command, 5);
    701     mr_free(command);
     731    paranoid_free(command);
    702732    return (res);
    703733}
Note: See TracChangeset for help on using the changeset viewer.