Changeset 2204 in MondoRescue


Ignore:
Timestamp:
May 13, 2009, 12:26:33 PM (15 years ago)
Author:
Bruno Cornec
Message:

First set of improvements for mountlist management (test with valgrind). Fix errors on scanf usage. More to come.

Location:
branches/2.2.9/mondo
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mondo/src/common/libmondo-mountlist.c

    r2202 r2204  
    357357
    358358    /*@ pointers ******************************************************* */
    359     char *part_table_fmt;
     359    char *part_table_fmt = NULL;
    360360
    361361    /*@ initialize ***************************************************** */
     
    490490
    491491
    492 
    493492/**
    494493 * Evaluate a whole mountlist for flaws. Calls evaluate_drive_within_mountlist()
     
    565564    /*@ int ************************************************************** */
    566565    int i = 0;
    567     char *tmp;
    568     char *flaws_str;
    569 
    570     malloc_string(tmp);
    571     malloc_string(flaws_str);
    572566
    573567    assert(mountlist != NULL);
     
    577571         && strcmp(mountlist->el[i].device, device) != 0; i++);
    578572
    579     paranoid_free(tmp);
    580     paranoid_free(flaws_str);
    581 
    582573    if (i == mountlist->entries) {
    583574        return (-1);
     
    586577    }
    587578}
    588 
    589 
    590 
    591579
    592580
     
    610598
    611599    /*@ buffetr ********************************************************* */
    612     char *curr_mountpoint;
    613     char *tmp;
    614 
    615     malloc_string(curr_mountpoint);
    616     malloc_string(tmp);
     600    char *curr_mountpoint = NULL;
     601    char *tmp = NULL;
     602
    617603    assert(mountlist != NULL);
    618604    assert(flaws_str != NULL);
     605
    619606    for (currline = 0; currline < mountlist->entries; currline++) {
    620         strcpy(curr_mountpoint, mountlist->el[currline].mountpoint);
     607        asprintf(&curr_mountpoint, "%s", mountlist->el[currline].mountpoint);
    621608        for (i = 0, copies = 0, last_copy = -1; i < mountlist->entries;
    622609             i++) {
     
    630617        if (copies > 1 && last_copy == currline
    631618            && strcmp(curr_mountpoint, "raid")) {
    632             sprintf(tmp, " %s %s's.", number_to_text(copies),
     619            asprintf(&tmp, " %s %s's.", number_to_text(copies),
    633620                    curr_mountpoint);
    634621            strcat(flaws_str, tmp);
    635622            log_it(tmp);
     623            paranoid_free(tmp);
    636624            res++;
    637625        }
    638     }
    639     paranoid_free(curr_mountpoint);
    640     paranoid_free(tmp);
     626        paranoid_free(curr_mountpoint);
     627    }
    641628    return (res);
    642629}
     
    660647
    661648    /*@ buffers ********************************************************* */
    662     char *drive;
    663     char *tmp;
     649    char *drive = NULL;
    664650
    665651    long long size;
    666652
    667     malloc_string(drive);
    668     malloc_string(tmp);
    669653    assert(mountlist != NULL);
    670654    assert(drivelist != NULL);
     
    672656    for (lino = 0, noof_drives = 0; lino < mountlist->entries; lino++) {
    673657
    674         strcpy(drive, mountlist->el[lino].device);
     658        asprintf(&drive, "%s", mountlist->el[lino].device);
    675659        if (!strncmp(drive, RAID_DEVICE_STUB, strlen(RAID_DEVICE_STUB))) {
    676             sprintf(tmp,
    677                     "Not putting %s in list of drives: it's a virtual drive",
     660            log_msg(8, "Not putting %s in list of drives: it's a virtual drive",
    678661                    drive);
    679             log_msg(8, tmp);
    680662            continue;
    681663        }
     
    683665        size = mountlist->el[lino].size;
    684666        if (size == 0) {
    685             sprintf(tmp,
     667            log_msg(8,
    686668                    "Not putting %s in list of drives: it has zero size (maybe an LVM volume)",
    687669                    drive);
    688             log_msg(8, tmp);
    689670            continue;
    690671        }
    691672
    692         sprintf(tmp,
    693                 "Putting %s with size %lli in list of drives",
     673        log_msg(8, "Putting %s with size %lli in list of drives",
    694674                drive, size);
    695         log_msg(8, tmp);
    696675
    697676        (void) truncate_to_drive_name(drive);
     
    706685    drivelist->entries = noof_drives;
    707686    log_msg(8, "Made list of drives");
    708     paranoid_free(drive);
    709     paranoid_free(tmp);
    710687
    711688    return (noof_drives);
    712689}
    713 
    714 
    715 
    716 
    717690
    718691
     
    738711
    739712    /*@ buffers ********************************************************* */
    740     char *tmp;
    741 
    742     malloc_string(tmp);
     713    char *tmp = NULL;
     714
    743715    assert(output_list != NULL);
    744716    assert(mountlist != NULL);
     
    758730                       (void *) &mountlist->el[i],
    759731                       sizeof(struct mountlist_line));
    760                 sprintf(tmp,
    761                         "%s is available; user may choose to add it to raid device",
    762                         output_list->el[items - 1].device);
    763                 log_it(tmp);
     732                asprintf(&tmp,
     733                         "%s is available; user may choose to add it to raid device",
     734                         output_list->el[items - 1].device);
     735                log_it(tmp);
     736                paranoid_free(tmp);
    764737            }
    765738        }
     
    767740    output_list->entries = items;
    768741    log_it("MLUORP -- ending");
    769     paranoid_free(tmp);
    770 }
    771 
    772 
    773 
     742}
    774743
    775744
     
    800769    }
    801770}
    802 
    803 
    804771
    805772
     
    816783int load_mountlist(struct mountlist_itself *mountlist, char *fname)
    817784{
    818     FILE *fin;
     785    FILE *fin = NULL;
    819786    /* malloc ** */
    820     char *incoming;
    821     char *siz;
    822     char *tmp;
    823     char *p;
    824 
    825     int items;
    826     int j;
     787    char *incoming = NULL;
     788    char *siz = NULL;
     789    char *tmp = NULL;
     790    char *p = NULL;
     791
     792    int items = 0;
     793    int j = 0;
     794    int res = 0;
    827795
    828796    assert(mountlist != NULL);
    829797    assert_string_is_neither_NULL_nor_zerolength(fname);
    830     malloc_string(incoming);
    831     malloc_string(siz);
    832     malloc_string(tmp);
     798
    833799    if (!(fin = fopen(fname, "r"))) {
    834800        log_it("Unable to open mountlist - '%s'", fname);
    835801        log_to_screen("Cannot open mountlist");
    836         paranoid_free(incoming);
    837         paranoid_free(siz);
    838         paranoid_free(tmp);
    839802        return (1);
    840803    }
    841     items = 0;
     804    malloc_string(incoming);
     805    malloc_string(siz);
    842806    (void) fgets(incoming, MAX_STR_LEN - 1, fin);
    843807    log_it("Loading mountlist...");
    844808    while (!feof(fin)) {
    845809#if linux
    846         sscanf(incoming,
     810        res = sscanf(incoming,
    847811               "%s %s %s %s %s",
    848812               mountlist->el[items].device,
     
    850814               mountlist->el[items].format,
    851815               siz, mountlist->el[items].label);
     816        if (res < 5) {
     817            /* no label found */
     818            asprintf(&(mountlist->el[items].label), "%s", "");
     819        }
    852820#elif __FreeBSD__
    853         sscanf(incoming,
     821        res = sscanf(incoming,
    854822               "%s %s %s %s",
    855823               mountlist->el[items].device,
    856824               mountlist->el[items].mountpoint,
    857825               mountlist->el[items].format, siz);
    858         strcpy(mountlist->el[items].label, "");
     826        asprintf(&(mountlist->el[items].label), "%s", "");
    859827#endif
    860828
     
    885853            if (j < items) {
    886854                strcat(mountlist->el[items].device, "_dup");
    887                 sprintf(tmp,
    888                         "Duplicate entry in mountlist - renaming to %s",
    889                         mountlist->el[items].device);
    890                 log_it(tmp);
    891             }
    892             strcpy(tmp, mountlist->el[items].device);
     855                asprintf(&tmp,
     856                         "Duplicate entry in mountlist - renaming to %s",
     857                         mountlist->el[items].device);
     858                log_it(tmp);
     859                paranoid_free(tmp);
     860            }
     861            asprintf(&tmp, "%s", mountlist->el[items].device);
    893862            if (strstr(tmp, "/dev/md/")) {
    894863                log_it("format_device() --- Contracting %s", tmp);
     
    903872                strcpy(mountlist->el[items].device, tmp);
    904873            }
    905 
    906             sprintf(tmp,
    907                     "%s %s %s %lld %s",
    908                     mountlist->el[items].device,
    909                     mountlist->el[items].mountpoint,
    910                     mountlist->el[items].format,
    911                     mountlist->el[items].size,
    912                     mountlist->el[items].label);
    913 
    914             log_it(tmp);
     874            paranoid_free(tmp);
     875
     876            log_it("%s %s %s %lld %s",
     877                     mountlist->el[items].device,
     878                     mountlist->el[items].mountpoint,
     879                     mountlist->el[items].format,
     880                     mountlist->el[items].size,
     881                     mountlist->el[items].label);
    915882            items++;
    916883        }
     
    921888
    922889    log_it("Mountlist loaded successfully.");
    923     sprintf(tmp, "%d entries in mountlist", items);
    924     log_it(tmp);
     890    log_it("%d entries in mountlist", items);
     891
    925892    paranoid_free(incoming);
    926893    paranoid_free(siz);
    927     paranoid_free(tmp);
    928894    return (0);
    929895}
     
    961927    return (0);
    962928}
    963 
    964929
    965930
     
    989954}
    990955
     956
    991957/**
    992958 * Sort the mountlist alphabetically by mountpoint.
     
    1028994{
    1029995    /*@ mallocs *** */
    1030     char device[64];
    1031     char mountpoint[256];
    1032     char format[64];
     996    char *device = NULL;
     997    char *mountpoint = NULL;
     998    char *format = NULL;
    1033999
    10341000    long long size;
     
    10381004    assert(b >= 0);
    10391005
    1040     strcpy(device, mountlist->el[a].device);
    1041     strcpy(mountpoint, mountlist->el[a].mountpoint);
    1042     strcpy(format, mountlist->el[a].format);
     1006    asprintf(&device, "%s", mountlist->el[a].device);
     1007    asprintf(&mountpoint, "%s", mountlist->el[a].mountpoint);
     1008    asprintf(&format, "%s", mountlist->el[a].format);
    10431009
    10441010    size = mountlist->el[a].size;
     
    10551021
    10561022    mountlist->el[b].size = size;
     1023    paranoid_free(device);
     1024    paranoid_free(mountpoint);
     1025    paranoid_free(format);
    10571026}
    10581027
  • branches/2.2.9/mondo/test/Makefile.am

    r2202 r2204  
    33## The program
    44sbin_PROGRAMS         = mrtest_mountlist
    5 mrtest_mountlist_SOURCES  = test-mountlist.c
     5mrtest_mountlist_SOURCES  = test-mountlist.c ${top_builddir}/src/mondorestore/mondo-prep.c ${top_builddir}/src/mondorestore/mondo-rstr-newt.c ${top_builddir}/src/mondorestore/mondo-rstr-tools.c
    66mrtest_mountlist_LDADD    = ${top_builddir}/src/common/libmondo.a @MONDORESTORE_STATIC@
  • branches/2.2.9/mondo/test/test-mountlist.c

    r2202 r2204  
    44#include "../common/libmondo.h"
    55#include "../mondorestore/mondorestore.h"
     6#include "../mondorestore/mr-externs.h"
    67
    78extern void twenty_seconds_til_yikes(void);
     
    3435
    3536extern char *MONDO_LOGFILE;
    36 
    37 void free_MR_global_filenames(void) {
    38 }
    39 
    4037extern int copy_from_src_to_dest(FILE * f_orig, FILE * f_archived,
    4138                                 char direction);
     
    4441
    4542    struct mountlist_itself *mountlist = NULL;
     43    struct raidlist_itself *raidlist;
     44
    4645    asprintf(&MONDO_LOGFILE,"/tmp/mrtest-mountlist.log");
    4746
     
    5150        fatal_error("Cannot malloc mountlist");
    5251    }
     52    if (!(raidlist = malloc(sizeof(struct raidlist_itself)))) {
     53        fatal_error("Cannot malloc raidlist");
     54    }
    5355    malloc_libmondo_global_strings();
    5456
     
    5658asprintf(&g_mountlist_fname, "/tmp/mountlist.txt");
    5759load_mountlist(mountlist, g_mountlist_fname);
    58 finish(0);
     60resize_mountlist_proportionately_to_suit_new_drives(mountlist);
     61load_raidtab_into_raidlist(raidlist, RAIDTAB_FNAME);
     62edit_mountlist(g_mountlist_fname, mountlist, raidlist);
     63exit(0);
    5964}
Note: See TracChangeset for help on using the changeset viewer.