Commit fb90aaa7 authored by monty@mysql.com's avatar monty@mysql.com

Cleanup during code review

Faster detection of wrong table names (like PRN) on windows
parent 93565c8e
...@@ -578,6 +578,7 @@ extern int my_access(const char *path, int amode); ...@@ -578,6 +578,7 @@ extern int my_access(const char *path, int amode);
#else #else
#define my_access access #define my_access access
#endif #endif
extern int check_if_legal_filename(const char *path);
#ifndef TERMINATE #ifndef TERMINATE
extern void TERMINATE(FILE *file); extern void TERMINATE(FILE *file);
......
...@@ -15,39 +15,107 @@ ...@@ -15,39 +15,107 @@
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "mysys_priv.h" #include "mysys_priv.h"
#include <m_string.h>
#ifdef __WIN__ #ifdef __WIN__
/* /*
* Check a file or path for accessability. Check a file or path for accessability.
*
* SYNOPSIS SYNOPSIS
* file_access() file_access()
* pathpath to check path Path to file
* amodemode to check amode Access method
*
* DESCRIPTION DESCRIPTION
* This function wraps the normal access method because the access This function wraps the normal access method because the access
* available in MSVCRT> +reports that filenames such as LPT1 and available in MSVCRT> +reports that filenames such as LPT1 and
* COM1 are valid (they are but should not be so for us). COM1 are valid (they are but should not be so for us).
*
* RETURN VALUES RETURN VALUES
* 0 ok 0 ok
* -1 error -1 error (We use -1 as my_access is mapped to access on other platforms)
*/ */
int my_access(const char *path, int amode) int my_access(const char *path, int amode)
{ {
WIN32_FILE_ATTRIBUTE_DATA fileinfo; WIN32_FILE_ATTRIBUTE_DATA fileinfo;
BOOL result; BOOL result;
result = GetFileAttributesEx(path, GetFileExInfoStandard, result= GetFileAttributesEx(path, GetFileExInfoStandard, &fileinfo);
&fileinfo); if (! result ||
if (! result) (fileinfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) && (amode & W_OK))
return -1; {
if ((fileinfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) && my_errno= errno= EACCES;
(amode & 2))
return -1; return -1;
}
return 0; return 0;
} }
#endif /* __WIN__ */
#if defined(MSDOS) || defined(__WIN__) || defined(__EMX__)
/*
List of file names that causes problem on windows
NOTE that one can also not have file names of type CON.TXT
*/
static const char *reserved_names[]=
{
"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6",
"COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6",
"LPT7", "LPT8", "LPT9", "CLOCK$",
NullS
};
#define MAX_RESERVED_NAME_LENGTH 6
/*
Check if a path will access a reserverd file name that may cause problems
SYNOPSIS
check_if_legal_filename
path Path to file
RETURN
0 ok
1 reserved file name
*/
int check_if_legal_filename(const char *path)
{
const char *end;
const char **reserved_name;
DBUG_ENTER("check_if_legal_filename");
path+= dirname_length(path); /* To start of filename */
if (!(end= strchr(path, FN_EXTCHAR)))
end= strend(path);
if (path == end || (uint) (path - end) > MAX_RESERVED_NAME_LENGTH)
DBUG_RETURN(0); /* Simplify inner loop */
for (reserved_name= reserved_names; *reserved_name; reserved_name++)
{
const char *name= path;
while (name != end)
{
if (my_toupper(&my_charset_latin1, *path) !=
my_toupper(&my_charset_latin1, *name))
break;
if (name++ == end)
DBUG_RETURN(1); /* Found wrong path */
}
}
DBUG_RETURN(0);
}
#endif #endif
#ifdef OS2
int check_if_legal_filename(const char *path)
{
return 0;
}
#endif /* OS2 */
...@@ -39,8 +39,11 @@ FILE *my_fopen(const char *FileName, int Flags, myf MyFlags) ...@@ -39,8 +39,11 @@ FILE *my_fopen(const char *FileName, int Flags, myf MyFlags)
very well very well
*/ */
#ifdef __WIN__ #ifdef __WIN__
if (! (Flags & O_CREAT) && my_access(FileName, F_OK)) if (check_if_legal_filename(FileName))
fd=0; {
errno= EACCES;
fd= 0;
}
else else
#endif #endif
{ {
......
...@@ -47,12 +47,15 @@ File my_open(const char *FileName, int Flags, myf MyFlags) ...@@ -47,12 +47,15 @@ File my_open(const char *FileName, int Flags, myf MyFlags)
FileName, Flags, MyFlags)); FileName, Flags, MyFlags));
#if defined(MSDOS) || defined(__WIN__) || defined(__EMX__) || defined(OS2) #if defined(MSDOS) || defined(__WIN__) || defined(__EMX__) || defined(OS2)
/* /*
if we are not creating, then we need to use my_access to make Check that we don't try to open or create a file name that may
sure the file exists since Windows doesn't handle files like cause problems for us in the future (like PRN)
"com1.sym" very well
*/ */
if (! (Flags & O_CREAT) && my_access(FileName, F_OK)) if (check_if_legal_filename(FileName))
return -1; {
errno= EACCES;
DBUG_RETURN(my_register_filename(-1, FileName, FILE_BY_OPEN,
EE_FILENOTFOUND, MyFlags));
}
if (Flags & O_SHARE) if (Flags & O_SHARE)
fd = sopen((my_string) FileName, (Flags & ~O_SHARE) | O_BINARY, SH_DENYNO, fd = sopen((my_string) FileName, (Flags & ~O_SHARE) | O_BINARY, SH_DENYNO,
MY_S_IREAD | MY_S_IWRITE); MY_S_IREAD | MY_S_IWRITE);
......
...@@ -420,7 +420,7 @@ static const uint signed_longlong_len=19; ...@@ -420,7 +420,7 @@ static const uint signed_longlong_len=19;
static const char *unsigned_longlong_str="18446744073709551615"; static const char *unsigned_longlong_str="18446744073709551615";
static const uint unsigned_longlong_len=20; static const uint unsigned_longlong_len=20;
inline static uint int_token(const char *str,uint length) static inline uint int_token(const char *str,uint length)
{ {
if (length < long_len) // quick normal case if (length < long_len) // quick normal case
return NUM; return NUM;
......
...@@ -2819,8 +2819,8 @@ unsent_create_error: ...@@ -2819,8 +2819,8 @@ unsent_create_error:
TABLE *table= tables->table; TABLE *table= tables->table;
/* Skip first table, which is the table we are inserting in */ /* Skip first table, which is the table we are inserting in */
lex->select_lex.table_list.first= (byte*) first_local_table->next; select_lex->table_list.first= (byte*) first_local_table->next;
tables= (TABLE_LIST *) lex->select_lex.table_list.first; tables= (TABLE_LIST *) select_lex->table_list.first;
first_local_table->next= 0; first_local_table->next= 0;
if (!(res= mysql_prepare_insert(thd, tables, first_local_table, if (!(res= mysql_prepare_insert(thd, tables, first_local_table,
...@@ -5389,6 +5389,7 @@ int multi_update_precheck(THD *thd, TABLE_LIST *tables) ...@@ -5389,6 +5389,7 @@ int multi_update_precheck(THD *thd, TABLE_LIST *tables)
1 error (message is sent to user) 1 error (message is sent to user)
-1 error (message is not sent to user) -1 error (message is not sent to user)
*/ */
int multi_delete_precheck(THD *thd, TABLE_LIST *tables, uint *table_count) int multi_delete_precheck(THD *thd, TABLE_LIST *tables, uint *table_count)
{ {
DBUG_ENTER("multi_delete_precheck"); DBUG_ENTER("multi_delete_precheck");
......
...@@ -152,7 +152,8 @@ File open_binlog(IO_CACHE *log, const char *log_file_name, ...@@ -152,7 +152,8 @@ File open_binlog(IO_CACHE *log, const char *log_file_name,
File file; File file;
DBUG_ENTER("open_binlog"); DBUG_ENTER("open_binlog");
if ((file = my_open(log_file_name, O_RDONLY | O_BINARY, MYF(MY_WME))) < 0) if ((file = my_open(log_file_name, O_RDONLY | O_BINARY | O_SHARE,
MYF(MY_WME))) < 0)
{ {
sql_print_error("Failed to open log (\ sql_print_error("Failed to open log (\
file '%s', errno %d)", log_file_name, my_errno); file '%s', errno %d)", log_file_name, my_errno);
...@@ -1338,13 +1339,11 @@ int show_binlogs(THD* thd) ...@@ -1338,13 +1339,11 @@ int show_binlogs(THD* thd)
{ {
IO_CACHE *index_file; IO_CACHE *index_file;
LOG_INFO cur; LOG_INFO cur;
IO_CACHE log;
File file; File file;
const char *errmsg= 0;
MY_STAT stat_area;
char fname[FN_REFLEN]; char fname[FN_REFLEN];
List<Item> field_list; List<Item> field_list;
uint length; uint length;
int cur_dir_len;
Protocol *protocol= thd->protocol; Protocol *protocol= thd->protocol;
DBUG_ENTER("show_binlogs"); DBUG_ENTER("show_binlogs");
...@@ -1364,34 +1363,35 @@ int show_binlogs(THD* thd) ...@@ -1364,34 +1363,35 @@ int show_binlogs(THD* thd)
index_file=mysql_bin_log.get_index_file(); index_file=mysql_bin_log.get_index_file();
mysql_bin_log.get_current_log(&cur); mysql_bin_log.get_current_log(&cur);
int cur_dir_len = dirname_length(cur.log_file_name); cur_dir_len= dirname_length(cur.log_file_name);
reinit_io_cache(index_file, READ_CACHE, (my_off_t) 0, 0, 0); reinit_io_cache(index_file, READ_CACHE, (my_off_t) 0, 0, 0);
/* The file ends with EOF or empty line */ /* The file ends with EOF or empty line */
while ((length=my_b_gets(index_file, fname, sizeof(fname))) > 1) while ((length=my_b_gets(index_file, fname, sizeof(fname))) > 1)
{ {
fname[--length] = '\0'; /* remove the newline */ int dir_len;
ulonglong file_length= 0; // Length if open fails
fname[--length] = '\0'; // remove the newline
protocol->prepare_for_resend(); protocol->prepare_for_resend();
int dir_len = dirname_length(fname); dir_len= dirname_length(fname);
protocol->store(fname + dir_len, length-dir_len, &my_charset_bin); length-= dir_len;
if(!(strncmp(fname+dir_len, cur.log_file_name+cur_dir_len, length-dir_len))) protocol->store(fname + dir_len, length, &my_charset_bin);
if (!(strncmp(fname+dir_len, cur.log_file_name+cur_dir_len, length)))
file_length= cur.pos; /* The active log, use the active position */
else
{ {
/* this is the active log, use the active position */
protocol->store((ulonglong) cur.pos);
} else {
/* this is an old log, open it and find the size */ /* this is an old log, open it and find the size */
if ((file=open_binlog(&log, fname+dir_len, &errmsg)) >= 0) if ((file= my_open(fname+dir_len, O_RDONLY | O_SHARE | O_BINARY,
MYF(0))) >= 0)
{ {
protocol->store((ulonglong) my_b_filelength(&log)); file_length= (ulonglong) my_seek(file, 0L, MY_SEEK_END, MYF(0));
end_io_cache(&log);
my_close(file, MYF(0)); my_close(file, MYF(0));
} else {
/* the file wasn't openable, but 0 is an invalid value anyway */
protocol->store((ulonglong) 0);
} }
} }
protocol->store(file_length);
if (protocol->write()) if (protocol->write())
goto err; goto err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment