~jlukas79/+junk/mysql-server

« back to all changes in this revision

Viewing changes to mysys/default.c

manual merge 6.0-main --> 6.0-bka-review

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
/* Which directories are searched for options (and in which order) */
49
49
 
50
50
#define MAX_DEFAULT_DIRS 6
51
 
const char *default_directories[MAX_DEFAULT_DIRS + 1];
 
51
#define DEFAULT_DIRS_SIZE (MAX_DEFAULT_DIRS + 1)  /* Terminate with NULL */
 
52
static const char **default_directories = NULL;
52
53
 
53
54
#ifdef __WIN__
54
55
static const char *f_extensions[]= { ".ini", ".cnf", 0 };
55
56
#define NEWLINE "\r\n"
56
 
static char system_dir[FN_REFLEN], shared_system_dir[FN_REFLEN],
57
 
            config_dir[FN_REFLEN];
58
57
#else
59
58
static const char *f_extensions[]= { ".cnf", 0 };
60
59
#define NEWLINE "\n"
85
84
                                        const char *config_file, int recursion_level);
86
85
 
87
86
 
88
 
 
89
87
/**
90
88
  Create the list of default directories.
91
89
 
 
90
  @param alloc  MEM_ROOT where the list of directories is stored
 
91
 
92
92
  @details
 
93
  The directories searched, in order, are:
 
94
  - Windows:     GetSystemWindowsDirectory()
 
95
  - Windows:     GetWindowsDirectory()
 
96
  - Windows:     C:/
 
97
  - Windows:     Directory above where the executable is located
 
98
  - Netware:     sys:/etc/
 
99
  - Unix:        /etc/
 
100
  - Unix:        /etc/mysql/
 
101
  - Unix:        --sysconfdir=<path> (compile-time option)
 
102
  - ALL:         getenv(DEFAULT_HOME_ENV)
 
103
  - ALL:         --defaults-extra-file=<path> (run-time option)
 
104
  - Unix:        ~/
 
105
 
93
106
  On all systems, if a directory is already in the list, it will be moved
94
107
  to the end of the list.  This avoids reading defaults files multiple times,
95
108
  while ensuring the correct precedence.
96
109
 
97
 
  @return void
 
110
  @retval NULL  Failure (out of memory, probably)
 
111
  @retval other Pointer to NULL-terminated array of default directories
98
112
*/
99
113
 
100
 
static void (*init_default_directories)();
 
114
static const char **init_default_directories(MEM_ROOT *alloc);
101
115
 
102
116
 
103
117
static char *remove_end_comment(char *ptr);
390
404
  struct handle_option_ctx ctx;
391
405
  DBUG_ENTER("load_defaults");
392
406
 
393
 
  init_default_directories();
394
407
  init_alloc_root(&alloc,512,0);
 
408
  if ((default_directories= init_default_directories(&alloc)) == NULL)
 
409
    goto err;
395
410
  /*
396
411
    Check if the user doesn't want any default option processing
397
412
    --no-defaults is always the first option
873
888
  my_bool have_ext= fn_ext(conf_file)[0] != 0;
874
889
  const char **exts_to_use= have_ext ? empty_list : f_extensions;
875
890
  char name[FN_REFLEN], **ext;
876
 
  const char **dirs;
877
891
 
878
 
  init_default_directories();
879
892
  puts("\nDefault options are read from the following files in the given order:");
880
893
 
881
894
  if (dirname_length(conf_file))
882
895
    fputs(conf_file,stdout);
883
896
  else
884
897
  {
885
 
    for (dirs=default_directories ; *dirs; dirs++)
886
 
    {
887
 
      for (ext= (char**) exts_to_use; *ext; ext++)
 
898
    /*
 
899
      If default_directories is already initialized, use it.  Otherwise,
 
900
      use a private MEM_ROOT.
 
901
    */
 
902
    const char **dirs = default_directories;
 
903
    MEM_ROOT alloc;
 
904
    init_alloc_root(&alloc,512,0);
 
905
 
 
906
    if (!dirs && (dirs= init_default_directories(&alloc)) == NULL)
 
907
    {
 
908
      fputs("Internal error initializing default directories list", stdout);
 
909
    }
 
910
    else
 
911
    {
 
912
      for ( ; *dirs; dirs++)
888
913
      {
889
 
        const char *pos;
890
 
        char *end;
891
 
        if (**dirs)
892
 
          pos= *dirs;
893
 
        else if (my_defaults_extra_file)
894
 
          pos= my_defaults_extra_file;
895
 
        else
896
 
          continue;
897
 
        end= convert_dirname(name, pos, NullS);
898
 
        if (name[0] == FN_HOMELIB)      /* Add . to filenames in home */
899
 
          *end++='.';
900
 
        strxmov(end, conf_file, *ext, " ", NullS);
901
 
        fputs(name,stdout);
 
914
        for (ext= (char**) exts_to_use; *ext; ext++)
 
915
        {
 
916
          const char *pos;
 
917
          char *end;
 
918
          if (**dirs)
 
919
            pos= *dirs;
 
920
          else if (my_defaults_extra_file)
 
921
            pos= my_defaults_extra_file;
 
922
          else
 
923
            continue;
 
924
          end= convert_dirname(name, pos, NullS);
 
925
          if (name[0] == FN_HOMELIB)    /* Add . to filenames in home */
 
926
            *end++= '.';
 
927
          strxmov(end, conf_file, *ext, " ", NullS);
 
928
          fputs(name, stdout);
 
929
        }
902
930
      }
903
931
    }
 
932
 
 
933
    free_root(&alloc, MYF(0));
904
934
  }
905
935
  puts("");
906
936
}
937
967
#include <help_end.h>
938
968
 
939
969
 
940
 
/*
941
 
  This extra complexity is to avoid declaring 'rc' if it won't be
942
 
  used.
943
 
*/
944
 
#define ADD_DIRECTORY_INTERNAL(DIR) \
945
 
  array_append_string_unique((DIR), default_directories, \
946
 
                             array_elements(default_directories))
947
 
#ifdef DBUG_OFF
948
 
#  define ADD_DIRECTORY(DIR)  (void) ADD_DIRECTORY_INTERNAL(DIR)
949
 
#else
950
 
#define ADD_DIRECTORY(DIR) \
951
 
  do { \
952
 
    my_bool rc= ADD_DIRECTORY_INTERNAL(DIR); \
953
 
    DBUG_ASSERT(rc == FALSE);                   /* Success */ \
954
 
  } while (0)
955
 
#endif
956
 
 
957
 
 
958
 
#define ADD_COMMON_DIRECTORIES() \
959
 
  do { \
960
 
    char *env; \
961
 
    if ((env= getenv(STRINGIFY_ARG(DEFAULT_HOME_ENV)))) \
962
 
      ADD_DIRECTORY(env); \
963
 
    /* Placeholder for --defaults-extra-file=<path> */ \
964
 
    ADD_DIRECTORY(""); \
965
 
  } while (0)
 
970
static int add_directory(MEM_ROOT *alloc, const char *dir, const char **dirs)
 
971
{
 
972
  char buf[FN_REFLEN];
 
973
  uint len;
 
974
  char *p;
 
975
  my_bool err __attribute__((unused));
 
976
 
 
977
  /* Normalize directory name */
 
978
  len= unpack_dirname(buf, dir);
 
979
  if (!(p= strmake_root(alloc, buf, len)))
 
980
    return 1;  /* Failure */
 
981
  /* Should never fail if DEFAULT_DIRS_SIZE is correct size */
 
982
  err= array_append_string_unique(p, dirs, DEFAULT_DIRS_SIZE);
 
983
  DBUG_ASSERT(err == FALSE);
 
984
 
 
985
  return 0;
 
986
}
966
987
 
967
988
 
968
989
#ifdef __WIN__
1001
1022
}
1002
1023
 
1003
1024
 
1004
 
/**
1005
 
  Initialize default directories for Microsoft Windows
1006
 
 
1007
 
  @details
1008
 
    1. GetSystemWindowsDirectory()
1009
 
    2. GetWindowsDirectory()
1010
 
    3. C:/
1011
 
    4. Directory above where the executable is located
1012
 
    5. getenv(DEFAULT_HOME_ENV)
1013
 
    6. --defaults-extra-file=<path> (run-time option)
1014
 
*/
1015
 
 
1016
 
static void init_default_directories_win()
 
1025
static const char *my_get_module_parent(char *buf, size_t size)
1017
1026
{
1018
 
  bzero((char *) default_directories, sizeof(default_directories));
1019
 
 
1020
 
  if (my_get_system_windows_directory(shared_system_dir,
1021
 
                                      sizeof(shared_system_dir)))
1022
 
    ADD_DIRECTORY(shared_system_dir);
1023
 
 
1024
 
  if (GetWindowsDirectory(system_dir,sizeof(system_dir)))
1025
 
    ADD_DIRECTORY(system_dir);
1026
 
 
1027
 
  ADD_DIRECTORY("C:/");
1028
 
 
1029
 
  if (GetModuleFileName(NULL, config_dir, sizeof(config_dir)))
 
1027
  char *last= NULL;
 
1028
  char *end;
 
1029
  if (!GetModuleFileName(NULL, buf, size))
 
1030
    return NULL;
 
1031
  end= strend(buf);
 
1032
 
 
1033
  /*
 
1034
    Look for the second-to-last \ in the filename, but hang on
 
1035
    to a pointer after the last \ in case we're in the root of
 
1036
    a drive.
 
1037
  */
 
1038
  for ( ; end > buf; end--)
1030
1039
  {
1031
 
    char *last= NULL, *end= strend(config_dir);
1032
 
    /*
1033
 
      Look for the second-to-last \ in the filename, but hang on
1034
 
      to a pointer after the last \ in case we're in the root of
1035
 
      a drive.
1036
 
    */
1037
 
    for ( ; end > config_dir; end--)
 
1040
    if (*end == FN_LIBCHAR)
1038
1041
    {
1039
 
      if (*end == FN_LIBCHAR)
 
1042
      if (last)
1040
1043
      {
1041
 
        if (last)
1042
 
        {
1043
 
          if (end != config_dir)
1044
 
          {
1045
 
            /* Keep the last '\' as this works both with D:\ and a directory */
1046
 
            end[1]= 0;
1047
 
          }
1048
 
          else
1049
 
          {
1050
 
            /* No parent directory (strange). Use current dir + '\' */
1051
 
            last[1]= 0;
1052
 
          }
1053
 
          break;
1054
 
        }
1055
 
        last= end;
 
1044
        /* Keep the last '\' as this works both with D:\ and a directory */
 
1045
        end[1]= 0;
 
1046
        break;
1056
1047
      }
 
1048
      last= end;
1057
1049
    }
1058
 
    ADD_DIRECTORY(config_dir);
1059
1050
  }
1060
1051
 
1061
 
  ADD_COMMON_DIRECTORIES();
 
1052
  return buf;
1062
1053
}
1063
 
 
1064
 
static void (*init_default_directories)()= init_default_directories_win;
 
1054
#endif /* __WIN__ */
 
1055
 
 
1056
 
 
1057
static const char **init_default_directories(MEM_ROOT *alloc)
 
1058
{
 
1059
  const char **dirs;
 
1060
  char *env;
 
1061
  int errors= 0;
 
1062
 
 
1063
  dirs= (const char **)alloc_root(alloc, DEFAULT_DIRS_SIZE * sizeof(char *));
 
1064
  if (dirs == NULL)
 
1065
    return NULL;
 
1066
  bzero((char *) dirs, DEFAULT_DIRS_SIZE * sizeof(char *));
 
1067
 
 
1068
#ifdef __WIN__
 
1069
 
 
1070
  {
 
1071
    char fname_buffer[FN_REFLEN];
 
1072
    if (my_get_system_windows_directory(fname_buffer, sizeof(fname_buffer)))
 
1073
      errors += add_directory(alloc, fname_buffer, dirs);
 
1074
 
 
1075
    if (GetWindowsDirectory(fname_buffer, sizeof(fname_buffer)))
 
1076
      errors += add_directory(alloc, fname_buffer, dirs);
 
1077
 
 
1078
    errors += add_directory(alloc, "C:/", dirs);
 
1079
 
 
1080
    if (my_get_module_parent(fname_buffer, sizeof(fname_buffer)) != NULL)
 
1081
      errors += add_directory(alloc, fname_buffer, dirs);
 
1082
  }
1065
1083
 
1066
1084
#elif defined(__NETWARE__)
1067
1085
 
1068
 
/**
1069
 
  Initialize default directories for Novell Netware
1070
 
 
1071
 
  @details
1072
 
    1. sys:/etc/
1073
 
    2. getenv(DEFAULT_HOME_ENV)
1074
 
    3. --defaults-extra-file=<path> (run-time option)
1075
 
*/
1076
 
 
1077
 
static void init_default_directories_netware()
1078
 
{
1079
 
  bzero((char *) default_directories, sizeof(default_directories));
1080
 
  ADD_DIRECTORY("sys:/etc/");
1081
 
  ADD_COMMON_DIRECTORIES();
1082
 
}
1083
 
 
1084
 
static void (*init_default_directories)()= init_default_directories_netware;
 
1086
  errors += add_directory(alloc, "sys:/etc/", dirs);
1085
1087
 
1086
1088
#else
1087
1089
 
1088
 
/**
1089
 
  Initialize default directories for Unix
1090
 
 
1091
 
  @details
1092
 
    1. /etc/
1093
 
    2. /etc/mysql/
1094
 
    3. --sysconfdir=<path> (compile-time option)
1095
 
    4. getenv(DEFAULT_HOME_ENV)
1096
 
    5. --defaults-extra-file=<path> (run-time option)
1097
 
    6. "~/"
1098
 
*/
1099
 
 
1100
 
static void init_default_directories_unix()
1101
 
{
1102
 
  bzero((char *) default_directories, sizeof(default_directories));
1103
 
  ADD_DIRECTORY("/etc/");
1104
 
  ADD_DIRECTORY("/etc/mysql/");
1105
 
#ifdef DEFAULT_SYSCONFDIR
 
1090
  errors += add_directory(alloc, "/etc/", dirs);
 
1091
  errors += add_directory(alloc, "/etc/mysql/", dirs);
 
1092
 
 
1093
#if defined(DEFAULT_SYSCONFDIR)
1106
1094
  if (DEFAULT_SYSCONFDIR != "")
1107
 
    ADD_DIRECTORY(DEFAULT_SYSCONFDIR);
1108
 
#endif
1109
 
  ADD_COMMON_DIRECTORIES();
1110
 
  ADD_DIRECTORY("~/");
 
1095
    errors += add_directory(alloc, DEFAULT_SYSCONFDIR, dirs);
 
1096
#endif /* DEFAULT_SYSCONFDIR */
 
1097
 
 
1098
#endif
 
1099
 
 
1100
  if ((env= getenv(STRINGIFY_ARG(DEFAULT_HOME_ENV))))
 
1101
    errors += add_directory(alloc, env, dirs);
 
1102
 
 
1103
  /* Placeholder for --defaults-extra-file=<path> */
 
1104
  errors += add_directory(alloc, "", dirs);
 
1105
 
 
1106
#if !defined(__WIN__) && !defined(__NETWARE__)
 
1107
  errors += add_directory(alloc, "~/", dirs);
 
1108
#endif
 
1109
 
 
1110
  return (errors > 0 ? NULL : dirs);
1111
1111
}
1112
 
 
1113
 
static void (*init_default_directories)()= init_default_directories_unix;
1114
 
 
1115
 
#endif