Ignore:
Timestamp:
Jun 3, 2009, 7:10:19 PM (15 years ago)
Author:
Bruno Cornec
Message:

r3089@localhost: bruno | 2009-05-18 06:41:05 +0200

  • move call to asprintf to call to mr_asprintf (suppress a compiler warning)
  • remove all the most obvious bad call to strcat and replace by mr_strcat as appropriate
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mondo/src/mondorestore/mondo-prep.c

    r2205 r2211  
    11/***************************************************************************
    22$Id$
    3 ***************************************************************************/
    4 
    5 /***************************************************************************
    6  *                                                                         *
    7  *   This program is free software; you can redistribute it and/or modify  *
    8  *   it under the terms of the GNU General Public License as published by  *
    9  *   the Free Software Foundation; either version 2 of the License, or     *
    10  *   (at your option) any later version.                                   *
    11  *                                                                         *
    12  ***************************************************************************/
    13 
    14 /**
    15  * @file
    16  * Functions for prepping hard drives: partitioning, formatting, etc.
    17  */
     3* Functions for prepping hard drives: partitioning, formatting, etc.
     4*/
    185
    196
    207#include "my-stuff.h"
     8#include "mr_mem.h"
    219#include "../common/mondostructures.h"
    2210#include "mondoprep.h"
     
    349337            log_it("%s... so I'll get creative.", tmp);
    350338            if (lvmversion == 2) {
    351                 asprintf(&tmp1, "tail -n5 %s | grep Insufficient | tail -n1", MONDO_LOGFILE);
     339                mr_asprintf(&tmp1, "tail -n5 %s | grep Insufficient | tail -n1", MONDO_LOGFILE);
    352340                strcpy(tmp, call_program_and_get_last_line_of_output(tmp1));
    353341                free(tmp1);
    354342            } else {
    355                 asprintf(&tmp1, "tail -n5 %s | grep lvcreate | tail -n1", MONDO_LOGFILE);
     343                mr_asprintf(&tmp1, "tail -n5 %s | grep lvcreate | tail -n1", MONDO_LOGFILE);
    356344                strcpy(tmp, call_program_and_get_last_line_of_output(tmp1));
    357345                free(tmp1);
     
    596584  }
    597585  // create device list from normal disks followed by spare ones
    598   asprintf(&devices, "%s", raidlist->el[i].data_disks.el[0].device);
     586  mr_asprintf(&devices, "%s", raidlist->el[i].data_disks.el[0].device);
    599587  for (j = 1; j < raidlist->el[i].data_disks.entries; j++) {
    600     asprintf(&strtmp, "%s", devices);
     588    mr_asprintf(&strtmp, "%s", devices);
    601589    paranoid_free(devices);
    602     asprintf(&devices, "%s %s", strtmp,
     590    mr_asprintf(&devices, "%s %s", strtmp,
    603591         raidlist->el[i].data_disks.el[j].device);
    604592    paranoid_free(strtmp);
    605593  }
    606594  for (j = 0; j < raidlist->el[i].spare_disks.entries; j++) {
    607     asprintf(&strtmp, "%s", devices);
     595    mr_asprintf(&strtmp, "%s", devices);
    608596    paranoid_free(devices);
    609     asprintf(&devices, "%s %s", strtmp,
     597    mr_asprintf(&devices, "%s %s", strtmp,
    610598         raidlist->el[i].spare_disks.el[j].device);
    611599    paranoid_free(strtmp);
     
    613601  // translate RAID level
    614602  if (raidlist->el[i].raid_level == -2) {
    615     asprintf(&level, "multipath");
     603    mr_asprintf(&level, "multipath");
    616604  } else if (raidlist->el[i].raid_level == -1) {
    617     asprintf(&level, "linear");
     605    mr_asprintf(&level, "linear");
    618606  } else {
    619     asprintf(&level, "raid%d", raidlist->el[i].raid_level);
     607    mr_asprintf(&level, "raid%d", raidlist->el[i].raid_level);
    620608  }
    621609  // create RAID device:
     
    624612  // - faulty devices ignored
    625613  // - persistent superblock always used as this is recommended
    626   asprintf(&program,
     614  mr_asprintf(&program,
    627615       "mdadm --create --force --run --auto=yes %s --level=%s --raid-devices=%d",
    628616       raidlist->el[i].raid_device, level,
    629617       raidlist->el[i].data_disks.entries);
    630618  if (raidlist->el[i].parity != -1) {
    631     asprintf(&strtmp, "%s", program);
     619    mr_asprintf(&strtmp, "%s", program);
    632620    paranoid_free(program);
    633621    switch(raidlist->el[i].parity) {
    634622    case 0:
    635       asprintf(&program, "%s --parity=%s", strtmp, "la");
     623      mr_asprintf(&program, "%s --parity=%s", strtmp, "la");
    636624      break;
    637625    case 1:
    638       asprintf(&program, "%s --parity=%s", strtmp, "ra");
     626      mr_asprintf(&program, "%s --parity=%s", strtmp, "ra");
    639627      break;
    640628    case 2:
    641       asprintf(&program, "%s --parity=%s", strtmp, "ls");
     629      mr_asprintf(&program, "%s --parity=%s", strtmp, "ls");
    642630      break;
    643631    case 3:
    644       asprintf(&program, "%s --parity=%s", strtmp, "rs");
     632      mr_asprintf(&program, "%s --parity=%s", strtmp, "rs");
    645633      break;
    646634    default:
     
    651639  }
    652640  if (raidlist->el[i].chunk_size != -1) {
    653     asprintf(&strtmp, "%s", program);
     641    mr_asprintf(&strtmp, "%s", program);
    654642    paranoid_free(program);
    655     asprintf(&program, "%s --chunk=%d", strtmp, raidlist->el[i].chunk_size);
     643    mr_asprintf(&program, "%s --chunk=%d", strtmp, raidlist->el[i].chunk_size);
    656644    paranoid_free(strtmp);
    657645  }
    658646  if (raidlist->el[i].spare_disks.entries > 0) {
    659     asprintf(&strtmp, "%s", program);
     647    mr_asprintf(&strtmp, "%s", program);
    660648    paranoid_free(program);
    661     asprintf(&program, "%s --spare-devices=%d", strtmp,
     649    mr_asprintf(&program, "%s --spare-devices=%d", strtmp,
    662650         raidlist->el[i].spare_disks.entries);
    663651    paranoid_free(strtmp);
    664652  }
    665   asprintf(&strtmp, "%s", program);
     653  mr_asprintf(&strtmp, "%s", program);
    666654  paranoid_free(program);
    667   asprintf(&program, "%s %s", strtmp, devices);
     655  mr_asprintf(&program, "%s %s", strtmp, devices);
    668656  paranoid_free(strtmp);
    669657  res = run_program_and_log_output(program, 1);
     
    690678{
    691679    /** int **************************************************************/
    692     int res;
    693     int retval = 0;
    694680#ifdef __FreeBSD__
    695681    static bool vinum_started_yet = FALSE;
     
    698684    /** buffers ***********************************************************/
    699685    char *program;
    700     char *tmp;
     686    char *tmp = NULL;
     687    int res = 0;
     688    int retval = 0;
    701689
    702690    /** end ****************************************************************/
    703691
    704692    malloc_string(program);
    705     malloc_string(tmp);
    706693    assert_string_is_neither_NULL_nor_zerolength(device);
    707694    assert(format != NULL);
    708695
    709696    if (strstr(format, "raid")) {   // do not form RAID disks; do it to /dev/md* instead
    710         sprintf(tmp, "Not formatting %s (it is a RAID disk)", device);
     697        mr_asprintf(&tmp, "Not formatting %s (it is a RAID disk)", device);
    711698        log_it(tmp);
     699        paranoid_free(tmp);
    712700        paranoid_free(program);
    713         paranoid_free(tmp);
    714701        return (0);
    715702    }
     
    723710#endif
    724711    if (strlen(format) <= 2) {
    725         sprintf(tmp,
     712        mr_asprintf(&tmp,
    726713                "%s has a really small format type ('%s') - this is probably a hexadecimal string, which would suggest the partition is an image --- I shouldn't format it",
    727714                device, format);
    728715        log_it(tmp);
     716        paranoid_free(tmp);
    729717        paranoid_free(program);
     718        return (0);
     719    }
     720    if (is_this_device_mounted(device)) {
     721        mr_asprintf(&tmp, "%s is mounted - cannot format it       ", device);
     722        log_to_screen(tmp);
    730723        paranoid_free(tmp);
    731         return (0);
    732     }
    733     if (is_this_device_mounted(device)) {
    734         sprintf(tmp, "%s is mounted - cannot format it       ", device);
    735         log_to_screen(tmp);
    736724        paranoid_free(program);
    737         paranoid_free(tmp);
    738725        return (1);
    739726    }
     
    762749            FILE *fin;
    763750            char line[MAX_STR_LEN];
    764             sprintf(tmp,
    765                     "Initializing Vinum device %s (this may take a *long* time)",
    766                     device);
     751
     752            mr_asprintf(&tmp, "Initializing Vinum device %s (this may take a *long* time)", device);
    767753            log_to_screen(tmp);
     754            paranoid_free(tmp);
     755
    768756            /* format raid partition */
    769757            //      sprintf (program, "mkraid --really-force %s", device); --- disabled -- BB, 02/12/2003
     
    780768                    *(strchr(line, '\n')) = '\0';   // get rid of the \n on the end
    781769
    782                 sprintf(tmp, "Initializing plex: %s", line);
     770                mr_asprintf(&tmp, "Initializing plex: %s", line);
    783771                open_evalcall_form(tmp);
    784                 sprintf(tmp, "vinum init %s", line);
     772                paranoid_free(tmp);
     773
     774                mr_asprintf(&tmp, "vinum init %s", line);
    785775                system(tmp);
     776                paranoid_free(tmp);
     777
    786778                while (1) {
    787                     sprintf(tmp,
     779                    mr_asprintf(&tmp,
    788780                            "vinum lp -r %s | grep '^S' | head -1 | tr -s ' ' | cut -d: -f2 | cut -f1 | sed 's/^ //' | sed 's/I //' | sed 's/%%//'",
    789781                            line);
    790782                    FILE *pin = popen(tmp, "r");
     783                    paranoid_free(tmp);
     784
    791785                    char status[MAX_STR_LEN / 4];
    792786                    fgets(status, MAX_STR_LEN / 4 - 1, pin);
     
    806800        }
    807801#else
    808         sprintf(tmp, "Initializing RAID device %s", device);
     802        mr_asprintf(&tmp, "Initializing RAID device %s", device);
    809803        log_to_screen(tmp);
     804        paranoid_free(tmp);
    810805
    811806// Shouldn't be necessary.
     
    856851    }
    857852    res = which_format_command_do_i_need(format, program);
    858     sprintf(tmp, "%s %s", program, device);
     853    mr_asprintf(&tmp, "%s %s", program, device);
    859854    if (strstr(program, "kludge")) {
    860         strcat(tmp, " /");
     855        mr_strcat(tmp, " /");
    861856    }
    862857    sprintf(program, "sh -c 'echo -en \"y\\ny\\ny\\n\" | %s'", tmp);
    863     sprintf(tmp, "Formatting %s as %s", device, format);
     858    paranoid_free(tmp);
     859
     860    mr_asprintf(&tmp, "Formatting %s as %s", device, format);
    864861    update_progress_form(tmp);
     862    paranoid_free(tmp);
     863
    865864    res = run_program_and_log_output(program, FALSE);
     865    mr_asprintf(&tmp, "");
    866866    if (res && strstr(program, "kludge")) {
    867         sprintf(tmp, "Kludge failed; using regular mkfs.%s to format %s",
     867        mr_strcat(tmp, "Kludge failed; using regular mkfs.%s to format %s",
    868868                format, device);
    869869#ifdef __FreeBSD__
     
    885885    retval += res;
    886886    if (retval) {
    887         strcat(tmp, "...failed");
     887        mr_strcat(tmp, "...failed");
    888888    } else {
    889         strcat(tmp, "...OK");
     889        mr_strcat(tmp, "...OK");
    890890    }
    891891
    892892    log_to_screen(tmp);
     893    paranoid_free(tmp);
     894
    893895    paranoid_free(program);
    894     paranoid_free(tmp);
    895896    system("sync");
    896897    sleep(1);
     
    17171718    char *tmp;
    17181719    char *logfile;
    1719     char *output;
     1720    char *output = NULL;
    17201721
    17211722    /** pointers **********************************************************/
     
    17301731    malloc_string(tmp);
    17311732    malloc_string(logfile);
    1732     malloc_string(output);
    17331733
    17341734    assert_string_is_neither_NULL_nor_zerolength(drive);
     
    17451745        paranoid_free(tmp);
    17461746        paranoid_free(logfile);
    1747         paranoid_free(output);
    17481747        return (0);
    17491748    }
     
    17661765        paranoid_free(tmp);
    17671766        paranoid_free(logfile);
    1768         paranoid_free(output);
    17691767        return (1);
    17701768/*
     
    17861784    /* BERLIOS: should not be called each time */
    17871785    part_table_fmt = which_partition_format(drive);
    1788     output[0] = '\0';
     1786    mr_asprintf(&output, "");
    17891787    /* make it a primary/extended/logical */
    17901788    if (partno <= 4) {
    1791         sprintf(output + strlen(output), "n\np\n%d\n", partno);
     1789        mr_strcat(output, "n\np\n%d\n", partno);
    17921790    } else {
    17931791        /* MBR needs an extended partition if more than 4 partitions */
     
    18041802                    return (1);
    18051803                } else {
    1806                     sprintf(output + strlen(output), "n\ne\n%d\n\n\n",
    1807                             prev_partno + 1);
     1804                    mr_strcat(output, "n\ne\n%d\n\n\n", prev_partno + 1);
    18081805                }
    18091806            }
    1810             strcat(output + strlen(output), "n\nl\n");
     1807            mr_strcat(output, "n\nl\n");
    18111808        } else {
    18121809            /* GPT allows more than 4 primary partitions */
    1813             sprintf(output + strlen(output), "n\np\n%d\n", partno);
    1814         }
    1815     }
    1816     strcat(output + strlen(output), "\n");  /*start block (ENTER for next free blk */
     1810            mr_strcat(output, "n\np\n%d\n", partno);
     1811        }
     1812    }
     1813    mr_strcat(output, "\n");    /*start block (ENTER for next free blk */
    18171814    if (partsize > 0) {
    18181815        if (!strcmp(format, "7")) {
     
    18201817            partsize += 512;
    18211818        }
    1822         sprintf(output + strlen(output), "+%lldK", (long long) (partsize));
    1823     }
    1824     strcat(output + strlen(output), "\n");
     1819        mr_strcat(output, "+%lldK", (long long) (partsize));
     1820    }
     1821    mr_strcat(output, "\n");
    18251822#if 0
    18261823/*
     
    18641861        }
    18651862    } else {
    1866         strcat(output, "w\n\n");
     1863        mr_strcat(output, "w\n\n");
    18671864        if (g_fprep) {
    18681865            fprintf(g_fprep, "echo \"%s\" | %s\n", output, program);
     
    19161913        }
    19171914    }
     1915    paranoid_free(output);
     1916
    19181917    g_current_progress++;
    19191918    log_it("partition_device() --- leaving");
     
    19221921    paranoid_free(tmp);
    19231922    paranoid_free(logfile);
    1924     paranoid_free(output);
    19251923    return (retval);
    19261924}
     
    20322030    char *partition;
    20332031    char *command;
    2034     char *output;
     2032    char *output = NULL;
    20352033    char *tmp;
    20362034    char *partcode;
     
    20512049    malloc_string(partition);
    20522050    malloc_string(command);
    2053     malloc_string(output);
    20542051    malloc_string(tmp);
    20552052    malloc_string(partcode);
     
    21382135            fput_string_one_char_at_a_time(pout_to_fdisk, "p\n");
    21392136        } else {
    2140             sprintf(output, "t\n%d\n%s\n", partno, partcode);
    2141             strcat(output, "w\n");
     2137            mr_asprintf(&output, "t\n%d\n%s\nw\n", partno, partcode);
    21422138            sprintf(command, "parted2fdisk %s >> %s 2>> %s", drive,
    21432139                    MONDO_LOGFILE, MONDO_LOGFILE);
     
    21542150                paranoid_pclose(fout);
    21552151            }
     2152            paranoid_free(output);
    21562153        }
    21572154        if (res) {
     
    21622159    paranoid_free(partition);
    21632160    paranoid_free(command);
    2164     paranoid_free(output);
    21652161    paranoid_free(tmp);
    21662162    paranoid_free(partcode);
Note: See TracChangeset for help on using the changeset viewer.