Skip to content

Commit da93a36

Browse files
author
msvensson@pilot.blaudden
committed
mysql_upgrade portability fixes
1 parent 2c95135 commit da93a36

File tree

4 files changed

+46
-78
lines changed

4 files changed

+46
-78
lines changed

client/mysql_upgrade.c

+35-76
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,25 @@
1717
#include <sslopt-vars.h>
1818
#include "../scripts/mysql_fix_privilege_tables_sql.c"
1919

20+
#ifdef HAVE_SYS_WAIT_H
21+
#include <sys/wait.h>
22+
#endif
23+
24+
#ifndef WEXITSTATUS
25+
# ifdef __WIN__
26+
# define WEXITSTATUS(stat_val) (stat_val)
27+
# else
28+
# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
29+
# endif
30+
#endif
31+
2032
static char mysql_path[FN_REFLEN];
2133
static char mysqlcheck_path[FN_REFLEN];
22-
static char defaults_file_option[32+FN_REFLEN];
2334

2435
static my_bool opt_force, opt_verbose;
2536
static char *opt_user= (char*)"root";
2637

27-
static DYNAMIC_STRING ds_options;
38+
static DYNAMIC_STRING ds_args;
2839

2940
static char *opt_password= 0;
3041
static my_bool tty_password= 0;
@@ -35,8 +46,6 @@ static char *default_dbug_option= (char*) "d:t:O,/tmp/mysql_upgrade.trace";
3546

3647
static char **defaults_argv;
3748

38-
static File defaults_fd= -1;
39-
4049
static my_bool not_used; /* Can't use GET_BOOL without a value pointer */
4150

4251
#include <help_start.h>
@@ -105,14 +114,10 @@ static struct my_option my_long_options[]=
105114

106115
static void free_used_memory(void)
107116
{
108-
/* Close the defaults file */
109-
if (defaults_fd != -1)
110-
my_close(defaults_fd, MYF(0));
111-
112117
/* Free memory allocated by 'load_defaults' */
113118
free_defaults(defaults_argv);
114119

115-
dynstr_free(&ds_options);
120+
dynstr_free(&ds_args);
116121
}
117122

118123

@@ -159,30 +164,30 @@ static void verbose(const char *fmt, ...)
159164

160165
/*
161166
Add one option - passed to mysql_upgrade on command line
162-
or by defaults file(my.cnf) - to a dynamic string that later
163-
can be written to a temporary file. In this way we pass the
164-
same arguments on to mysql and mysql_check
167+
or by defaults file(my.cnf) - to a dynamic string, in
168+
this way we pass the same arguments on to mysql and mysql_check
165169
*/
166170

167171
static void add_one_option(DYNAMIC_STRING* ds,
168172
const struct my_option *opt,
169173
const char* argument)
170-
{
171-
dynstr_append(ds, opt->name);
172174

175+
{
176+
const char* eq= NullS;
177+
const char* arg= NullS;
173178
if (opt->arg_type != NO_ARG)
174179
{
175-
dynstr_append(ds, "=");
180+
eq= "=";
176181
switch (opt->var_type & GET_TYPE_MASK) {
177182
case GET_STR:
178-
case GET_STR_ALLOC:
179-
dynstr_append(ds, argument);
183+
arg= argument;
180184
break;
181185
default:
182186
die("internal error at %s: %d",__FILE__, __LINE__);
183187
}
184188
}
185-
dynstr_append(ds, "\n");
189+
dynstr_append_os_quoted(ds, "--", opt->name, eq, arg, NullS);
190+
dynstr_append(&ds_args, " ");
186191
}
187192

188193

@@ -211,8 +216,8 @@ get_one_option(int optid, const struct my_option *opt,
211216
add_option= FALSE;
212217
if (argument)
213218
{
214-
/* Add password to ds_options before overwriting the arg with x's */
215-
add_one_option(&ds_options, opt, argument);
219+
/* Add password to ds_args before overwriting the arg with x's */
220+
add_one_option(&ds_args, opt, argument);
216221
while (*argument)
217222
*argument++= 'x'; /* Destroy argument */
218223
tty_password= 0;
@@ -232,51 +237,14 @@ get_one_option(int optid, const struct my_option *opt,
232237
/*
233238
This is an option that is accpted by mysql_upgrade just so
234239
it can be passed on to "mysql" and "mysqlcheck"
235-
Save it in the ds_options string
240+
Save it in the ds_args string
236241
*/
237-
add_one_option(&ds_options, opt, argument);
242+
add_one_option(&ds_args, opt, argument);
238243
}
239244
return 0;
240245
}
241246

242247

243-
/*
244-
Write the options that should be passed on to
245-
mysql and mysqlcheck to a temporary file
246-
*/
247-
248-
static void create_defaults_file(void)
249-
{
250-
static char defaults_file_path[FN_REFLEN];
251-
DBUG_ENTER("create_defaults_file");
252-
253-
if ((defaults_fd= create_temp_file(defaults_file_path, NULL,
254-
"cnf", O_CREAT | O_SHARE,
255-
MYF(MY_WME))) < 0)
256-
die("Failed to create temporary file for defaults");
257-
258-
DBUG_PRINT("info", ("Writing options: %s", ds_options.str));
259-
if (my_write(defaults_fd, ds_options.str, ds_options.length,
260-
MYF(MY_FNABP | MY_WME)))
261-
die("Failed to write to '%s'", defaults_file_path);
262-
263-
/*
264-
Dont close the temporary file yet, it will be used
265-
by mysql and mysqlcheck
266-
*/
267-
268-
/*
269-
Create the option that should be added to
270-
tools in order to use this file
271-
*/
272-
my_snprintf(defaults_file_option, sizeof(defaults_file_option),
273-
"--defaults-file=%s", defaults_file_path);
274-
DBUG_PRINT("info", ("defaults_file_option: %s", defaults_file_option));
275-
276-
DBUG_VOID_RETURN;
277-
}
278-
279-
280248
static int run_command(char* cmd,
281249
DYNAMIC_STRING *ds_res)
282250
{
@@ -466,7 +434,7 @@ static int run_query(const char *query, DYNAMIC_STRING *ds_res,
466434
DBUG_ENTER("run_query");
467435
DBUG_PRINT("enter", ("query: %s", query));
468436
if ((fd= create_temp_file(query_file_path, NULL,
469-
"sql", O_CREAT | O_SHARE,
437+
"sql", O_CREAT | O_SHARE | O_RDWR,
470438
MYF(MY_WME))) < 0)
471439
die("Failed to create temporary file for defaults");
472440

@@ -476,7 +444,7 @@ static int run_query(const char *query, DYNAMIC_STRING *ds_res,
476444

477445
ret= run_tool(mysql_path,
478446
ds_res,
479-
defaults_file_option,
447+
ds_args.str, // MASV... quoted?
480448
"--database=mysql",
481449
"--batch", /* Turns off pager etc. */
482450
force ? "--force": "--skip-force",
@@ -631,7 +599,7 @@ static int run_mysqlcheck_upgrade(void)
631599
verbose("Running 'mysqlcheck'...");
632600
return run_tool(mysqlcheck_path,
633601
NULL, /* Send output from mysqlcheck directly to screen */
634-
defaults_file_option,
602+
ds_args.str,
635603
"--check-upgrade",
636604
"--all-databases",
637605
"--auto-repair",
@@ -747,7 +715,7 @@ int main(int argc, char **argv)
747715
setscreenmode(SCR_AUTOCLOSE_ON_EXIT);
748716
#endif
749717

750-
if (init_dynamic_string(&ds_options, "[client]\n", 512, 256))
718+
if (init_dynamic_string(&ds_args, "", 512, 256))
751719
die("Out of memory");
752720

753721
load_defaults("my", load_default_groups, &argc, &argv);
@@ -760,28 +728,19 @@ int main(int argc, char **argv)
760728
{
761729
opt_password= get_tty_password(NullS);
762730
/* add password to defaults file */
763-
dynstr_append(&ds_options, "password=");
764-
dynstr_append(&ds_options, opt_password);
765-
dynstr_append(&ds_options, "\n");
731+
dynstr_append_os_quoted(&ds_args, "--password=", opt_password, NullS);
732+
dynstr_append(&ds_args, " ");
766733
}
767734
/* add user to defaults file */
768-
dynstr_append(&ds_options, "user=");
769-
dynstr_append(&ds_options, opt_user);
770-
dynstr_append(&ds_options, "\n");
735+
dynstr_append_os_quoted(&ds_args, "--user=", opt_user, NullS);
736+
dynstr_append(&ds_args, " ");
771737

772738
/* Find mysql */
773739
find_tool(mysql_path, IF_WIN("mysql.exe", "mysql"));
774740

775741
/* Find mysqlcheck */
776742
find_tool(mysqlcheck_path, IF_WIN("mysqlcheck.exe", "mysqlcheck"));
777743

778-
/*
779-
Create the defaults file(a small my.cnf) which will pass
780-
all arguments accepted by mysql_upgrade on to mysqlcheck
781-
and mysql command line client
782-
*/
783-
create_defaults_file();
784-
785744
/*
786745
Read the mysql_upgrade_info file to check if mysql_upgrade
787746
already has been run for this installation of MySQL

mysql-test/r/mysql_upgrade.result

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ mysql.time_zone_transition_type OK
5858
mysql.user OK
5959
DROP USER mysqltest1@'%';
6060
Run mysql_upgrade with a non existing server socket
61-
mysqlcheck: Got error: 2002: Can't connect to local MySQL server through socket 'var/tmp/no_sock_here' (2) when trying to connect
61+
mysqlcheck: Got error: 2005: Unknown MySQL server host 'not_existing_host' (1) when trying to connect
6262
FATAL ERROR: Upgrade failed

mysql-test/t/mysql_upgrade.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ DROP USER mysqltest1@'%';
5858
--replace_result $MYSQLTEST_VARDIR var
5959
--replace_regex /.*mysqlcheck.*: Got/mysqlcheck: Got/
6060
--error 1
61-
--exec $MYSQL_UPGRADE --skip-verbose --force --socket=$MYSQLTEST_VARDIR/tmp/no_sock_here 2>&1
61+
--exec $MYSQL_UPGRADE --skip-verbose --force --host=not_existing_host 2>&1

scripts/comp_sql.c

+9
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ int main(int argc, char *argv[])
106106
curr++;
107107
}
108108
}
109+
if (*(curr-1) != '\n')
110+
{
111+
/*
112+
Some compilers have a max string length,
113+
insert a newline at every 512th char in long
114+
strings
115+
*/
116+
fprintf(out, "\"\n\"");
117+
}
109118
}
110119

111120
fprintf(out, "\\\n\"};\n");

0 commit comments

Comments
 (0)