1
0
mirror of https://github.com/MariaDB/server.git synced 2025-04-18 21:44:20 +03:00

MDEV-23818 mysql option --script-dir

This commit introduces an additional command line option to the mariadb
client.
--script-dir=<directory> will cause the `source` command to look for
files initially in CWD, then in <script-dir> if not found in CWD.
This commit is contained in:
Hossam Hassan 2025-02-18 14:07:14 +02:00 committed by Vicențiu-Marian Ciorbaru
parent 3a81664cb8
commit 689bed1940
3 changed files with 100 additions and 2 deletions

View File

@ -280,6 +280,7 @@ static STATUS status;
static ulong select_limit,max_join_size,opt_connect_timeout=0;
static char mysql_charsets_dir[FN_REFLEN+1];
static char *opt_plugin_dir= 0, *opt_default_auth= 0;
static char *script_dir = NULL;
static const char *xmlmeta[] = {
"&", "&amp;",
"<", "&lt;",
@ -1838,6 +1839,8 @@ static struct my_option my_long_options[] =
&safe_updates, &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"sandbox", 0, "Disallow commands that access the file system (except \\P without an argument and \\e).",
&status.sandbox, &status.sandbox, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
{"script-dir", 0, "Set an alternative directory path for searching scripts invoked via the source command.",
&script_dir, &script_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"secure-auth", 0, "Refuse client connecting to server if it"
" uses old (pre-4.1.1) protocol.", &opt_secure_auth,
&opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
@ -4692,6 +4695,7 @@ static int com_connect(String *buffer, char *line)
static int com_source(String *, char *line)
{
char source_name[FN_REFLEN], *end, *param;
char full_path[FN_REFLEN];
LINE_BUFFER *line_buff;
int error;
STATUS old_status;
@ -4718,8 +4722,17 @@ static int com_source(String *, char *line)
/* open file name */
if (!(sql_file = my_fopen(source_name, O_RDONLY | O_BINARY,MYF(0))))
{
char buff[FN_REFLEN+60];
sprintf(buff,"Failed to open file '%s', error: %d", source_name,errno);
if (script_dir)
{
snprintf(full_path, sizeof(full_path), "%s/%s", script_dir, source_name);
sql_file = my_fopen(full_path, O_RDONLY | O_BINARY, MYF(0));
}
}
if (!sql_file)
{
char buff[FN_REFLEN + 60];
sprintf(buff, "Failed to open file '%s', error: %d", source_name, errno);
return put_info(buff, INFO_ERROR, 0);
}

View File

@ -60,3 +60,23 @@ drop table t1;
#
# End of 10.5 tests
#
#
# MDEV-23818: mysql option --script-dir
#
# test 1: can't find the file at all
ERROR at line 1: Failed to open file 'file1', error: 2
# test 2: file in the current working directory
1
1
# test 3: file is present in CWD and also in script-dir
hello from file1
hello from file1
# test 4: file is only present in the script-dir
hello from dir1/file1.sql
hello from dir1/file1.sql
# test 5: script-dir file has source command that references CWD
hello from file2.sql
hello from file2.sql
# test 6: script-dir file has source command that references script-dir
hello from file2.sql
hello from file2.sql

View File

@ -44,3 +44,68 @@ drop table t1;
--echo #
--echo # End of 10.5 tests
--echo #
--echo #
--echo # MDEV-23818: mysql option --script-dir
--echo #
--echo # test 1: can't find the file at all
--mkdir $MYSQLTEST_VARDIR/dir1
--error 1
--exec echo "source file1;" | $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ 2>&1
--rmdir $MYSQLTEST_VARDIR/dir1
--echo # test 2: file in the current working directory
--mkdir $MYSQLTEST_VARDIR/dir1
--write_file $MYSQLTEST_VARDIR/file1.sql
select 1;
EOF
--exec echo "source $MYSQLTEST_VARDIR/file1.sql;" | $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ 2>&1
--remove_file $MYSQLTEST_VARDIR/file1.sql
--rmdir $MYSQLTEST_VARDIR/dir1
--echo # test 3: file is present in CWD and also in script-dir
--mkdir $MYSQLTEST_VARDIR/dir1
--write_file $MYSQLTEST_VARDIR/file1.sql
select 'hello from file1'
EOF
--write_file $MYSQLTEST_VARDIR/dir1/file1.sql
select 'hello from dir1/file1.sql';
EOF
--exec echo "source $MYSQLTEST_VARDIR/file1.sql;" | $MYSQL --script-dir=./dir1/ 2>&1
--remove_file $MYSQLTEST_VARDIR/file1.sql
--remove_file $MYSQLTEST_VARDIR/dir1/file1.sql
--rmdir $MYSQLTEST_VARDIR/dir1
--echo # test 4: file is only present in the script-dir
--mkdir $MYSQLTEST_VARDIR/dir1
--write_file $MYSQLTEST_VARDIR/dir1/file1.sql
select 'hello from dir1/file1.sql';
EOF
--exec echo "source file1.sql;" | $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ 2>&1
--remove_file $MYSQLTEST_VARDIR/dir1/file1.sql
--rmdir $MYSQLTEST_VARDIR/dir1
--echo # test 5: script-dir file has source command that references CWD
--mkdir $MYSQLTEST_VARDIR/dir1
--write_file $MYSQLTEST_VARDIR/dir1/file1.sql
source file2.sql;
EOF
--exec echo "select 'hello from file2.sql'" > $MYSQLTEST_VARDIR/file2.sql
--exec cd $MYSQLTEST_VARDIR && $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ -e "source file1.sql;" 2>&1
--remove_file $MYSQLTEST_VARDIR/dir1/file1.sql
--remove_file $MYSQLTEST_VARDIR/file2.sql
--rmdir $MYSQLTEST_VARDIR/dir1
--echo # test 6: script-dir file has source command that references script-dir
--mkdir $MYSQLTEST_VARDIR/dir1
--write_file $MYSQLTEST_VARDIR/dir1/file1.sql
source file2.sql;
EOF
--write_file $MYSQLTEST_VARDIR/dir1/file2.sql
select 'hello from file2.sql';
EOF
--exec echo "source file1.sql" | $MYSQL --script-dir=$MYSQLTEST_VARDIR/dir1/ 2>&1
--remove_file $MYSQLTEST_VARDIR/dir1/file1.sql
--remove_file $MYSQLTEST_VARDIR/dir1/file2.sql
--rmdir $MYSQLTEST_VARDIR/dir1