Changeset 120 in MondoRescue for trunk


Ignore:
Timestamp:
Nov 9, 2005, 2:44:43 AM (18 years ago)
Author:
bcornec
Message:

libmondo-filelist.c memory review (asprintf and the like) - compiles but no test made yet

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/AUTHORS

    r30 r120  
    1 Hugo Rabson <hugorabson@msn.com>
     1mondorescue is copyrighted under the GPL by the following authors:
     2
     3Hugo Rabson <hugo.rabson@mondorescue.org>
    24Jesse Keating <hosting@j2solutions.net>
    35Stan Benoit <troff@nakedsoul.org>
    46Mikael Hultgren <mikael.hultgren@gmx.net>
     7Bruno Cornec <bcornec@users.berlios.de>
     8Andree Leidenfrost <andree@users.berlios.de>
  • trunk/mondo/mondo/common/libmondo-filelist-EXT.h

    r59 r120  
    55extern void free_filelist(struct s_node *filelist);
    66extern int get_last_filelist_number(struct s_bkpinfo *bkpinfo);
     7/* BERLIOS: Useless ?
    78extern int add_string_at_node(struct s_node *startnode,
    89                              char *string_to_add);
     10                              */
    911extern struct s_node *load_filelist(char *filelist_fname);
    1012extern void reload_filelist(struct s_node *filelist);
  • trunk/mondo/mondo/common/libmondo-filelist.c

    r87 r120  
    22
    33for subroutines which manipulate the filelist
    4 
    54*/
    65
     
    6463
    6564
    66 
    67 
    68 
    6965/**
    7066 * @addtogroup filelistGroup
     
    8682    char *filelist;
    8783    char *tempfile;
    88     char *cksumlist;
    89     char *tmp;
    9084    long noof_sets;
    9185
     
    9791    int i, retval = 0;
    9892
    99     malloc_string(dev);
    100     malloc_string(filelist);
    101     malloc_string(tempfile);
    102     malloc_string(cksumlist);
    103     malloc_string(tmp);
    10493    mvaddstr_and_log_it(g_currentY, 0, "Dividing filelist into sets");
    10594
     
    114103    { i=0; log_to_screen ("This will take more time. Please be patient."); }
    115104*/
    116     sprintf(filelist, "%s/archives/filelist.full", bkpinfo->scratchdir);
    117     sprintf(cksumlist, "%s/cklist.full", bkpinfo->tmpdir);
     105    asprintf(&filelist, "%s/archives/filelist.full", bkpinfo->scratchdir);
    118106    if (!does_file_exist(filelist)) {
    119107        log_it("filelist %s not found", filelist);
     
    124112        chop_filelist(filelist, bkpinfo->tmpdir,
    125113                      bkpinfo->optimal_set_size);
     114    paranoid_free(filelist);
     115
    126116    estimate_noof_media_required(bkpinfo, noof_sets);   // for cosmetic purposes
    127117
    128     sprintf(tempfile, "%s/biggielist.txt", bkpinfo->tmpdir);
     118    asprintf(&tempfile, "%s/biggielist.txt", bkpinfo->tmpdir);
    129119    if (!(fout = fopen(tempfile, "a"))) {
    130120        log_OS_error("Cannot append to biggielist");
    131121        retval++;
    132         goto end_of_func;
    133     }
     122    }
     123    paranoid_free(tempfile);
    134124    log_it(bkpinfo->image_devs);
    135125
     
    137127
    138128    while (ptr && *ptr) {
    139         strcpy(dev, ptr);
     129        asprintf(&dev, ptr);
    140130        log_it("Examining imagedev %s", dev);
    141131        for (i = 0; i < (int) strlen(dev) && dev[i] != ' '; i++);
     
    146136        fprintf(fout, "%s\n", dev);
    147137        log_it("Adding '%s' to biggielist", dev);
     138        paranoid_free(dev);
     139
    148140        if ((ptr = strchr(ptr, ' '))) {
    149141            ptr++;
     
    153145    mvaddstr_and_log_it(g_currentY++, 74, "Done.");
    154146
    155   end_of_func:
    156     paranoid_free(filelist);
    157     paranoid_free(tempfile);
    158     paranoid_free(cksumlist);
    159     paranoid_free(dev);
    160     paranoid_free(tmp);
    161147    return (retval);
    162148}
     
    171157
    172158    log_msg(1, "Sorting file %s", orig_fname);
    173     malloc_string(tmp_fname);
    174     malloc_string(command);
    175     sprintf(tmp_fname, "/tmp/sort.%d.%d.%d", (int) (random() % 32768),
     159    asprintf(&tmp_fname, "/tmp/sort.%d.%d.%d", (int) (random() % 32768),
    176160            (int) (random() % 32768), (int) (random() % 32768));
    177161
     
    180164    }                           // no sense in trying to sort an empty file
    181165
    182     sprintf(command, "sort %s > %s 2>> %s", orig_fname, tmp_fname,
     166    asprintf(&command, "sort %s > %s 2>> %s", orig_fname, tmp_fname,
    183167            MONDO_LOGFILE);
    184168    retval = system(command);
     169    paranoid_free(command);
     170
    185171    if (retval) {
    186172        log_msg(2, "Failed to sort %s - oh dear", orig_fname);
     
    188174        log_msg(2, "Sorted %s --> %s OK. Copying it back to %s now",
    189175                orig_fname, tmp_fname, orig_fname);
    190         sprintf(command, "mv -f %s %s", tmp_fname, orig_fname);
     176        asprintf(&command, "mv -f %s %s", tmp_fname, orig_fname);
    191177        retval += run_program_and_log_output(command, 2);
     178        paranoid_free(command);
     179
    192180        if (retval) {
    193181            log_msg(2, "Failed to copy %s back to %s - oh dear", tmp_fname,
     
    198186    }
    199187    paranoid_free(tmp_fname);
    200     paranoid_free(command);
    201188    log_msg(1, "Finished sorting file %s", orig_fname);
    202189    return (retval);
    203190}
    204 
    205191
    206192
     
    228214    /*@ int **************************************** */
    229215    int i;
     216    int n = 0;
    230217    long curr_set_no;
    231218
     
    233220    char *outfname;
    234221    char *biggie_fname;
    235     char *incoming;
     222    char *incoming = NULL;
    236223    char *tmp;
    237     char *acl_fname;
    238     char *xattr_fname;
    239224
    240225    /*@ pointers *********************************** */
     
    245230    /*@ structures ********************************* */
    246231    struct stat buf;
    247     int err = 0;
    248 
    249     malloc_string(outfname);
    250     malloc_string(biggie_fname);
    251     incoming = malloc(MAX_STR_LEN * 2);
    252     malloc_string(tmp);
    253     malloc_string(acl_fname);
    254     malloc_string(xattr_fname);
    255232
    256233    assert_string_is_neither_NULL_nor_zerolength(filelist);
     
    272249    curr_set_no = 0;
    273250    curr_set_size = 0;
    274     sprintf(outfname, "%s/filelist.%ld", outdir, curr_set_no);
    275     sprintf(biggie_fname, "%s/biggielist.txt", outdir);
     251    asprintf(&outfname, "%s/filelist.%ld", outdir, curr_set_no);
     252    asprintf(&biggie_fname, "%s/biggielist.txt", outdir);
    276253    log_it("outfname=%s; biggie_fname=%s", outfname, biggie_fname);
    277254    if (!(fbig = fopen(biggie_fname, "w"))) {
    278255        log_OS_error("Cannot openout biggie_fname");
    279         err++;
    280         goto end_of_func;
     256        return(0);
    281257    }
    282258    if (!(fout = fopen(outfname, "w"))) {
    283259        log_OS_error("Cannot openout outfname");
    284         err++;
    285         goto end_of_func;
    286     }
    287     (void) fgets(incoming, MAX_STR_LEN * 2 - 1, fin);
     260        return(0);
     261    }
     262    (void) fgets(&incoming, &n, fin);
    288263    while (!feof(fin)) {
    289264        lino++;
     
    292267            i = 0;
    293268        }
     269        /* BERLIOS: Useless with getline
    294270        if (i > MAX_STR_LEN - 1) {
    295271            incoming[MAX_STR_LEN - 30] = '\0';
     
    297273            err++;
    298274        }
     275        */
    299276        if (incoming[i] < 32) {
    300277            incoming[i] = '\0';
     
    317294                paranoid_fclose(fout);
    318295                sort_file(outfname);
     296                paranoid_free(outfname);
     297
    319298                curr_set_no++;
    320299                curr_set_size = 0;
    321                 sprintf(outfname, "%s/filelist.%ld", outdir, curr_set_no);
     300                asprintf(&outfname, "%s/filelist.%ld", outdir, curr_set_no);
    322301                if (!(fout = fopen(outfname, "w"))) {
    323302                    log_OS_error("Unable to openout outfname");
    324                     err++;
    325                     goto end_of_func;
     303                    return(0);
    326304                }
    327                 sprintf(tmp, "Fileset #%ld chopped ", curr_set_no - 1);
    328305                update_evalcall_form((int) (lino * 100 / noof_lines));
    329                 /*              if (!g_text_mode) {newtDrawRootText(0,22,tmp);newtRefresh();} else {log_it(tmp);} */
    330306            }
    331307        }
    332         (void) fgets(incoming, MAX_STR_LEN * 2 - 1, fin);
     308        (void) getline(&incoming, &n, fin);
    333309    }
    334310    paranoid_fclose(fin);
     
    343319    sort_file(outfname);
    344320    sort_file(biggie_fname);
    345     sprintf(outfname, "%s/LAST-FILELIST-NUMBER", outdir);
    346     sprintf(tmp, "%ld", curr_set_no);
     321    paranoid_free(biggie_fname);
     322    paranoid_free(outfname);
     323
     324    asprintf(&outfname, "%s/LAST-FILELIST-NUMBER", outdir);
     325    asprintf(&tmp, "%ld", curr_set_no);
    347326    if (write_one_liner_data_file(outfname, tmp)) {
    348327        log_OS_error
    349328            ("Unable to echo write one-liner to LAST-FILELIST-NUMBER");
    350         err = 1;
    351     }
     329        return(0);
     330    }
     331    paranoid_free(outfname);
     332    paranoid_free(tmp);
     333
    352334    if (curr_set_no == 0) {
    353         sprintf(tmp, "Only one fileset. Fine.");
     335        asprintf(&tmp, "Only one fileset. Fine.");
    354336    } else {
    355         sprintf(tmp, "Filelist divided into %ld sets", curr_set_no + 1);
     337        asprintf(&tmp, "Filelist divided into %ld sets", curr_set_no + 1);
    356338    }
    357339    log_msg(1, tmp);
     340    paranoid_free(tmp);
    358341    close_evalcall_form();
    359342    /* This is to work around an obscure bug in Newt; open a form, close it,
     
    369352    }
    370353#endif
    371   end_of_func:
    372     paranoid_free(outfname);
    373     paranoid_free(biggie_fname);
    374     paranoid_free(incoming);
    375     paranoid_free(tmp);
    376     paranoid_free(acl_fname);
    377     paranoid_free(xattr_fname);
    378     return (err ? 0 : curr_set_no + 1);
    379 }
    380 
    381 
    382 
     354    return (curr_set_no + 1);
     355}
    383356
    384357
     
    437410{
    438411    FILE *pattr;
    439     char *tmp;
     412    char *tmp = NULL;
     413    int n = 0;
     414
    440415    pattr = popen(syscall, "r");
    441416    if (!pattr) {
     
    448423        return (2);
    449424    }
    450     malloc_string(tmp);
    451     for (fgets(tmp, MAX_STR_LEN, pattr); !feof(pattr);
    452          fgets(tmp, MAX_STR_LEN, pattr)) {
     425    for (getline(&tmp, &n, pattr); !feof(pattr);
     426         getline(&tmp, &n, pattr)) {
    453427        fputs(tmp, pout);
    454428    }
     
    457431    return (0);
    458432}
    459 
    460433
    461434
     
    467440    char *pout_command;
    468441    char *syscall;
    469     char *file_to_analyze;
     442    char *tmp;
     443    char *file_to_analyze = NULL;
    470444    int i;
     445    int n = 0;
    471446
    472447    if (!(fin = fopen(filelist, "r"))) {
     
    474449        return (1);
    475450    }
    476     malloc_string(pout_command);
    477     sprintf(pout_command, "gzip -c1 > %s", auxlist_fname);
     451    asprintf(&pout_command, "gzip -c1 > %s", auxlist_fname);
    478452    if (!(pout = popen(pout_command, "w"))) {
    479453        log_msg(1, "Cannot openout auxlist_fname %s", auxlist_fname);
     
    482456        return (4);
    483457    }
    484     malloc_string(syscall);
    485     malloc_string(file_to_analyze);
    486     for (fgets(file_to_analyze, MAX_STR_LEN, fin); !feof(fin);
    487          fgets(file_to_analyze, MAX_STR_LEN, fin)) {
     458    paranoid_free(pout_command);
     459
     460    for (getline(&file_to_analyze, &n, fin); !feof(fin);
     461         getline(&file_to_analyze, &n, fin)) {
    488462        i = strlen(file_to_analyze);
    489463        if (i > 0 && file_to_analyze[i - 1] < 32) {
     
    491465        }
    492466        log_msg(8, "Analyzing %s", file_to_analyze);
    493         sprintf(syscall, syscall_sprintf, file_to_analyze);
    494         strcat(syscall, " 2>> /dev/null");  // " MONDO_LOGFILE);
     467        asprintf(&tmp, "%s 2>> /dev/null", syscall_sprintf);
     468        asprintf(&syscall, tmp, file_to_analyze);
     469        paranoid_free(tmp);
    495470        call_exe_and_pipe_output_to_fd(syscall, pout);
     471        paranoid_free(syscall);
    496472    }
    497473    paranoid_fclose(fin);
    498474    paranoid_pclose(pout);
    499475    paranoid_free(file_to_analyze);
    500     paranoid_free(syscall);
    501     paranoid_free(pout_command);
    502476    return (0);
    503477}
     
    509483    int retval = 0;
    510484
    511     malloc_string(command);
    512     sprintf(command, "touch %s", facl_fname);
     485    asprintf(&command, "touch %s", facl_fname);
    513486    run_program_and_log_output(command, 8);
     487    paranoid_free(command);
     488
    514489    if (find_home_of_exe("getfacl")) {
    515490//      sort_file(filelist); // FIXME - filelist chopper sorts, so this isn't necessary
    516         sprintf(command,
     491        asprintf(&command,
    517492                "cat %s | getfacl --all-effective -P - 2>> %s | gzip -c1 > %s 2>> %s",
    518493                filelist, MONDO_LOGFILE, facl_fname, MONDO_LOGFILE);
    519494        iamhere(command);
    520495        retval = system(command);
    521     }
    522     paranoid_free(command);
     496        paranoid_free(command);
     497    }
    523498    return (retval);
    524499}
     
    530505    int retval = 0;
    531506
    532     malloc_string(command);
    533     sprintf(command, "touch %s", fattr_fname);
     507    asprintf(&command, "touch %s", fattr_fname);
    534508    run_program_and_log_output(command, 8);
     509    paranoid_free(command);
     510
    535511    if (find_home_of_exe("getfattr")) {
    536512//      sort_file(filelist); // FIXME - filelist chopper sorts, so this isn't necessary
     
    539515                         fattr_fname);
    540516    }
    541     paranoid_free(command);
    542517    return (retval);
    543518}
     
    565540
    566541
    567 
    568542int set_EXAT_list(char *orig_msklist, char *original_exat_fname,
    569543                  char *executable)
    570544{
    571545    const int my_depth = 8;
    572     char *command, *syscall_pin, *syscall_pout, *incoming;
    573     char *current_subset_file, *current_master_file, *masklist;
     546    char *command, *syscall_pin, *syscall_pout;
     547    char *incoming = NULL;
     548    char *current_subset_file = NULL;
     549    char *current_master_file, *masklist;
    574550    int retval = 0;
    575551    int i;
     552    int n = 0;
    576553    char *p, *q;
    577554    FILE *pin, *pout, *faclin;
    578555
    579     malloc_string(command);
    580556    log_msg(1, "set_EXAT_list(%s, %s, %s)", orig_msklist,
    581557            original_exat_fname, executable);
     
    584560        log_msg(1,
    585561                "No masklist provided. I shall therefore set ALL attributes.");
    586         sprintf(command, "cat %s | gzip -dc | %s --restore - 2>> %s",
     562        asprintf(&command, "cat %s | gzip -dc | %s --restore - 2>> %s",
    587563                original_exat_fname, executable, MONDO_LOGFILE);
    588564        log_msg(1, "command = %s", command);
     
    596572                "original_exat_fname %s is empty or missing, so no need to set EXAT list",
    597573                original_exat_fname);
    598         paranoid_free(command);
    599574        return (0);
    600575    }
    601     malloc_string(incoming);
    602     malloc_string(masklist);
    603     malloc_string(current_subset_file);
    604     malloc_string(current_master_file);
    605     malloc_string(syscall_pin);
    606     malloc_string(syscall_pout);
    607     sprintf(masklist, "/tmp/%d.%d.mask", (int) (random() % 32768),
     576    asprintf(&masklist, "/tmp/%d.%d.mask", (int) (random() % 32768),
    608577            (int) (random() % 32768));
    609     sprintf(command, "cp -f %s %s", orig_msklist, masklist);
     578    asprintf(&command, "cp -f %s %s", orig_msklist, masklist);
    610579    run_program_and_log_output(command, 1);
     580    paranoid_free(command);
     581
    611582    sort_file(masklist);
    612     current_subset_file[0] = current_master_file[0] = '\0';
    613     sprintf(syscall_pin, "cat %s | gzip -dc", original_exat_fname);
    614     sprintf(syscall_pout, "%s --restore - 2>> %s", executable,
     583    asprintf(&syscall_pin, "cat %s | gzip -dc", original_exat_fname);
     584    asprintf(&syscall_pout, "%s --restore - 2>> %s", executable,
    615585            MONDO_LOGFILE);
    616586
     
    622592        return (1);
    623593    }
     594    paranoid_free(syscall_pout);
     595
    624596    pin = popen(syscall_pin, "r");
    625597    if (!pin) {
     
    628600        return (1);
    629601    }
     602    paranoid_free(syscall_pin);
     603
    630604    faclin = fopen(masklist, "r");
    631605    if (!faclin) {
     
    637611//  printf("Hi there. Starting the loop\n");
    638612
    639     fgets(current_subset_file, MAX_STR_LEN, faclin);
    640     fgets(incoming, MAX_STR_LEN, pin);
     613    getline(&current_subset_file, &n, faclin);
     614    getline(&incoming, &n, pin);
    641615    while (!feof(pin) && !feof(faclin)) {
    642616//      printf("incoming = %s", incoming);
    643617
    644         strcpy(current_master_file, incoming + 8);
     618        asprintf(&current_master_file, incoming + 8);
    645619
    646620        p = current_subset_file;
     
    670644        if (i < 0) {            // read another subset file in.
    671645            log_msg(my_depth, "Reading next subset line in\n\n");
    672             fgets(current_subset_file, MAX_STR_LEN, faclin);
     646            getline(&current_subset_file, &n, faclin);
    673647            continue;
    674648        }
     
    677651            fputs(incoming, pout);
    678652        }
    679         fgets(incoming, MAX_STR_LEN, pin);
     653        getline(&incoming, &n, pin);
    680654        if (!i) {
    681655            log_msg(my_depth, "Copying master %s", q);
     
    690664                fputs(incoming, pout);
    691665            }
    692             fgets(incoming, MAX_STR_LEN, pin);
     666            getline(&incoming, &n, pin);
    693667        }
    694668        if (!i) {
    695             fgets(current_subset_file, MAX_STR_LEN, faclin);
    696         }
    697     }
     669            getline(&current_subset_file, &n, faclin);
     670        }
     671        paranoid_free(current_master_file);
     672    }
     673    paranoid_free(current_subset_file);
     674
    698675    while (!feof(pin)) {
    699         fgets(incoming, MAX_STR_LEN, pin);
    700     }
     676        getline(&incoming, &n, pin);
     677    }
     678    paranoid_free(incoming);
    701679    fclose(faclin);
    702680    pclose(pin);
     
    706684
    707685    unlink(masklist);
    708     paranoid_free(current_subset_file);
    709     paranoid_free(current_master_file);
    710     paranoid_free(syscall_pout);
    711     paranoid_free(syscall_pin);
    712686    paranoid_free(masklist);
    713     paranoid_free(incoming);
    714     paranoid_free(command);
     687
    715688    return (retval);
    716689}
     
    728701    return (set_EXAT_list(masklist, acl_fname, "setfacl"));
    729702}
    730 
    731 /*
    732   if (find_home_of_exe("setfattr"))
    733     {
    734       sprintf(command, "cat %s | gzip -dc | setfattr --restore - 2>> %s", acl_fname, MONDO_LOGFILE);
    735       log_msg(1, "command = %s", command);
    736       retval = system(command);
    737     }
    738   paranoid_free(acl_subset_fname);
    739   paranoid_free(syscall_pin);
    740   paranoid_free(command);
    741   return(retval);
    742 */
    743 
    744 
    745 
    746 
    747703
    748704
     
    757713    /*@ buffers ***************************************************** */
    758714    char val_sz[MAX_STR_LEN];
    759     char cfg_fname[MAX_STR_LEN];
    760 /*  char tmp[MAX_STR_LEN]; remove stan benoit apr 2002 */
     715    char *cfg_fname;
    761716
    762717    /*@ long ******************************************************** */
     
    767722    assert(bkpinfo != NULL);
    768723
    769     sprintf(cfg_fname, "%s/mondo-restore.cfg", bkpinfo->tmpdir);
     724    asprintf(&cfg_fname, "%s/mondo-restore.cfg", bkpinfo->tmpdir);
    770725    read_cfg_var(cfg_fname, "last-filelist-number", val_sz);
    771726    val_i = atoi(val_sz);
     
    773728        val_i = 500;
    774729    }
     730    paranoid_free(cfg_fname);
    775731    return (val_i);
    776732}
     
    784740 * @bug I don't understand this function. Would someone care to explain it?
    785741 */
     742
    786743int add_string_at_node(struct s_node *startnode, char *string_to_add)
    787744{
    788745
    789 
    790     /*@ int ******************************************************** */
    791746    int noof_chars;
    792747    int i;
    793748    int res;
    794749
    795     /*@ sturctures ************************************************* */
    796750    struct s_node *node, *newnode;
    797751
    798     /*@ char  ****************************************************** */
    799752    char char_to_add;
    800 
    801     /*@ bools ****************************************************** */
    802753
    803754    const bool sosodef = FALSE;
     
    813764    }
    814765
    815     noof_chars = strlen(string_to_add) + 1; /* we include the '\0' */
    816 
    817 /* walk across tree if necessary */
     766    noof_chars = strlen(string_to_add) + 1; // we include the '\0'
     767
     768    // walk across tree if necessary
    818769    node = startnode;
    819770    char_to_add = string_to_add[0];
     
    824775    }
    825776
    826 /* walk down tree if appropriate */
     777    // walk down tree if appropriate
    827778    if (node->down != NULL && node->ch == char_to_add) {
    828779        log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_add);
     
    838789    }
    839790
    840 /* add here */
     791    // add here
    841792    if (!(newnode = (struct s_node *) malloc(sizeof(struct s_node)))) {
    842793        log_to_screen("failed to malloc");
     
    866817        return (0);
    867818    }
    868 // add the rest
     819    // add the rest
    869820    log_msg(6, "Adding remaining chars ('%s')", string_to_add + 1);
    870821    for (i = 1; i < noof_chars; i++) {
     
    890841
    891842
    892 
    893 
    894843/**
    895844 * Load a filelist into a <tt>struct s_node</tt>.
     
    908857
    909858    /*@ buffers **************************************************** */
    910     char command_to_open_fname[MAX_STR_LEN];
    911     char fname[MAX_STR_LEN];
    912     char tmp[MAX_STR_LEN];
     859    char *command_to_open_fname;
     860    char *fname = NULL;
     861    char *tmp;
    913862    int pos_in_fname;
     863    int n = 0;
    914864    /*@ int ******************************************************** */
    915865    int percentage;
     
    926876    }
    927877    log_to_screen("Loading filelist");
    928     sprintf(command_to_open_fname, "gzip -dc %s", filelist_fname);
    929     sprintf(tmp, "zcat %s | wc -l", filelist_fname);
     878    asprintf(&command_to_open_fname, "gzip -dc %s", filelist_fname);
     879    asprintf(&tmp, "zcat %s | wc -l", filelist_fname);
    930880    log_msg(6, "tmp = %s", tmp);
    931881    lines_in_filelist =
    932882        atol(call_program_and_get_last_line_of_output(tmp));
     883    paranoid_free(tmp);
     884
    933885    if (lines_in_filelist < 3) {
    934886        log_to_screen("Warning - surprisingly short filelist.");
     
    949901        return (NULL);
    950902    }
     903    paranoid_free(command_to_open_fname);
     904
    951905    open_evalcall_form("Loading filelist from disk");
    952     for (fgets(fname, MAX_STR_LEN, pin); !feof(pin);
    953          fgets(fname, MAX_STR_LEN, pin)) {
     906    for (getline(&fname, &n, pin); !feof(pin);
     907         getline(&fname, &n, pin)) {
    954908        if ((fname[strlen(fname) - 1] == 13
    955909             || fname[strlen(fname) - 1] == 10) && strlen(fname) > 0) {
    956910            fname[strlen(fname) - 1] = '\0';
    957911        }
    958 //      strip_spaces (fname);
    959912        if (!strlen(fname)) {
    960913            continue;
     
    964917                continue;
    965918            }
    966             strcpy(tmp, fname);
     919            asprintf(&tmp, fname);
    967920            tmp[pos_in_fname] = '\0';
    968921            if (strlen(tmp)) {
    969922                add_string_at_node(filelist, tmp);
    970923            }
     924            paranoid_free(tmp);
    971925        }
    972926        add_string_at_node(filelist, fname);
     
    976930        }
    977931    }
     932    paranoid_free(fname);
    978933    paranoid_pclose(pin);
    979934    close_evalcall_form();
     
    1020975
    1021976
    1022 
    1023 
    1024977/**
    1025978 * Reset the filelist to the state it was when it was loaded. This does not
     
    1034987    toggle_all_root_dirs_on(filelist);
    1035988}
    1036 
    1037989
    1038990
     
    11001052
    11011053
    1102 
    11031054/**
    11041055 * Toggle all root dirs on.
     
    11681119    /*@ buffers **************************************************** */
    11691120    static char current_filename[MAX_STR_LEN];
    1170 
    1171 /*  char tmp[MAX_STR_LEN+2]; */
    11721121
    11731122    /*@ end vars *************************************************** */
     
    12231172}
    12241173
     1174
    12251175/**
    12261176 * Toggle whether a path is selected.
     
    12431193    /*@ buffers ***************************************************** */
    12441194    static char current_filename[MAX_STR_LEN];
    1245     char tmp[MAX_STR_LEN + 2];
    12461195
    12471196    /*@ end vars *************************************************** */
     
    12661215                    || current_filename[j] == '\0') {
    12671216                    node->selected = on_or_off;
    1268                     sprintf(tmp, "%s is now %s\n", current_filename,
    1269                             (on_or_off ? "ON" : "OFF"));
    12701217                }
    12711218            }
     
    13051252
    13061253
    1307 
    1308 
    1309 
    1310 
    1311 
    1312 /**
    1313  * The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
    1314  */
    1315 char *g_skeleton_filelist = NULL;
    1316 
    13171254/**
    13181255 * Number of entries in the skeleton filelist.
    13191256 */
    13201257long g_skeleton_entries = 0;
     1258
    13211259
    13221260/**
     
    13881326 */
    13891327int open_and_list_dir(char *dir, char *sth, FILE * fout,
    1390                       time_t time_of_last_full_backup)
     1328                      time_t time_of_last_full_backup, char *skeleton_filelist)
    13911329{
    13921330    DIR *dip;
    13931331    struct dirent *dit;
    13941332    struct stat statbuf;
    1395     char new[MAX_STR_LEN];
     1333    char *new;
    13961334    char *tmp;
    1397     char *sth_B;
    13981335    static int percentage = 0;
    1399     char *ith_B;
    14001336    char *skip_these;
    14011337    char *new_with_spaces;
     
    14111347    time_t this_time;
    14121348
    1413     malloc_string(tmp);
    1414     malloc_string(sth_B);
    1415     malloc_string(ith_B);
    1416     malloc_string(new_with_spaces);
    14171349    p = strrchr(dir, '/');
    14181350    if (p) {
     
    14231355
    14241356    if (!depth) {
    1425         malloc_string(name_of_evalcall_form);
    14261357        malloc_string(find_skeleton_marker);
    14271358#if linux
    14281359        // 2.6 has /sys as a proc-type thing -- must be excluded
    1429         sprintf(tmp,
     1360        asprintf(&tmp,
    14301361                "find %s -maxdepth %d -path /proc -prune -o -path /tmp -prune -o -path /sys -prune -o -path /dev/shm -prune -o -path /media/floppy -prune -o -type d -a -print > %s 2> /dev/null",
    1431                 dir, MAX_SKEL_DEPTH, g_skeleton_filelist);
     1362                dir, MAX_SKEL_DEPTH, skeleton_filelist);
    14321363#else
    14331364        // On BSD, for example, /sys is the kernel sources -- don't exclude
    1434         sprintf(tmp,
     1365        asprintf(&tmp,
    14351366                "find %s -maxdepth %d -path /proc -prune -o -path /tmp -prune -o -type d -a -print > %s 2> /dev/null",
    1436                 dir, MAX_SKEL_DEPTH, g_skeleton_filelist);
     1367                dir, MAX_SKEL_DEPTH, skeleton_filelist);
    14371368#endif
    14381369        system(tmp);
    1439         sprintf(tmp, "wc -l %s | awk '{print $1;}'", g_skeleton_filelist);
     1370        paranoid_free(tmp);
     1371
     1372        asprintf(&tmp, "wc -l %s | awk '{print $1;}'", skeleton_filelist);
    14401373        g_skeleton_entries =
    14411374            1 + atol(call_program_and_get_last_line_of_output(tmp));
    1442         sprintf(name_of_evalcall_form, "Making catalog of %s", dir);
     1375        paranoid_free(tmp);
     1376
     1377        asprintf(&name_of_evalcall_form, "Making catalog of %s", dir);
    14431378        open_evalcall_form(name_of_evalcall_form);
    14441379        find_skeleton_marker[0] = '\0';
     
    14501385        sprintf(find_skeleton_marker,
    14511386                "fgrep -v \"%s\" %s > %s.new 2> /dev/null", dir,
    1452                 g_skeleton_filelist, g_skeleton_filelist);
     1387                skeleton_filelist, skeleton_filelist);
    14531388//    log_msg(0, "fsm = %s", find_skeleton_marker);
    14541389        if (!system(find_skeleton_marker)) {
     
    14581393//        log_msg(2, "Incrementing skeleton_lino; now %ld/%ld (%d%%)", skeleton_lino, g_skeleton_entries, percentage);
    14591394            sprintf(find_skeleton_marker, "mv -f %s.new %s",
    1460                     g_skeleton_filelist, g_skeleton_filelist);
     1395                    skeleton_filelist, skeleton_filelist);
    14611396//        log_msg(6, "fsm = %s", find_skeleton_marker);
    14621397            run_program_and_log_output(find_skeleton_marker, 8);
     
    14661401#ifndef _XWIN
    14671402                if (!g_text_mode) {
    1468                     sprintf(tmp, "Reading %-68s", dir);
     1403                    asprintf(&tmp, "Reading %-68s", dir);
    14691404                    newtDrawRootText(0, g_noof_rows - 3, tmp);
     1405                    paranoid_free(tmp);
    14701406                }
    14711407#endif
     
    14791415//  log_msg(0, "Cataloguing %s", dir);
    14801416    if (sth[0] == ' ') {
    1481         skip_these = sth;
     1417        asprintf(&skip_these, "%s", sth);
    14821418    } else {
    1483         skip_these = sth_B;
    1484         sprintf(skip_these, " %s ", sth);
    1485     }
    1486     sprintf(new_with_spaces, " %s ", dir);
     1419        asprintf(&skip_these, " %s ", sth);
     1420    }
     1421    asprintf(&new_with_spaces, " %s ", dir);
    14871422    if ((dip = opendir(dir)) == NULL) {
    14881423        log_OS_error("opendir");
     
    14931428        while ((dit = readdir(dip)) != NULL) {
    14941429            i++;
    1495             strcpy(new, dir);
    14961430            if (strcmp(dir, "/")) {
    1497                 strcat(new, "/");
     1431                asprintf(&new, "%s/%s", dir, dit->d_name);
     1432            } else {
     1433                asprintf(&new, "%s%s", dir, dit->d_name);
    14981434            }
    1499             strcat(new, dit->d_name);
     1435            paranoid_free(new_with_spaces);
     1436            asprintf(&new_with_spaces, " %s ", new);
     1437            /* BERLIOS: Old code
    15001438            new_with_spaces[0] = ' ';
    15011439            strcpy(new_with_spaces + 1, new);
    15021440            strcat(new_with_spaces, " ");
     1441            */
    15031442            if (strstr(skip_these, new_with_spaces)) {
    15041443                fprintf(fout, "%s\n", new);
     
    15081447                        && S_ISDIR(statbuf.st_mode)) {
    15091448                        open_and_list_dir(new, skip_these, fout,
    1510                                           time_of_last_full_backup);
     1449                                          time_of_last_full_backup, skeleton_filelist);
    15111450                    } else {
    15121451                        if (time_of_last_full_backup == 0
     
    15171456                                counter = 0;
    15181457                                uberctr++;
    1519                                 sprintf(tmp, " %c ",
     1458                                asprintf(&tmp, " %c ",
    15201459                                        special_dot_char(uberctr));
    15211460#ifndef _XWIN
     
    15251464                                    newtRefresh();
    15261465                                }
     1466                                paranoid_free(tmp);
    15271467#endif
    15281468                            }
     
    15311471                }
    15321472            }
    1533         }
    1534     }
     1473            paranoid_free(new);
     1474        }
     1475    }
     1476    paranoid_free(new_with_spaces);
     1477    paranoid_free(skip_these);
     1478
    15351479    if (dip) {
    15361480        if (closedir(dip) == -1) {
     
    15431487        paranoid_free(name_of_evalcall_form);
    15441488        paranoid_free(find_skeleton_marker);
    1545         unlink(g_skeleton_filelist);
     1489        unlink(skeleton_filelist);
    15461490        log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
    15471491    }
    1548     paranoid_free(tmp);
    1549     paranoid_free(sth_B);
    1550     paranoid_free(ith_B);
    1551     paranoid_free(new_with_spaces);
    15521492    return (0);
    15531493}
    1554 
    15551494
    15561495
     
    15801519    return (sz_res);
    15811520}
    1582 
    15831521
    15841522
     
    16021540    char sz_datefile_wildcard[] = "/var/cache/mondo-archive/difflevel.%d";
    16031541    char *p, *q;
    1604     char sz_datefile[80];
    1605     char *sz_filelist, *exclude_paths, *tmp;
     1542    char *sz_datefile;
     1543    char *sz_filelist, *exclude_paths;
    16061544    int i;
    16071545    FILE *fout;
     
    16091547    time_t time_of_last_full_backup = 0;
    16101548    struct stat statbuf;
    1611 
    1612     malloc_string(command);
    1613     malloc_string(tmp);
    1614     malloc_string(sz_filelist);
    1615     malloc_string(g_skeleton_filelist);
    1616     if (!(exclude_paths = malloc(1000))) {
    1617         fatal_error("Cannot malloc exclude_paths");
    1618     }
    1619     log_msg(3, "Trying to write test string to exclude_paths");
    1620     strcpy(exclude_paths, "/blah /froo");
    1621     log_msg(3, "...Success!");
    1622     sprintf(sz_datefile, sz_datefile_wildcard, 0);
     1549    char *skeleton_filelist;
     1550    // The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
     1551
     1552    asprintf(&sz_datefile, sz_datefile_wildcard, 0);
    16231553    if (!include_paths && !userdef_filelist) {
    16241554        fatal_error
     
    16261556    }
    16271557// make hole for filelist
    1628     sprintf(command, "mkdir -p %s/archives", scratchdir);
     1558    asprintf(&command, "mkdir -p %s/archives", scratchdir);
    16291559    paranoid_system(command);
    1630     sprintf(sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
     1560    paranoid_free(command);
     1561
     1562    asprintf(&sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
    16311563    make_hole_for_file(sz_filelist);
    16321564
    16331565    if (differential == 0) {
    16341566        // restore last good datefile if it exists
    1635         sprintf(command, "cp -f %s.aborted %s", sz_datefile, sz_datefile);
     1567        asprintf(&command, "cp -f %s.aborted %s", sz_datefile, sz_datefile);
    16361568        run_program_and_log_output(command, 3);
     1569        paranoid_free(command);
     1570
    16371571        // backup last known good datefile just in case :)
    16381572        if (does_file_exist(sz_datefile)) {
    1639             sprintf(command, "mv -f %s %s.aborted", sz_datefile,
     1573            asprintf(&command, "mv -f %s %s.aborted", sz_datefile,
    16401574                    sz_datefile);
    16411575            paranoid_system(command);
     1576            paranoid_free(command);
    16421577        }
    16431578        make_hole_for_file(sz_datefile);
     
    16541589        log_msg(2, "Differential backup. Yay.");
    16551590    }
     1591    paranoid_free(sz_datefile);
    16561592
    16571593// use user-specified filelist (if specified)
     
    16601596                "Using the user-specified filelist - %s - instead of calculating one",
    16611597                userdef_filelist);
    1662         sprintf(command, "cp -f %s %s", userdef_filelist, sz_filelist);
     1598        asprintf(&command, "cp -f %s %s", userdef_filelist, sz_filelist);
    16631599        if (run_program_and_log_output(command, 3)) {
    16641600            fatal_error("Failed to copy user-specified filelist");
    16651601        }
     1602        paranoid_free(command);
    16661603    } else {
    16671604        log_msg(2, "include_paths = '%s'", include_paths);
    16681605        log_msg(1, "Calculating filelist");
    1669         sprintf(exclude_paths, " %s %s %s %s %s %s . .. \
     1606        asprintf(&exclude_paths, " %s %s %s %s %s %s . .. \
    16701607" MNT_CDROM " " MNT_FLOPPY " /media/cdrom /media/cdrecorder \
    16711608/proc /sys /tmp /root/images/mondo /root/images/mindi ", excp, call_program_and_get_last_line_of_output("locate /win386.swp 2> /dev/null"), call_program_and_get_last_line_of_output("locate /hiberfil.sys 2> /dev/null"), call_program_and_get_last_line_of_output("locate /pagefile.sys 2> /dev/null"), (tmpdir[0] == '/' && tmpdir[1] == '/') ? (tmpdir + 1) : tmpdir, (scratchdir[0] == '/' && scratchdir[1] == '/') ? (scratchdir + 1) : scratchdir);
     
    16741611        log_msg(2,
    16751612                "Generating skeleton filelist so that we can track our progress");
    1676         sprintf(g_skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
    1677         make_hole_for_file(g_skeleton_filelist);
     1613        asprintf(&skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
     1614        make_hole_for_file(skeleton_filelist);
     1615
    16781616        log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
    16791617        log_msg(2, "Opening out filelist to %s", sz_filelist);
     
    16851623            log_msg(1, "Including only '/' in %s", sz_filelist);
    16861624            open_and_list_dir("/", exclude_paths, fout,
    1687                               time_of_last_full_backup);
     1625                              time_of_last_full_backup, skeleton_filelist);
    16881626        } else {
    16891627            p = include_paths;
     
    16921630                log_msg(1, "Including %s in filelist %s", q, sz_filelist);
    16931631                open_and_list_dir(q, exclude_paths, fout,
    1694                                   time_of_last_full_backup);
     1632                                  time_of_last_full_backup, skeleton_filelist);
    16951633                p += strlen(q);
    16961634                while (*p == ' ') {
     
    17001638        }
    17011639        paranoid_fclose(fout);
     1640        paranoid_free(skeleton_filelist);
     1641        paranoid_free(exclude_paths);
    17021642    }
    17031643    log_msg(2, "Copying new filelist to scratchdir");
    1704     sprintf(command, "mkdir -p %s/archives", scratchdir);
     1644    asprintf(&command, "mkdir -p %s/archives", scratchdir);
    17051645    paranoid_system(command);
    1706     sprintf(command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
     1646    paranoid_free(command);
     1647
     1648    asprintf(&command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
    17071649    paranoid_system(command);
    1708     sprintf(command, "mv -f %s %s", sz_filelist, tmpdir);
     1650    paranoid_free(command);
     1651
     1652    asprintf(&command, "mv -f %s %s", sz_filelist, tmpdir);
    17091653    paranoid_system(command);
    1710     log_msg(2, "Freeing variables");
     1654    paranoid_free(command);
    17111655    paranoid_free(sz_filelist);
    1712     paranoid_free(command);
    1713     paranoid_free(exclude_paths);
    1714     paranoid_free(tmp);
    1715     paranoid_free(g_skeleton_filelist);
     1656
    17161657    log_msg(2, "Exiting");
    17171658    return (0);
    17181659}
    1719 
    1720 
    17211660
    17221661
     
    17811720    }
    17821721}
    1783 
    17841722
    17851723
     
    18241762            }
    18251763            paranoid_free(fname);
    1826             asprintf(&fname, tmp);
    1827             paranoid_free(tmp);
     1764            fname = tmp;
    18281765        }
    18291766        while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
     
    18451782                    asprintf(&tmp, fname + 1);
    18461783                    paranoid_free(fname);
    1847                     asprintf(&fname, tmp);
    1848                     paranoid_free(tmp);
     1784                    fname = tmp;
    18491785                }
    18501786                log_msg(5, "Found '%s'", fname);
     
    18621798
    18631799
    1864 
    1865 
    1866 
    1867 
    18681800/**
    18691801 * Add all files listed in @p list_of_files_fname to the directory structure rooted at
     
    18781810{
    18791811    FILE *fin;
    1880     char *tmp;
     1812    char *tmp = NULL;
     1813    int n = 0;
    18811814    struct s_node *nod;
    18821815
    1883     malloc_string(tmp);
    18841816    log_msg(3, "Adding %s to filelist", list_of_files_fname);
    18851817    if (!(fin = fopen(list_of_files_fname, "r"))) {
     
    18871819        return (1);
    18881820    }
    1889     for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
    1890          fgets(tmp, MAX_STR_LEN, fin)) {
     1821    for (getline(&tmp, &n, fin); !feof(fin);
     1822         getline(&tmp, &n, fin)) {
    18911823        if (!tmp[0]) {
    18921824            continue;
  • trunk/mondo/mondo/common/libmondo-verify.c

    r87 r120  
    11/* $Id$ */
    2 /*
    3 copyright : (C) 2002 Mondo  Hugo Rabson <hugorabson@msn.com>
    4 edited by : by Stan Benoit 4/2002 troff@nakedsoul.org
    5 */
    6 
    72
    83/**
  • trunk/tools/quality

    r119 r120  
    3232echo "monodrescue uses : `find . -name '*.c' -o -name '*.h' |  egrep -v '\.svn' | egrep -v '/\*' | xargs grep -r strcat | wc -l` strcat"
    3333echo "monodrescue uses : `find . -name '*.c' -o -name '*.h' |  egrep -v '\.svn' | egrep -v '/\*' | xargs grep -r strcpy | wc -l` strcpy"
     34echo "monodrescue uses : `find . -name '*.c' -o -name '*.h' |  egrep -v '\.svn' | egrep -v '/\*' | xargs grep -r fgets | wc -l` fgets"
    3435echo "monodrescue uses : `find . -name '*.c' -o -name '*.h' |  egrep -v '\.svn' | egrep -v '/\*' | xargs grep -r malloc_string | wc -l` malloc_string"
    3536echo "monodrescue uses : `find . -name '*.c' -o -name '*.h' |  egrep -v '\.svn' | egrep -v '/\*' | xargs grep -r MAX_STR_LEN | wc -l` MAX_STR_LEN"
     37echo "monodrescue uses : `find . -name '*.c' -o -name '*.h' |  egrep -v '\.svn' | egrep -v '/\*' | xargs grep -r goto | wc -l` goto"
    3638
    3739echo " "
Note: See TracChangeset for help on using the changeset viewer.