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


Ignore:
Timestamp:
Dec 8, 2005, 5:20:29 PM (18 years ago)
Author:
bcornec
Message:

memory management continues:

  • mondoarchive handled completely
  • bkpinfo, begining of dyn. alloc.
  • lot of changes around memory everywhere

=> even if it compiles, i'm pretty sure it doesn't work yet (even not tried)

File:
1 edited

Legend:

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

    r149 r171  
    1 /* libmondo-files.c                                  file manipulation
    2    $Id$
    3 .
    4 
    5 
    6 04/16/04
    7 - find_home_of_exe() really does return NULL now if file not found
    8 
    9 03/22/04
    10 - added mode_of_file()
    11 
    12 10/02/03
    13 - cleaned up grab_percentage_from_last_line_of_file()
    14 
    15 09/18
    16 - added int make_grub_install_scriptlet()
    17 
    18 09/16
    19 - cleaned up mkisofs feedback window
    20 
    21 09/12
    22 - fixed Teuton-hostile bug in size_of_all_biggiefiles_K()
    23 
    24 09/05
    25 - added size_of_partition_in_mountlist_K()
    26 - size_of_all_biggiefiles_K() now calls get_phys_size_of_drive(fname)
    27 
    28 07/02
    29 - fixed calls to popup_and_get_string()
    30 
    31 05/19
    32 - added CP_BIN
    33 
    34 05/05
    35 - added Joshua Oreman's FreeBSD patches
    36 
    37 05/04
    38 - find_home_of_exe() now returns NULL if file not found
    39 
    40 04/26
    41 - if >4 media est'd, say one meeeellion
    42 
    43 04/25
    44 - fixed minor bug in find_home_of_exe()
    45 
    46 04/24
    47 - added lots of assert()'s and log_OS_error()'s
    48 
    49 04/07
    50 - fix find_home_of_exe()
    51 - cleaned up code a bit
    52 
    53 03/27
    54 - copy_mondo_and_mindi_stuff --- if _homedir_/payload.tgz exists then untar it to CD
    55 
    56 01/14/2003
    57 - if backup media type == nfs then don't estimate no. of media reqd
    58 
    59 11/25/2002
    60 - don't log/echo estimated # of media required if >=50
    61 
    62 10/01 - 11/09
    63 - chmod uses 0x, not decimal :)
    64 - added is_this_file_compressed()
    65 - replace convoluted grep with wc (KP)
    66 
    67 09/01 - 09/30
    68 - only show "number of media" estimate if no -x
    69 - run_program_and_log_output() now takes boolean operator to specify
    70   whether it will log its activities in the event of _success_
    71 
    72 08/01 - 08/31
    73 - handle unknown media sizes
    74 - cleaned up some log_it() calls
    75 
    76 07/24
    77 - created
     1/*  $Id$
     2 * file manipulation
    783*/
    794
     
    11742{
    11843    /*@ buffers ***************************************************** */
    119     static char output[MAX_STR_LEN];
    120     char command[MAX_STR_LEN * 2];
    121     char tmp[MAX_STR_LEN];
     44    static char *output = NULL;
     45    char *command;
     46    char *tmp;
     47    size_t n = 0;
    12248
    12349    /*@ pointers **************************************************** */
     
    13258
    13359    assert_string_is_neither_NULL_nor_zerolength(filename);
     60
    13461    if (does_file_exist(filename)) {
    135         sprintf(command, "md5sum \"%s\"", filename);
     62        asprintf(&command, "md5sum \"%s\"", filename);
    13663        fin = popen(command, "r");
     64        paranoid_free(command);
     65
    13766        if (fin) {
    138             (void) fgets(output, MAX_STR_LEN, fin);
     67            (void) getline(&output, &n, fin);
    13968            p = strchr(output, ' ');
    14069            paranoid_pclose(fin);
    14170        }
    14271    } else {
    143         sprintf(tmp, "File '%s' not found; cannot calc checksum",
     72        asprintf(&tmp, "File '%s' not found; cannot calc checksum",
    14473                filename);
    14574        log_it(tmp);
     75        paranoid_free(tmp);
    14676    }
    14777    if (p) {
     
    16393
    16494    /*@ buffers ***************************************************** */
    165     static char curr_cksum[1000];
     95    char *curr_cksum;
    16696
    16797    /*@ pointers **************************************************** */
     
    170100    struct stat buf;
    171101
    172     /*@ initialize data *************************************************** */
    173     curr_cksum[0] = '\0';
    174 
    175102    /*@************************************************************** */
    176103
    177104    assert_string_is_neither_NULL_nor_zerolength(curr_fname);
    178105    if (lstat(curr_fname, &buf)) {
    179         return (curr_cksum);    // empty
    180     }
    181 
    182     sprintf(curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size),
     106        asprintf(&curr_cksum, "");
     107    } else {
     108        asprintf(&curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size),
    183109            (long) (buf.st_mtime), (long) (buf.st_ctime));
     110    }
    184111    return (curr_cksum);
    185112}
    186 
    187113
    188114
     
    197123
    198124    /*@ buffers ***************************************************** */
    199     char command[MAX_STR_LEN * 2];
    200     char incoming[MAX_STR_LEN];
    201     char tmp[MAX_STR_LEN];
     125    char *command;
     126    char *incoming = NULL;
     127    char *tmp;
    202128
    203129    /*@ long ******************************************************** */
    204130    long noof_lines = -1L;
    205131
     132    /*@ int ******************************************************** */
     133    size_t n = 0;
     134
    206135    /*@ pointers **************************************************** */
    207136    FILE *fin;
    208137
    209     /*@ initialize [0] to null ******************************************** */
    210     incoming[0] = '\0';
    211 
    212138    assert_string_is_neither_NULL_nor_zerolength(filename);
    213139    if (!does_file_exist(filename)) {
    214         sprintf(tmp,
     140        asprintf(&tmp,
    215141                "%s does not exist, so I cannot found the number of lines in it",
    216142                filename);
    217143        log_it(tmp);
     144        paranoid_free(tmp);
    218145        return (0);
    219146    }
    220     sprintf(command, "cat %s | wc -l", filename);
     147    asprintf(&command, "cat %s | wc -l", filename);
    221148    if (!does_file_exist(filename)) {
    222149        return (-1);
    223150    }
    224151    fin = popen(command, "r");
     152    paranoid_free(command);
     153
    225154    if (fin) {
    226155        if (feof(fin)) {
    227156            noof_lines = 0;
    228157        } else {
    229             (void) fgets(incoming, MAX_STR_LEN - 1, fin);
     158            (void) getline(&incoming, &n, fin);
    230159            while (strlen(incoming) > 0
    231160                   && incoming[strlen(incoming) - 1] < 32) {
     
    233162            }
    234163            noof_lines = atol(incoming);
     164            paranoid_free(incoming);
    235165        }
    236166        paranoid_pclose(fin);
     
    254184
    255185    assert(filename != NULL);
    256     //  assert_string_is_neither_NULL_nor_zerolength(filename);
     186
    257187    if (lstat(filename, &buf)) {
    258188        log_msg(20, "%s does not exist", filename);
     
    265195
    266196
    267 
    268 
    269 
    270 
    271197/**
    272198 * Modify @p inout (a file containing a list of files) to only contain files
     
    278204void exclude_nonexistent_files(char *inout)
    279205{
    280     char infname[MAX_STR_LEN];
    281     char outfname[MAX_STR_LEN];
    282     char tmp[MAX_STR_LEN];
    283     char incoming[MAX_STR_LEN];
     206    char *infname;
     207    char *outfname;
     208    char *tmp;
     209    char *incoming = NULL;
    284210
    285211    /*@ int ********************************************************* */
    286212    int i;
     213    size_t n = 0;
    287214
    288215    /*@ pointers **************************************************** */
     
    293220
    294221    assert_string_is_neither_NULL_nor_zerolength(inout);
    295     sprintf(infname, "%s.in", inout);
    296     sprintf(outfname, "%s", inout);
    297     sprintf(tmp, "cp -f %s %s", inout, infname);
     222
     223    asprintf(&infname, "%s.in", inout);
     224
     225    asprintf(&tmp, "cp -f %s %s", inout, infname);
    298226    run_program_and_log_output(tmp, FALSE);
     227    paranoid_free(tmp);
     228
    299229    if (!(fin = fopen(infname, "r"))) {
    300230        log_OS_error("Unable to openin infname");
     231        paranoid_free(infname);
    301232        return;
    302233    }
     234
     235    asprintf(&outfname, "%s", inout);
    303236    if (!(fout = fopen(outfname, "w"))) {
    304237        log_OS_error("Unable to openout outfname");
     238        paranoid_free(outfname);
    305239        return;
    306240    }
    307     for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
    308          fgets(incoming, MAX_STR_LEN, fin)) {
     241    paranoid_free(outfname);
     242
     243    for (getline(&incoming, &n, fin); !feof(fin);
     244         getline(&incoming, &n, fin)) {
    309245        i = strlen(incoming) - 1;
    310246        if (i >= 0 && incoming[i] < 32) {
     
    314250            fprintf(fout, "%s\n", incoming);
    315251        } else {
    316             sprintf(tmp, "Excluding '%s'-nonexistent\n", incoming);
     252            asprintf(&tmp, "Excluding '%s'-nonexistent\n", incoming);
    317253            log_it(tmp);
    318         }
    319     }
     254            paranoid_free(tmp);
     255        }
     256    }
     257    paranoid_free(incoming);
    320258    paranoid_fclose(fout);
    321259    paranoid_fclose(fin);
    322260    unlink(infname);
    323 }
    324 
    325 
    326 
    327 
    328 
    329 
    330 
     261    paranoid_free(infname);
     262}
    331263
    332264
     
    340272int figure_out_kernel_path_interactively_if_necessary(char *kernel)
    341273{
    342     char tmp[MAX_STR_LEN];
     274    char *tmp;
    343275
    344276    if (!kernel[0]) {
     
    360292                ("Kernel not found. Please specify with the '-k' flag.");
    361293        }
    362         sprintf(tmp, "User says kernel is at %s", kernel);
     294        asprintf(&tmp, "User says kernel is at %s", kernel);
    363295        log_it(tmp);
     296        paranoid_free(tmp);
    364297    }
    365298    return (0);
    366299}
    367 
    368 
    369 
    370 
    371300
    372301
     
    387316    char *command;
    388317
    389     malloc_string(incoming);
    390     malloc_string(command);
    391     incoming[0] = '\0';
    392318    /*@******************************* */
    393319
    394320    assert_string_is_neither_NULL_nor_zerolength(fname);
    395     sprintf(command, "which %s 2> /dev/null", fname);
    396     strcpy(incoming, call_program_and_get_last_line_of_output(command));
     321    asprintf(&command, "which %s 2> /dev/null", fname);
     322    asprintf(&incoming, call_program_and_get_last_line_of_output(command));
     323    paranoid_free(command);
     324
    397325    if (incoming[0] == '\0') {
    398326        if (system("which file > /dev/null 2> /dev/null")) {
    399327            paranoid_free(incoming);
    400             paranoid_free(command);
    401             output[0] = '\0';
    402328            return (NULL);      // forget it :)
    403329        }
    404         sprintf(command,
     330        asprintf(&command,
    405331                "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null",
    406332                incoming);
    407         strcpy(incoming,
     333        paranoid_free(incoming);
     334
     335        asprintf(&incoming,
    408336               call_program_and_get_last_line_of_output(command));
     337        paranoid_free(command);
    409338    }
    410339    if (incoming[0] == '\0')    // yes, it is == '\0' twice, not once :)
    411340    {
    412         sprintf(command, "dirname %s 2> /dev/null", incoming);
    413         strcpy(incoming,
     341        asprintf(&command, "dirname %s 2> /dev/null", incoming);
     342        paranoid_free(incoming);
     343
     344        asprintf(&incoming,
    414345               call_program_and_get_last_line_of_output(command));
     346        paranoid_free(command);
    415347    }
    416348    strcpy(output, incoming);
     349    paranoid_free(incoming);
     350
    417351    if (output[0] != '\0' && does_file_exist(output)) {
    418352        log_msg(4, "find_home_of_exe () --- Found %s at %s", fname,
    419                 incoming);
     353                output);
    420354    } else {
    421355        output[0] = '\0';
    422356        log_msg(4, "find_home_of_exe() --- Could not find %s", fname);
    423357    }
    424     paranoid_free(incoming);
    425     paranoid_free(command);
    426358    if (!output[0]) {
    427359        return (NULL);
     
    432364
    433365
    434 
    435 
    436 
    437 
    438 
    439 
    440366/**
    441367 * Get the last sequence of digits surrounded by non-digits in the first 32k of
     
    458384
    459385    assert_string_is_neither_NULL_nor_zerolength(logfile);
     386
    460387    if (!(fin = fopen(logfile, "r"))) {
    461388        log_OS_error("Unable to open logfile");
     
    471398    for (; len > 0 && isdigit(datablock[len - 1]); len--);
    472399    trackno = atoi(datablock + len);
    473     /*
    474        sprintf(tmp,"datablock=%s; trackno=%d",datablock+len, trackno);
    475        log_it(tmp);
    476      */
    477400    return (trackno);
    478401}
    479 
    480 
    481 
    482 
    483 
    484402
    485403
     
    494412
    495413    /*@ buffers ***************************************************** */
    496     char tmp[MAX_STR_LEN];
    497     char lastline[MAX_STR_LEN];
    498     char command[MAX_STR_LEN];
     414    char *lastline;
     415    char *command;
    499416    /*@ pointers **************************************************** */
    500417    char *p;
     
    507424         && !strstr(err_log_lines[i], "% done"); i--);
    508425    if (i < 0) {
    509         sprintf(command,
     426        asprintf(&command,
    510427                "tail -n3 %s | fgrep -i \"%c\" | tail -n1 | awk '{print $0;}'",
    511428                filename, '%');
    512         strcpy(lastline,
     429        asprintf(&lastline,
    513430               call_program_and_get_last_line_of_output(command));
     431        paranoid_free(command);
    514432        if (!lastline[0]) {
     433            paranoid_free(lastline);
    515434            return (0);
    516435        }
    517436    } else {
    518         strcpy(lastline, err_log_lines[i]);
     437        asprintf(&lastline, err_log_lines[i]);
    519438    }
    520439
     
    523442        *p = '\0';
    524443    }
    525 //  log_msg(2, "lastline='%s', ", p, lastline);
    526444    if (!p) {
     445        paranoid_free(lastline);
    527446        return (0);
    528447    }
     
    533452    }
    534453    i = atoi(p);
    535 
    536     sprintf(tmp, "'%s' --> %d", p, i);
    537 //     log_to_screen(tmp);
     454    paranoid_free(lastline);
    538455
    539456    return (i);
    540457}
    541 
    542 
    543 
    544458
    545459
     
    554468    /*@ buffers ***************************************************** */
    555469    static char output[MAX_STR_LEN];
    556     static char command[MAX_STR_LEN * 2];
    557     static char tmp[MAX_STR_LEN];
     470    static char *command;
     471    static char *tmp;
    558472
    559473    /*@ pointers **************************************************** */
     
    563477
    564478    if (!does_file_exist(filename)) {
    565         sprintf(tmp, "Tring to get last line of nonexistent file (%s)",
     479        asprintf(&tmp, "Tring to get last line of nonexistent file (%s)",
    566480                filename);
    567481        log_it(tmp);
     482        paranoid_free(tmp);
     483
    568484        output[0] = '\0';
    569485        return (output);
    570486    }
    571     sprintf(command, "tail -n1 %s", filename);
     487    asprintf(&command, "tail -n1 %s", filename);
    572488    fin = popen(command, "r");
     489    paranoid_free(command);
     490
    573491    (void) fgets(output, MAX_STR_LEN, fin);
    574492    paranoid_pclose(fin);
     
    578496    return (output);
    579497}
     498
    580499
    581500/**
     
    605524
    606525
    607 
    608 /**
    609  * ?????
    610  * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed.
    611  */
    612 int
    613 make_checksum_list_file(char *filelist, char *cksumlist, char *comppath)
    614 {
    615     /*@ pointers **************************************************** */
    616     FILE *fin;
    617     FILE *fout;
    618 
    619     /*@ int   ******************************************************* */
    620     int percentage;
    621     int i;
    622     int counter = 0;
    623 
    624     /*@ buffer ****************************************************** */
    625     char stub_fname[1000];
    626     char curr_fname[1000];
    627     char curr_cksum[1000];
    628     char tmp[1000];
    629 
    630     /*@ long [long] ************************************************* */
    631     long long filelist_length;
    632     long curr_pos;
    633     long start_time;
    634     long current_time;
    635     long time_taken;
    636     long time_remaining;
    637 
    638     /*@ end vars *************************************************** */
    639 
    640     start_time = get_time();
    641     filelist_length = length_of_file(filelist);
    642     sprintf(tmp, "filelist = %s; cksumlist = %s", filelist, cksumlist);
    643     log_it(tmp);
    644     fin = fopen(filelist, "r");
    645     if (fin == NULL) {
    646         log_OS_error("Unable to fopen-in filelist");
    647         log_to_screen("Can't open filelist");
    648         return (1);
    649     }
    650     fout = fopen(cksumlist, "w");
    651     if (fout == NULL) {
    652         log_OS_error("Unable to openout cksumlist");
    653         paranoid_fclose(fin);
    654         log_to_screen("Can't open checksum list");
    655         return (1);
    656     }
    657     for (fgets(stub_fname, 999, fin); !feof(fin);
    658          fgets(stub_fname, 999, fin)) {
    659         if (stub_fname[(i = strlen(stub_fname) - 1)] < 32) {
    660             stub_fname[i] = '\0';
    661         }
    662         sprintf(tmp, "%s%s", comppath, stub_fname);
    663         strcpy(curr_fname, tmp + 1);
    664         strcpy(curr_cksum, calc_file_ugly_minichecksum(curr_fname));
    665         fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);
    666         if (counter++ > 12) {
    667             current_time = get_time();
    668             counter = 0;
    669             curr_fname[37] = '\0';
    670             curr_pos = ftell(fin) / 1024;
    671             percentage = (int) (curr_pos * 100 / filelist_length);
    672             time_taken = current_time - start_time;
    673             if (percentage == 0) {
    674                 /*              printf("%0d%% done      \r",percentage); */
    675             } else {
    676                 time_remaining =
    677                     time_taken * 100 / (long) (percentage) - time_taken;
    678                 sprintf(tmp,
    679                         "%02d%% done   %02d:%02d taken   %02d:%02d remaining  %-37s\r",
    680                         percentage, (int) (time_taken / 60),
    681                         (int) (time_taken % 60),
    682                         (int) (time_remaining / 60),
    683                         (int) (time_remaining % 60), curr_fname);
    684                 log_to_screen(tmp);
    685             }
    686             sync();
    687         }
    688     }
    689     paranoid_fclose(fout);
    690     paranoid_fclose(fin);
    691     log_it("Done.");
    692     return (0);
    693 }
    694 
    695 
    696526/**
    697527 * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>.
     
    702532int make_hole_for_dir(char *outdir_fname)
    703533{
    704     char tmp[MAX_STR_LEN * 2];
     534    char *tmp;
    705535    int res = 0;
    706536
    707537    assert_string_is_neither_NULL_nor_zerolength(outdir_fname);
    708     sprintf(tmp, "mkdir -p %s", outdir_fname);
     538    asprintf(&tmp, "mkdir -p %s", outdir_fname);
    709539    res = system(tmp);
     540    paranoid_free(tmp);
    710541    return (res);
    711542}
     
    722553{
    723554    /*@ buffer ****************************************************** */
    724     char command[MAX_STR_LEN * 2];
     555    char *command;
    725556
    726557    /*@ int  ******************************************************** */
     
    732563    assert(!strstr(outfile_fname, MNT_CDROM));
    733564    assert(!strstr(outfile_fname, "/dev/cdrom"));
    734     sprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
     565
     566    asprintf(&command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
    735567    res += system(command);
    736     sprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
     568    paranoid_free(command);
     569
     570    asprintf(&command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
    737571    res += system(command);
    738     sprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
     572    paranoid_free(command);
     573
     574    asprintf(&command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
    739575    res += system(command);
     576    paranoid_free(command);
    740577    unlink(outfile_fname);
    741578    return (0);
    742579}
    743 
    744 
    745580
    746581
     
    760595
    761596    /*@ buffers **************************************************** */
    762     char incoming[MAX_STR_LEN];
    763 
     597    char *incoming = NULL;
     598
     599    size_t n = 0;
    764600    /*@ end vars *************************************************** */
    765601
     
    771607        return (0);
    772608    }
    773     (void) fgets(incoming, MAX_STR_LEN - 1, fin);
     609    (void) getline(&incoming, &n, fin);
    774610    while (!feof(fin)) {
    775611        if (strstr(incoming, wildcard)) {
    776612            matches++;
    777613        }
    778         (void) fgets(incoming, MAX_STR_LEN - 1, fin);
     614        (void) getline(&incoming, &n, fin);
    779615    }
    780616    paranoid_fclose(fin);
     617    paranoid_free(incoming);
    781618    return (matches);
    782619}
    783 
    784 
    785620
    786621
     
    794629void register_pid(pid_t pid, char *name_str)
    795630{
    796     char tmp[MAX_STR_LEN + 1], lockfile_fname[MAX_STR_LEN + 1];
     631    char *tmp;
     632    char *lockfile_fname;
    797633    int res;
     634    size_t n = 0;
    798635    FILE *fin;
    799636
    800     sprintf(lockfile_fname, "/var/run/monitas-%s.pid", name_str);
     637    asprintf(&lockfile_fname, "/var/run/monitas-%s.pid", name_str);
    801638    if (!pid) {
    802639        log_it("Unregistering PID");
     
    804641            log_it("Error unregistering PID");
    805642        }
     643        paranoid_free(lockfile_fname);
    806644        return;
    807645    }
    808646    if (does_file_exist(lockfile_fname)) {
    809         tmp[0] = '\0';
    810647        if ((fin = fopen(lockfile_fname, "r"))) {
    811             (void) fgets(tmp, MAX_STR_LEN, fin);
     648            (void) getline(&tmp, &n, fin);
    812649            paranoid_fclose(fin);
    813650        } else {
     
    815652        }
    816653        pid = (pid_t) atol(tmp);
    817         sprintf(tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid);
     654        paranoid_free(tmp);
     655
     656        asprintf(&tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid);
    818657        res = system(tmp);
     658        paranoid_free(tmp);
    819659        if (!res) {
    820660            log_it
     
    823663        }
    824664    }
    825     sprintf(tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(),
     665    asprintf(&tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(),
    826666            lockfile_fname);
     667    paranoid_free(lockfile_fname);
     668
    827669    if (system(tmp)) {
    828670        fatal_error("Cannot register PID");
    829671    }
    830 }
    831 
     672    paranoid_free(tmp);
     673    return;
     674}
    832675
    833676
     
    840683long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
    841684{
    842     char command[MAX_STR_LEN];
    843     char mountlist[MAX_STR_LEN];
    844     char sz_res[MAX_STR_LEN];
     685    char *command;
     686    char *sz_res;
    845687    long file_len_K;
    846688
    847     sprintf(mountlist, "%s/mountlist.txt", tmpdir);
    848     sprintf(command,
    849             "cat %s/mountlist.txt | grep \"%s \" | head -n1 | awk '{print $4;}'",
    850             tmpdir, dev);
     689    asprintf(&command,
     690            "grep '%s ' %s/mountlist.txt | head -n1 | awk '{print $4;}'",
     691            dev, tmpdir);
    851692    log_it(command);
    852     strcpy(sz_res, call_program_and_get_last_line_of_output(command));
     693    asprintf(&sz_res, call_program_and_get_last_line_of_output(command));
    853694    file_len_K = atol(sz_res);
    854695    log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
     696    paranoid_free(command);
     697    paranoid_free(sz_res);
    855698    return (file_len_K);
    856699}
     700
    857701
    858702/**
     
    874718    /*@ pointers *************************************************** */
    875719    FILE *fin = NULL;
     720    size_t n = 0;
    876721
    877722    /*@ end vars *************************************************** */
    878723
    879     malloc_string(fname);
    880     malloc_string(biggielist);
    881     malloc_string(comment);
    882724    log_it("Calculating size of all biggiefiles (in total)");
    883     sprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
     725    asprintf(&biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
    884726    log_it("biggielist = %s", biggielist);
    885727    if (!(fin = fopen(biggielist, "r"))) {
     
    888730    } else {
    889731        log_msg(4, "Reading it...");
    890         for (fgets(fname, MAX_STR_LEN, fin); !feof(fin);
    891              fgets(fname, MAX_STR_LEN, fin)) {
     732        for (getline(&fname, &n, fin); !feof(fin);
     733             getline(&fname, &n, fin)) {
    892734            if (fname[strlen(fname) - 1] <= 32) {
    893735                fname[strlen(fname) - 1] = '\0';
     
    902744                log_msg(4, "%s --> %ld K", fname, file_len_K);
    903745            }
    904             sprintf(comment,
     746            asprintf(&comment,
    905747                    "After adding %s, scratchL+%ld now equals %ld", fname,
    906748                    file_len_K, scratchL);
    907749            log_msg(4, comment);
     750            paranoid_free(comment);
     751
    908752            if (feof(fin)) {
    909753                break;
    910754            }
    911755        }
    912     }
     756        paranoid_free(fname);
     757    }
     758    paranoid_free(biggielist);
     759
    913760    log_it("Closing...");
    914761    paranoid_fclose(fin);
    915762    log_it("Finished calculating total size of all biggiefiles");
    916     paranoid_free(fname);
    917     paranoid_free(biggielist);
    918     paranoid_free(comment);
    919763    return (scratchL);
    920764}
     765
    921766
    922767/**
     
    929774{
    930775    /*@ buffer ****************************************************** */
    931     char tmp[MAX_STR_LEN];
    932     char command[MAX_STR_LEN * 2];
     776    char *tmp = NULL;
     777    char *command;
    933778    long long llres;
     779    size_t n = 0;
    934780    /*@ pointers **************************************************** */
    935781    char *p;
     
    938784    /*@ end vars *************************************************** */
    939785
    940     sprintf(command, "du -sk %s", mountpt);
     786    asprintf(&command, "du -sk %s", mountpt);
    941787    fin = popen(command, "r");
    942     (void) fgets(tmp, MAX_STR_LEN, fin);
     788    paranoid_free(command);
     789
     790    (void) getline(&tmp, &n, fin);
    943791    paranoid_pclose(fin);
    944792    p = strchr(tmp, '\t');
     
    950798        llres += (int) (*p - '0');
    951799    }
     800    paranoid_free(tmp);
    952801    return (llres);
    953802}
     
    969818}
    970819
     820
    971821/**
    972822 * Update a reverse CRC checksum to include another character.
     
    985835
    986836
    987 
    988 
    989837/**
    990838 * Check for an executable on the user's system; write a message to the
     
    996844{
    997845    /*@ buffers *** */
    998     char command[MAX_STR_LEN * 2];
    999     char errorstr[MAX_STR_LEN];
    1000 
    1001 
    1002     sprintf(command, "which %s > /dev/null 2> /dev/null", fname);
    1003     sprintf(errorstr,
     846    char *command;
     847    char *errorstr;
     848    int res = 0;
     849
     850
     851    asprintf(&command, "which %s > /dev/null 2> /dev/null", fname);
     852    res = system(command);
     853    paranoid_free(command);
     854
     855    if (res) {
     856        asprintf(&errorstr,
    1004857            "Please install '%s'. I cannot find it on your system.",
    1005858            fname);
    1006     if (system(command)) {
    1007859        log_to_screen(errorstr);
     860        paranoid_free(errorstr);
    1008861        log_to_screen
    1009             ("There may be hyperlink at http://www.mondorescue.com which");
     862            ("There may be an hyperlink at http://www.mondorescue.org which");
    1010863        log_to_screen("will take you to the relevant (missing) package.");
    1011864        return (1);
     
    1014867    }
    1015868}
    1016 
    1017 
    1018 
    1019 
    1020869
    1021870
     
    1050899
    1051900
    1052 
    1053901/**
    1054902 * Read @p fname into @p contents.
     
    1083931    return (res);
    1084932}
    1085 
    1086 
    1087 
    1088 
    1089 
    1090 
    1091 
    1092933
    1093934
     
    1105946{
    1106947    /*@ Char buffers ** */
    1107     char command[MAX_STR_LEN * 2];
    1108     char tmp[MAX_STR_LEN];
     948    char *command;
     949    char *tmp;
    1109950    char old_pwd[MAX_STR_LEN];
    1110951
     
    1118959        g_mondo_home = find_and_store_mondoarchives_home();
    1119960    }
    1120     sprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,
     961    asprintf(&command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,
    1121962            bkpinfo->scratchdir);
    1122963
     
    1125966        fatal_error("Failed to copy Mondo's stuff to scratchdir");
    1126967    }
    1127 
    1128     sprintf(tmp, "%s/payload.tgz", g_mondo_home);
     968    paranoid_free(command);
     969
     970    asprintf(&tmp, "%s/payload.tgz", g_mondo_home);
    1129971    if (does_file_exist(tmp)) {
    1130972        log_it("Untarring payload %s to scratchdir %s", tmp,
     
    1132974        (void) getcwd(old_pwd, MAX_STR_LEN - 1);
    1133975        chdir(bkpinfo->scratchdir);
    1134         sprintf(command, "tar -zxvf %s", tmp);
     976        asprintf(&command, "tar -zxvf %s", tmp);
    1135977        if (run_program_and_log_output(command, FALSE)) {
    1136978            fatal_error("Failed to untar payload");
    1137979        }
     980        paranoid_free(command);
    1138981        chdir(old_pwd);
    1139982    }
    1140 
    1141     sprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir,
     983    paranoid_free(tmp);
     984
     985    asprintf(&command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir,
    1142986            bkpinfo->scratchdir);
    1143 
    1144987    if (run_program_and_log_output(command, FALSE)) {
    1145988        fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir");
    1146989    }
    1147 
    1148     strcpy(tmp,
     990    paranoid_free(command);
     991
     992    asprintf(&tmp,
    1149993           call_program_and_get_last_line_of_output("which mondorestore"));
    1150994    if (!tmp[0]) {
     
    1152996            ("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?");
    1153997    }
    1154     sprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir);
     998    asprintf(&command, "cp -f %s %s", tmp, bkpinfo->tmpdir);
     999    paranoid_free(tmp);
     1000
    11551001    if (run_program_and_log_output(command, FALSE)) {
    11561002        fatal_error("Failed to copy mondorestore to tmpdir");
    11571003    }
    1158 
    1159     sprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
     1004    paranoid_free(command);
     1005
     1006    asprintf(&command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
    11601007    paranoid_system(command);
     1008    paranoid_free(command);
    11611009
    11621010    if (bkpinfo->postnuke_tarball[0]) {
    1163         sprintf(command, "cp -f %s %s/post-nuke.tgz",
     1011        asprintf(&command, "cp -f %s %s/post-nuke.tgz",
    11641012                bkpinfo->postnuke_tarball, bkpinfo->tmpdir);
    11651013        if (run_program_and_log_output(command, FALSE)) {
    11661014            fatal_error("Unable to copy post-nuke tarball to tmpdir");
    11671015        }
    1168     }
    1169 
     1016        paranoid_free(command);
     1017    }
    11701018
    11711019    mvaddstr_and_log_it(g_currentY++, 74, "Done.");
    11721020}
    1173 
    1174 
    1175 
    11761021
    11771022
     
    11881033
    11891034    /*@ buffers ******** */
    1190     char outfile[MAX_STR_LEN];
    1191     char nfs_dev[MAX_STR_LEN];
    1192     char nfs_mount[MAX_STR_LEN];
    1193     char nfs_client_ipaddr[MAX_STR_LEN];
    1194     char nfs_client_netmask[MAX_STR_LEN];
    1195     char nfs_client_defgw[MAX_STR_LEN];
    1196     char nfs_server_ipaddr[MAX_STR_LEN];
    1197     char tmp[MAX_STR_LEN];
    1198     char command[MAX_STR_LEN * 2];
     1035    char *outfile;
     1036    char *nfs_dev;
     1037    char *nfs_mount;
     1038    char *nfs_client_ipaddr;
     1039    char *nfs_client_netmask;
     1040    char *nfs_client_defgw;
     1041    char *nfs_server_ipaddr;
     1042    char *tmp;
     1043    char *command;
    11991044
    12001045    /*@ pointers ***** */
     
    12051050
    12061051    log_it("Storing NFS configuration");
    1207     strcpy(tmp, bkpinfo->nfs_mount);
     1052    asprintf(&tmp, bkpinfo->nfs_mount);
    12081053    p = strchr(tmp, ':');
    12091054    if (!p) {
     
    12121057    }
    12131058    *(p++) = '\0';
    1214     strcpy(nfs_server_ipaddr, tmp);
    1215     strcpy(nfs_mount, p);
    1216     sprintf(command,
     1059    asprintf(&nfs_server_ipaddr, tmp);
     1060    paranoid_free(tmp);
     1061
     1062    asprintf(&nfs_mount, p);
     1063    asprintf(&command,
    12171064            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1");
    1218     strcpy(nfs_dev, call_program_and_get_last_line_of_output(command));
    1219     sprintf(command,
     1065    asprintf(&nfs_dev, call_program_and_get_last_line_of_output(command));
     1066    paranoid_free(command);
     1067
     1068    asprintf(&command,
    12201069            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2");
    1221     strcpy(nfs_client_ipaddr,
     1070    asprintf(&nfs_client_ipaddr,
    12221071           call_program_and_get_last_line_of_output(command));
    1223     sprintf(command,
     1072    paranoid_free(command);
     1073
     1074    asprintf(&command,
    12241075            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2");
    1225     strcpy(nfs_client_netmask,
     1076    asprintf(&nfs_client_netmask,
    12261077           call_program_and_get_last_line_of_output(command));
    1227     sprintf(command,
     1078    paranoid_free(command);
     1079
     1080    asprintf(&command,
    12281081            "route | egrep '^default' | awk '{printf $2}'");
    1229     strcpy(nfs_client_defgw,
     1082    asprintf(&nfs_client_defgw,
    12301083           call_program_and_get_last_line_of_output(command));
    1231     sprintf(tmp,
    1232             "nfs_client_ipaddr=%s; nfs_server_ipaddr=%s; nfs_mount=%s",
    1233             nfs_client_ipaddr, nfs_server_ipaddr, nfs_mount);
     1084    paranoid_free(command);
     1085
     1086    asprintf(&tmp,
     1087            "nfs_client_ipaddr=%s; nfs_client_netmask=%s; nfs_server_ipaddr=%s; nfs_mount=%s; nfs_client_defgw=%s;  ",
     1088            nfs_client_ipaddr, nfs_client_netmask, nfs_server_ipaddr, nfs_mount, nfs_client_defgw);
     1089    paranoid_free(nfs_mount);
     1090    log_it(tmp);
     1091    paranoid_free(tmp);
     1092
    12341093    if (strlen(nfs_dev) < 2) {
    12351094        fatal_error
    12361095            ("Unable to find ethN (eth0, eth1, ...) adapter via NFS mount you specified.");
    12371096    }
    1238     sprintf(outfile, "%s/start-nfs", bkpinfo->tmpdir);
    1239     sprintf(tmp, "outfile = %s", outfile);
     1097    asprintf(&outfile, "%s/start-nfs", bkpinfo->tmpdir);
     1098    asprintf(&tmp, "outfile = %s", outfile);
    12401099    log_it(tmp);
     1100    paranoid_free(tmp);
     1101
    12411102    if (!(fout = fopen(outfile, "w"))) {
    12421103        fatal_error("Cannot store NFS config");
     
    12561117//  paranoid_system ("mkdir -p /var/cache/mondo-archive 2> /dev/null");
    12571118
    1258     sprintf(tmp, "cp -f %s /var/cache/mondo-archive", outfile);
     1119    asprintf(&tmp, "cp -f %s /var/cache/mondo-archive", outfile);
     1120    paranoid_free(outfile);
     1121
    12591122    run_program_and_log_output(tmp, FALSE);
    1260 
    1261     sprintf(tmp, "%s/NFS-DEV", bkpinfo->tmpdir);
     1123    paranoid_free(tmp);
     1124
     1125    asprintf(&tmp, "%s/NFS-DEV", bkpinfo->tmpdir);
    12621126    write_one_liner_data_file(tmp, nfs_dev);
    1263 
    1264     sprintf(tmp, "%s/NFS-CLIENT-IPADDR", bkpinfo->tmpdir);
     1127    paranoid_free(nfs_dev);
     1128    paranoid_free(tmp);
     1129
     1130    asprintf(&tmp, "%s/NFS-CLIENT-IPADDR", bkpinfo->tmpdir);
    12651131    write_one_liner_data_file(tmp, nfs_client_ipaddr);
    1266     sprintf(tmp, "%s/NFS-CLIENT-NETMASK", bkpinfo->tmpdir);
     1132    paranoid_free(nfs_client_ipaddr);
     1133    paranoid_free(tmp);
     1134
     1135    asprintf(&tmp, "%s/NFS-CLIENT-NETMASK", bkpinfo->tmpdir);
    12671136    write_one_liner_data_file(tmp, nfs_client_netmask);
    1268     sprintf(tmp, "%s/NFS-CLIENT-DEFGW", bkpinfo->tmpdir);
     1137    paranoid_free(nfs_client_netmask);
     1138    paranoid_free(tmp);
     1139
     1140    asprintf(&tmp, "%s/NFS-CLIENT-DEFGW", bkpinfo->tmpdir);
    12691141    write_one_liner_data_file(tmp, nfs_client_defgw);
    1270     sprintf(tmp, "%s/NFS-SERVER-IPADDR", bkpinfo->tmpdir);
     1142    paranoid_free(nfs_client_defgw);
     1143    paranoid_free(tmp);
     1144
     1145    asprintf(&tmp, "%s/NFS-SERVER-IPADDR", bkpinfo->tmpdir);
    12711146    write_one_liner_data_file(tmp, nfs_server_ipaddr);
    1272     sprintf(tmp, "%s/NFS-SERVER-MOUNT", bkpinfo->tmpdir);
     1147    paranoid_free(nfs_server_ipaddr);
     1148    paranoid_free(tmp);
     1149
     1150    asprintf(&tmp, "%s/NFS-SERVER-MOUNT", bkpinfo->tmpdir);
    12731151    write_one_liner_data_file(tmp, bkpinfo->nfs_mount);
    1274     sprintf(tmp, "%s/NFS-SERVER-PATH", bkpinfo->tmpdir);
     1152    paranoid_free(tmp);
     1153
     1154    asprintf(&tmp, "%s/NFS-SERVER-PATH", bkpinfo->tmpdir);
    12751155    write_one_liner_data_file(tmp, bkpinfo->nfs_remote_dir);
    1276     sprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
     1156    paranoid_free(tmp);
     1157
     1158    asprintf(&tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
    12771159    write_one_liner_data_file(tmp, bkpinfo->prefix);
     1160    paranoid_free(tmp);
     1161
    12781162    log_it("Finished storing NFS configuration");
    12791163}
    1280 
    1281 
    1282 
    1283 
    12841164
    12851165
     
    13041184{
    13051185    /*@ buffers *************** */
    1306     char tmp[MAX_STR_LEN];
     1186    char *tmp;
    13071187
    13081188    /*@ long long ************* */
     
    13291209    }
    13301210    if (scratchLL <= 1) {
    1331         sprintf(tmp,
     1211        asprintf(&tmp,
    13321212                "Your backup will probably occupy a single %s. Maybe two.",
    13331213                media_descriptor_string(bkpinfo->backup_media_type));
    13341214    } else {
    1335         sprintf(tmp, "Your backup will occupy approximately %s media.",
     1215        asprintf(&tmp, "Your backup will occupy approximately %s media.",
    13361216                number_to_text((int) (scratchLL + 1)));
    13371217    }
     
    13391219        log_to_screen(tmp);
    13401220    }
    1341 }
    1342 
    1343 
    1344 /**
    1345  * Get the last suffix of @p instr.
    1346  * If @p instr was "httpd.log.gz", we would return "gz".
    1347  * @param instr The filename to get the suffix of.
    1348  * @return The suffix (without a dot), or "" if none.
    1349  * @note The returned string points to static storage that will be overwritten with each call.
    1350  */
    1351 char *sz_last_suffix(char *instr)
    1352 {
    1353     static char outstr[MAX_STR_LEN];
    1354     char *p;
    1355 
    1356     p = strrchr(instr, '.');
    1357     if (!p) {
    1358         outstr[0] = '\0';
    1359     } else {
    1360         strcpy(outstr, p);
    1361     }
    1362     return (outstr);
     1221    paranoid_free(tmp);
     1222    return;
    13631223}
    13641224
     
    13721232bool is_this_file_compressed(char *filename)
    13731233{
    1374     char do_not_compress_these[MAX_STR_LEN];
    1375     char tmp[MAX_STR_LEN];
     1234    char *do_not_compress_these;
     1235    char *tmp;
    13761236    char *p;
    13771237
    1378     sprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
     1238    asprintf(&tmp, "%s/do-not-compress-these", g_mondo_home);
    13791239    if (!does_file_exist(tmp)) {
     1240        paranoid_free(tmp);
    13801241        return (FALSE);
    13811242    }
    1382     strcpy(do_not_compress_these, last_line_of_file(tmp));
     1243    paranoid_free(tmp);
     1244
     1245    asprintf(&do_not_compress_these, last_line_of_file(tmp));
    13831246    for (p = do_not_compress_these; p != NULL; p++) {
    1384         strcpy(tmp, p);
     1247        asprintf(&tmp, p);
    13851248        if (strchr(tmp, ' ')) {
    13861249            *(strchr(tmp, ' ')) = '\0';
    13871250        }
    1388         if (!strcmp(sz_last_suffix(filename), tmp)) {   /*printf("MATCH\n"); */
     1251        if (!strcmp(strrchr(filename, '.'), tmp)) {
     1252            paranoid_free(do_not_compress_these);
     1253            paranoid_free(tmp);
    13891254            return (TRUE);
    13901255        }
     1256        paranoid_free(tmp);
     1257
    13911258        if (!(p = strchr(p, ' '))) {
    13921259            break;
    13931260        }
    13941261    }
     1262    paranoid_free(do_not_compress_these);
    13951263    return (FALSE);
    13961264}
    1397 
    13981265
    13991266
     
    14111278
    14121279
    1413 
    1414 
    14151280/**
    14161281 * Create a small script that mounts /boot, calls @c grub-install, and syncs the disks.
     
    14241289    int retval = 0;
    14251290
    1426     malloc_string(tmp);
    14271291    if ((fout = fopen(outfile, "w"))) {
    14281292        fprintf(fout,
     
    14301294        paranoid_fclose(fout);
    14311295        log_msg(2, "Created %s", outfile);
    1432         sprintf(tmp, "chmod +x %s", outfile);
     1296        asprintf(&tmp, "chmod +x %s", outfile);
    14331297        paranoid_system(tmp);
     1298        paranoid_free(tmp);
     1299
    14341300        retval = 0;
    14351301    } else {
    14361302        retval = 1;
    14371303    }
    1438     paranoid_free(tmp);
    14391304    return (retval);
    14401305}
    14411306
    14421307/* @} - end fileGroup */
     1308
     1309void paranoid_alloc(char *alloc, char *orig)
     1310{
     1311        paranoid_free(alloc);
     1312        asprintf(&alloc, orig);
     1313}
     1314
Note: See TracChangeset for help on using the changeset viewer.