diff options
Diffstat (limited to 'sql/upgrade_conf_file.cc')
-rw-r--r-- | sql/upgrade_conf_file.cc | 211 |
1 files changed, 33 insertions, 178 deletions
diff --git a/sql/upgrade_conf_file.cc b/sql/upgrade_conf_file.cc index c255026fd9c..543df7b9bdf 100644 --- a/sql/upgrade_conf_file.cc +++ b/sql/upgrade_conf_file.cc @@ -158,196 +158,51 @@ static int cmp_strings(const void* a, const void *b) return strcmp((const char *)a, *(const char **)b); } -static bool is_utf8_str(const char *s) +/** + Convert file from a previous version, by removing +*/ +int upgrade_config_file(const char *myini_path) { - const unsigned char *bytes= (const unsigned char *) s; - int num; - - while (*bytes) +#define MY_INI_SECTION_SIZE 32*1024 +3 + static char section_data[MY_INI_SECTION_SIZE]; + for (const char *section_name : { "mysqld","server","mariadb" }) { - if ((*bytes & 0x80) == 0x00) - num= 1; - else if ((*bytes & 0xE0) == 0xC0) - num= 2; - else if ((*bytes & 0xF0) == 0xE0) - num= 3; - else if ((*bytes & 0xF8) == 0xF0) - num= 4; - else - return false; - - bytes++; - for (int i= 1; i < num; i++) + DWORD size = GetPrivateProfileSection(section_name, section_data, MY_INI_SECTION_SIZE, myini_path); + if (size == MY_INI_SECTION_SIZE - 2) { - if ((*bytes & 0xC0) != 0x80) - return false; - bytes++; + return -1; } - } - return true; -} - - -static UINT get_system_acp() -{ - static DWORD system_acp; - if (system_acp) - return system_acp; - - char str_cp[10]; - int cch= GetLocaleInfo(GetSystemDefaultLCID(), LOCALE_IDEFAULTANSICODEPAGE, - str_cp, sizeof(str_cp)); - - system_acp= cch > 0 ? atoi(str_cp) : 1252; - - return system_acp; -} - -#define MY_INI_SECTION_SIZE 32 * 1024 + 3 - -static char *ansi_to_utf8(const char *s) -{ -#define MAX_STR_LEN MY_INI_SECTION_SIZE - static wchar_t utf16_buf[MAX_STR_LEN]; - static char utf8_buf[MAX_STR_LEN]; - if (MultiByteToWideChar(get_system_acp(), 0, s, -1, utf16_buf, MAX_STR_LEN)) - { - if (WideCharToMultiByte(CP_UTF8, 0, utf16_buf, -1, utf8_buf, MAX_STR_LEN, - 0, 0)) - return utf8_buf; - } - return 0; -} - - -int fix_section(const char *myini_path, const char *section_name, - bool is_server) -{ - if (!is_server && GetACP() != CP_UTF8) - return 0; - - static char section_data[MY_INI_SECTION_SIZE]; - DWORD size= GetPrivateProfileSection(section_name, section_data, - MY_INI_SECTION_SIZE, myini_path); - if (size == MY_INI_SECTION_SIZE - 2) - { - return -1; - } - - for (char *keyval= section_data; *keyval; keyval += strlen(keyval)+1) - { - char varname[256]; - char *value; - - char *key_end= strchr(keyval, '='); - if (!key_end) - key_end= keyval + strlen(keyval); - - if (key_end - keyval > sizeof(varname)) - continue; - value= key_end + 1; - if (GetACP() == CP_UTF8 && !is_utf8_str(value)) + for (char *keyval = section_data; *keyval; keyval += strlen(keyval) + 1) { - char *new_val= ansi_to_utf8(value); - if (new_val) + char varname[256]; + char *key_end = strchr(keyval, '='); + if (!key_end) + key_end = keyval+ strlen(keyval); + + if (key_end - keyval > sizeof(varname)) + continue; + // copy and normalize (convert dash to underscore) to variable names + for (char *p = keyval, *q = varname;; p++,q++) { - *key_end= 0; - fprintf(stdout, "Fixing variable '%s' charset, value=%s\n", keyval, - new_val); - WritePrivateProfileString(section_name, keyval, new_val, myini_path); - *key_end= '='; + if (p == key_end) + { + *q = 0; + break; + } + *q = (*p == '-') ? '_' : *p; } - } - if (!is_server) - continue; + const char *v = (const char *)bsearch(varname, removed_variables, sizeof(removed_variables) / sizeof(removed_variables[0]), + sizeof(char *), cmp_strings); - // Check if variable should be removed from config. - // First, copy and normalize (convert dash to underscore) to variable - // names - for (char *p= keyval, *q= varname;; p++, q++) - { - if (p == key_end) + if (v) { - *q= 0; - break; + fprintf(stdout, "Removing variable '%s' from config file\n", varname); + // delete variable + *key_end = 0; + WritePrivateProfileString(section_name, keyval, 0, myini_path); } - *q= (*p == '-') ? '_' : *p; - } - const char *v= (const char *) bsearch(varname, removed_variables, sizeof(removed_variables) / sizeof(removed_variables[0]), - sizeof(char *), cmp_strings); - - if (v) - { - fprintf(stdout, "Removing variable '%s' from config file\n", varname); - // delete variable - *key_end= 0; - WritePrivateProfileString(section_name, keyval, 0, myini_path); } } return 0; } - -static bool is_mariadb_section(const char *name, bool *is_server) -{ - if (strncmp(name, "mysql", 5) - && strncmp(name, "mariadb", 7) - && strcmp(name, "client") - && strcmp(name, "client-server") - && strcmp(name, "server")) - { - return false; - } - - for (const char *section_name : {"mysqld", "server", "mariadb"}) - if (*is_server= !strcmp(section_name, name)) - break; - - return *is_server; -} - - -/** - Convert file from a previous version, by removing obsolete variables - Also, fix values to be UTF8, if MariaDB is running in utf8 mode -*/ -int upgrade_config_file(const char *myini_path) -{ - static char all_sections[MY_INI_SECTION_SIZE]; - int sz= GetPrivateProfileSectionNamesA(all_sections, MY_INI_SECTION_SIZE, - myini_path); - if (!sz) - return 0; - if (sz > MY_INI_SECTION_SIZE - 2) - { - fprintf(stderr, "Too many sections in config file\n"); - return -1; - } - for (char *section= all_sections; *section; section+= strlen(section) + 1) - { - bool is_server_section; - if (is_mariadb_section(section, &is_server_section)) - fix_section(myini_path, section, is_server_section); - } - return 0; -} - -#ifdef MAIN -int main(int argc, char **argv) -{ - if (argc != 2) - { - fprintf(stderr, "Usage : %s <config_file>\n", argv[0]); - return 1; - } - int rc= upgrade_config_file(argv[1]); - if (rc) - { - fprintf(stderr, "upgrade_config_file(\"%s\") returned an error\n", - argv[1]); - return 1; - } - return 0; -} -#endif - |