diff options
author | monty@mishka.local <> | 2005-07-18 15:33:18 +0300 |
---|---|---|
committer | monty@mishka.local <> | 2005-07-18 15:33:18 +0300 |
commit | 78c65b5adc2b5a7160820ceed8273b684cc1e8e8 (patch) | |
tree | 1505fc52926154e1f91d7d822237aa1b9352cb3f /mysys | |
parent | 774916a902fd33a0d1260e9dd5dc9ecd181a8703 (diff) | |
download | mariadb-git-78c65b5adc2b5a7160820ceed8273b684cc1e8e8.tar.gz |
Cleanups during review
Changed defaults option --instance to --defaults-group-suffix
Changed option handling to allow --defaults-file, --defaults-extra-file and --defaults-group-suffix to be given in any order
Changed MYSQL_INSTANCE to MYSQL_GROUP_SUFFIX
mysql_print_defaults now understands --defaults-group-suffix
Remove usage of my_tempnam() (not safe function)
if( -> if ( and while( to while (
Diffstat (limited to 'mysys')
-rw-r--r-- | mysys/Makefile.am | 3 | ||||
-rw-r--r-- | mysys/default.c | 190 | ||||
-rw-r--r-- | mysys/default_modify.c | 65 | ||||
-rw-r--r-- | mysys/my_bitmap.c | 1 | ||||
-rw-r--r-- | mysys/my_tempnam.c | 173 |
5 files changed, 140 insertions, 292 deletions
diff --git a/mysys/Makefile.am b/mysys/Makefile.am index 03ee692645f..0007a8808bd 100644 --- a/mysys/Makefile.am +++ b/mysys/Makefile.am @@ -44,7 +44,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c my_mmap.c \ ptr_cmp.c mf_radix.c queues.c \ tree.c list.c hash.c array.c string.c typelib.c \ my_copy.c my_append.c my_lib.c \ - my_delete.c my_rename.c my_redel.c my_tempnam.c \ + my_delete.c my_rename.c my_redel.c \ my_chsize.c my_lread.c my_lwrite.c my_clock.c \ my_quick.c my_lockmem.c my_static.c \ my_sync.c my_getopt.c my_mkdir.c \ @@ -68,6 +68,7 @@ DEFS = -DDEFAULT_BASEDIR=\"$(prefix)\" \ -DDEFAULT_CHARSET_HOME="\"$(MYSQLBASEdir)\"" \ -DSHAREDIR="\"$(MYSQLSHAREdir)\"" \ -DDEFAULT_HOME_ENV=MYSQL_HOME \ + -DDEFAULT_GROUP_SUFFIX_ENV=MYSQL_GROUP_SUFFIX \ @DEFS@ libmysys_a_DEPENDENCIES= @THREAD_LOBJECTS@ diff --git a/mysys/default.c b/mysys/default.c index 1fa8deaa65c..994749f1c54 100644 --- a/mysys/default.c +++ b/mysys/default.c @@ -30,8 +30,8 @@ --no-defaults ; no options are read. --defaults-file=full-path-to-default-file ; Only this file will be read. --defaults-extra-file=full-path-to-default-file ; Read this file before ~/ - --print-defaults ; Print the modified command line and exit - --instance ; also read groups with concat(group, instance) + --defaults-group-suffix ; Also read groups with concat(group, suffix) + --print-defaults ; Print the modified command line and exit ****************************************************************************/ #include "mysys_priv.h" @@ -42,8 +42,7 @@ #include <winbase.h> #endif -const char *defaults_instance=0; -static const char instance_option[] = "--instance="; +const char *defaults_group_suffix=0; char *defaults_extra_file=0; /* Which directories are searched for options (and in which order) */ @@ -60,6 +59,9 @@ static const char *f_extensions[]= { ".cnf", 0 }; #define NEWLINE "\n" #endif +static int handle_default_option(void *in_ctx, const char *group_name, + const char *option); + /* This structure defines the context that we pass to callback function 'handle_default_option' used in search_default_file @@ -100,35 +102,81 @@ static char *remove_end_comment(char *ptr); func_ctx It's context. Usually it is the structure to store additional options. DESCRIPTION + Process the default options from argc & argv + Read through each found config file looks and calls 'func' to process + each option. + + NOTES + --defaults-group-suffix is only processed if we are called from + load_defaults(). - This function looks for config files in default directories. Then it - travesrses each of the files and calls func to process each option. RETURN 0 ok 1 given cinf_file doesn't exist + + The global variable 'defaults_group_suffix' is updated with value for + --defaults_group_suffix */ int my_search_option_files(const char *conf_file, int *argc, char ***argv, - uint *args_used, Process_option_func func, - void *func_ctx) + uint *args_used, Process_option_func func, + void *func_ctx) { const char **dirs, *forced_default_file, *forced_extra_defaults; int error= 0; DBUG_ENTER("my_search_option_files"); /* Check if we want to force the use a specific default file */ - get_defaults_files(*argc - *args_used, *argv + *args_used, - (char **)&forced_default_file, - (char **)&forced_extra_defaults); - if (forced_default_file) - forced_default_file= strchr(forced_default_file,'=')+1; - if (forced_extra_defaults) - defaults_extra_file= strchr(forced_extra_defaults,'=')+1; + *args_used+= get_defaults_options(*argc - *args_used, *argv + *args_used, + (char **) &forced_default_file, + (char **) &forced_extra_defaults, + (char **) &defaults_group_suffix); - (*args_used)+= (forced_default_file ? 1 : 0) + - (forced_extra_defaults ? 1 : 0); + if (! defaults_group_suffix) + defaults_group_suffix= getenv(STRINGIFY_ARG(DEFAULT_GROUP_SUFFIX_ENV)); + + /* + We can only handle 'defaults-group-suffix' if we are called from + load_defaults() as otherwise we can't know the type of 'func_ctx' + */ + if (defaults_group_suffix && func == handle_default_option) + { + /* Handle --defaults-group-suffix= */ + uint i; + const char **extra_groups; + const uint instance_len= strlen(defaults_group_suffix); + struct handle_option_ctx *ctx= (struct handle_option_ctx*) func_ctx; + char *ptr; + TYPELIB *group= ctx->group; + + if (!(extra_groups= + (const char**)alloc_root(ctx->alloc, + (2*group->count+1)*sizeof(char*)))) + goto err; + + for (i= 0; i < group->count; i++) + { + uint len; + extra_groups[i]= group->type_names[i]; /** copy group */ + + len= strlen(extra_groups[i]); + if (!(ptr= alloc_root(ctx->alloc, len+instance_len+1))) + goto err; + + extra_groups[i+group->count]= ptr; + + /** Construct new group */ + memcpy(ptr, extra_groups[i], len); + memcpy(ptr+len, defaults_group_suffix, instance_len+1); + } + + group->count*= 2; + group->type_names= extra_groups; + group->type_names[group->count]= 0; + } + if (forced_default_file) { if ((error= search_default_file_with_ext(func, func_ctx, "", "", @@ -221,32 +269,54 @@ static int handle_default_option(void *in_ctx, const char *group_name, /* - Gets --defaults-file and --defaults-extra-file options from command line. + Gets options from the command line SYNOPSIS - get_defaults_files() + get_defaults_options() argc Pointer to argc of original program argv Pointer to argv of original program defaults --defaults-file option extra_defaults --defaults-extra-file option RETURN - defaults and extra_defaults will be set to appropriate items - of argv array, or to NULL if there are no such options + # Number of arguments used from *argv + defaults and extra_defaults will be set to option of the appropriate + items of argv array, or to NULL if there are no such options */ -void get_defaults_files(int argc, char **argv, - char **defaults, char **extra_defaults) +int get_defaults_options(int argc, char **argv, + char **defaults, + char **extra_defaults, + char **group_suffix) { - *defaults=0; - *extra_defaults=0; - if (argc >= 2) + int org_argc= argc, prev_argc= 0; + *defaults= *extra_defaults= *group_suffix= 0; + + while (argc >= 2 && argc != prev_argc) { - if (is_prefix(argv[1],"--defaults-file=")) - *defaults= argv[1]; - else if (is_prefix(argv[1],"--defaults-extra-file=")) - *extra_defaults= argv[1]; + /* Skip program name or previously handled argument */ + argv++; + prev_argc= argc; /* To check if we found */ + if (!*defaults && is_prefix(*argv,"--defaults-file=")) + { + *defaults= *argv + sizeof("--defaults-file=")-1; + argc--; + continue; + } + if (!*extra_defaults && is_prefix(*argv,"--defaults-extra-file=")) + { + *extra_defaults= *argv + sizeof("--defaults-extra-file=")-1; + argc--; + continue; + } + if (!*group_suffix && is_prefix(*argv, "--defaults-group-suffix=")) + { + *group_suffix= *argv + sizeof("--defaults-group-suffix=")-1; + argc--; + continue; + } } + return org_argc - argc; } @@ -296,6 +366,10 @@ int load_defaults(const char *conf_file, const char **groups, init_default_directories(); init_alloc_root(&alloc,512,0); + /* + Check if the user doesn't want any default option processing + --no-defaults is always the first option + */ if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults")) { /* remove the --no-defaults argument and return only the other arguments */ @@ -328,51 +402,8 @@ int load_defaults(const char *conf_file, const char **groups, ctx.args= &args; ctx.group= &group; - if (*argc >= 2 + args_used && - is_prefix(argv[0][1+args_used], instance_option)) - { - args_used++; - defaults_instance= argv[0][args_used]+sizeof(instance_option)-1; - } - else - { - defaults_instance= getenv("MYSQL_INSTANCE"); - } - - if (defaults_instance) - { - /** Handle --instance= */ - uint i, len; - const char **extra_groups; - const uint instance_len= strlen(defaults_instance); - - if (!(extra_groups= - (const char**)alloc_root(&alloc, (2*group.count+1)*sizeof(char*)))) - goto err; - - for (i= 0; i<group.count; i++) - { - extra_groups[i]= group.type_names[i]; /** copy group */ - - len= strlen(extra_groups[i]); - if (!(ptr= alloc_root(&alloc, len+instance_len+1))) - goto err; - - extra_groups[i+group.count]= ptr; - - /** Construct new group */ - memcpy(ptr, extra_groups[i], len); - ptr+= len; - memcpy(ptr, defaults_instance, instance_len+1); - } - - group.count*= 2; - group.type_names= extra_groups; - group.type_names[group.count]= 0; - } - error= my_search_option_files(conf_file, argc, argv, &args_used, - handle_default_option, (void *) &ctx); + handle_default_option, (void *) &ctx); /* Here error contains <> 0 only if we have a fully specified conf_file or a forced default file @@ -385,11 +416,14 @@ int load_defaults(const char *conf_file, const char **groups, /* copy name + found arguments + command line arguments to new array */ res[0]= argv[0][0]; /* Name MUST be set, even by embedded library */ memcpy((gptr) (res+1), args.buffer, args.elements*sizeof(char*)); - /* Skip --defaults-file and --defaults-extra-file */ + /* Skip --defaults-xxx options */ (*argc)-= args_used; (*argv)+= args_used; - /* Check if we wan't to see the new argument list */ + /* + Check if we wan't to see the new argument list + This options must always be the last of the default options + */ if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults")) { found_print_defaults=1; @@ -850,14 +884,14 @@ void print_defaults(const char *conf_file, const char **groups) fputs(*groups,stdout); } - if (defaults_instance) + if (defaults_group_suffix) { groups= groups_save; for ( ; *groups ; groups++) { fputc(' ',stdout); fputs(*groups,stdout); - fputs(defaults_instance,stdout); + fputs(defaults_group_suffix,stdout); } } puts("\nThe following options may be given as the first argument:\n\ diff --git a/mysys/default_modify.c b/mysys/default_modify.c index ea384f9f27a..de03d783c68 100644 --- a/mysys/default_modify.c +++ b/mysys/default_modify.c @@ -20,8 +20,7 @@ #include <my_dir.h> #define BUFF_SIZE 1024 -/* should be big enough to handle at least one line */ -#define RESERVE 1024 +#define RESERVE 1024 /* Extend buffer with this extent */ #ifdef __WIN__ #define NEWLINE "\r\n" @@ -70,7 +69,7 @@ int modify_defaults_file(const char *file_location, const char *option, char linebuff[BUFF_SIZE], *src_ptr, *dst_ptr, *file_buffer; uint opt_len, optval_len, sect_len, nr_newlines= 0, buffer_size; my_bool in_section= FALSE, opt_applied= 0; - uint reserve_extended= 1, old_opt_len= 0; + uint reserve_extended; uint new_opt_len; int reserve_occupied= 0; DBUG_ENTER("modify_defaults_file"); @@ -88,25 +87,21 @@ int modify_defaults_file(const char *file_location, const char *option, new_opt_len= opt_len + 1 + optval_len + NEWLINE_LEN; /* calculate the size of the buffer we need */ - buffer_size= sizeof(char) * (file_stat.st_size + - /* option name len */ - opt_len + - /* reserve for '=' char */ - 1 + - /* option value len */ - optval_len + - /* reserve space for newline */ - NEWLINE_LEN + - /* The ending zero */ - 1 + - /* reserve some additional space */ - RESERVE); + reserve_extended= (opt_len + + 1 + /* For '=' char */ + optval_len + /* Option value len */ + NEWLINE_LEN + /* Space for newline */ + RESERVE); /* Some additional space */ + + buffer_size= (file_stat.st_size + + 1); /* The ending zero */ /* Reserve space to read the contents of the file and some more for the option we want to add. */ - if (!(file_buffer= (char*) my_malloc(buffer_size, MYF(MY_WME)))) + if (!(file_buffer= (char*) my_malloc(buffer_size + reserve_extended, + MYF(MY_WME)))) goto malloc_err; sect_len= (uint) strlen(section_name); @@ -130,31 +125,20 @@ int modify_defaults_file(const char *file_location, const char *option, my_isspace(&my_charset_latin1, *(src_ptr + opt_len)) || *(src_ptr + opt_len) == '\0')) { - /* - we should change all options. If opt_applied is set, we are running - into reserved memory area. Hence we should check for overruns. - */ - if (opt_applied) + char *old_src_ptr= src_ptr; + src_ptr= strend(src_ptr+ opt_len); /* Find the end of the line */ + + /* could be negative */ + reserve_occupied+= (int) new_opt_len - (int) (src_ptr - old_src_ptr); + if (reserve_occupied >= (int) reserve_extended) { - src_ptr+= opt_len; /* If we correct an option, we know it's name */ - old_opt_len= opt_len; - - while (*src_ptr++) /* Find the end of the line */ - old_opt_len++; - - /* could be negative */ - reserve_occupied+= (int) new_opt_len - (int) old_opt_len; - if ((int) reserve_occupied > (int) (RESERVE*reserve_extended)) - { - if (!(file_buffer= (char*) my_realloc(file_buffer, buffer_size + - RESERVE*reserve_extended, - MYF(MY_WME|MY_FREE_ON_ERROR)))) - goto malloc_err; - reserve_extended++; - } + reserve_extended= (uint) reserve_occupied + RESERVE; + if (!(file_buffer= (char*) my_realloc(file_buffer, buffer_size + + reserve_extended, + MYF(MY_WME|MY_FREE_ON_ERROR)))) + goto malloc_err; } - else - opt_applied= 1; + opt_applied= 1; dst_ptr= add_option(dst_ptr, option_value, option, remove_option); } else @@ -164,6 +148,7 @@ int modify_defaults_file(const char *file_location, const char *option, { dst_ptr= add_option(dst_ptr, option_value, option, remove_option); opt_applied= 1; /* set the flag to do write() later */ + reserve_occupied= new_opt_len+ opt_len + 1 + NEWLINE_LEN; } for (; nr_newlines; nr_newlines--) diff --git a/mysys/my_bitmap.c b/mysys/my_bitmap.c index ba958b234d2..c70c0fa0754 100644 --- a/mysys/my_bitmap.c +++ b/mysys/my_bitmap.c @@ -152,6 +152,7 @@ my_bool bitmap_test_and_set(MY_BITMAP *map, uint bitmap_bit) bitmap_lock(map); res= bitmap_fast_test_and_set(map, bitmap_bit); bitmap_unlock(map); + return res; } uint bitmap_set_next(MY_BITMAP *map) diff --git a/mysys/my_tempnam.c b/mysys/my_tempnam.c deleted file mode 100644 index 9f765298fb6..00000000000 --- a/mysys/my_tempnam.c +++ /dev/null @@ -1,173 +0,0 @@ -/* Copyright (C) 2000 MySQL AB - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - -/* - This function is only used by some old ISAM code. - When we remove ISAM support from MySQL, we should also delete this file - - One should instead use the functions in mf_tempfile.c -*/ - -#include "mysys_priv.h" -#include <m_string.h> - -/* HPUX 11.0 doesn't allow us to change the environ pointer */ -#ifdef HPUX11 -#undef HAVE_TEMPNAM -#endif - -#include "my_static.h" -#include "mysys_err.h" - -#define TMP_EXT ".tmp" /* Extension of tempfile */ -#if ! defined(P_tmpdir) -#define P_tmpdir "" -#endif - -#ifdef HAVE_TEMPNAM -#if !defined( MSDOS) && !defined(OS2) && !defined(__NETWARE__) -extern char **environ; -#endif -#endif - -/* Make a uniq temp file name by using dir and adding something after - pfx to make name uniq. Name is made by adding a uniq 8 length-string and - TMP_EXT after pfx. - Returns pointer to malloced area for filename. Should be freed by - free(). - The name should be uniq, but it isn't checked if it file allready exists. - Uses tempnam() if function exist on system. - This function fixes that if dir is given it's used. For example - MSDOS tempnam() uses always TMP environment-variable if it exists. -*/ - /* ARGSUSED */ - -my_string my_tempnam(const char *dir, const char *pfx, - myf MyFlags __attribute__((unused))) -{ -#ifdef _MSC_VER - char temp[FN_REFLEN],*end,*res,**old_env,*temp_env[1]; - old_env=environ; - if (dir) - { - end=strend(dir)-1; - if (!dir[0]) - { /* Change empty string to current dir */ - temp[0]= FN_CURLIB; - temp[1]= 0; - dir=temp; - } - else if (*end == FN_DEVCHAR) - { /* Get current dir for drive */ - _fullpath(temp,dir,FN_REFLEN); - dir=temp; - } - else if (*end == FN_LIBCHAR && dir < end && end[-1] != FN_DEVCHAR) - { - strmake(temp,dir,(uint) (end-dir)); /* Copy and remove last '\' */ - dir=temp; - } - environ=temp_env; /* Force use of dir (dir not checked) */ - temp_env[0]=0; - } - res=tempnam((char*) dir,(my_string) pfx); - environ=old_env; - return res; -#else -#ifdef __ZTC__ - if (!dir) - { /* If empty test first if TMP can be used */ - dir=getenv("TMP"); - } - return tempnam((char*) dir,(my_string) pfx); /* Use stand. dir with prefix */ -#else -#ifdef HAVE_TEMPNAM - char temp[2],*res,**old_env,*temp_env[1]; - - if (dir && !dir[0]) - { /* Change empty string to current dir */ - temp[0]= FN_CURLIB; - temp[1]= 0; - dir=temp; - } -#ifdef OS2 - /* changing environ variable doesn't work with VACPP */ - char buffer[256], *end; - buffer[sizeof[buffer)-1]= 0; - end= strxnmov(buffer, sizeof(buffer)-1, (char*) "TMP=", dir, NullS); - /* remove ending backslash */ - if (end[-1] == '\\') - end[-1]= 0; - putenv(buffer); -#elif !defined(__NETWARE__) - old_env=(char**)environ; - if (dir) - { /* Don't use TMPDIR if dir is given */ - /* - The following strange cast is required because the IBM compiler on AIX - doesn't allow us to cast the value of environ. - The cast of environ is needed as some systems doesn't allow us to - update environ with a char ** pointer. (const mismatch) - */ - (*(char***) &environ)=(char**) temp_env; - temp_env[0]=0; - } -#endif - res=tempnam((char*) dir,(my_string) pfx); /* Use stand. dir with prefix */ -#if !defined(OS2) && !defined(__NETWARE__) - (*(char***) &environ)=(char**) old_env; -#endif - if (!res) - DBUG_PRINT("error",("Got error: %d from tempnam",errno)); - return res; -#else - register long uniq; - register int length; - my_string pos,end_pos; - DBUG_ENTER("my_tempnam"); - /* Make a uniq nummber */ - pthread_mutex_lock(&THR_LOCK_open); - uniq= ((long) getpid() << 20) + (long) _my_tempnam_used++ ; - pthread_mutex_unlock(&THR_LOCK_open); - if (!dir && !(dir=getenv("TMPDIR"))) /* Use this if possibly */ - dir=P_tmpdir; /* Use system default */ - length=strlen(dir)+strlen(pfx)+1; - - DBUG_PRINT("test",("mallocing %d byte",length+8+sizeof(TMP_EXT)+1)); - if (!(pos=(char*) malloc(length+8+sizeof(TMP_EXT)+1))) - { - if (MyFlags & MY_FAE+MY_WME) - my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG), - length+8+sizeof(TMP_EXT)+1); - DBUG_RETURN(NullS); - } - end_pos=strmov(pos,dir); - if (end_pos != pos && end_pos[-1] != FN_LIBCHAR) - *end_pos++=FN_LIBCHAR; - end_pos=strmov(end_pos,pfx); - - for (length=0 ; length < 8 && uniq ; length++) - { - *end_pos++= _dig_vec_upper[(int) (uniq & 31)]; - uniq >>= 5; - } - VOID(strmov(end_pos,TMP_EXT)); - DBUG_PRINT("exit",("tempnam: '%s'",pos)); - DBUG_RETURN(pos); -#endif /* HAVE_TEMPNAM */ -#endif /* __ZTC__ */ -#endif /* _MSC_VER */ -} /* my_tempnam */ |