Changeset 688 in MondoRescue for trunk/mondo/mondo/common/libmondo-files.c


Ignore:
Timestamp:
Jul 17, 2006, 3:44:46 PM (18 years ago)
Author:
bcornec
Message:

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/mondo/common/libmondo-files.c

    r687 r688  
    2222//static char cvsid[] = "$Id$";
    2323
    24 extern char err_log_lines[NOOF_ERR_LINES][MAX_STR_LEN];
     24extern char **err_log_lines;
    2525
    2626extern int g_currentY;
     
    270270int figure_out_kernel_path_interactively_if_necessary(char *kernel)
    271271{
    272     char *tmp;
    273     char *command;
    274 
    275     if (!kernel[0]) {
    276         strcpy(kernel,
    277                call_program_and_get_last_line_of_output
     272    char *tmp = NULL;
     273    char *command = NULL;
     274
     275    if (kernel == NULL) {
     276        kernel = call_program_and_get_last_line_of_output
    278277               ("mindi --findkernel 2> /dev/null"));
    279278    }
    280279    // If we didn't get anything back, check whether mindi raised a fatal error
    281     if (!kernel[0]) {
     280    if (kernel == NULL) {
    282281        asprintf(&command, "grep 'Fatal error' /var/log/mindi.log");
    283         asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     282        tmp = call_program_and_get_last_line_of_output(command);
    284283        if (strlen(tmp) > 1) {
    285284            popup_and_OK(tmp);
     
    289288        paranoid_free(tmp);
    290289    }
    291     log_it("Calling Mindi with kernel path of '%s'", kernel);
    292     while (!kernel[0]) {
     290
     291    if (kernel != NULL) {
     292        log_it("Calling Mindi with kernel path of '%s'", kernel);
     293    } else {
     294        log_it("Calling Mindi without kernel, so asking one");
     295    }
     296
     297    while (kernel == NULL) {
    293298        if (!ask_me_yes_or_no
    294299            (_("Kernel not found or invalid. Choose another?"))) {
     
    298303            (_("Kernel path"),
    299304             _("What is the full path and filename of your kernel, please?"),
    300              kernel, MAX_STR_LEN / 4)) {
     305             kernel)) {
    301306            fatal_error
    302307                ("Kernel not found. Please specify with the '-k' flag.");
    303308        }
    304         asprintf(&tmp, "User says kernel is at %s", kernel);
    305         log_it(tmp);
    306         paranoid_free(tmp);
     309        log_it("User says kernel is at %s", kernel);
    307310    }
    308311    return (0);
     
    322325{
    323326    /*@ buffers ********************* */
    324     static char output[MAX_STR_LEN];
    325     char *incoming;
    326     char *command;
     327    char *output = NULL;
     328    char *incoming = NULL;
     329    char *command = NULL;
    327330
    328331    /*@******************************* */
    329332
    330333    assert_string_is_neither_NULL_nor_zerolength(fname);
     334
    331335    asprintf(&command, "which %s 2> /dev/null", fname);
    332     asprintf(&incoming, call_program_and_get_last_line_of_output(command));
    333     paranoid_free(command);
    334 
    335     if (incoming[0] == '\0') {
     336    incoming = call_program_and_get_last_line_of_output(command);
     337    paranoid_free(command);
     338
     339    if (incoming == NULL) {
    336340        if (system("which file > /dev/null 2> /dev/null")) {
    337             paranoid_free(incoming);
    338341            return (NULL);      // forget it :)
    339342        }
     
    343346        paranoid_free(incoming);
    344347
    345         asprintf(&incoming,
    346                call_program_and_get_last_line_of_output(command));
     348        incoming = call_program_and_get_last_line_of_output(command);
    347349        paranoid_free(command);
    348350    }
    349     if (incoming[0] == '\0')    // yes, it is == '\0' twice, not once :)
     351    if (incoming == NULL)   // yes, it is == '\0' twice, not once :)
    350352    {
    351353        asprintf(&command, "dirname %s 2> /dev/null", incoming);
    352354        paranoid_free(incoming);
    353355
    354         asprintf(&incoming,
    355                call_program_and_get_last_line_of_output(command));
     356        incoming = call_program_and_get_last_line_of_output(command);
    356357        paranoid_free(command);
    357358    }
    358     strcpy(output, incoming);
    359     paranoid_free(incoming);
    360 
    361     if (output[0] != '\0' && does_file_exist(output)) {
    362         log_msg(4, "find_home_of_exe () --- Found %s at %s", fname,
    363                 output);
     359    output = incoming;
     360
     361    if (output != NULL && does_file_exist(output)) {
     362        log_msg(4, "find_home_of_exe () --- Found %s at %s", fname, output);
    364363    } else {
    365         output[0] = '\0';
     364        paranoid_free(output);
    366365        log_msg(4, "find_home_of_exe() --- Could not find %s", fname);
    367366    }
    368     if (!output[0]) {
    369         return (NULL);
    370     } else {
    371         return (output);
    372     }
     367    return (output);
    373368}
    374369
     
    421416{
    422417
    423     /*@ buffers ***************************************************** */
    424     char *lastline;
    425     char *command;
    426     /*@ pointers **************************************************** */
    427     char *p;
    428 
    429     /*@ int's ******************************************************* */
     418    char *lastline = NULL;
     419    char *command = NULL;
     420    char *p = NULL;
    430421    int i;
    431422
    432     for (i = NOOF_ERR_LINES - 1;
     423    for (i = g_noof_log_lines - 1;
    433424         i >= 0 && !strstr(err_log_lines[i], "% Done")
    434425         && !strstr(err_log_lines[i], "% done"); i--);
     
    437428                "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'",
    438429                filename, '%');
    439         asprintf(&lastline,
    440                call_program_and_get_last_line_of_output(command));
     430        lastline = call_program_and_get_last_line_of_output(command);
    441431        paranoid_free(command);
    442         if (!lastline[0]) {
    443             paranoid_free(lastline);
     432        if (!lastline) {
    444433            return (0);
    445434        }
     
    477466{
    478467    /*@ buffers ***************************************************** */
    479     static char output[MAX_STR_LEN];
    480     static char *command;
    481     static char *tmp;
     468    char *output = NULL;
     469    char *command = NULL;
     470    char *tmp = NULL;
    482471
    483472    /*@ pointers **************************************************** */
    484473    FILE *fin;
     474    size_t n = 0;
    485475
    486476    /*@ end vars **************************************************** */
     
    491481        log_it(tmp);
    492482        paranoid_free(tmp);
    493 
    494         output[0] = '\0';
     483        asprintf(&output, "");
     484
    495485        return (output);
    496486    }
     
    499489    paranoid_free(command);
    500490
    501     (void) fgets(output, MAX_STR_LEN, fin);
     491    getline(&output, &n, fin);
    502492    paranoid_pclose(fin);
    503493    while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
     
    693683long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
    694684{
    695     char *command;
    696     char *sz_res;
    697     long file_len_K;
     685    char *command = NULL;
     686    char *sz_res = NULL;
     687    long file_len_K = 0L;
    698688
    699689    asprintf(&command,
     
    701691            dev, tmpdir);
    702692    log_it(command);
    703     asprintf(&sz_res, call_program_and_get_last_line_of_output(command));
     693    sz_res = call_program_and_get_last_line_of_output(command);
    704694    file_len_K = atol(sz_res);
    705695    log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
     
    717707long size_of_all_biggiefiles_K(struct s_bkpinfo *bkpinfo)
    718708{
    719     /*@ buffers ***************************************************** */
    720     char *fname;
    721     char *biggielist;
    722     char *comment;
    723     char *tmp;
    724     char *command;
     709    char *fname = NULL;
     710    char *biggielist = NULL;
     711    char *comment = NULL;
     712    char *tmp = NULL;
     713    char *command = NULL;
    725714
    726715    /*@ long ******************************************************** */
    727     long scratchL = 0;
    728     long file_len_K;
     716    long scratchL = 0L;
     717    long file_len_K = 0L;
    729718
    730719    /*@ pointers *************************************************** */
     
    734723    /*@ end vars *************************************************** */
    735724
    736     malloc_string(tmp);
    737     malloc_string(command);
    738725    log_it("Calculating size of all biggiefiles (in total)");
    739726    asprintf(&biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
     
    754741                        fatal_error("ntfsresize not found");
    755742                    }
    756                     sprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
     743                    asprintf(&command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
    757744                    log_it("command = %s", command);
    758                     strcpy (tmp, call_program_and_get_last_line_of_output(command));
     745                    tmp = call_program_and_get_last_line_of_output(command);
     746                    paranoid_free(command);
     747
    759748                    log_it("res of it = %s", tmp);
    760749                    file_len_K = atoll(tmp) / 1024L;
     750                    paranoid_free(tmp);
    761751                } else {
    762752                    file_len_K = get_phys_size_of_drive(fname) * 1024L;
     
    787777    paranoid_fclose(fin);
    788778    log_it("Finished calculating total size of all biggiefiles");
    789     paranoid_free(tmp);
    790     paranoid_free(command);
    791779    return (scratchL);
    792780}
     
    803791    /*@ buffer ****************************************************** */
    804792    char *tmp = NULL;
    805     char *command;
     793    char *command = NULL;
    806794    long long llres;
    807795    size_t n = 0;
    808796    /*@ pointers **************************************************** */
    809     char *p;
    810     FILE *fin;
     797    char *p = NULL;
     798    FILE *fin = NULL;
    811799
    812800    /*@ end vars *************************************************** */
     
    10251013    paranoid_free(command);
    10261014
    1027     asprintf(&tmp,
    1028            call_program_and_get_last_line_of_output("which mondorestore"));
    1029     if (!tmp[0]) {
     1015    tmp = call_program_and_get_last_line_of_output("which mondorestore");
     1016    if (!tmp) {
    10301017        fatal_error
    10311018            ("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?");
     
    10681055
    10691056    /*@ buffers ******** */
    1070     char *outfile;
    1071     char *nfs_dev;
    1072     char *nfs_mount;
    1073     char *nfs_client_ipaddr;
    1074     char *nfs_client_netmask;
    1075     char *nfs_client_broadcast;;
    1076     char *nfs_client_defgw;
    1077     char *nfs_server_ipaddr;
    1078     char *tmp;
    1079     char *command;
     1057    char *outfile = NULL;
     1058    char *nfs_dev = NULL;
     1059    char *nfs_mount = NULL;
     1060    char *nfs_client_ipaddr = NULL;
     1061    char *nfs_client_netmask = NULL;
     1062    char *nfs_client_broadcast = NULL;
     1063    char *nfs_client_defgw = NULL;
     1064    char *nfs_server_ipaddr = NULL;
     1065    char *tmp = NULL;
     1066    char *command = NULL;
    10801067
    10811068    /*@ pointers ***** */
     
    10991086    asprintf(&command,
    11001087            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1");
    1101     asprintf(&nfs_dev, call_program_and_get_last_line_of_output(command));
     1088    nfs_dev = call_program_and_get_last_line_of_output(command);
    11021089    paranoid_free(command);
    11031090
    11041091    asprintf(&command,
    11051092            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2");
    1106     asprintf(&nfs_client_ipaddr,
    1107            call_program_and_get_last_line_of_output(command));
     1093    nfs_client_ipaddr = call_program_and_get_last_line_of_output(command);
    11081094    paranoid_free(command);
    11091095
    11101096    asprintf(&command,
    11111097            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2");
    1112     asprintf(&nfs_client_netmask,
    1113            call_program_and_get_last_line_of_output(command));
     1098    nfs_client_netmask = call_program_and_get_last_line_of_output(command);
    11141099    paranoid_free(command);
    11151100
    11161101    asprintf(&command,
    11171102            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f8 | cut -d':' -f2");
    1118     strcpy(nfs_client_broadcast,
    1119            call_program_and_get_last_line_of_output(command));
    1120     sprintf(command,
     1103    nfs_client_broadcast = call_program_and_get_last_line_of_output(command);
     1104    paranoid_free(command);
     1105
     1106    asprintf(&command,
    11211107            "route -n | grep '^0.0.0.0' | awk '{print $2}'");
    1122     asprintf(&nfs_client_defgw,
    1123            call_program_and_get_last_line_of_output(command));
     1108    nfs_client_defgw = call_program_and_get_last_line_of_output(command);
    11241109    paranoid_free(command);
    11251110
     
    13171302    paranoid_free(tmp);
    13181303
    1319     asprintf(&do_not_compress_these, last_line_of_file(tmp));
     1304    do_not_compress_these = last_line_of_file(tmp);
    13201305    for (p = do_not_compress_these; p != NULL; p++) {
    13211306        asprintf(&tmp, p);
Note: See TracChangeset for help on using the changeset viewer.