Skip to content

Commit fe05c16

Browse files
committed
MDEV-23052 mysql_install_db.exe can run on existing non-empty directory,
and remove it on error Disable existing non-empty datadir for mysql_install_db.exe
1 parent 1ea266f commit fe05c16

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

sql/mysql_install_db.cc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@ static int register_service()
399399

400400
static void clean_directory(const char *dir)
401401
{
402-
char dir2[MAX_PATH+2];
403-
*(strmake_buf(dir2, dir)+1)= 0;
402+
char dir2[MAX_PATH + 4]= {};
403+
snprintf(dir2, MAX_PATH+2, "%s\\*", dir);
404404

405405
SHFILEOPSTRUCT fileop;
406406
fileop.hwnd= NULL; /* no status display */
@@ -551,7 +551,7 @@ static int create_db_instance()
551551
DWORD cwd_len= MAX_PATH;
552552
char cmdline[3*MAX_PATH];
553553
FILE *in;
554-
bool cleanup_datadir= true;
554+
bool created_datadir= false;
555555
DWORD last_error;
556556

557557
verbose("Running bootstrap");
@@ -560,7 +560,11 @@ static int create_db_instance()
560560

561561
/* Create datadir and datadir/mysql, if they do not already exist. */
562562

563-
if (!CreateDirectory(opt_datadir, NULL) && (GetLastError() != ERROR_ALREADY_EXISTS))
563+
if (CreateDirectory(opt_datadir, NULL))
564+
{
565+
created_datadir= true;
566+
}
567+
else if (GetLastError() != ERROR_ALREADY_EXISTS)
564568
{
565569
last_error = GetLastError();
566570
switch(last_error)
@@ -597,9 +601,11 @@ static int create_db_instance()
597601
}
598602
}
599603

600-
if (PathIsDirectoryEmpty(opt_datadir))
604+
if (!PathIsDirectoryEmpty(opt_datadir))
601605
{
602-
cleanup_datadir= false;
606+
fprintf(stderr,"ERROR : Data directory %s is not empty."
607+
" Only new or empty existing directories are accepted for --datadir\n",opt_datadir);
608+
exit(1);
603609
}
604610

605611
if (!CreateDirectory("mysql",NULL))
@@ -735,10 +741,12 @@ static int create_db_instance()
735741
}
736742

737743
end:
738-
if (ret && cleanup_datadir)
744+
if (ret)
739745
{
740746
SetCurrentDirectory(cwd);
741747
clean_directory(opt_datadir);
748+
if (created_datadir)
749+
RemoveDirectory(opt_datadir);
742750
}
743751
return ret;
744752
}

0 commit comments

Comments
 (0)