Ignore:
Timestamp:
Sep 25, 2013, 8:55:45 AM (11 years ago)
Author:
Bruno Cornec
Message:
  • Lots of memory management backports from 3.1 to 3.2 - still not finished, nor working ATM. the common subdir was done during travel, so this is essentially a backup !
File:
1 edited

Legend:

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

    r3185 r3191  
    88#include "my-stuff.h"
    99#include "mr_mem.h"
     10#include "mr_str.h"
    1011#include "mondostructures.h"
    1112#include "libmondo-fork.h"
     
    3839    /*@ buffers ***************************************************** */
    3940    static char result[MAX_STR_LEN];
    40     char *tmp;
    41     char *p;
     41    char *tmp = NULL;
    4242
    4343    /*@ pointers **************************************************** */
    44     FILE *fin;
     44    FILE *fin = NULL;
     45    int res = 0;
    4546
    4647    /*@ initialize data ********************************************* */
    47     malloc_string(tmp);
    4848    result[0] = '\0';
    49     tmp[0] = '\0';
    5049
    5150    /*@******************************************************************** */
     
    5352    assert_string_is_neither_NULL_nor_zerolength(call);
    5453    if ((fin = popen(call, "r"))) {
    55         for (p = fgets(tmp, MAX_STR_LEN, fin); !feof(fin) && (p != NULL);
    56              p = fgets(tmp, MAX_STR_LEN, fin)) {
     54        for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
    5755            if (strlen(tmp) > 1) {
    5856                strcpy(result, tmp);
    5957            }
     58            mr_free(tmp);
    6059        }
    6160        paranoid_pclose(fin);
    6261    } else {
    63         log_OS_error("Unable to popen call");
     62        log_OS_error("Unable to open resulting file");
    6463    }
    6564    strip_spaces(result);
    66     paranoid_free(tmp);
    67     return (result);
    68 }
    69 
    70 
    71 
    72 
     65    mr_free(tmp);
     66    return(result);
     67}
    7368
    7469
     
    8681 * @param isofile Replaces @c _ISO_ in @p basic_call. Should probably be the ISO image to create (-o parameter to mkisofs).
    8782 * @param cd_no Replaces @c _CD#_ in @p basic_call. Should probably be the CD number.
    88  * @param logstub Unused.
    8983 * @param what_i_am_doing The action taking place (e.g. "Making ISO #1"). Used as the title of the progress dialog.
    9084 * @return Exit code of @c mkisofs (0 is success, anything else indicates failure).
    91  * @bug @p logstub is unused.
    9285 */
    9386int
     
    10194
    10295    /*@ buffers      *** */
    103     char *midway_call, *ultimate_call, *tmp, *command, *incoming,
    104         *old_stderr, *cd_number_str;
    105     char *p;
     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;
    106103    char *tmp1 = NULL;
     104    char *tmp2 = NULL;
    107105
    108106/*@***********   End Variables ***************************************/
     
    110108    log_msg(3, "Starting");
    111109    assert(bkpinfo != NULL);
    112     // BERLIOS: doesn't work even if the string is correct !
    113     //assert_string_is_neither_NULL_nor_zerolength(basic_call);
    114110    assert_string_is_neither_NULL_nor_zerolength(isofile);
    115     assert_string_is_neither_NULL_nor_zerolength(logstub);
     111
    116112    if (!(midway_call = malloc(1200))) {
    117113        fatal_error("Cannot malloc midway_call");
     
    123119        fatal_error("Cannot malloc tmp");
    124120    }
    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';
    134121
    135122    if ((bkpinfo->netfs_user) && (strstr(bkpinfo->netfs_proto,"nfs"))) {
     
    139126    }
    140127
    141     sprintf(cd_number_str, "%d", cd_no);
     128    mr_asprintf(cd_number_str, "%d", cd_no);
    142129    resolve_naff_tokens(midway_call, tmp1, isofile, "_ISO_");
     130    log_msg(4, "basic call = '%s'", tmp1);
     131    mr_free(tmp1);
     132
    143133    resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
     134    log_msg(4, "midway_call = '%s'", midway_call);
     135    mr_free(cd_number_str);
     136
    144137    resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
    145     log_msg(4, "basic call = '%s'", tmp1);
    146     log_msg(4, "midway_call = '%s'", midway_call);
    147     log_msg(4, "tmp = '%s'", tmp);
    148138    log_msg(4, "ultimate call = '%s'", ultimate_call);
    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);
     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);
    156143    if (bkpinfo->manual_cd_tray) {
    157         p = strstr(tmp, "2>>");
     144        mr_asprintf(tmp2, "%s", command);
     145        p = strstr(tmp2, "2>>");
    158146        if (p) {
    159147            sprintf(p, "   ");
     
    161149                p++;
    162150            }
    163             for (; *p != ' '; p++) {
     151            for (; (*p != ' ') && (*p != '\0'); p++) {
    164152                *p = ' ';
    165153            }
    166154        }
    167         strcpy(command, tmp);
    168 #ifndef _XWIN
     155        mr_free(command);
     156        command = tmp2;
    169157        if (!g_text_mode) {
    170158            newtSuspend();
    171159        }
    172 #endif
    173160        log_msg(1, "command = '%s'", command);
    174161        retval += system(command);
     
    177164        }
    178165        if (retval) {
    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");
     166            popup_and_OK("mkisofs and/or cdrecord returned an error. CD was not created");
    183167        }
    184168    }
     
    186170    else {
    187171        log_msg(3, "command = '%s'", 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);
     172        retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
     173    }
     174    mr_free(command);
    195175    paranoid_free(midway_call);
    196176    paranoid_free(ultimate_call);
    197177    paranoid_free(tmp);
    198     paranoid_free(command);
    199     paranoid_free(incoming);
    200     paranoid_free(old_stderr);
    201     paranoid_free(cd_number_str);
    202178    return (retval);
    203179}
     
    232208    log_msg(1, "command = '%s'", command);
    233209    if (!g_text_mode) {
    234         retval = run_external_binary_with_percentage_indicator_NEW
    235             (what_i_am_doing, command);
     210        retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
    236211    } else {
    237212        retval += system(command);
     
    277252        return (1);
    278253    }
    279 //  if (debug_level == TRUE) { debug_level=5; }
    280 
    281     //  assert_string_is_neither_NULL_nor_zerolength(program);
    282254
    283255    if (debug_level <= g_loglevel) {
     
    322294                }
    323295            }
    324             /* end of patch */
    325296            strip_spaces(incoming);
    326297            if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
     
    342313        }
    343314    }
    344 //  else
    345 //    { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
    346315    return (res);
    347316}
     
    368337
    369338    /*@ buffers **************************************************** */
    370     char *tmp = NULL;
    371339    char *command = NULL;
    372     char lockfile[MAX_STR_LEN];
     340    char *lockfile = NULL;
    373341
    374342    /*@ end vars *************************************************** */
     
    376344    assert_string_is_neither_NULL_nor_zerolength(basic_call);
    377345
    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);
     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);
    383349    open_evalcall_form(what_i_am_doing);
    384     mr_asprintf(tmp, "Executing %s", basic_call);
    385     log_msg(2, tmp);
    386     mr_free(tmp);
     350    log_msg(2, "Executing %s", basic_call);
    387351
    388352    if (!(fin = popen(command, "r"))) {
    389353        log_OS_error("Unable to popen-in command");
    390         mr_asprintf(tmp, "Failed utterly to call '%s'", command);
    391         log_to_screen(tmp);
    392         mr_free(tmp);
     354        log_to_screen("Failed utterly to call '%s'", command);
    393355        mr_free(command);
     356        mr_free(lockfile);
    394357        return (1);
    395358    }
     359
    396360    if (!does_file_exist(lockfile)) {
    397361        log_to_screen("Waiting for '%s' to start",command);
     
    401365    }
    402366    mr_free(command);
    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. */
     367
    417368    for (; does_file_exist(lockfile); sleep(1)) {
    418369        log_file_end_to_screen(MONDO_LOGFILE, "");
    419370        update_evalcall_form(1);
    420371    }
    421 #endif
     372
    422373    /* Evaluate the status returned by pclose to get the exit code of the called program. */
    423374    errno = 0;
     
    434385    close_evalcall_form();
    435386    unlink(lockfile);
     387    mr_free(lockfile);
     388
    436389    return (retval);
    437390}
     
    453406// if dir=='w' then copy from orig to archived
    454407// if dir=='r' then copy from archived to orig
    455     char *tmp;
    456     char *buf;
    457     char filestr[MAX_STR_LEN];
     408    char *tmp = NULL;
     409    char *tmp1 = NULL;
     410    char *buf = NULL;
     411    char *filestr = NULL;
    458412    long int bytes_to_be_read, bytes_read_in, bytes_written_out =
    459413        0, bufcap, subsliceno = 0;
     
    465419
    466420    log_msg(5, "Opening.");
    467     if (!(tmp = malloc(tmpcap))) {
    468         fatal_error("Failed to malloc() tmp");
    469     }
    470     tmp[0] = '\0';
     421
    471422    bufcap = 256L * 1024L;
    472423    if (!(buf = malloc(bufcap))) {
     
    477428        fin = f_orig;
    478429        fout = f_archived;
    479         sprintf(tmp, "%-64s", PIMP_START_SZ);
     430        mr_asprintf(tmp, "%-64s", PIMP_START_SZ);
    480431        if (fwrite(tmp, 1, 64, fout) != 64) {
     432            mr_free(tmp);
    481433            fatal_error("Can't write the introductory block");
    482434        }
     435        mr_free(tmp);
     436
    483437        while (1) {
    484438            bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
     
    486440                break;
    487441            }
    488             sprintf(tmp, "%-64ld", bytes_read_in);
     442            mr_asprintf(tmp, "%-64ld", bytes_read_in);
    489443            if (fwrite(tmp, 1, 64, fout) != 64) {
     444                mr_free(tmp);
    490445                fatal_error("Cannot write introductory block");
    491446            }
     447            mr_free(tmp);
     448
    492449            log_msg(7,
    493450                    "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
    494451                    subsliceno, bytes_read_in, bytes_to_be_read);
    495452            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
    496             sprintf(tmp, "%-64ld", subsliceno);
     453            mr_asprintf(tmp, "%-64ld", subsliceno);
    497454            if (fwrite(tmp, 1, 64, fout) != 64) {
     455                mr_free(tmp);
    498456                fatal_error("Cannot write post-thingy block");
    499457            }
     458            mr_free(tmp);
    500459            log_msg(7, "Subslice #%d written OK", subsliceno);
    501460            subsliceno++;
    502461        }
    503         sprintf(tmp, "%-64ld", 0L);
     462        mr_asprintf(tmp, "%-64ld", 0L);
    504463        if (fwrite(tmp, 1, 64L, fout) != 64L) {
     464            mr_free(tmp);
    505465            fatal_error("Cannot write final introductory block");
    506466        }
     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);
    507475    } else {
    508476        fin = f_archived;
    509477        fout = f_orig;
    510         if (fread(tmp, 1, 64L, fin) != 64L) {
     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);
    511483            fatal_error("Cannot read the introductory block");
    512484        }
    513         log_msg(5, "tmp is %s", tmp);
    514         if (!strstr(tmp, PIMP_START_SZ)) {
     485        log_msg(5, "tmp1 is %s", tmp1);
     486        if (!strstr(tmp1, PIMP_START_SZ)) {
     487            mr_free(tmp1);
    515488            fatal_error("Can't find intro blk");
    516489        }
    517         if (fread(tmp, 1, 64L, fin) != 64L) {
     490        if (fread(tmp1, 1, 64L, fin) != 64L) {
     491            mr_free(tmp1);
    518492            fatal_error("Cannot read introductory blk");
    519493        }
    520         bytes_to_be_read = atol(tmp);
     494        bytes_to_be_read = atol(tmp1);
    521495        while (bytes_to_be_read > 0) {
    522             log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
    523                     bytes_to_be_read);
     496            log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, bytes_to_be_read);
    524497            bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
    525498            if (bytes_read_in != bytes_to_be_read) {
    526                 fatal_error
    527                     ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
     499                mr_free(tmp1);
     500                fatal_error("Danger, WIll Robinson. Failed to read whole subvol from archives.");
    528501            }
    529502            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
    530             if (fread(tmp, 1, 64, fin) != 64) {
     503            if (fread(tmp1, 1, 64, fin) != 64) {
     504                mr_free(tmp1);
    531505                fatal_error("Cannot read post-thingy block");
    532506            }
    533             if (atol(tmp) != subsliceno) {
    534                 log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
    535                         subsliceno, atol(tmp), tmp);
     507            if (atol(tmp1) != subsliceno) {
     508                log_msg(1, "Wanted subslice %ld but got %ld ('%s')", subsliceno, atol(tmp1), tmp1);
    536509            }
    537510            log_msg(7, "Subslice #%ld read OK", subsliceno);
    538511            subsliceno++;
    539             if (fread(tmp, 1, 64, fin) != 64) {
     512            if (fread(tmp1, 1, 64, fin) != 64) {
     513                mr_free(tmp1);
    540514                fatal_error("Cannot read introductory block");
    541515            }
    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);
     516            bytes_to_be_read = atol(tmp1);
     517        }
     518    }
    547519
    548520    if (direction == 'w') {
    549         sprintf(tmp, "%-64s", PIMP_END_SZ);
     521        mr_asprintf(tmp, "%-64s", PIMP_END_SZ);
    550522        if (fwrite(tmp, 1, 64, fout) != 64) {
     523            mr_free(tmp);
    551524            fatal_error("Can't write the final block");
    552525        }
     526        mr_free(tmp);
    553527    } else {
    554         log_msg(1, "tmpA is %s", tmp);
    555         if (!strstr(tmp, PIMP_END_SZ)) {
    556             if (fread(tmp, 1, 64, fin) != 64) {
     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);
    557532                fatal_error("Can't read the final block");
    558533            }
    559             log_msg(5, "tmpB is %s", tmp);
    560             if (!strstr(tmp, PIMP_END_SZ)) {
    561                 sprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
     534            log_msg(5, "tmpB is %s", tmp1);
     535            if (!strstr(tmp1, PIMP_END_SZ)) {
     536                mr_asprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
    562537                ftmp = fopen(filestr, "w");
    563                 bytes_read_in = fread(tmp, 1, 64, fin);
     538                mr_free(filestr);
     539
     540                bytes_read_in = fread(tmp1, 1, 64, fin);
    564541                log_msg(1, "bytes_read_in = %ld", bytes_read_in);
    565 //      if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
     542
    566543                if (fwrite(tmp, 1, bytes_read_in, ftmp)) {
    567544                    fatal_error("Can't fwrite here");
    568545                }
    569                 sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
    570 //    log_msg(0, tmp);
     546
     547                mr_asprintf(tmp, "I am here - %lld", (long long)ftello(fin));
    571548                if (fread(tmp, 1, tmpcap, fin)) {
     549                    mr_free(tmp);
    572550                    fatal_error("Can't fread here");
    573551                }
    574552                log_msg(0, "tmp = '%s'", tmp);
    575553                if (fwrite(tmp, 1, tmpcap, ftmp)) {
     554                    mr_free(tmp);
    576555                    fatal_error("Can't fwrite there");
    577556                }
    578557                fclose(ftmp);
     558                mr_free(tmp);
    579559                fatal_error("Missing terminating block");
    580560            }
    581561        }
    582562    }
     563    mr_free(tmp1);
    583564
    584565    paranoid_free(buf);
    585     paranoid_free(tmp);
    586566    log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
    587567    return (retval);
     
    601581// BACKUP
    602582    int res = -1;
    603     char*command;
     583    char *command = NULL;
    604584
    605585    if (!does_file_exist(input_device)) {
     
    609589        fatal_error("ntfsclone not found");
    610590    }
    611     malloc_string(command);
    612     sprintf(command, "ntfsclone --rescue --force --save-image --overwrite %s %s", output_fname, input_device);
     591    mr_asprintf(command, "ntfsclone --rescue --force --save-image --overwrite %s %s", output_fname, input_device);
    613592    res = run_program_and_log_output(command, 5);
    614     paranoid_free(command);
     593    mr_free(command);
     594
    615595    unlink(output_fname);
    616596    return (res);
     
    638618
    639619
    640 int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd)
    641 {
     620int run_external_binary_with_percentage_indicator_NEW(char *tt, char *cmd) {
    642621
    643622    /*@ int *************************************************************** */
     
    650629
    651630    /*@ buffers *********************************************************** */
    652     char *command;
    653     char *title;
     631    char *command = NULL;
    654632    /*@ pointers ********************************************************** */
    655633    static int chldres = 0;
     
    662640    *pchild_result = 999;
    663641
    664     malloc_string(title);
    665     malloc_string(command);
    666     strcpy(title, tt);
    667     sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
     642    mr_asprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
    668643    log_msg(3, "command = '%s'", command);
    669     if ((res =
    670          pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
    671                         (void *) command))) {
     644    if ((res = pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, (void *) command))) {
    672645        fatal_error("Unable to create an archival thread");
    673646    }
    674647
    675648    log_msg(8, "Parent running");
    676     open_evalcall_form(title);
     649    open_evalcall_form(tt);
     650
    677651    for (sleep(1); command[0] != '\0'; sleep(1)) {
    678652        pcno = grab_percentage_from_last_line_of_file(MONDO_LOGFILE);
    679         if (pcno <= 0 || pcno > 100) {
    680             log_msg(8, "Weird pc#");
     653        if (pcno < 0 || pcno > 100) {
     654            log_msg(8, "Weird pc# %d", pcno);
    681655            continue;
    682656        }
     
    684658        if (pcno <= 5 && last_pcno >= 40) {
    685659            close_evalcall_form();
    686             strcpy(title, "Verifying...");
    687             open_evalcall_form(title);
     660            open_evalcall_form("Verifying...");
    688661        }
    689662        if (counter++ >= 5) {
     
    694667        update_evalcall_form(percentage);
    695668    }
     669    mr_free(command);
     670
    696671    log_file_end_to_screen(MONDO_LOGFILE, "");
    697672    close_evalcall_form();
     
    703678    }
    704679    log_msg(3, "Parent res = %d", res);
    705     paranoid_free(command);
    706     paranoid_free(title);
    707680    return (res);
    708681}
    709 
    710 
    711682
    712683
     
    721692// RESTORE
    722693    int res = -1;
    723     char *command;
     694    char *command = NULL;
    724695
    725696    if ( !find_home_of_exe("ntfsclone")) {
    726697        fatal_error("ntfsclone not found");
    727698    }
    728     malloc_string(command);
    729     sprintf(command, "ntfsclone --rescue --force --restore-image --overwrite %s %s", output_device, input_fifo);
     699    mr_asprintf(command, "ntfsclone --rescue --force --restore-image --overwrite %s %s", output_device, input_fifo);
    730700    res = run_program_and_log_output(command, 5);
    731     paranoid_free(command);
     701    mr_free(command);
    732702    return (res);
    733703}
Note: See TracChangeset for help on using the changeset viewer.