Ignore:
Timestamp:
Jul 23, 2009, 2:53:30 PM (15 years ago)
Author:
Bruno Cornec
Message:
  • Replace sprintf by mr_asprintf in mondo-rstr-compare.c
  • All mr_asprintf call should be on one line (for quality script) and use a string as second param
  • Improve quality script
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.10/mondo/src/mondorestore/mondo-rstr-compare.c

    r2291 r2296  
    4747    char *bigfile_fname_ptr;
    4848    char *tmp_ptr = NULL;
    49     char *command_ptr;
     49    char *command_ptr = NULL;
    5050
    5151    char *checksum, *original_cksum, *bigfile_fname, *tmp, *command;
     
    6565    malloc_string(original_cksum_ptr);
    6666    malloc_string(bigfile_fname_ptr);
    67     malloc_string(command_ptr);
    6867
    6968  /*********************************************************************
     
    7473    memset(original_cksum_ptr, '\0', sizeof(original_cksum));
    7574    memset(bigfile_fname_ptr, '\0', sizeof(bigfile_fname));
    76     memset(command_ptr, '\0', sizeof(command));
    7775  /** end **/
    7876
     
    113111    if (!strncmp(bigfile_fname_ptr, "/dev/", 5)) {
    114112        strcpy(original_cksum_ptr, "IGNORE");
    115     } else {
    116         sprintf(command_ptr,
    117                 "md5sum \"%s%s\" > /tmp/md5sum.txt 2> /tmp/errors",
    118                 MNT_RESTORING, bigfile_fname_ptr);
    119     }
     113        log_msg(2, "IGNORING %s as begining with /dev", bigfile_fname_ptr);
     114        return (1);
     115    }
     116
     117    mr_asprintf(&command_ptr, "md5sum \"%s%s\" > /tmp/md5sum.txt 2> /tmp/errors", MNT_RESTORING, bigfile_fname_ptr);
    120118    log_msg(2, command_ptr);
     119    i =  system(command_ptr);
     120    mr_free(command_ptr);
     121
    121122    mr_asprintf(&tmp_ptr, "cat /tmp/errors >> %s 2> /dev/null", MONDO_LOGFILE);
    122123    paranoid_system(tmp_ptr);
    123124    paranoid_free(tmp_ptr);
    124125
    125     if (system(command_ptr)) {
     126    if (i) {
    126127        log_OS_error("Warning - command failed");
    127128        original_cksum[0] = '\0';
     
    169170    paranoid_free(checksum_ptr);
    170171    paranoid_free(checksum);
    171     paranoid_free(command_ptr);
    172172    paranoid_free(command);
    173173    paranoid_free(tmp);
     
    190190    int res;
    191191    long noof_biggiefiles, bigfileno = 0;
    192     char tmp[MAX_STR_LEN];
     192    char *tmp = NULL;
    193193
    194194    log_msg(1, "Comparing biggiefiles");
     
    211211                       noof_biggiefiles);
    212212    for (bigfileno = 0; bigfileno < noof_biggiefiles; bigfileno++) {
    213         sprintf(tmp, "Comparing big file #%ld", bigfileno + 1);
     213        mr_asprintf(&tmp, "Comparing big file #%ld", bigfileno + 1);
    214214        log_msg(1, tmp);
    215215        update_progress_form(tmp);
     216        mr_free(tmp);
     217
    216218        res = compare_a_biggiefile(bigfileno);
    217219        retval += res;
     
    250252
    251253  /***  needs malloc *********/
    252     char *command, *tmp, *filelist_name, *logfile,
    253         *compressor_exe;
     254    char *command = NULL;
     255    char *tmp = NULL;
    254256    char *archiver_exe = NULL;
    255 
    256     malloc_string(command);
    257     malloc_string(tmp);
    258     malloc_string(filelist_name);
    259     malloc_string(logfile);
    260     malloc_string(compressor_exe);
     257    char *filelist_name = NULL;
     258    char *logfile = NULL;
     259    char *compressor_exe = NULL;
    261260
    262261    use_star = (strstr(tarball_fname, ".star")) ? TRUE : FALSE;
    263262    assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
    264     sprintf(logfile, "/tmp/afio.log.%d", current_tarball_number);
    265     sprintf(filelist_name, MNT_CDROM "/archives/filelist.%d",
    266             current_tarball_number);
    267 
     263    mr_asprintf(&filelist_name, MNT_CDROM "/archives/filelist.%d", current_tarball_number);
    268264    noof_lines = count_lines_in_file(filelist_name);
     265    mr_free(filelist_name);
    269266
    270267    if (strstr(tarball_fname, ".bz2")) {
    271         strcpy(compressor_exe, "bzip2");
     268        mr_asprintf(&compressor_exe, "bzip2");
    272269    } else if (strstr(tarball_fname, ".gz")) {
    273         strcpy(compressor_exe, "gzip");
     270        mr_asprintf(&compressor_exe, "gzip");
    274271    } else if (strstr(tarball_fname, ".lzo")) {
    275         strcpy(compressor_exe, "lzop");
    276     } else {
    277         compressor_exe[0] = '\0';
     272        mr_asprintf(&compressor_exe, "lzop");
    278273    }
    279274
     
    284279    }
    285280
    286     if (compressor_exe[0]) {
    287         strcpy(tmp, compressor_exe);
    288         if (!find_home_of_exe(tmp)) {
     281    if (compressor_exe) {
     282        if (!find_home_of_exe(compressor_exe)) {
     283            mr_free(compressor_exe);
     284            mr_free(archiver_exe);
    289285            fatal_error("(compare_a_tarball) Compression program missing");
    290286        }
    291         if (use_star)           // star
    292         {
     287        if (use_star) {
    293288            if (!strcmp(compressor_exe, "bzip2")) {
    294289                mr_strcat(archiver_exe, " -bz");
    295290            } else {
    296                 fatal_error
    297                     ("(compare_a_tarball) Please use only bzip2 with star");
     291                mr_free(compressor_exe);
     292                mr_free(archiver_exe);
     293                fatal_error("(compare_a_tarball) Please use only bzip2 with star");
    298294            }
    299         } else                  // afio
    300         {
    301             sprintf(compressor_exe, "-P %s -Z", tmp);
    302         }
    303     }
    304 // star -diff H=star -bz file=....
     295        } else {
     296            // afio
     297            mr_free(compressor_exe);
     298            mr_asprintf(&tmp, "%s", compressor_exe);
     299            mr_asprintf(&compressor_exe, "-P %s -Z", tmp);
     300            mr_free(tmp);
     301        }
     302    }
    305303
    306304#ifdef __FreeBSD__
     
    309307#define BUFSIZE (1024L*1024L)/TAPE_BLOCK_SIZE
    310308#endif
    311     if (use_star)               // doesn't use compressor_exe
    312     {
    313         sprintf(command,
    314                 "%s -diff H=star file=%s >> %s 2>> %s",
    315                 archiver_exe, tarball_fname, logfile, logfile);
    316     } else {
    317         sprintf(command,
    318                 "%s -r -b %ld -M 16m -c %ld %s %s >> %s 2>> %s",
    319                 archiver_exe,
    320                 TAPE_BLOCK_SIZE,
    321                 BUFSIZE, compressor_exe, tarball_fname, logfile, logfile);
    322     }
     309    mr_asprintf(&logfile, "/tmp/afio.log.%d", current_tarball_number);
     310
     311    if (use_star) {
     312        // doesn't use compressor_exe
     313        mr_asprintf(&command, "%s -diff H=star file=%s >> %s 2>> %s", archiver_exe, tarball_fname, logfile, logfile);
     314    } else {
     315        mr_asprintf(&command, "%s -r -b %ld -M 16m -c %ld %s %s >> %s 2>> %s", archiver_exe, TAPE_BLOCK_SIZE, BUFSIZE, compressor_exe, tarball_fname, logfile, logfile);
     316    }
     317    mr_free(compressor_exe);
    323318    paranoid_free(archiver_exe);
    324319
     
    329324    if (res) {
    330325        log_OS_error(command);
    331         sprintf(tmp, "Warning - afio returned error = %d", res);
    332         log_msg(2, tmp);
    333     }
     326        log_msg(2, "Warning - afio returned error = %d", res);
     327    }
     328    mr_free(command);
     329
    334330    if (length_of_file(logfile) > 5) {
    335         sprintf(command,
    336                 "sed s/': \\\"'/\\|/ %s | sed s/'\\\": '/\\|/ | cut -d'|' -f2 | sort -u | grep -vE \"^dev/.*\" >> "MONDO_CACHE"/changed.txt",
    337                 logfile);
     331        mr_asprintf(&command, "sed s/': \\\"'/\\|/ %s | sed s/'\\\": '/\\|/ | cut -d'|' -f2 | sort -u | grep -vE \"^dev/.*\" >> "MONDO_CACHE"/changed.txt", logfile);
    338332        system(command);
     333        mr_free(command);
     334
    339335        archiver_errors = count_lines_in_file(logfile);
    340336    } else {
    341337        archiver_errors = 0;
    342338    }
    343     sprintf(tmp, "%ld difference%c in fileset #%d          ",
    344             archiver_errors, (archiver_errors != 1) ? 's' : ' ',
    345             current_tarball_number);
    346339    if (archiver_errors) {
    347         sprintf(tmp,
    348                 "Differences found while processing fileset #%d       ",
    349                 current_tarball_number);
    350         log_msg(1, tmp);
     340        log_msg(1, "%ld difference%c in fileset #%d          ", archiver_errors, (archiver_errors != 1) ? 's' : ' ', current_tarball_number);
    351341    }
    352342    unlink(logfile);
    353     paranoid_free(command);
    354     paranoid_free(tmp);
    355     paranoid_free(filelist_name);
    356     paranoid_free(logfile);
    357     paranoid_free(compressor_exe);
     343    mr_free(logfile);
     344
    358345    return (retval);
    359346}
     
    377364  /**  needs malloc **********/
    378365
    379     char *tarball_fname, *progress_str, *tmp;
     366    char *tarball_fname = NULL;
     367    char *progress_str = NULL;
     368    char *tmp = NULL;
    380369    char *mds = NULL;
    381370    long max_val;
    382371
    383     malloc_string(tarball_fname);
    384     malloc_string(progress_str);
    385     malloc_string(tmp);
    386372
    387373    assert(bkpinfo != NULL);
    388374    mvaddstr_and_log_it(g_currentY, 0, "Comparing archives");
     375
     376    malloc_string(tmp);
    389377    read_cfg_var(g_mondo_cfg_file, "last-filelist-number", tmp);
    390 
    391378    max_val = atol(tmp);
     379    paranoid_free(tmp);
     380
    392381    mds = media_descriptor_string(bkpinfo->backup_media_type);
    393     sprintf(progress_str, "Comparing with %s #%d ", mds, g_current_media_number);
     382    mr_asprintf(&progress_str, "Comparing with %s #%d ", mds, g_current_media_number);
    394383
    395384    open_progress_form("Comparing files",
     
    403392        insist_on_this_cd_number(g_current_media_number);
    404393        update_progress_form(progress_str);
    405         sprintf(tarball_fname,
    406                 MNT_CDROM "/archives/%d.afio.bz2", current_tarball_number);
     394        mr_asprintf(&tarball_fname, MNT_CDROM "/archives/%d.afio.bz2", current_tarball_number);
    407395
    408396        if (!does_file_exist(tarball_fname)) {
    409             sprintf(tarball_fname, MNT_CDROM "/archives/%d.afio.lzo",
    410                     current_tarball_number);
     397            mr_free(tarball_fname);
     398            mr_asprintf(&tarball_fname, MNT_CDROM "/archives/%d.afio.lzo", current_tarball_number);
    411399        }
    412400        if (!does_file_exist(tarball_fname)) {
    413             sprintf(tarball_fname, MNT_CDROM "/archives/%d.afio.gz",
    414                     current_tarball_number);
     401            mr_free(tarball_fname);
     402            mr_asprintf(&tarball_fname, MNT_CDROM "/archives/%d.afio.gz", current_tarball_number);
    415403        }
    416404        if (!does_file_exist(tarball_fname)) {
    417             sprintf(tarball_fname, MNT_CDROM "/archives/%d.afio.",
    418                     current_tarball_number);
     405            mr_free(tarball_fname);
     406            mr_asprintf(&tarball_fname, MNT_CDROM "/archives/%d.afio.", current_tarball_number);
    419407        }
    420408        if (!does_file_exist(tarball_fname)) {
    421             sprintf(tarball_fname, MNT_CDROM "/archives/%d.star.bz2",
    422                     current_tarball_number);
     409            mr_free(tarball_fname);
     410            mr_asprintf(&tarball_fname, MNT_CDROM "/archives/%d.star.bz2", current_tarball_number);
    423411        }
    424412        if (!does_file_exist(tarball_fname)) {
    425             sprintf(tarball_fname, MNT_CDROM "/archives/%d.star.",
    426                     current_tarball_number);
     413            mr_free(tarball_fname);
     414            mr_asprintf(&tarball_fname, MNT_CDROM "/archives/%d.star.", current_tarball_number);
    427415        }
    428416        if (!does_file_exist(tarball_fname)) {
     
    432420                == 0) {
    433421                log_msg(2, "OK, I think I'm done with tarballs...");
     422                mr_free(tarball_fname);
    434423                break;
    435424            }
    436425            log_msg(2, "OK, I think it's time for another CD...");
    437426            g_current_media_number++;
    438             sprintf(progress_str, "Comparing with %s #%d ", mds, g_current_media_number);
     427
     428            mr_free(progress_str);
     429            mr_asprintf(&progress_str, "Comparing with %s #%d ", mds, g_current_media_number);
    439430            log_to_screen(progress_str);
    440431        } else {
     
    444435            current_tarball_number++;
    445436        }
    446     }
     437        mr_free(tarball_fname);
     438    }
     439    mr_free(progress_str);
    447440    mr_free(mds);
    448441
     
    453446        mvaddstr_and_log_it(g_currentY++, 74, "Done.");
    454447    }
    455     paranoid_free(tarball_fname);
    456     paranoid_free(progress_str);
    457     paranoid_free(tmp);
    458448    return (retval);
    459449}
     
    478468{
    479469  /** needs malloc *********/
    480     char *tmp, *cwd, *new, *command;
     470    char *tmp = NULL;
     471    char *cwd, *new;
     472    char *command = NULL;
    481473    int resA = 0;
    482474    int resB = 0;
    483475    long noof_changed_files;
    484476
    485     malloc_string(tmp);
    486477    malloc_string(cwd);
    487478    malloc_string(new);
    488     malloc_string(command);
    489479
    490480    assert(bkpinfo != NULL);
     
    501491    noof_changed_files = count_lines_in_file(MONDO_CACHE"/changed.txt");
    502492    if (noof_changed_files) {
    503         sprintf(tmp, "%ld files do not match the backup            ",
    504                 noof_changed_files);
    505         //      mvaddstr_and_log_it( g_currentY++, 0, tmp );
     493        mr_asprintf(&tmp, "%ld files do not match the backup            ", noof_changed_files);
    506494        log_to_screen(tmp);
    507         sprintf(command, "cat "MONDO_CACHE"/changed.txt >> %s", MONDO_LOGFILE);
     495        mr_free(tmp);
     496
     497        mr_asprintf(&command, "cat "MONDO_CACHE"/changed.txt >> %s", MONDO_LOGFILE);
    508498        paranoid_system(command);
    509     } else {
    510         sprintf(tmp, "All files match the backup                     ");
     499        mr_free(command);
     500    } else {
     501        mr_asprintf(&tmp, "All files match the backup                     ");
    511502        mvaddstr_and_log_it(g_currentY++, 0, tmp);
    512503        log_to_screen(tmp);
    513     }
    514 
    515     paranoid_free(tmp);
     504        mr_free(tmp);
     505    }
     506
    516507    paranoid_free(cwd);
    517508    paranoid_free(new);
    518     paranoid_free(command);
    519509
    520510    return (resA + resB);
     
    544534    int res = 0;
    545535    long q;
    546     char *tmp;
     536    char *tmp = NULL;
    547537    char *new;
    548538    char *cwd;
    549539
    550     malloc_string(tmp);
    551540    malloc_string(new);
    552541    malloc_string(cwd);
     
    575564    if (g_text_mode) {
    576565        save_mountlist_to_disk(mountlist, g_mountlist_fname);
    577         sprintf(tmp, "%s %s", find_my_editor(), g_mountlist_fname);
     566        mr_asprintf(&tmp, "%s %s", find_my_editor(), g_mountlist_fname);
    578567        res = system(tmp);
     568        mr_free(tmp);
     569
    579570        load_mountlist(mountlist, g_mountlist_fname);
    580571    } else {
     
    622613        } else {
    623614            q = count_lines_in_file(MONDO_CACHE"/changed.files");
    624             sprintf(tmp, "%ld significant difference%s found.", q,
    625                     (q != 1) ? "s" : "");
     615            mr_asprintf(&tmp, "%ld significant difference%s found.", q, (q != 1) ? "s" : "");
    626616            mvaddstr_and_log_it(g_currentY++, 0, tmp);
    627617            log_to_screen(tmp);
    628 
    629             strcpy(tmp,
    630                   "Type 'less /tmp/changed.files' for a list of non-matching files");
     618            mr_free(tmp);
     619
     620            mr_asprintf(&tmp, "Type 'less /tmp/changed.files' for a list of non-matching files");
    631621            mvaddstr_and_log_it(g_currentY++, 0, tmp);
    632622            log_to_screen(tmp);
     623            mr_free(tmp);
    633624
    634625            log_msg(2, "calling popup_changelist_from_file()");
     
    647638
    648639    kill_petris();
    649     paranoid_free(tmp);
    650640    paranoid_free(new);
    651641    paranoid_free(cwd);
     
    670660
    671661  /** needs malloc **/
    672     char *dir, *command;
     662    char *dir;
     663    char *command = NULL;
    673664
    674665    assert(bkpinfo != NULL);
    675666    malloc_string(dir);
    676     malloc_string(command);
    677667    getcwd(dir, MAX_STR_LEN);
    678668    chdir(bkpinfo->restore_path);
    679669
    680     sprintf(command, "cp -f /tmp/LAST-FILELIST-NUMBER %s/tmp",
    681             bkpinfo->restore_path);
     670    mr_asprintf(&command, "cp -f /tmp/LAST-FILELIST-NUMBER %s/tmp", bkpinfo->restore_path);
    682671    run_program_and_log_output(command, FALSE);
     672    mr_free(command);
     673
    683674    mvaddstr_and_log_it(g_currentY,
    684675                        0, "Verifying archives against filesystem");
     
    704695    mvaddstr_and_log_it(g_currentY++, 74, "Done.");
    705696    paranoid_free(dir);
    706     paranoid_free(command);
    707697    return (res);
    708698}
     
    727717{
    728718    int res;
    729     char *dir, *command;
     719    char *dir;
     720    char *command = NULL;
    730721
    731722    assert(bkpinfo != NULL);
    732723    malloc_string(dir);
    733     malloc_string(command);
    734724
    735725    getcwd(dir, MAX_STR_LEN);
    736726    chdir(bkpinfo->restore_path);
    737     sprintf(command, "cp -f /tmp/LAST-FILELIST-NUMBER %s/tmp",
    738             bkpinfo->restore_path);
     727    mr_asprintf(&command, "cp -f /tmp/LAST-FILELIST-NUMBER %s/tmp", bkpinfo->restore_path);
    739728    run_program_and_log_output(command, FALSE);
     729    mr_free(command);
     730
    740731    mvaddstr_and_log_it(g_currentY,
    741732                        0, "Verifying archives against filesystem");
     
    748739    }
    749740    paranoid_free(dir);
    750     paranoid_free(command);
    751741    return (res);
    752742}
Note: See TracChangeset for help on using the changeset viewer.