Changeset 2520


Ignore:
Timestamp:
Jan 4, 2010, 7:42:27 PM (9 years ago)
Author:
bruno
Message:

r3230@localhost (orig r2264): bruno | 2009-07-12 02:04:37 +0200

r3205@localhost: bruno | 2009-07-06 19:46:07 +0200

  • Replace sprintf by mr_asprintf in libmondo-stream.c


File:
1 edited

Legend:

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

    r2240 r2520  
    233233bool mt_says_tape_exists(char *dev)
    234234{
    235     char *command;
     235    char *command = NULL;
    236236    int res;
    237237
    238     malloc_string(command);
    239     sprintf(command, "mt -f %s status", dev);
     238    mr_asprintf(&command, "mt -f %s status", dev);
    240239    res = run_program_and_log_output(command, 1);
    241     paranoid_free(command);
     240    mr_free(command);
     241
    242242    if (res) {
    243243        return (FALSE);
     
    259259{
    260260    char tmp[MAX_STR_LEN];
    261     char command[MAX_STR_LEN * 2];
     261    char *command = NULL;
    262262    char cdr_exe[MAX_STR_LEN];
    263263    int res;
     
    270270        strcpy(cdr_exe, "dvdrecord");
    271271    }
    272     sprintf(command, "%s -scanbus 2> /dev/null | grep -i tape | wc -l",
     272    mr_asprintf(&command, "%s -scanbus 2> /dev/null | grep -i tape | wc -l",
    273273            cdr_exe);
    274274    strcpy(tmp, call_program_and_get_last_line_of_output(command));
     275    mr_free(command);
     276
    275277    if (atoi(tmp) != 1) {
    276278        log_it
     
    279281        return 1;
    280282    }
    281     sprintf(command,
     283    mr_asprintf(&command,
    282284            "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | cut -d' ' -f2 | head -n1",
    283285            cdr_exe);
    284286    strcpy(tmp, call_program_and_get_last_line_of_output(command));
     287    mr_free(command);
     288
    285289    if (strlen(tmp) < 2) {
    286290        log_it("Could not find tape device");
    287291        return 1;
    288292    }
    289     sprintf(command,
     293    mr_asprintf(&command,
    290294            "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | cut -d' ' -f3 | cut -d')' -f1 | head -n1",
    291295            cdr_exe);
    292296    strcpy(tmp, call_program_and_get_last_line_of_output(command));
     297    mr_free(command);
     298
    293299    strcpy(dev, VANILLA_SCSI_TAPE);
    294300    dev[strlen(dev) - 1] = '\0';
     
    462468{
    463469    int i;
    464     char tmp[MAX_STR_LEN];
     470    char *tmp = NULL;
    465471
    466472    log_it("Insisting on tape #%d", tapeno);
    467473    if (g_current_media_number != tapeno) {
    468         //      log_it("g_current_media_number = %d", g_current_media_number);
    469         sprintf(tmp,
     474        mr_asprintf(&tmp,
    470475                "When the tape drive goes quiet, please insert volume %d in this series.",
    471476                tapeno);
    472477        popup_and_OK(tmp);
     478        mr_free(tmp);
    473479        open_evalcall_form("Waiting while the tape drive settles");
    474480    } else {
     
    525531    int last, curr, i;
    526532    t_archtype type = other;
    527     char command[MAX_STR_LEN];
    528     char tmpdir[MAX_STR_LEN];
    529     char old_fname[MAX_STR_LEN];
     533    char *command = NULL;
     534    char *tmpdir = NULL;
     535    char *old_fname = NULL;
    530536    char *p;
    531537    char suffix[16];
    532538
    533539    bufsize_K = (long long) (1024LL * (1 + g_tape_buffer_size_MB));
    534     sprintf(tmpdir, "%s/tmpfs/backcatalog", td);
    535540    if ((p = strrchr(latest_fname, '.'))) {
    536541        strcpy(suffix, ++p);
     
    547552            ("Unknown type. Internal error in maintain_collection_of_recent_archives()");
    548553    }
     554    mr_asprintf(&tmpdir, "%s/tmpfs/backcatalog", td);
    549555    mkdir(tmpdir, 0x700);
    550     sprintf(command, "cp -f %s %s", latest_fname, tmpdir);
     556    mr_asprintf(&command, "cp -f %s %s", latest_fname, tmpdir);
    551557    if (run_program_and_log_output(command, 6)) {
    552558        log_it("Warning - failed to copy %s to backcatalog at %s",
    553559               latest_fname, tmpdir);
    554560    }
     561    mr_free(command);
     562
    555563    last = g_tapecatalog->entries - 1;
    556564    if (last <= 0) {
    557565        log_it("Too early to start deleting from collection.");
     566        mr_free(tmpdir);
    558567        return (0);
    559568    }
     
    569578    if (curr < 0) {
    570579        log_it("Not far enough into tape to start deleting old archives from collection.");
     580        mr_free(tmpdir);
    571581        return (0);
    572582    }
    573 //  log_it( "There are %lld KB (more than %d KB) in my backcatalog", final_alleged_writeK - final_actually_certain_writeK, bufsize_K);
    574583
    575584    for (i = curr - 1; i >= 0 && curr - i < 10; i--) {
    576         sprintf(old_fname, "%s/%s", tmpdir, g_tapecatalog->el[i].fname);
     585        mr_asprintf(&old_fname, "%s/%s", tmpdir, g_tapecatalog->el[i].fname);
    577586        unlink(old_fname);
    578     }
     587        mr_free(old_fname);
     588    }
     589    mr_free(tmpdir);
    579590    return (0);
    580591}
     
    604615int set_tape_block_size_with_mt(long internal_tape_block_size)
    605616{
    606     char *tmp;
     617    char *tmp = NULL;
    607618    int res;
    608619
     
    612623        return (0);
    613624    }
    614     malloc_string(tmp);
    615     sprintf(tmp, "mt -f %s setblk %ld", bkpinfo->media_device, internal_tape_block_size);
     625    mr_asprintf(&tmp, "mt -f %s setblk %ld", bkpinfo->media_device, internal_tape_block_size);
    616626    res = run_program_and_log_output(tmp, 3);
    617     paranoid_free(tmp);
     627    mr_free(tmp);
    618628    return (res);
    619629}
     
    748758    char fname[MAX_STR_LEN];
    749759    char *datablock;
    750     char tmp[MAX_STR_LEN];
     760    char *tmp = NULL;
    751761    char old_cwd[MAX_STR_LEN];
    752     char outfname[MAX_STR_LEN];
     762    char *outfname = NULL;
    753763    /*@ int ******************************************************* */
    754764    int i;
     
    793803
    794804    insist_on_this_tape_number(1);
    795     sprintf(outfname, "%s/tmp/all.tar.gz", bkpinfo->tmpdir);
     805    mr_asprintf(&outfname, "%s/tmp/all.tar.gz", bkpinfo->tmpdir);
    796806    make_hole_for_file(outfname);
    797807
     
    804814        log_OS_error(g_tape_fifo);
    805815        log_to_screen("Cannot openin stream device");
     816        mr_free(outfname);
    806817        return (1);
    807818    }
     
    814825        log_OS_error(outfname);
    815826        log_to_screen("Cannot openout datadisk all.tar.gz file");
     827        mr_free(outfname);
    816828        return (-1);
    817829    }
    818830    if (!(datablock = (char *) malloc(256 * 1024))) {
    819831        log_to_screen("Unable to malloc 256*1024");
     832        mr_free(outfname);
    820833        finish(1);
    821834    }
     
    837850    paranoid_fclose(fout);
    838851    paranoid_free(datablock);
     852
    839853/* find initial blocks */
    840854    res = read_header_block_from_stream(&size, fname, &ctrl_chr);
     
    852866    (void) getcwd(old_cwd, MAX_STR_LEN);
    853867    chdir(bkpinfo->tmpdir);
    854     sprintf(tmp, "tar -zxf %s ./tmp/mondo-restore.cfg 2> /dev/null",
     868    mr_asprintf(&tmp, "tar -zxf %s ./tmp/mondo-restore.cfg 2> /dev/null",
    855869            outfname);
    856870    paranoid_system(tmp);
     871    mr_free(tmp);
     872
    857873    paranoid_system("cp -f tmp/mondo-restore.cfg . 2> /dev/null");
    858874    chdir(old_cwd);
    859875    unlink(outfname);
     876    mr_free(outfname);
    860877    return (retval);
    861878}
     
    872889{
    873890    /*@ buffers ***************************************************** */
    874     char command[MAX_STR_LEN * 2];
     891    char *command = NULL;
    875892
    876893    /*@ end vars *************************************************** */
    877894
    878 /*  add 'dummy' if testing */
    879     sprintf(command,
    880             "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s",
    881             cddev, speed, MONDO_LOGFILE, MONDO_LOGFILE);
    882 /*  initialise the catalog */
     895    /*  add 'dummy' if testing */
     896    mr_asprintf(&command, "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s", cddev, speed, MONDO_LOGFILE, MONDO_LOGFILE);
     897    /*  initialise the catalog */
    883898    g_current_media_number = 1;
    884899    if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
     
    886901    }
    887902    g_tapecatalog->entries = 0;
    888 /* log stuff */
     903    /* log stuff */
    889904    log_it("Opening OUT cdstream with the command");
    890905    log_it(command);
    891 /*  log_it("Let's see what happens, shall we?"); */
     906    /*  log_it("Let's see what happens, shall we?"); */
    892907    g_tape_stream = popen(command, "w");
     908    mr_free(command);
     909
    893910    if (g_tape_stream) {
    894911        return (0);
     
    898915    }
    899916}
     917
    900918
    901919/**
     
    10031021{
    10041022    /*@ buffers ***************************************************** */
    1005     char *tmp;
     1023    char *tmp = NULL;
    10061024    char *datablock;
    10071025    char *temp_fname;
    10081026    char *temp_cksum;
    1009     char *actual_cksum;
     1027    char *actual_cksum = NULL;
    10101028//  char *pA, *pB;
    10111029
     
    10381056
    10391057    /*@ init  ******************************************************* */
    1040     malloc_string(tmp);
    10411058    malloc_string(temp_fname);
    10421059    malloc_string(temp_cksum);
    1043     malloc_string(actual_cksum);
    10441060    datablock = malloc(TAPE_BLOCK_SIZE);
    10451061    crc16 = 0;
     
    10511067    res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
    10521068    if (orig_size != temp_size && orig_size != -1) {
    1053         sprintf(tmp,
    1054                 "output file's size should be %ld K but is apparently %ld K",
    1055                 (long) size >> 10, (long) temp_size >> 10);
     1069        mr_asprintf(&tmp, "output file's size should be %ld K but is apparently %ld K", (long) size >> 10, (long) temp_size >> 10);
    10561070        log_to_screen(tmp);
     1071        mr_free(tmp);
    10571072    }
    10581073    if (ctrl_chr != BLK_START_FILE) {
     
    10601075        return (1);
    10611076    }
    1062     /* Not used
    1063     sprintf(tmp, "Reading file from tape; writing to '%s'; %ld KB",
    1064             outfname, (long) size >> 10);
    1065             */
    10661077
    10671078    if (foutstream) {
     
    11191130    log_msg(6, ".......................... Should be %lld", orig_size);
    11201131    g_tape_posK += total_read_from_tape_for_this_file / 1024;
    1121     sprintf(actual_cksum, "%04x%04x", crc16, crctt);
     1132    mr_asprintf(&actual_cksum, "%04x%04x", crc16, crctt);
    11221133    if (foutstream) {           /*log_it("Finished writing to foutstream"); */
    11231134    } else {
     
    11271138    if (ctrl_chr != BLK_STOP_FILE) {
    11281139        wrong_marker(BLK_STOP_FILE, ctrl_chr);
    1129 //      fatal_error("Bad marker"); // return(1);
    11301140    }
    11311141    if (strcmp(temp_cksum, actual_cksum)) {
    1132         sprintf(tmp, "actual cksum=%s; recorded cksum=%s", actual_cksum,
     1142        mr_asprintf(&tmp, "actual cksum=%s; recorded cksum=%s", actual_cksum,
    11331143                temp_cksum);
    11341144        log_to_screen(tmp);
    1135         sprintf(tmp, "%s (%ld K) is corrupt on tape", temp_fname,
     1145        mr_free(tmp);
     1146
     1147        mr_asprintf(&tmp, "%s (%ld K) is corrupt on tape", temp_fname,
    11361148                (long) orig_size >> 10);
    11371149        log_to_screen(tmp);
     1150        mr_free(tmp);
     1151
    11381152        retval++;
    1139     } else {
    1140         sprintf(tmp, "%s is GOOD on tape", temp_fname);
    1141         /*      log_it(tmp); */
    1142     }
     1153    }
     1154    mr_free(actual_cksum);
     1155
    11431156    paranoid_free(datablock);
    1144     paranoid_free(tmp);
    11451157    paranoid_free(temp_fname);
    11461158    paranoid_free(temp_cksum);
    1147     paranoid_free(actual_cksum);
    11481159    return (retval);
    11491160}
     
    14731484{
    14741485    int res = 0;
    1475     char command[MAX_STR_LEN * 2];
     1486    char *command = NULL;
     1487
    14761488    paranoid_pclose(g_tape_stream);
    14771489    system("sync");
     
    14851497    }
    14861498    if (bkpinfo->backup_media_type == cdstream) {
    1487         sprintf(command,
     1499        mr_asprintf(&command,
    14881500                "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s",
    14891501                bkpinfo->media_device, bkpinfo->cdrw_speed, MONDO_LOGFILE,
     
    14931505        log_it("Let's see what happens, shall we?");
    14941506        g_tape_stream = popen(command, "w");
     1507        mr_free(command);
     1508
    14951509        if (!g_tape_stream) {
    14961510            log_to_screen("Failed to openout to cdstream (fifo)");
     
    15331547{
    15341548    int i, last, res = 0;
    1535     char *fname;
     1549    char *fname = NULL;
    15361550
    15371551    log_msg(2, "I am now writing back catalog to tape");
    1538     malloc_string(fname);
    15391552    last = g_tapecatalog->entries - 1;
    15401553    for (i = 0; i <= last; i++) {
    1541         sprintf(fname, "%s/tmpfs/backcatalog/%s", bkpinfo->tmpdir,
    1542                 g_tapecatalog->el[i].fname);
     1554        mr_asprintf(&fname, "%s/tmpfs/backcatalog/%s", bkpinfo->tmpdir, g_tapecatalog->el[i].fname);
    15431555        if (!does_file_exist(fname)) {
    15441556            log_msg(6, "Can't write %s - it doesn't exist.", fname);
     
    15581570            }
    15591571        }
    1560     }
    1561     paranoid_free(fname);
     1572        mr_free(fname);
     1573    }
    15621574    log_msg(2, "Finished writing back catalog to tape");
    15631575    return (res);
     
    15751587    /*@ pointers *************************************************** */
    15761588    FILE *fin;
    1577     char tmp[MAX_STR_LEN];
     1589    char *tmp = NULL;
    15781590
    15791591    /*@ long ******************************************************* */
     
    15931605    log_it("Data disks = %s", fname);
    15941606    if (!does_file_exist(fname)) {
    1595         sprintf(tmp, "Cannot find %s", fname);
     1607        mr_asprintf(&tmp, "Cannot find %s", fname);
    15961608        log_to_screen(tmp);
     1609        mr_free(tmp);
    15971610        return (1);
    15981611    }
     
    16371650{
    16381651    /*@ buffers **************************************************** */
    1639     char tmp[MAX_STR_LEN];
     1652    char *tmp = NULL;
    16401653    char datablock[TAPE_BLOCK_SIZE];
    1641     char checksum[MAX_STR_LEN];
     1654    char *checksum = NULL;
    16421655    char *infile_basename;
    16431656
     
    16901703        p++;
    16911704    }
    1692     sprintf(tmp, "Writing file '%s' to tape (%ld KB)", p,
     1705    mr_asprintf(&tmp, "Writing file '%s' to tape (%ld KB)", p,
    16931706            (long) filesize >> 10);
    16941707    log_it(tmp);
     1708    mr_free(tmp);
    16951709    write_header_block_to_stream(filesize, infile_basename,
    16961710                                 BLK_START_FILE);
     
    17321746    }
    17331747    paranoid_fclose(fin);
    1734     sprintf(checksum, "%04x%04x", crc16, crctt);
     1748    mr_asprintf(&checksum, "%04x%04x", crc16, crctt);
    17351749    /* BERLIOS: what does it do ??? */
    1736     write_header_block_to_stream((off_t)g_current_media_number, checksum,
    1737                                  BLK_STOP_FILE);
     1750    write_header_block_to_stream((off_t)g_current_media_number, checksum, BLK_STOP_FILE);
     1751    mr_free(checksum);
     1752
    17381753//  log_it("File '%s' written to tape.", infile);
    17391754    return (retval);
     
    17611776    /*@ buffers **************************************************** */
    17621777    char tempblock[TAPE_BLOCK_SIZE];
    1763     char tmp[MAX_STR_LEN];
     1778    char *tmp = NULL;
    17641779    char *p;
    17651780
     
    17891804    sprintf(tempblock + 6000 + control_char, STR_HEADER);
    17901805    tempblock[7000] = control_char;
    1791 /*  for(i=0;i<8;i++) {tempblock[7001+i]=olen&0xff; olen>>=8;} */
    17921806    memcpy(tempblock + 7001, (char *) &olen, sizeof(off_t));
    1793 /*  if (length_of_incoming_file) {memcpy(tempblock+7001,(char*)&length_of_incoming_file,sizeof(long long));} */
    17941807    strcpy(tempblock + 1000, filename);
    1795 /*  strcpy(tempblock+5555,cksum); */
    17961808    g_tape_posK +=
    17971809        fwrite(tempblock, 1, (size_t) TAPE_BLOCK_SIZE,
    17981810               g_tape_stream) / 1024;
    1799     sprintf(tmp, "%s (fname=%s, size=%ld K)",
     1811    mr_asprintf(&tmp, "%s (fname=%s, size=%ld K)",
    18001812            marker_to_string(control_char), p,
    18011813            (long) length_of_incoming_file >> 10);
    18021814    log_msg(6, tmp);
    1803 /*  log_tape_pos(); */
     1815    mr_free(tmp);
    18041816    return (0);
    18051817}
     
    18221834{
    18231835    /*@ buffer ***************************************************** */
    1824     char tmp[MAX_STR_LEN];
     1836    char *tmp = NULL;
    18251837
    18261838
    18271839    /*@ end vars *************************************************** */
    1828     sprintf(tmp, "Wrong marker! (Should be %s, ",
    1829             marker_to_string(should_be));
    1830     sprintf(tmp + strlen(tmp), "is actually %s)", marker_to_string(it_is));
     1840    mr_asprintf(&tmp, "Wrong marker! (Should be %s, is actually %s)", marker_to_string(should_be), marker_to_string(it_is));
    18311841    log_to_screen(tmp);
     1842    mr_free(tmp);
    18321843}
    18331844
Note: See TracChangeset for help on using the changeset viewer.